Skip to content

Commit

Permalink
linter for package main
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin8105 committed May 30, 2024
1 parent 5bd1240 commit ff16bd1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
21 changes: 11 additions & 10 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/Konstantin8105/c4go/preprocessor"
)

// Positioner interface for walking
type Positioner interface {
Position() ast.Position
Inject(lines [][]byte, filePP preprocessor.FilePP) error
Expand Down Expand Up @@ -159,16 +160,16 @@ func (v argument) Inject(lines [][]byte, filePP preprocessor.FilePP) error {
}
}

var index int = -1
for i := range FuncArgs {
if FuncArgs[i].cType == v.cType {
index := -1
for i := range funcArgs {
if funcArgs[i].cType == v.cType {

Check warning on line 165 in debug.go

View check run for this annotation

Codecov / codecov/patch

debug.go#L163-L165

Added lines #L163 - L165 were not covered by tests
index = i
}
}
if index >= 0 {
// find argument type
function := fmt.Sprintf(";%s%s(%d,\"%s\",\"%s\",%s);",
debugArgument, FuncArgs[index].postfix,
debugArgument, funcArgs[index].postfix,

Check warning on line 172 in debug.go

View check run for this annotation

Codecov / codecov/patch

debug.go#L172

Added line #L172 was not covered by tests
v.pos.Line, v.description, v.varName, v.varName)
lines[v.pos.Line-1] = append(lines[v.pos.Line-1][:v.pos.Column],
append([]byte(function), lines[v.pos.Line-1][v.pos.Column:]...)...)
Expand All @@ -190,7 +191,7 @@ func generateDebugCCode(args ProgramArgs, lines []string, filePP preprocessor.Fi
}

// convert lines to tree ast
tree, errs := FromLinesToTree(args.verbose, lines, filePP)
tree, errs := fromLinesToTree(args.verbose, lines, filePP)

Check warning on line 194 in debug.go

View check run for this annotation

Codecov / codecov/patch

debug.go#L194

Added line #L194 was not covered by tests
for i := range errs {
fmt.Fprintf(os.Stderr, "AST error #%d:\n%v\n",
i, errs[i].Error())
Expand Down Expand Up @@ -338,9 +339,9 @@ func generateDebugCCode(args ProgramArgs, lines []string, filePP preprocessor.Fi
if err2 != nil {
// error is ignored
_ = err2
} else {
// non error is ignored
continue

Check warning on line 342 in debug.go

View check run for this annotation

Codecov / codecov/patch

debug.go#L342

Added line #L342 was not covered by tests
}
// non error is ignored
}

// add main debug function
Expand Down Expand Up @@ -427,15 +428,15 @@ void c4go_debug_function_arg_string(int line, const char * arg_pos, const char *
`

for i := range FuncArgs {
for i := range funcArgs {

Check warning on line 431 in debug.go

View check run for this annotation

Codecov / codecov/patch

debug.go#L431

Added line #L431 was not covered by tests
body += fmt.Sprintf("\nc4go_arg(%s,%s,\"%s\");\n",
FuncArgs[i].cType, FuncArgs[i].postfix, FuncArgs[i].format)
funcArgs[i].cType, funcArgs[i].postfix, funcArgs[i].format)

Check warning on line 433 in debug.go

View check run for this annotation

Codecov / codecov/patch

debug.go#L433

Added line #L433 was not covered by tests
}

return body
}

var FuncArgs = []struct {
var funcArgs = []struct {
cType string
postfix string
format string
Expand Down
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ type ProgramArgs struct {
debugPrefix string
}

// ProgramState is state of program
type ProgramState int

// States of program
const (
StateAst ProgramState = iota
StateTranspile
Expand Down Expand Up @@ -257,7 +259,7 @@ var goKeywords = [...]string{
"imag", "print", "println", "error", "Type", "Type1",
"IntegerType", "FloatType", "ComplexType",
}
var letters string = "_qwertyuiopasdfghjklzxcvbnm1234567890><"
var letters = "_qwertyuiopasdfghjklzxcvbnm1234567890><"

func isLetter(b byte) bool {
b = strings.ToLower(string(b))[0]
Expand Down Expand Up @@ -453,7 +455,7 @@ func generateAstLines(args ProgramArgs) (lines []string, filePP preprocessor.Fil
return
}

func FromLinesToTree(verbose bool, lines []string, filePP preprocessor.FilePP) (tree []ast.Node, errs []error) {
func fromLinesToTree(verbose bool, lines []string, filePP preprocessor.FilePP) (tree []ast.Node, errs []error) {
// Converting to nodes
if verbose {
fmt.Fprintln(os.Stdout, "Converting to nodes...")
Expand Down Expand Up @@ -492,7 +494,7 @@ func generateGoCode(p *program.Program, args ProgramArgs, lines []string, filePP
p.PreprocessorFile = filePP

// convert lines to tree ast
tree, errs := FromLinesToTree(args.verbose, lines, filePP)
tree, errs := fromLinesToTree(args.verbose, lines, filePP)
for i := range errs {
fmt.Fprintf(os.Stderr, "AST error #%d:\n%v\n",
i, errs[i].Error())
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,11 +963,11 @@ func TestWrongAST(t *testing.T) {
defer func() {
os.Stderr = oldstderr
}()
new, err := ioutil.TempFile("", "stderr")
tempFile, err := ioutil.TempFile("", "stderr")
if err != nil {
t.Fatal(err)
}
os.Stderr = new
os.Stderr = tempFile

lines, filePP, args, err := generateASTtree()
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions other_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func getFileList(prefix, gitSource string) (fileList []string, err error) {

// find all C source files
err = filepath.Walk(folder, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(strings.ToLower(f.Name()), ".c") {
fileList = append(fileList, path)
}
Expand Down
18 changes: 7 additions & 11 deletions unused.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func unused(filenames ...string) {
decls := f.Decls[i]
if fd, ok := decls.(*ast.FuncDecl); ok {
name := fd.Name.Name
var cs CallSearcher
var cs callSearcher

Check warning on line 31 in unused.go

View check run for this annotation

Codecov / codecov/patch

unused.go#L31

Added line #L31 was not covered by tests
ast.Walk(&cs, fd)
m[name] = cs.used
}
Expand Down Expand Up @@ -92,11 +92,11 @@ func unused(filenames ...string) {
}
}

type CallSearcher struct {
type callSearcher struct {
used []string
}

var GoFuncs = []string{
var goFuncs = []string{
"len", "make", "append",
"string",
"float64", "float32",
Expand All @@ -105,12 +105,13 @@ var GoFuncs = []string{
"panic",
}

func (c *CallSearcher) Visit(node ast.Node) (w ast.Visitor) {
// Visit for walking by node tree
func (c *callSearcher) Visit(node ast.Node) (w ast.Visitor) {

Check warning on line 109 in unused.go

View check run for this annotation

Codecov / codecov/patch

unused.go#L109

Added line #L109 was not covered by tests
if call, ok := node.(*ast.CallExpr); ok {
if f, ok := call.Fun.(*ast.Ident); ok {
isFound := false
for i := range GoFuncs {
if f.Name == GoFuncs[i] {
for i := range goFuncs {
if f.Name == goFuncs[i] {

Check warning on line 114 in unused.go

View check run for this annotation

Codecov / codecov/patch

unused.go#L113-L114

Added lines #L113 - L114 were not covered by tests
isFound = true
}
}
Expand All @@ -121,8 +122,3 @@ func (c *CallSearcher) Visit(node ast.Node) (w ast.Visitor) {
}
return c
}

func search(f *ast.FuncDecl) (used []string) {

return
}

0 comments on commit ff16bd1

Please sign in to comment.