From ca2a886cbade477e2c7cf09cdac604b757fb70a9 Mon Sep 17 00:00:00 2001 From: griesemer Date: Fri, 10 Nov 2017 11:38:51 -0800 Subject: 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 #22660. Change-Id: I9ecfa254f053defba9c802222874155fa12fee2c Reviewed-on: https://go-review.googlesource.com/77090 Reviewed-by: David Crawshaw Reviewed-by: Russ Cox --- src/cmd/compile/internal/gc/main.go | 1 + src/cmd/compile/internal/gc/subr.go | 2 +- src/cmd/compile/internal/syntax/parser.go | 7 ++++--- src/cmd/compile/internal/syntax/parser_test.go | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) (limited to 'src/cmd/compile') diff --git a/src/cmd/compile/internal/gc/main.go b/src/cmd/compile/internal/gc/main.go index 6f633a494f..be2fe552f6 100644 --- a/src/cmd/compile/internal/gc/main.go +++ b/src/cmd/compile/internal/gc/main.go @@ -192,6 +192,7 @@ func Main(archInit func(*Arch)) { objabi.Flagcount("E", "debug symbol export", &Debug['E']) objabi.Flagfn1("I", "add `directory` to import search path", addidir) objabi.Flagcount("K", "debug missing line numbers", &Debug['K']) + objabi.Flagcount("L", "show full file names in error messages", &Debug['L']) objabi.Flagcount("N", "disable optimizations", &Debug['N']) flag.BoolVar(&Debug_asm, "S", false, "print assembly listing") objabi.AddVersionFlag() // -V diff --git a/src/cmd/compile/internal/gc/subr.go b/src/cmd/compile/internal/gc/subr.go index b7d530c98c..81441ba700 100644 --- a/src/cmd/compile/internal/gc/subr.go +++ b/src/cmd/compile/internal/gc/subr.go @@ -93,7 +93,7 @@ func hcrash() { } func linestr(pos src.XPos) string { - return Ctxt.OutermostPos(pos).Format(Debug['C'] == 0) + return Ctxt.OutermostPos(pos).Format(Debug['C'] == 0, Debug['L'] == 1) } // lasterror keeps track of the most recently issued error. diff --git a/src/cmd/compile/internal/syntax/parser.go b/src/cmd/compile/internal/syntax/parser.go index 7047266a70..ff3e769864 100644 --- a/src/cmd/compile/internal/syntax/parser.go +++ b/src/cmd/compile/internal/syntax/parser.go @@ -80,11 +80,12 @@ func (p *parser) updateBase(line, col uint, text string) { p.error_at(p.pos_at(line, col+uint(i+1)), "invalid line number: "+nstr) return } - absFile := text[:i] + filename := text[:i] + absFilename := filename if p.fileh != nil { - absFile = p.fileh(absFile) + absFilename = p.fileh(filename) } - p.base = src.NewLinePragmaBase(src.MakePos(p.base.Pos().Base(), line, col), absFile, uint(n)) + p.base = src.NewLinePragmaBase(src.MakePos(p.base.Pos().Base(), line, col), filename, absFilename, uint(n)) } func (p *parser) got(tok token) bool { diff --git a/src/cmd/compile/internal/syntax/parser_test.go b/src/cmd/compile/internal/syntax/parser_test.go index 0478088ec8..309f1333f4 100644 --- a/src/cmd/compile/internal/syntax/parser_test.go +++ b/src/cmd/compile/internal/syntax/parser_test.go @@ -221,7 +221,7 @@ func TestLineDirectives(t *testing.T) { if msg := perr.Msg; msg != test.msg { t.Errorf("%s: got msg = %q; want %q", test.src, msg, test.msg) } - if filename := perr.Pos.RelFilename(); filename != test.filename { + if filename := perr.Pos.AbsFilename(); filename != test.filename { t.Errorf("%s: got filename = %q; want %q", test.src, filename, test.filename) } if line := perr.Pos.RelLine(); line != test.line+linebase { -- cgit v1.3