Commit 28ffb38f authored by Robert Griesemer's avatar Robert Griesemer

go/printer, gofmt: don't indent line directives

This was broken by https://golang.org/cl/5643066
which introduced lazy indentation printing.

Fixes #2990.

R=rsc
CC=golang-dev
https://golang.org/cl/5655067
parent c11361e2
...@@ -275,8 +275,6 @@ func (p *printer) writeString(pos token.Position, s string, isLit bool) { ...@@ -275,8 +275,6 @@ func (p *printer) writeString(pos token.Position, s string, isLit bool) {
p.last = p.pos p.last = p.pos
} }
const linePrefix = "//line "
// writeCommentPrefix writes the whitespace before a comment. // writeCommentPrefix writes the whitespace before a comment.
// If there is any pending whitespace, it consumes as much of // If there is any pending whitespace, it consumes as much of
// it as is likely to help position the comment nicely. // it as is likely to help position the comment nicely.
...@@ -397,16 +395,10 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev, comment *as ...@@ -397,16 +395,10 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev, comment *as
} }
if n > 0 { 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; // use formfeeds to break columns before a comment;
// this is analogous to using formfeeds to separate // this is analogous to using formfeeds to separate
// individual lines of /*-style comments // individual lines of /*-style comments
p.writeByte('\f', nlimit(n)) p.writeByte('\f', nlimit(n))
p.indent = indent // restore indent
} }
} }
} }
...@@ -588,30 +580,33 @@ func stripCommonPrefix(lines []string) { ...@@ -588,30 +580,33 @@ func stripCommonPrefix(lines []string) {
func (p *printer) writeComment(comment *ast.Comment) { func (p *printer) writeComment(comment *ast.Comment) {
text := comment.Text text := comment.Text
pos := p.posFor(comment.Pos())
if strings.HasPrefix(text, linePrefix) { const linePrefix = "//line "
pos := strings.TrimSpace(text[len(linePrefix):]) if strings.HasPrefix(text, linePrefix) && (!pos.IsValid() || pos.Column == 1) {
i := strings.LastIndex(pos, ":") // possibly a line directive
if i >= 0 { ldir := strings.TrimSpace(text[len(linePrefix):])
// The line directive we are about to print changed if i := strings.LastIndex(ldir, ":"); i >= 0 {
// the Filename and Line number used by go/token if line, err := strconv.Atoi(ldir[i+1:]); err == nil && line > 0 {
// as it was reading the input originally. // The line directive we are about to print changed
// In order to match the original input, we have to // the Filename and Line number used for subsequent
// update our own idea of the file and line number // tokens. We have to update our AST-space position
// accordingly, after printing the directive. // accordingly and suspend indentation temporarily.
file := pos[:i] indent := p.indent
line, _ := strconv.Atoi(pos[i+1:]) p.indent = 0
defer func() { defer func() {
p.pos.Filename = file p.pos.Filename = ldir[:i]
p.pos.Line = line p.pos.Line = line
p.pos.Column = 1 p.pos.Column = 1
}() p.indent = indent
}()
}
} }
} }
// shortcut common case of //-style comments // shortcut common case of //-style comments
if text[1] == '/' { if text[1] == '/' {
p.writeString(p.posFor(comment.Pos()), text, true) p.writeString(pos, text, true)
return return
} }
...@@ -622,7 +617,6 @@ func (p *printer) writeComment(comment *ast.Comment) { ...@@ -622,7 +617,6 @@ func (p *printer) writeComment(comment *ast.Comment) {
// write comment lines, separated by formfeed, // write comment lines, separated by formfeed,
// without a line break after the last line // without a line break after the last line
pos := p.posFor(comment.Pos())
for i, line := range lines { for i, line := range lines {
if i > 0 { if i > 0 {
p.writeByte('\f', 1) p.writeByte('\f', 1)
......
...@@ -479,6 +479,25 @@ func _() { ...@@ -479,6 +479,25 @@ func _() {
} }
} }
// 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 // Line comments with tabs
func _() { func _() {
var finput *bufio.Reader // input file var finput *bufio.Reader // input file
......
...@@ -487,6 +487,25 @@ func _() { ...@@ -487,6 +487,25 @@ func _() {
} }
// 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 // Line comments with tabs
func _() { func _() {
var finput *bufio.Reader // input file var finput *bufio.Reader // input file
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment