From 78178cfb8c749a862d7df2ac09ee66b956565027 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Fri, 10 Jul 2020 18:50:44 +0200 Subject: tools/syz-linter: fix comments check Turns out ast.Inspect does not visit most comments. Walk file.Comments manually. Update #1876 --- tools/syz-check/dwarf.go | 2 +- tools/syz-linter/linter.go | 29 +++++++++++----------- .../testdata/src/lintertest/lintertest.go | 4 +++ 3 files changed, 20 insertions(+), 15 deletions(-) (limited to 'tools') diff --git a/tools/syz-check/dwarf.go b/tools/syz-check/dwarf.go index d3e1adb12..1f728f40a 100644 --- a/tools/syz-check/dwarf.go +++ b/tools/syz-check/dwarf.go @@ -19,7 +19,7 @@ func parseKernelObject(obj string) (map[string]*dwarf.StructType, error) { var sections []*elf.Section for _, sec := range file.Sections { // We don't need these for our purposes and dropping them speeds up parsing a lot. - //nolint:misspell + // nolint:misspell if sec.Name == ".debug_line" || strings.HasPrefix(sec.Name, ".rela.") { continue } diff --git a/tools/syz-linter/linter.go b/tools/syz-linter/linter.go index d69ca0a2a..8bc8eda23 100644 --- a/tools/syz-linter/linter.go +++ b/tools/syz-linter/linter.go @@ -63,9 +63,12 @@ func run(p *analysis.Pass) (interface{}, error) { for _, file := range pass.Files { ast.Inspect(file, func(n ast.Node) bool { switch n := n.(type) { - case *ast.Comment: - pass.checkMulitlineComments(n) - pass.checkCommentSpace(n) + case *ast.File: + for _, group := range n.Comments { + for _, comment := range group.List { + pass.checkComment(comment) + } + } case *ast.BinaryExpr: pass.checkStringLenCompare(n) case *ast.FuncType: @@ -94,19 +97,17 @@ func (pass *Pass) typ(e ast.Expr) types.Type { return pass.TypesInfo.Types[e].Type } -// checkMulitlineComments warns about C++-style multiline comments. -// We don't use them in the codebase. -func (pass *Pass) checkMulitlineComments(n *ast.Comment) { - if !strings.HasPrefix(n.Text, "/*") { +// checkComment warns about C++-style multiline comments (we don't use them in the codebase) +// and about "//nospace", "// tabs and spaces" and similar. +func (pass *Pass) checkComment(n *ast.Comment) { + if strings.HasPrefix(n.Text, "/*") { + pass.report(n, "Use C-style comments // instead of /* */") return } - pass.report(n, "Use C-style comments // instead of /* */") -} - -// checkCommentSpace warns about "//nospace", "// tabs and spaces" and similar. -func (pass *Pass) checkCommentSpace(n *ast.Comment) { - if !strings.HasPrefix(n.Text, "//") || - allowedComments.MatchString(n.Text) { + if allowedComments.MatchString(n.Text) { + return + } + if strings.HasPrefix(n.Text, "//go:generate") { return } pass.report(n, "Use either //comment or //comment format for comments") diff --git a/tools/syz-linter/testdata/src/lintertest/lintertest.go b/tools/syz-linter/testdata/src/lintertest/lintertest.go index 9a8c965b8..12553c230 100644 --- a/tools/syz-linter/testdata/src/lintertest/lintertest.go +++ b/tools/syz-linter/testdata/src/lintertest/lintertest.go @@ -32,8 +32,12 @@ func returnString() string { return "foo" } // Tab and spaces. // want "Use either //comment or //comment format for comments" // Space and tab. // want "Use either //comment or //comment format for comments" func checkCommentSpace() { + // Comment without a dot at the end + checkCommentSpace() } +//No space. // want "Use either //comment or //comment format for comments" + func funcArgsGood(a, b int) (int, int) { return 0, 0 } -- cgit mrf-deployment