Skip to content

Commit ca2a886

Browse files
committed
cmd/compile: record original and absolute file names for line directives
Also, with this change, error locations don't print absolute positions in [] brackets following positions relative to line directives. To get the absolute positions as well, specify the -L flag. Fixes golang#22660. Change-Id: I9ecfa254f053defba9c802222874155fa12fee2c Reviewed-on: https://go-review.googlesource.com/77090 Reviewed-by: David Crawshaw <crawshaw@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
1 parent 2c00dea commit ca2a886

File tree

12 files changed

+83
-27
lines changed

12 files changed

+83
-27
lines changed

misc/cgo/errors/errors_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func expect(t *testing.T, file string, errors []*regexp.Regexp) {
6363
defer os.RemoveAll(dir)
6464

6565
dst := filepath.Join(dir, strings.TrimSuffix(file, ".go"))
66-
cmd := exec.Command("go", "build", "-o="+dst, path(file))
66+
cmd := exec.Command("go", "build", "-gcflags=-L", "-o="+dst, path(file)) // TODO(gri) no need for -gcflags=-L if go tool is adjusted
6767
out, err := cmd.CombinedOutput()
6868
if err == nil {
6969
t.Errorf("expected cgo to fail but it succeeded")

src/cmd/asm/internal/lex/input.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"text/scanner"
1414

1515
"cmd/asm/internal/flags"
16+
"cmd/internal/objabi"
1617
"cmd/internal/src"
1718
)
1819

@@ -454,7 +455,7 @@ func (in *Input) line() {
454455
in.Error("unexpected token at end of #line: ", tok)
455456
}
456457
pos := src.MakePos(in.Base(), uint(in.Line()), uint(in.Col()))
457-
in.Stack.SetBase(src.NewLinePragmaBase(pos, file, uint(line)))
458+
in.Stack.SetBase(src.NewLinePragmaBase(pos, file, objabi.AbsFile(objabi.WorkingDir(), file, *flags.TrimPath), uint(line)))
458459
}
459460

460461
// #undef processing

src/cmd/compile/internal/gc/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func Main(archInit func(*Arch)) {
192192
objabi.Flagcount("E", "debug symbol export", &Debug['E'])
193193
objabi.Flagfn1("I", "add `directory` to import search path", addidir)
194194
objabi.Flagcount("K", "debug missing line numbers", &Debug['K'])
195+
objabi.Flagcount("L", "show full file names in error messages", &Debug['L'])
195196
objabi.Flagcount("N", "disable optimizations", &Debug['N'])
196197
flag.BoolVar(&Debug_asm, "S", false, "print assembly listing")
197198
objabi.AddVersionFlag() // -V

src/cmd/compile/internal/gc/subr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func hcrash() {
9393
}
9494

9595
func linestr(pos src.XPos) string {
96-
return Ctxt.OutermostPos(pos).Format(Debug['C'] == 0)
96+
return Ctxt.OutermostPos(pos).Format(Debug['C'] == 0, Debug['L'] == 1)
9797
}
9898

9999
// lasterror keeps track of the most recently issued error.

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,12 @@ func (p *parser) updateBase(line, col uint, text string) {
8080
p.error_at(p.pos_at(line, col+uint(i+1)), "invalid line number: "+nstr)
8181
return
8282
}
83-
absFile := text[:i]
83+
filename := text[:i]
84+
absFilename := filename
8485
if p.fileh != nil {
85-
absFile = p.fileh(absFile)
86+
absFilename = p.fileh(filename)
8687
}
87-
p.base = src.NewLinePragmaBase(src.MakePos(p.base.Pos().Base(), line, col), absFile, uint(n))
88+
p.base = src.NewLinePragmaBase(src.MakePos(p.base.Pos().Base(), line, col), filename, absFilename, uint(n))
8889
}
8990

