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
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,
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"
}
}
}
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:]
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)
}
}
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)
}