Skip to content

Commit 0adb0a3

Browse files
griesemereric
authored andcommitted
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 golang#51006. Change-Id: I749e8f31b532edbf8568f27ba1546dc849efd143 Reviewed-on: https://go-review.googlesource.com/c/go/+/456155 Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Cherry Mui <[email protected]>
1 parent bff3ce4 commit 0adb0a3

File tree

7 files changed

+52
-48
lines changed

7 files changed

+52
-48
lines changed

src/cmd/compile/internal/syntax/testing.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package syntax
99
import (
1010
"io"
1111
"regexp"
12-
"strings"
1312
)
1413

1514
// CommentsDo parses the given source and calls the provided handler for each
@@ -23,40 +22,38 @@ func CommentsDo(src io.Reader, handler func(line, col uint, text string)) {
2322
}
2423
}
2524

26-
// ERROR comments must start with text `ERROR "msg"` or `ERROR msg`.
27-
// Space around "msg" or msg is ignored.
28-
var errRx = regexp.MustCompile(`^ *ERROR *"?([^"]*)"?`)
29-
30-
// ErrorMap collects all comments with comment text of the form
31-
// `ERROR "msg"` or `ERROR msg` from the given src and returns them
32-
// as []Error lists in a map indexed by line number. The position
33-
// for each Error is the position of the token immediately preceding
34-
// the comment, the Error message is the message msg extracted from
35-
// the comment, with all errors that are on the same line collected
36-
// in a slice, in source order. If there is no preceding token (the
37-
// `ERROR` comment appears in the beginning of the file), then the
38-
// recorded position is unknown (line, col = 0, 0). If there are no
39-
// ERROR comments, the result is nil.
40-
func ErrorMap(src io.Reader) (errmap map[uint][]Error) {
25+
// CommentMap collects all comments in the given src with comment text
26+
// that matches the supplied regular expression rx and returns them as
27+
// []Error lists in a map indexed by line number. The comment text is
28+
// the comment with any comment markers ("//", "/*", or "*/") stripped.
29+
// The position for each Error is the position of the token immediately
30+
// preceding the comment and the Error message is the comment text,
31+
// with all comments that are on the same line collected in a slice, in
32+
// source order. If there is no preceding token (the matching comment
33+
// appears at the beginning of the file), then the recorded position
34+
// is unknown (line, col = 0, 0). If there are no matching comments,
35+
// the result is nil.
36+
func CommentMap(src io.Reader, rx *regexp.Regexp) (res map[uint][]Error) {
4137
// position of previous token
4238
var base *PosBase
4339
var prev struct{ line, col uint }
4440

4541
var s scanner
4642
s.init(src, func(_, _ uint, text string) {
4743
if text[0] != '/' {
48-
return // error, ignore
44+
return // not a comment, ignore
4945
}
5046
if text[1] == '*' {
5147
text = text[:len(text)-2] // strip trailing */
5248
}
53-
if s := errRx.FindStringSubmatch(text[2:]); len(s) == 2 {
49+
text = text[2:] // strip leading // or /*
50+
if rx.MatchString(text) {
5451
pos := MakePos(base, prev.line, prev.col)
55-
err := Error{pos, strings.TrimSpace(s[1])}
56-
if errmap == nil {
57-
errmap = make(map[uint][]Error)
52+
err := Error{pos, text}
53+
if res == nil {
54+
res = make(map[uint][]Error)
5855
}
59-
errmap[prev.line] = append(errmap[prev.line], err)
56+
res[prev.line] = append(res[prev.line], err)
6057
}
6158
}, comments)
6259

src/cmd/compile/internal/syntax/testing_test.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,43 @@ package syntax
66

77
import (
88
"fmt"
9+
"regexp"
910
"strings"
1011
"testing"
1112
)
1213

13-
func TestErrorMap(t *testing.T) {
14-
const src = `/* ERROR 0:0 */ /* ERROR "0:0" */ // ERROR 0:0
14+
func TestCommentMap(t *testing.T) {
15+
const src = `/* ERROR "0:0" */ /* ERROR "0:0" */ // ERROR "0:0"
1516
// ERROR "0:0"
16-
x /* ERROR 3:1 */ // ignore automatically inserted semicolon here
17-
/* ERROR 3:1 */ // position of x on previous line
18-
x /* ERROR 5:4 */ ; // do not ignore this semicolon
19-
/* ERROR 5:22 */ // position of ; on previous line
20-
package /* ERROR 7:2 */ // indented with tab
21-
import /* ERROR 8:9 */ // indented with blanks
17+
x /* ERROR "3:1" */ // ignore automatically inserted semicolon here
18+
/* ERROR "3:1" */ // position of x on previous line
19+
x /* ERROR "5:4" */ ; // do not ignore this semicolon
20+
/* ERROR "5:24" */ // position of ; on previous line
21+
package /* ERROR "7:2" */ // indented with tab
22+
import /* ERROR "8:9" */ // indented with blanks
2223
`
23-
m := ErrorMap(strings.NewReader(src))
24-
got := 0 // number of errors found
24+
m := CommentMap(strings.NewReader(src), regexp.MustCompile("^ ERROR "))
25+
found := 0 // number of errors found
2526
for line, errlist := range m {
2627
for _, err := range errlist {
2728
if err.Pos.Line() != line {
2829
t.Errorf("%v: got map line %d; want %d", err, err.Pos.Line(), line)
2930
continue
3031
}
3132
// err.Pos.Line() == line
32-
msg := fmt.Sprintf("%d:%d", line, err.Pos.Col())
33-
if err.Msg != msg {
34-
t.Errorf("%v: got msg %q; want %q", err, err.Msg, msg)
33+
34+
got := strings.TrimSpace(err.Msg[len(" ERROR "):])
35+
want := fmt.Sprintf(`"%d:%d"`, line, err.Pos.Col())
36+
if got != want {
37+
t.Errorf("%v: got msg %q; want %q", err, got, want)
3538
continue
3639
}
40+
found++
3741
}
38-
got += len(errlist)
3942
}
4043

41-
want := strings.Count(src, "ERROR")
42-
if got != want {
43-
t.Errorf("ErrorMap got %d errors; want %d", got, want)
44+
want := strings.Count(src, " ERROR ")
45+
if found != want {
46+
t.Errorf("CommentMap got %d errors; want %d", found, want)
4447
}
4548
}

src/cmd/compile/internal/types2/check_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
184184
t.Error(err)
185185
continue
186186
}
187-
if m := syntax.ErrorMap(f); len(m) > 0 {
187+
if m := syntax.CommentMap(f, regexp.MustCompile("^ ERROR ")); len(m) > 0 {
188188
errmap[filename] = m
189189
}
190190
f.Close()
@@ -207,7 +207,11 @@ func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
207207
// one of errors in list should match the current error
208208
index := -1 // list index of matching message, if any
209209
for i, want := range list {
210-
rx, err := regexp.Compile(want.Msg)
210+
pattern := strings.TrimSpace(want.Msg[len(" ERROR "):])
211+
if n := len(pattern); n >= 2 && pattern[0] == '"' && pattern[n-1] == '"' {
212+
pattern = pattern[1 : n-1]
213+
}
214+
rx, err := regexp.Compile(pattern)
211215
if err != nil {
212216
t.Errorf("%s:%d:%d: %v", filename, line, want.Pos.Col(), err)
213217
continue

src/internal/types/testdata/check/builtins0.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ func Alignof1() {
707707
_ = unsafe.Alignof(new(struct{}))
708708
_ = unsafe.Alignof(1<<10)
709709
_ = unsafe.Alignof(1 << /* ERROR constant shift overflow */ 1000)
710-
_ = unsafe.Alignof(nil /* ERROR "untyped nil */ )
710+
_ = unsafe.Alignof(nil /* ERROR untyped nil */ )
711711
unsafe /* ERROR not used */ .Alignof(x)
712712

713713
var y S0

src/internal/types/testdata/check/decls3.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,16 @@ func _() {
221221
_ = S2{}.B
222222
_ = S2{}.C
223223
_ = S2{}.D /* ERROR "no field or method" */
224-
_ = S3{}.S1 /* ERROR "ambiguous selector S3{}.S1" */
224+
_ = S3{}.S1 /* ERROR "ambiguous selector S3{}\.S1" */
225225
_ = S3{}.A
226-
_ = S3{}.B /* ERROR "ambiguous selector" S3{}.B */
226+
_ = S3{}.B /* ERROR "ambiguous selector S3{}\.B" */
227227
_ = S3{}.D
228228
_ = S3{}.E
229229
_ = S4{}.A
230230
_ = S4{}.B /* ERROR "no field or method" */
231-
_ = S5{}.X /* ERROR "ambiguous selector S5{}.X" */
231+
_ = S5{}.X /* ERROR "ambiguous selector S5{}\.X" */
232232
_ = S5{}.Y
233-
_ = S10{}.X /* ERROR "ambiguous selector S10{}.X" */
233+
_ = S10{}.X /* ERROR "ambiguous selector S10{}\.X" */
234234
_ = S10{}.Y
235235
}
236236

src/internal/types/testdata/check/stmt0.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ func switches0() {
509509
switch a {
510510
case [3]int{1, 2, 3}:
511511
case [3]int{1, 2, 3}: // no duplicate detection
512-
case [ /* ERROR "mismatched types */ 4]int{4, 5, 6}:
512+
case [ /* ERROR "mismatched types" */ 4]int{4, 5, 6}:
513513
}
514514

515515
// switch on channel

src/internal/types/testdata/fixedbugs/issue43190.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import ; // ERROR missing import path
1111
import "" // ERROR invalid import path \(empty string\)
1212
import
1313
var /* ERROR missing import path */ _ int
14-
import .; // ERROR missing import path
14+
import .; // ERROR missing import path
1515
import 'x' // ERROR import path must be a string
1616
var _ int
1717
import /* ERROR imports must appear before other declarations */ _ "math"

0 commit comments

Comments
 (0)