1
0
mirror of https://github.com/golang/go synced 2024-11-20 05:14:41 -07:00
go/test/fixedbugs/issue22662b.go
Robert Griesemer 0c884d0810 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>
2018-02-28 03:51:23 +00:00

66 lines
1.8 KiB
Go

// run
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Verify the impact of line directives on error positions and position formatting.
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
"runtime"
"strings"
)
// Each of these tests is expected to fail (missing package clause)
// at the position determined by the preceeding line directive.
var tests = []struct {
src, pos string
}{
{"//line :10\n", ":10:"}, // no filename means no filename
{"//line :10:4\n", "filename:10:4"}, // no filename means use existing filename
{"//line foo.go:10\n", "foo.go:10:"}, // no column means don't print a column
{"//line foo.go:10:4\n", "foo.go:10:4:"}, // column means print a column
{"//line foo.go:10:4\n\n", "foo.go:11:1:"}, // relative columns start at 1 after newline
{"/*line :10*/", ":10:"},
{"/*line :10:4*/", "filename:10:4"},
{"/*line foo.go:10*/", "foo.go:10:"},
{"/*line foo.go:10:4*/", "foo.go:10:4:"},
{"/*line foo.go:10:4*/\n", "foo.go:11:1:"},
}
func main() {
if runtime.GOOS == "nacl" {
return // no file system available on builders
}
f, err := ioutil.TempFile("", "issue22662b.go")
if err != nil {
log.Fatal(err)
}
f.Close()
defer os.Remove(f.Name())
for _, test := range tests {
if err := ioutil.WriteFile(f.Name(), []byte(test.src), 0660); err != nil {
log.Fatal(err)
}
out, err := exec.Command("go", "tool", "compile", f.Name()).CombinedOutput()
if err == nil {
log.Fatalf("expected compiling\n---\n%s\n---\nto fail", test.src)
}
errmsg := strings.Replace(string(out), f.Name(), "filename", -1) // use "filename" instead of actual (long) filename
if !strings.HasPrefix(errmsg, test.pos) {
log.Fatalf("%q: got %q; want position %q", test.src, errmsg, test.pos)
}
}
}