From 76130bdeeacc5d6d78102b4cbb6e0f110a812eb4 Mon Sep 17 00:00:00 2001 From: Roger Peppe Date: Mon, 31 Jan 2011 14:31:34 -0500 Subject: [PATCH] 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 --- src/cmd/cgo/ast.go | 15 +++++++++++---- src/cmd/cgo/gcc.go | 8 +------- src/cmd/cgo/main.go | 3 ++- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/cmd/cgo/ast.go b/src/cmd/cgo/ast.go index 8689ac3dac2..9bb8a55fd4b 100644 --- a/src/cmd/cgo/ast.go +++ b/src/cmd/cgo/ast.go @@ -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" } } } diff --git a/src/cmd/cgo/gcc.go b/src/cmd/cgo/gcc.go index 57dc4dc83d3..e400fcdde7e 100644 --- a/src/cmd/cgo/gcc.go +++ b/src/cmd/cgo/gcc.go @@ -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] == ":" { - 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 <