From e390a29d2f0cb98ecd4cab299d0ba1e0b2e408b2 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Wed, 10 Nov 2021 21:57:33 +0100 Subject: pkg/compiler: warn about confusing comments that fake directives It's a somewhat common mistake to write comments instead of directives: #include #define FOO BAR because that's how it's done in C. Warn about such cases. --- pkg/compiler/check.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'pkg/compiler/check.go') diff --git a/pkg/compiler/check.go b/pkg/compiler/check.go index 75857be40..c5fc226d6 100644 --- a/pkg/compiler/check.go +++ b/pkg/compiler/check.go @@ -8,6 +8,7 @@ package compiler import ( "errors" "fmt" + "regexp" "strings" "github.com/google/syzkaller/pkg/ast" @@ -16,6 +17,7 @@ import ( ) func (comp *compiler) typecheck() { + comp.checkComments() comp.checkDirectives() comp.checkNames() comp.checkFields() @@ -34,6 +36,18 @@ func (comp *compiler) check() { comp.checkDupConsts() } +func (comp *compiler) checkComments() { + confusingComment := regexp.MustCompile(`^\s*(include|incdir|define)`) + for _, decl := range comp.desc.Nodes { + switch n := decl.(type) { + case *ast.Comment: + if confusingComment.MatchString(n.Text) { + comp.error(n.Pos, "confusing comment faking a directive (rephrase if it's intentional)") + } + } + } +} + func (comp *compiler) checkDirectives() { includes := make(map[string]bool) incdirs := make(map[string]bool) -- cgit mrf-deployment