[go] push by g...@golang.org - go/printer, gofmt: don't indent line directives... on 2012-02-13 19:50 GMT

0 views
Skip to first unread message

g...@googlecode.com

unread,
Feb 13, 2012, 2:55:15 PM2/13/12
to golang-...@googlegroups.com
Revision: 17f57b6e8bf3
Author: Robert Griesemer <g...@golang.org>
Date: Mon Feb 13 11:50:53 2012
Log: go/printer, gofmt: don't indent line directives

This was broken by http://codereview.appspot.com/5643066
which introduced lazy indentation printing.

Fixes issue 2990.

R=rsc
CC=golang-dev
http://codereview.appspot.com/5655067
http://code.google.com/p/go/source/detail?r=17f57b6e8bf3

Modified:
/src/pkg/go/printer/printer.go
/src/pkg/go/printer/testdata/comments.golden
/src/pkg/go/printer/testdata/comments.input

=======================================
--- /src/pkg/go/printer/printer.go Mon Feb 13 10:58:17 2012
+++ /src/pkg/go/printer/printer.go Mon Feb 13 11:50:53 2012
@@ -274,8 +274,6 @@

p.last = p.pos
}
-
-const linePrefix = "//line "

// writeCommentPrefix writes the whitespace before a comment.
// If there is any pending whitespace, it consumes as much of
@@ -397,16 +395,10 @@
}

if n > 0 {
- // turn off indent if we're about to print a line directive
- indent := p.indent
- if strings.HasPrefix(comment.Text, linePrefix) {
- p.indent = 0
- }
// use formfeeds to break columns before a comment;
// this is analogous to using formfeeds to separate
// individual lines of /*-style comments
p.writeByte('\f', nlimit(n))
- p.indent = indent // restore indent
}
}
}
@@ -588,30 +580,33 @@

func (p *printer) writeComment(comment *ast.Comment) {
text := comment.Text
-
- if strings.HasPrefix(text, linePrefix) {
- pos := strings.TrimSpace(text[len(linePrefix):])
- i := strings.LastIndex(pos, ":")
- if i >= 0 {
- // The line directive we are about to print changed
- // the Filename and Line number used by go/token
- // as it was reading the input originally.
- // In order to match the original input, we have to
- // update our own idea of the file and line number
- // accordingly, after printing the directive.
- file := pos[:i]
- line, _ := strconv.Atoi(pos[i+1:])
- defer func() {
- p.pos.Filename = file
- p.pos.Line = line
- p.pos.Column = 1
- }()
+ pos := p.posFor(comment.Pos())
+
+ const linePrefix = "//line "
+ if strings.HasPrefix(text, linePrefix) && (!pos.IsValid() || pos.Column
== 1) {
+ // possibly a line directive
+ ldir := strings.TrimSpace(text[len(linePrefix):])
+ if i := strings.LastIndex(ldir, ":"); i >= 0 {
+ if line, err := strconv.Atoi(ldir[i+1:]); err == nil && line > 0 {
+ // The line directive we are about to print changed
+ // the Filename and Line number used for subsequent
+ // tokens. We have to update our AST-space position
+ // accordingly and suspend indentation temporarily.
+ indent := p.indent
+ p.indent = 0
+ defer func() {
+ p.pos.Filename = ldir[:i]
+ p.pos.Line = line
+ p.pos.Column = 1
+ p.indent = indent
+ }()
+ }
}
}

// shortcut common case of //-style comments
if text[1] == '/' {
- p.writeString(p.posFor(comment.Pos()), text, true)
+ p.writeString(pos, text, true)
return
}

@@ -622,7 +617,6 @@

// write comment lines, separated by formfeed,
// without a line break after the last line
- pos := p.posFor(comment.Pos())
for i, line := range lines {
if i > 0 {
p.writeByte('\f', 1)
=======================================
--- /src/pkg/go/printer/testdata/comments.golden Fri Feb 10 13:28:29 2012
+++ /src/pkg/go/printer/testdata/comments.golden Mon Feb 13 11:50:53 2012
@@ -478,6 +478,25 @@
i++ /* comment before opening curly brace */ {
}
}
+
+// Print line directives correctly.
+
+// The following is a legal line directive.
+//line foo:1
+func _() {
+ _ = 0
+ // The following is a legal line directive. It must not be indented:
+//line foo:2
+ _ = 1
+
+ // The following is not a legal line directive (it doesn't start in
column 1):
+ //line foo:2
+ _ = 2
+
+ // The following is not a legal line directive (negative line number):
+ //line foo:-3
+ _ = 3
+}

// Line comments with tabs
func _() {
=======================================
--- /src/pkg/go/printer/testdata/comments.input Fri Feb 10 13:28:29 2012
+++ /src/pkg/go/printer/testdata/comments.input Mon Feb 13 11:50:53 2012
@@ -486,6 +486,25 @@
}
}

+
+// Print line directives correctly.
+
+// The following is a legal line directive.
+//line foo:1
+func _() {
+ _ = 0
+// The following is a legal line directive. It must not be indented:
+//line foo:2
+ _ = 1
+
+// The following is not a legal line directive (it doesn't start in column
1):
+ //line foo:2
+ _ = 2
+
+// The following is not a legal line directive (negative line number):
+//line foo:-3
+ _ = 3
+}

// Line comments with tabs
func _() {

Reply all
Reply to author
Forward
0 new messages