cgo: improve error reporting slightly.

If there were warnings or errors in the user code,
cgo would print the first error from gcc and then stop,
which is not helpful.
This CL makes cgo ignore errors from user code
in the first pass - they will be shown later.
It also prints errors from user preamble code
with the correct line numbers.
(Also fixed misleading usage message).

R=iant, rsc
CC=golang-dev
https://golang.org/cl/4082047
This commit is contained in:
Roger Peppe 2011-01-31 14:31:34 -05:00 committed by Russ Cox
parent 5c0aab9cae
commit 76130bdeea
3 changed files with 14 additions and 12 deletions

View file

@ -35,6 +35,10 @@ func parse(name string, flags uint) *ast.File {
return ast1
}
func sourceLine(n ast.Node) int {
return fset.Position(n.Pos()).Line
}
// ReadGo populates f with information learned from reading the
// Go source file with the given file name. It gathers the C preamble
// attached to the import "C" comment, a list of references to C.xxx,
@ -69,10 +73,13 @@ func (f *File) ReadGo(name string) {
if s.Name != nil {
error(s.Path.Pos(), `cannot rename import "C"`)
}
if s.Doc != nil {
f.Preamble += doc.CommentText(s.Doc) + "\n"
} else if len(d.Specs) == 1 && d.Doc != nil {
f.Preamble += doc.CommentText(d.Doc) + "\n"
cg := s.Doc
if cg == nil && len(d.Specs) == 1 {
cg = d.Doc
}
if cg != nil {
f.Preamble += fmt.Sprintf("#line %d %q\n", sourceLine(cg), name)
f.Preamble += doc.CommentText(cg) + "\n"
}
}
}

View file

@ -207,9 +207,7 @@ func (p *Package) guessKinds(f *File) []*Name {
for _, line := range strings.Split(stderr, "\n", -1) {
if len(line) < 9 || line[0:9] != "cgo-test:" {
if len(line) > 8 && line[0:8] == "<stdin>:" {
fatal("gcc produced unexpected output:\n%s\non input:\n%s", line, b.Bytes())
}
// the user will see any compiler errors when the code is compiled later.
continue
}
line = line[9:]
@ -570,10 +568,6 @@ func runGcc(stdin []byte, args []string) (string, string) {
os.Stderr.Write(stderr)
}
if !ok {
fmt.Fprint(os.Stderr, "Error running gcc:\n")
fmt.Fprintf(os.Stderr, "$ %s <<EOF\n", strings.Join(args, " "))
os.Stderr.Write(stdin)
fmt.Fprint(os.Stderr, "EOF\n")
os.Stderr.Write(stderr)
os.Exit(2)
}

View file

@ -98,7 +98,8 @@ type FuncType struct {
}
func usage() {
fmt.Fprint(os.Stderr, "usage: cgo [compiler options] file.go ...\n")
fmt.Fprint(os.Stderr, "usage: cgo -- [compiler options] file.go ...\n")
flag.PrintDefaults()
os.Exit(2)
}