]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile/internal/syntax: rename ErrorMap to CommentMap, make more flexible
authorRobert Griesemer <gri@golang.org>
Thu, 8 Dec 2022 00:57:27 +0000 (16:57 -0800)
committerGopher Robot <gobot@golang.org>
Tue, 17 Jan 2023 19:53:18 +0000 (19:53 +0000)
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>

src/cmd/compile/internal/syntax/testing.go
src/cmd/compile/internal/syntax/testing_test.go
src/cmd/compile/internal/types2/check_test.go
src/internal/types/testdata/check/builtins0.go
src/internal/types/testdata/check/decls3.go
src/internal/types/testdata/check/stmt0.go
src/internal/types/testdata/fixedbugs/issue43190.go

index 6a97dc0c2a64a52c30291fddbb583f556ab0c473..202b2efc3e0d913b3aaef4fafb8e98a27bdacc6a 100644 (file)
@@ -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)
 
index d34e5eafafd241ae604c3889495cceb6f8d35204..7e439c5523998698c632a561587c982eb29e5a26 100644 (file)
@@ -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)
        }
 }
index c4c28cc04ddbd111d1a2b3cf1c0b11eab8d0e7b4..645b5b15721630e8cde2db2505e5e90426573e5a 100644 (file)
@@ -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
index 308f70b9ccec4778d3abeddab18628defe4c9502..b54d3390699d1755258aa954dbb73696a8cec617 100644 (file)
@@ -707,7 +707,7 @@ func Alignof1() {
        _ = unsafe.Alignof(new(struct{}))
        _ = unsafe.Alignof(1<<10)
        _ = unsafe.Alignof(1 << /* ERROR constant shift overflow */ 1000)
-       _ = unsafe.Alignof(nil /* ERROR "untyped nil */ )
+       _ = unsafe.Alignof(nil /* ERROR untyped nil */ )
        unsafe /* ERROR not used */ .Alignof(x)
 
        var y S0
index 01d4ffe4b5be163d5d568227d43da717294939db..947ab307ba8452f199a049db259253837251c07f 100644 (file)
@@ -221,16 +221,16 @@ func _() {
        _ = S2{}.B
        _ = S2{}.C
        _ = S2{}.D /* ERROR "no field or method" */
-       _ = S3{}.S1 /* ERROR "ambiguous selector S3{}.S1" */
+       _ = S3{}.S1 /* ERROR "ambiguous selector S3{}\.S1" */
        _ = S3{}.A
-       _ = S3{}.B /* ERROR "ambiguous selector" S3{}.B */
+       _ = S3{}.B /* ERROR "ambiguous selector S3{}\.B" */
        _ = S3{}.D
        _ = S3{}.E
        _ = S4{}.A
        _ = S4{}.B /* ERROR "no field or method" */
-       _ = S5{}.X /* ERROR "ambiguous selector S5{}.X" */
+       _ = S5{}.X /* ERROR "ambiguous selector S5{}\.X" */
        _ = S5{}.Y
-       _ = S10{}.X /* ERROR "ambiguous selector S10{}.X" */
+       _ = S10{}.X /* ERROR "ambiguous selector S10{}\.X" */
        _ = S10{}.Y
 }
 
index c456aace7c4b67e85c77ff1c92b3be94d4e8b20b..3e8cb6836b48505419e00bd376dc5e38c244ba8f 100644 (file)
@@ -509,7 +509,7 @@ func switches0() {
        switch a {
        case [3]int{1, 2, 3}:
        case [3]int{1, 2, 3}: // no duplicate detection
-       case [ /* ERROR "mismatched types */ 4]int{4, 5, 6}:
+       case [ /* ERROR "mismatched types" */ 4]int{4, 5, 6}:
        }
 
        // switch on channel
index d1b46b59f240586976b38b48ea77baf869f7d2a3..ace0487229740af27c81f05ef91b9fdd39379d1f 100644 (file)
@@ -11,7 +11,7 @@ import ; // ERROR missing import path
 import "" // ERROR invalid import path \(empty string\)
 import
 var /* ERROR missing import path */ _ int
-import .; //  ERROR missing import path
+import .; // ERROR missing import path
 import 'x' // ERROR import path must be a string
 var _ int
 import /* ERROR imports must appear before other declarations */ _ "math"