9091
func (p *parser) got(tok token) bool {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func TestLineDirectives(t *testing.T) {
221221
if msg := perr.Msg; msg != test.msg {
222222
t.Errorf("%s: got msg = %q; want %q", test.src, msg, test.msg)
223223
}
224-
if filename := perr.Pos.RelFilename(); filename != test.filename {
224+
if filename := perr.Pos.AbsFilename(); filename != test.filename {
225225
t.Errorf("%s: got filename = %q; want %q", test.src, filename, test.filename)
226226
}
227227
if line := perr.Pos.RelLine(); line != test.line+linebase {

src/cmd/internal/obj/line_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestLinkgetlineFromPos(t *testing.T) {
1717

1818
afile := src.NewFileBase("a.go", "a.go")
1919
bfile := src.NewFileBase("b.go", "/foo/bar/b.go")
20-
lfile := src.NewLinePragmaBase(src.MakePos(afile, 7, 0), "linedir", 100)
20+
lfile := src.NewLinePragmaBase(src.MakePos(afile, 7, 0), "linedir", "linedir", 100)
2121

2222
var tests = []struct {
2323
pos src.Pos

src/cmd/internal/obj/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const REG_NONE = 0
1515

1616
// Line returns a string containing the filename and line number for p
1717
func (p *Prog) Line() string {
18-
return p.Ctxt.OutermostPos(p.Pos).Format(false)
18+
return p.Ctxt.OutermostPos(p.Pos).Format(false, true)
1919
}
2020

2121
// LineNumber returns a string containing the line number for p's position

src/cmd/internal/src/pos.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ func (p Pos) AbsFilename() string { return p.base.AbsFilename() }
7979
func (p Pos) SymFilename() string { return p.base.SymFilename() }
8080

8181
func (p Pos) String() string {
82-
return p.Format(true)
82+
return p.Format(true, true)
8383
}
8484

8585
// Format formats a position as "filename:line" or "filename:line:column",
86-
// controlled by the showCol flag.
87-
// If the position is relative to a line directive, the original position
88-
// is appended in square brackets without column (since the column doesn't
89-
// change).
90-
func (p Pos) Format(showCol bool) string {
86+
// controlled by the showCol flag. A position relative to a line directive
87+
// is always formatted without column information. In that case, if showOrig
88+
// is set, the original position (again controlled by showCol) is appended
89+
// in square brackets: "filename:line[origfile:origline:origcolumn]".
90+
func (p Pos) Format(showCol, showOrig bool) string {
9191
if !p.IsKnown() {
9292
return "<unknown line number>"
9393
}
@@ -105,8 +105,11 @@ func (p Pos) Format(showCol bool) string {
105105
// that's provided via a line directive).
106106
// TODO(gri) This may not be true if we have an inlining base.
107107
// We may want to differentiate at some point.
108-
return format(p.RelFilename(), p.RelLine(), 0, false) +
109-
"[" + format(p.Filename(), p.Line(), p.Col(), showCol) + "]"
108+
s := format(p.RelFilename(), p.RelLine(), 0, false)
109+
if showOrig {
110+
s += "[" + format(p.Filename(), p.Line(), p.Col(), showCol) + "]"
111+
}
112+
return s
110113
}
111114

112115
// format formats a (filename, line, col) tuple as "filename:line" (showCol
@@ -155,8 +158,8 @@ func NewFileBase(filename, absFilename string) *PosBase {
155158
// NewLinePragmaBase returns a new *PosBase for a line pragma of the form
156159
// //line filename:line
157160
// at position pos.
158-
func NewLinePragmaBase(pos Pos, filename string, line uint) *PosBase {
159-
return &PosBase{pos, filename, filename, FileSymPrefix + filename, line - 1, -1}
161+
func NewLinePragmaBase(pos Pos, filename, absFilename string, line uint) *PosBase {
162+
return &PosBase{pos, filename, absFilename, FileSymPrefix + absFilename, line - 1, -1}
160163
}
161164

162165
// NewInliningBase returns a copy of the old PosBase with the given inlining

src/cmd/internal/src/pos_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ import (
1212
func TestPos(t *testing.T) {
1313
f0 := NewFileBase("", "")
1414
f1 := NewFileBase("f1", "f1")
15-
f2 := NewLinePragmaBase(Pos{}, "f2", 10)
16-
f3 := NewLinePragmaBase(MakePos(f1, 10, 1), "f3", 100)
17-
f4 := NewLinePragmaBase(MakePos(f3, 10, 1), "f4", 100)
15+
f2 := NewLinePragmaBase(Pos{}, "f2", "f2", 10)
16+
f3 := NewLinePragmaBase(MakePos(f1, 10, 1), "f3", "f3", 100)
17+
f4 := NewLinePragmaBase(MakePos(f3, 10, 1), "f4", "f4", 100)
1818

1919
// line directives from issue #19392
2020
fp := NewFileBase("p.go", "p.go")
21-
fc := NewLinePragmaBase(MakePos(fp, 3, 0), "c.go", 10)
22-
ft := NewLinePragmaBase(MakePos(fp, 6, 0), "t.go", 20)
23-
fv := NewLinePragmaBase(MakePos(fp, 9, 0), "v.go", 30)
24-
ff := NewLinePragmaBase(MakePos(fp, 12, 0), "f.go", 40)
21+
fc := NewLinePragmaBase(MakePos(fp, 3, 0), "c.go", "c.go", 10)
22+
ft := NewLinePragmaBase(MakePos(fp, 6, 0), "t.go", "t.go", 20)
23+
fv := NewLinePragmaBase(MakePos(fp, 9, 0), "v.go", "v.go", 30)
24+
ff := NewLinePragmaBase(MakePos(fp, 12, 0), "f.go", "f.go", 40)
2525

2626
for _, test := range []struct {
2727
pos Pos

0 commit comments

Comments
 (0)