]> Cypherpunks repositories - gostls13.git/commitdiff
cgo: improve error reporting slightly.
authorRoger Peppe <rogpeppe@gmail.com>
Mon, 31 Jan 2011 19:31:34 +0000 (14:31 -0500)
committerRuss Cox <rsc@golang.org>
Mon, 31 Jan 2011 19:31:34 +0000 (14:31 -0500)
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
src/cmd/cgo/gcc.go
src/cmd/cgo/main.go

index 8689ac3dac2510293a975dfbf5422d7701325cb3..9bb8a55fd4b920e1f17908ff88afbc677105c058 100644 (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"
                        }
                }
        }
index 57dc4dc83d32a6129596e06a5810c5a5731f9b4b..e400fcdde7e813f98ad5dc1e31b2b70da35f54a3 100644 (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)
        }
index 14bb7ec2b08b626c8ed7ca5a03c1b75ec5b1a274..5d2bfd0e3bd74c35bd387350ace49800905c9e3a 100644 (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)
 }