From ff16bd1f344a56e6b976169a393bb246e4c43d58 Mon Sep 17 00:00:00 2001 From: Konstantin8105 Date: Thu, 30 May 2024 13:52:03 +0300 Subject: [PATCH] linter for package main --- debug.go | 21 +++++++++++---------- main.go | 8 +++++--- main_test.go | 4 ++-- other_test.go | 3 +++ unused.go | 18 +++++++----------- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/debug.go b/debug.go index b8a5fc7b..aa105857 100644 --- a/debug.go +++ b/debug.go @@ -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 @@ -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 { 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, 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:]...)...) @@ -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) for i := range errs { fmt.Fprintf(os.Stderr, "AST error #%d:\n%v\n", i, errs[i].Error()) @@ -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 } + // non error is ignored } // add main debug function @@ -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 { 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) } return body } -var FuncArgs = []struct { +var funcArgs = []struct { cType string postfix string format string diff --git a/main.go b/main.go index 5d4550d3..31dd6068 100644 --- a/main.go +++ b/main.go @@ -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 @@ -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] @@ -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...") @@ -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()) diff --git a/main_test.go b/main_test.go index f404e33d..51c716b6 100644 --- a/main_test.go +++ b/main_test.go @@ -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 { diff --git a/other_test.go b/other_test.go index 220b601f..d1ee45c2 100644 --- a/other_test.go +++ b/other_test.go @@ -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) } diff --git a/unused.go b/unused.go index 9361c28e..0ae3fc36 100644 --- a/unused.go +++ b/unused.go @@ -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 ast.Walk(&cs, fd) m[name] = cs.used } @@ -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", @@ -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) { 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] { isFound = true } } @@ -121,8 +122,3 @@ func (c *CallSearcher) Visit(node ast.Node) (w ast.Visitor) { } return c } - -func search(f *ast.FuncDecl) (used []string) { - - return -}