cmd/compile, cmd/compile/internal/syntax: print relative column info

This change enables printing of relative column information if a
prior line directive specified a valid column. If there was no
line directive, or the line directive didn't specify a column
(or the -C flag is specified), no column information is shown in
file positions.

Implementation: Column values (and line values, for that matter)
that are zero are interpreted as "unknown". A line directive that
doesn't specify a column records that as a zero column in the
respective PosBase data structure. When computing relative columns,
a relative value is zero of the base's column value is zero.
When formatting a position, a zero column value is not printed.

To make this work without special cases, the PosBase for a file
is given a concrete (non-0:0) position 1:1 with the PosBase's
line and column also being 1:1. In other words, at the position
1:1 of a file, it's relative positions are starting with 1:1 as
one would expect.

In the package syntax, this requires self-recursive PosBases for
file bases, matching what cmd/internal/src.PosBase was already
doing. In src.PosBase, file and inlining bases also need to be
based at 1:1 to indicate "known" positions.

This change completes the cmd/compiler part of the issue below.

Fixes #22662.

Change-Id: I6c3d2dee26709581fba0d0261b1d12e93f1cba1a
Reviewed-on: https://go-review.googlesource.com/97375
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Robert Griesemer 2018-02-26 17:35:29 -08:00
parent b5bd5bfbc7
commit 0c884d0810
7 changed files with 272 additions and 151 deletions

View file

@ -85,8 +85,8 @@ func (p *parser) init(file *PosBase, r io.Reader, errh ErrorHandler, pragh Pragm
// updateBase sets the current position base to a new line base at pos.
// The base's filename, line, and column values are extracted from text
// which is positioned at (line, col) (only needed for error messages).
func (p *parser) updateBase(pos Pos, line, col uint, text string) {
// which is positioned at (tline, tcol) (only needed for error messages).
func (p *parser) updateBase(pos Pos, tline, tcol uint, text string) {
i, n, ok := trailingDigits(text)
if i == 0 {
return // ignore (not a line directive)
@ -95,38 +95,39 @@ func (p *parser) updateBase(pos Pos, line, col uint, text string) {
if !ok {
// text has a suffix :xxx but xxx is not a number
p.errorAt(p.posAt(line, col+i), "invalid line number: "+text[i:])
p.errorAt(p.posAt(tline, tcol+i), "invalid line number: "+text[i:])
return
}
var line, col uint
i2, n2, ok2 := trailingDigits(text[:i-1])
if ok2 {
//line filename:line:col
i, i2 = i2, i
n, n2 = n2, n
if n2 == 0 || n2 > PosMax {
p.errorAt(p.posAt(line, col+i2), "invalid column number: "+text[i2:])
line, col = n2, n
if col == 0 || col > PosMax {
p.errorAt(p.posAt(tline, tcol+i2), "invalid column number: "+text[i2:])
return
}
text = text[:i2-1] // lop off :col
text = text[:i2-1] // lop off ":col"
} else {
//line filename:line
n2 = colbase // use start of line for column
line = n
}
if n == 0 || n > PosMax {
p.errorAt(p.posAt(line, col+i), "invalid line number: "+text[i:])
if line == 0 || line > PosMax {
p.errorAt(p.posAt(tline, tcol+i), "invalid line number: "+text[i:])
return
}
// If we have a column (//line filename:line:col form),
// an empty filename means to use the previous filename.
filename := text[:i-1] // lop off :line
filename := text[:i-1] // lop off ":line"
if filename == "" && ok2 {
filename = p.base.Filename()
}
p.base = NewLineBase(pos, filename, n, n2)
p.base = NewLineBase(pos, filename, line, col)
}
func commentText(s string) string {