cmd/compile: replace GOROOT in //line directives

The compiler replaces any path of the form /path/to/goroot/src/net/port.go
with GOROOT/src/net/port.go so that the same object file is
produced if the GOROOT is moved. It was skipping this transformation
for any absolute path into the GOROOT that came from //line directives,
such as those generated by cmd/cgo.

Fixes #21373
Fixes #21720
Fixes #21825

Change-Id: I2784c701b4391cfb92e23efbcb091a84957d61dd
Reviewed-on: https://go-review.googlesource.com/63693
Run-TryBot: David Crawshaw <crawshaw@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
David Crawshaw 2017-09-13 19:04:25 -04:00
parent af86083812
commit 27e80f7c4d
7 changed files with 43 additions and 18 deletions

View file

@ -131,7 +131,7 @@ func verifyPrint(filename string, ast1 *File) {
panic(err)
}
ast2, err := ParseBytes(src.NewFileBase(filename, filename), buf1.Bytes(), nil, nil, 0)
ast2, err := ParseBytes(src.NewFileBase(filename, filename), buf1.Bytes(), nil, nil, nil, 0)
if err != nil {
panic(err)
}
@ -155,7 +155,7 @@ func verifyPrint(filename string, ast1 *File) {
}
func TestIssue17697(t *testing.T) {
_, err := ParseBytes(nil, nil, nil, nil, 0) // return with parser error, don't panic
_, err := ParseBytes(nil, nil, nil, nil, nil, 0) // return with parser error, don't panic
if err == nil {
t.Errorf("no error reported")
}
@ -199,8 +199,16 @@ func TestLineDirectives(t *testing.T) {
// test effect of //line directive on (relative) position information
{"//line foo:123\n foo", "syntax error: package statement must be first", "foo", 123 - linebase, 3},
{"//line foo:123\n//line bar:345\nfoo", "syntax error: package statement must be first", "bar", 345 - linebase, 0},
{"//line " + runtime.GOROOT() + "/src/a/a.go:123\n foo", "syntax error: package statement must be first", "$GOROOT/src/a/a.go", 123 - linebase, 3},
} {
_, err := ParseBytes(nil, []byte(test.src), nil, nil, 0)
fileh := func(name string) string {
if strings.HasPrefix(name, runtime.GOROOT()) {
return "$GOROOT" + name[len(runtime.GOROOT()):]
}
return name
}
_, err := ParseBytes(nil, []byte(test.src), nil, nil, fileh, 0)
if err == nil {
t.Errorf("%s: no error reported", test.src)
continue