diff options
author | Russ Cox <rsc@golang.org> | 2013-02-01 08:34:08 -0800 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2013-02-01 08:34:08 -0800 |
commit | 20b0675608e8cf52e17bd61f4cc4f4eef0145114 (patch) | |
tree | d3b5dce49cdafcca5e3a122b58541bc9ce49e3e8 /src/cmd/cgo | |
parent | 71ca34e2a52e07edb6525f7db7c8f61f729f60b9 (diff) | |
download | go-20b0675608e8cf52e17bd61f4cc4f4eef0145114.tar.gz |
cmd/cgo: fix line number annotations in generated C code
The old version was using go/ast's CommentGroup.Text method,
but that method drops leading blank lines from the result, so that
if the comment looked like one of
//
// syntax error
import "C"
/*
syntax error
*/
import "C"
then the line numbers for the syntax error would be off by the
number of leading blank lines (1 in each of the above cases).
The new text extractor preserves blank lines.
Fixes issue 4019.
R=golang-dev, iant
CC=golang-dev
https://codereview.appspot.com/7232071
Diffstat (limited to 'src/cmd/cgo')
-rw-r--r-- | src/cmd/cgo/ast.go | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/cmd/cgo/ast.go b/src/cmd/cgo/ast.go index 381e606ef..dbae3b7b1 100644 --- a/src/cmd/cgo/ast.go +++ b/src/cmd/cgo/ast.go @@ -78,7 +78,7 @@ func (f *File) ReadGo(name string) { } if cg != nil { f.Preamble += fmt.Sprintf("#line %d %q\n", sourceLine(cg), name) - f.Preamble += cg.Text() + "\n" + f.Preamble += commentText(cg) + "\n" } } } @@ -131,6 +131,30 @@ func (f *File) ReadGo(name string) { f.AST = ast2 } +// Like ast.CommentGroup's Text method but preserves +// leading blank lines, so that line numbers line up. +func commentText(g *ast.CommentGroup) string { + if g == nil { + return "" + } + var pieces []string + for _, com := range g.List { + c := string(com.Text) + // Remove comment markers. + // The parser has given us exactly the comment text. + switch c[1] { + case '/': + //-style comment (no newline at the end) + c = c[2:] + "\n" + case '*': + /*-style comment */ + c = c[2 : len(c)-2] + } + pieces = append(pieces, c) + } + return strings.Join(pieces, "") +} + // Save references to C.xxx for later processing. func (f *File) saveRef(x interface{}, context string) { n, ok := x.(*ast.Expr) |