diff options
author | Robert Griesemer <gri@golang.org> | 2012-02-10 13:27:32 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2012-02-10 13:27:32 -0800 |
commit | 7b8496e9b843b11c06a7548916896335530329cc (patch) | |
tree | 0038eb2ded75dfebd94302b1ce9a9b6dc78a4b02 /src/cmd | |
parent | 254ef965be266b3cfdda29c124777f6224c5f512 (diff) | |
download | go-7b8496e9b843b11c06a7548916896335530329cc.tar.gz |
go/printer: implement SourcePos mode
If a printer is configured with the SourcePos mode
set, it will emit //-line comments as necessary to
ensure that the result - if reparsed - reflects the
original source position information.
This change required a bit of reworking of the
output section in printer.go. Specifically:
- Introduced new Config mode 'SourcePos'.
- Introduced new position 'out' which tracks the
position of the generated output if it were read
in again. If there is a discrepancy between out
and the current AST/source position, a //line
comment is emitted to correct for it.
- Lazy emission of indentation so that //line
comments can be placed correctly. As a result,
the trimmer will have to do less work.
- Merged writeItem into writeString.
- Merged writeByteN into writeByte.
- Use a []byte instead of a byte.Buffer both in the
printer and in the trimmer (eliminates dependency).
Also: introduced explicit printer.Mode type (in
sync w/ parser.Mode, scanner.Mode, etc.)
Runs all tests. Applied gofmt to src, misc w/o changes.
Fixes issue 1047.
Fixes issue 2697.
R=rsc, rsc
CC=golang-dev
http://codereview.appspot.com/5643066
Diffstat (limited to 'src/cmd')
-rw-r--r-- | src/cmd/cgo/godefs.go | 2 | ||||
-rw-r--r-- | src/cmd/cgo/out.go | 17 | ||||
-rw-r--r-- | src/cmd/gofmt/gofmt.go | 2 |
3 files changed, 11 insertions, 10 deletions
diff --git a/src/cmd/cgo/godefs.go b/src/cmd/cgo/godefs.go index 683872927..478ed261c 100644 --- a/src/cmd/cgo/godefs.go +++ b/src/cmd/cgo/godefs.go @@ -109,7 +109,7 @@ func (p *Package) godefs(f *File, srcfile string) string { } } - printer.Fprint(&buf, fset, f.AST) + conf.Fprint(&buf, fset, f.AST) return buf.String() } diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index 2c0107499..bfbcf50dc 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -17,6 +17,8 @@ import ( "strings" ) +var conf = printer.Config{Mode: printer.SourcePos, Tabwidth: 8} + // writeDefs creates output files to be compiled by 6g, 6c, and gcc. // (The comments here say 6g and 6c but the code applies to the 8 and 5 tools too.) func (p *Package) writeDefs() { @@ -57,7 +59,7 @@ func (p *Package) writeDefs() { for name, def := range typedef { fmt.Fprintf(fgo2, "type %s ", name) - printer.Fprint(fgo2, fset, def) + conf.Fprint(fgo2, fset, def) fmt.Fprintf(fgo2, "\n\n") } fmt.Fprintf(fgo2, "type _Ctype_void [0]byte\n") @@ -87,7 +89,7 @@ func (p *Package) writeDefs() { fmt.Fprintf(fc, "\n") fmt.Fprintf(fgo2, "var %s ", n.Mangle) - printer.Fprint(fgo2, fset, &ast.StarExpr{X: n.Type.Go}) + conf.Fprint(fgo2, fset, &ast.StarExpr{X: n.Type.Go}) fmt.Fprintf(fgo2, "\n") } fmt.Fprintf(fc, "\n") @@ -255,7 +257,7 @@ func (p *Package) writeDefsFunc(fc, fgo2 *os.File, n *Name) { Name: ast.NewIdent(n.Mangle), Type: gtype, } - printer.Fprint(fgo2, fset, d) + conf.Fprint(fgo2, fset, d) if *gccgo { fmt.Fprintf(fgo2, " __asm__(\"%s\")\n", n.C) } else { @@ -327,8 +329,7 @@ func (p *Package) writeOutput(f *File, srcfile string) { // Write Go output: Go input with rewrites of C.xxx to _C_xxx. fmt.Fprintf(fgo1, "// Created by cgo - DO NOT EDIT\n\n") - fmt.Fprintf(fgo1, "//line %s:1\n", srcfile) - printer.Fprint(fgo1, fset, f.AST) + conf.Fprint(fgo1, fset, f.AST) // While we process the vars and funcs, also write 6c and gcc output. // Gcc output starts with the preamble. @@ -542,11 +543,11 @@ func (p *Package) writeExports(fgo2, fc, fm *os.File) { // a Go wrapper function. if fn.Recv != nil { fmt.Fprintf(fgo2, "func %s(recv ", goname) - printer.Fprint(fgo2, fset, fn.Recv.List[0].Type) + conf.Fprint(fgo2, fset, fn.Recv.List[0].Type) forFieldList(fntype.Params, func(i int, atype ast.Expr) { fmt.Fprintf(fgo2, ", p%d ", i) - printer.Fprint(fgo2, fset, atype) + conf.Fprint(fgo2, fset, atype) }) fmt.Fprintf(fgo2, ")") if gccResult != "void" { @@ -556,7 +557,7 @@ func (p *Package) writeExports(fgo2, fc, fm *os.File) { if i > 0 { fmt.Fprint(fgo2, ", ") } - printer.Fprint(fgo2, fset, atype) + conf.Fprint(fgo2, fset, atype) }) fmt.Fprint(fgo2, ")") } diff --git a/src/cmd/gofmt/gofmt.go b/src/cmd/gofmt/gofmt.go index 6d610adc0..55c01beb5 100644 --- a/src/cmd/gofmt/gofmt.go +++ b/src/cmd/gofmt/gofmt.go @@ -45,7 +45,7 @@ var ( exitCode = 0 rewrite func(*ast.File) *ast.File parserMode parser.Mode - printerMode uint + printerMode printer.Mode ) func report(err error) { |