diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2021-11-10 21:57:33 +0100 |
|---|---|---|
| committer | Aleksandr Nogikh <wp32pw@gmail.com> | 2021-11-12 16:32:52 +0100 |
| commit | e390a29d2f0cb98ecd4cab299d0ba1e0b2e408b2 (patch) | |
| tree | 398efa5edb3428199d303a869c1e4508091dd027 | |
| parent | 0afefce7b7d6ac6f7f4489b6c0b9d06f8be2f5a6 (diff) | |
pkg/compiler: warn about confusing comments that fake directives
It's a somewhat common mistake to write comments instead of directives:
#include <foo>
#define FOO BAR
because that's how it's done in C.
Warn about such cases.
| -rw-r--r-- | pkg/compiler/check.go | 14 | ||||
| -rw-r--r-- | pkg/compiler/testdata/errors.txt | 5 |
2 files changed, 19 insertions, 0 deletions
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) diff --git a/pkg/compiler/testdata/errors.txt b/pkg/compiler/testdata/errors.txt index 2280d34b8..65eaf8a97 100644 --- a/pkg/compiler/testdata/errors.txt +++ b/pkg/compiler/testdata/errors.txt @@ -3,6 +3,11 @@ # Errors that happen during type checking phase. +#include "something" ### confusing comment faking a directive (rephrase if it's intentional) +#define FOO BAR ### confusing comment faking a directive (rephrase if it's intentional) +# include "something" ### confusing comment faking a directive (rephrase if it's intentional) +# incdir "dir" ### confusing comment faking a directive (rephrase if it's intentional) + foo$0(x fileoff, y int8, z buffer[in]) foo$1(x "bar") ### unexpected string "bar", expect type foo$2(x 123, y "bar") ### unexpected int 123, expect type ### unexpected string "bar", expect type |
