diff options
| author | Robert Griesemer <gri@golang.org> | 2022-12-07 16:57:27 -0800 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-01-17 19:53:18 +0000 |
| commit | 1e0051658345a13ca9be9eeccdce2dabb016c627 (patch) | |
| tree | 04aa00fce01bb3d666b2ec234a2ddb9aa9f7d7ec /src/cmd | |
| parent | 83f8688915de59d974e74dd6accd7a3d54e9a6e7 (diff) | |
| download | go-1e0051658345a13ca9be9eeccdce2dabb016c627.tar.xz | |
cmd/compile/internal/syntax: rename ErrorMap to CommentMap, make more flexible
Change the ErrorMap function to collect all comments with a comment
text that matches a given regexp pattern. Also rename it to CommentMap.
Adjust uses and corresponding test.
Adjust various type-checker tests with incorrect ERROR patterns.
For #51006.
Change-Id: I749e8f31b532edbf8568f27ba1546dc849efd143
Reviewed-on: https://go-review.googlesource.com/c/go/+/456155
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/cmd')
| -rw-r--r-- | src/cmd/compile/internal/syntax/testing.go | 41 | ||||
| -rw-r--r-- | src/cmd/compile/internal/syntax/testing_test.go | 37 | ||||
| -rw-r--r-- | src/cmd/compile/internal/types2/check_test.go | 8 |
3 files changed, 45 insertions, 41 deletions
diff --git a/src/cmd/compile/internal/syntax/testing.go b/src/cmd/compile/internal/syntax/testing.go index 6a97dc0c2a..202b2efc3e 100644 --- a/src/cmd/compile/internal/syntax/testing.go +++ b/src/cmd/compile/internal/syntax/testing.go @@ -9,7 +9,6 @@ package syntax import ( "io" "regexp" - "strings" ) // CommentsDo parses the given source and calls the provided handler for each @@ -23,21 +22,18 @@ func CommentsDo(src io.Reader, handler func(line, col uint, text string)) { } } -// ERROR comments must start with text `ERROR "msg"` or `ERROR msg`. -// Space around "msg" or msg is ignored. -var errRx = regexp.MustCompile(`^ *ERROR *"?([^"]*)"?`) - -// ErrorMap collects all comments with comment text of the form -// `ERROR "msg"` or `ERROR msg` from the given src and returns them -// as []Error lists in a map indexed by line number. The position -// for each Error is the position of the token immediately preceding -// the comment, the Error message is the message msg extracted from -// the comment, with all errors that are on the same line collected -// in a slice, in source order. If there is no preceding token (the -// `ERROR` comment appears in the beginning of the file), then the -// recorded position is unknown (line, col = 0, 0). If there are no -// ERROR comments, the result is nil. -func ErrorMap(src io.Reader) (errmap map[uint][]Error) { +// CommentMap collects all comments in the given src with comment text +// that matches the supplied regular expression rx and returns them as +// []Error lists in a map indexed by line number. The comment text is +// the comment with any comment markers ("//", "/*", or "*/") stripped. +// The position for each Error is the position of the token immediately +// preceding the comment and the Error message is the comment text, +// with all comments that are on the same line collected in a slice, in +// source order. If there is no preceding token (the matching comment +// appears at the beginning of the file), then the recorded position +// is unknown (line, col = 0, 0). If there are no matching comments, +// the result is nil. +func CommentMap(src io.Reader, rx *regexp.Regexp) (res map[uint][]Error) { // position of previous token var base *PosBase var prev struct{ line, col uint } @@ -45,18 +41,19 @@ func ErrorMap(src io.Reader) (errmap map[uint][]Error) { var s scanner s.init(src, func(_, _ uint, text string) { if text[0] != '/' { - return // error, ignore + return // not a comment, ignore } if text[1] == '*' { text = text[:len(text)-2] // strip trailing */ } - if s := errRx.FindStringSubmatch(text[2:]); len(s) == 2 { + text = text[2:] // strip leading // or /* + if rx.MatchString(text) { pos := MakePos(base, prev.line, prev.col) - err := Error{pos, strings.TrimSpace(s[1])} - if errmap == nil { - errmap = make(map[uint][]Error) + err := Error{pos, text} + if res == nil { + res = make(map[uint][]Error) } - errmap[prev.line] = append(errmap[prev.line], err) + res[prev.line] = append(res[prev.line], err) } }, comments) diff --git a/src/cmd/compile/internal/syntax/testing_test.go b/src/cmd/compile/internal/syntax/testing_test.go index d34e5eafaf..7e439c5523 100644 --- a/src/cmd/compile/internal/syntax/testing_test.go +++ b/src/cmd/compile/internal/syntax/testing_test.go @@ -6,22 +6,23 @@ package syntax import ( "fmt" + "regexp" "strings" "testing" ) -func TestErrorMap(t *testing.T) { - const src = `/* ERROR 0:0 */ /* ERROR "0:0" */ // ERROR 0:0 +func TestCommentMap(t *testing.T) { + const src = `/* ERROR "0:0" */ /* ERROR "0:0" */ // ERROR "0:0" // ERROR "0:0" -x /* ERROR 3:1 */ // ignore automatically inserted semicolon here -/* ERROR 3:1 */ // position of x on previous line - x /* ERROR 5:4 */ ; // do not ignore this semicolon -/* ERROR 5:22 */ // position of ; on previous line - package /* ERROR 7:2 */ // indented with tab - import /* ERROR 8:9 */ // indented with blanks +x /* ERROR "3:1" */ // ignore automatically inserted semicolon here +/* ERROR "3:1" */ // position of x on previous line + x /* ERROR "5:4" */ ; // do not ignore this semicolon +/* ERROR "5:24" */ // position of ; on previous line + package /* ERROR "7:2" */ // indented with tab + import /* ERROR "8:9" */ // indented with blanks ` - m := ErrorMap(strings.NewReader(src)) - got := 0 // number of errors found + m := CommentMap(strings.NewReader(src), regexp.MustCompile("^ ERROR ")) + found := 0 // number of errors found for line, errlist := range m { for _, err := range errlist { if err.Pos.Line() != line { @@ -29,17 +30,19 @@ x /* ERROR 3:1 */ // ignore automatically inserted semicolon here continue } // err.Pos.Line() == line - msg := fmt.Sprintf("%d:%d", line, err.Pos.Col()) - if err.Msg != msg { - t.Errorf("%v: got msg %q; want %q", err, err.Msg, msg) + + got := strings.TrimSpace(err.Msg[len(" ERROR "):]) + want := fmt.Sprintf(`"%d:%d"`, line, err.Pos.Col()) + if got != want { + t.Errorf("%v: got msg %q; want %q", err, got, want) continue } + found++ } - got += len(errlist) } - want := strings.Count(src, "ERROR") - if got != want { - t.Errorf("ErrorMap got %d errors; want %d", got, want) + want := strings.Count(src, " ERROR ") + if found != want { + t.Errorf("CommentMap got %d errors; want %d", found, want) } } diff --git a/src/cmd/compile/internal/types2/check_test.go b/src/cmd/compile/internal/types2/check_test.go index c4c28cc04d..645b5b1572 100644 --- a/src/cmd/compile/internal/types2/check_test.go +++ b/src/cmd/compile/internal/types2/check_test.go @@ -184,7 +184,7 @@ func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) { t.Error(err) continue } - if m := syntax.ErrorMap(f); len(m) > 0 { + if m := syntax.CommentMap(f, regexp.MustCompile("^ ERROR ")); len(m) > 0 { errmap[filename] = m } f.Close() @@ -207,7 +207,11 @@ func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) { // one of errors in list should match the current error index := -1 // list index of matching message, if any for i, want := range list { - rx, err := regexp.Compile(want.Msg) + pattern := strings.TrimSpace(want.Msg[len(" ERROR "):]) + if n := len(pattern); n >= 2 && pattern[0] == '"' && pattern[n-1] == '"' { + pattern = pattern[1 : n-1] + } + rx, err := regexp.Compile(pattern) if err != nil { t.Errorf("%s:%d:%d: %v", filename, line, want.Pos.Col(), err) continue |
