Skip to content

Commit 9fd6cc1

Browse files
committed
go/printer: canonicalize //go:build and // +build lines while formatting
Part of //go:build change (#41184). See https://golang.org/design/draft-gobuild Gofmt and any other go/printer-using program will now: - move //go:build and //+build lines to the appropriate file location - if there's no //go:build line, add one derived from the // +build lines - if there is a //go:build line, recompute and replace any // +build lines to match what the //go:build line says For Go 1.17. Change-Id: Ide5cc3b4a07507ba9ed6f8b0de846e840876f49f Reviewed-on: https://go-review.googlesource.com/c/go/+/240608 Trust: Russ Cox <[email protected]> Trust: Jay Conrod <[email protected]> Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 5b76343 commit 9fd6cc1

19 files changed

+307
-10
lines changed

src/go/build/deps_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,10 @@ var depsRules = `
279279
< go/ast
280280
< go/parser;
281281
282-
go/parser, text/tabwriter
282+
FMT
283+
< go/build/constraint;
284+
285+
go/build/constraint, go/parser, text/tabwriter
283286
< go/printer
284287
< go/format;
285288
@@ -292,9 +295,6 @@ var depsRules = `
292295
container/heap, go/constant, go/parser, regexp
293296
< go/types;
294297
295-
FMT
296-
< go/build/constraint;
297-
298298
go/build/constraint, go/doc, go/parser, internal/goroot, internal/goversion
299299
< go/build;
300300

src/go/format/format_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ var tests = []string{
151151
// erroneous programs
152152
"ERROR1 + 2 +",
153153
"ERRORx := 0",
154+
155+
// build comments
156+
"// copyright\n\n//go:build x\n\npackage p\n",
157+
"// copyright\n\n//go:build x\n// +build x\n\npackage p\n",
154158
}
155159

156160
func String(s string) (string, error) {

src/go/printer/gobuild.go

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package printer
6+
7+
import (
8+
"go/build/constraint"
9+
"sort"
10+
"text/tabwriter"
11+
)
12+
13+
func (p *printer) fixGoBuildLines() {
14+
if len(p.goBuild)+len(p.plusBuild) == 0 {
15+
return
16+
}
17+
18+
// Find latest possible placement of //go:build and // +build comments.
19+
// That's just after the last blank line before we find a non-comment.
20+
// (We'll add another blank line after our comment block.)
21+
// When we start dropping // +build comments, we can skip over /* */ comments too.
22+
// Note that we are processing tabwriter input, so every comment
23+
// begins and ends with a tabwriter.Escape byte.
24+
// And some newlines have turned into \f bytes.
25+
insert := 0
26+
for pos := 0; ; {
27+
// Skip leading space at beginning of line.
28+
blank := true
29+
for pos < len(p.output) && (p.output[pos] == ' ' || p.output[pos] == '\t') {
30+
pos++
31+
}
32+
// Skip over // comment if any.
33+
if pos+3 < len(p.output) && p.output[pos] == tabwriter.Escape && p.output[pos+1] == '/' && p.output[pos+2] == '/' {
34+
blank = false
35+
for pos < len(p.output) && !isNL(p.output[pos]) {
36+
pos++
37+
}
38+
}
39+
// Skip over \n at end of line.
40+
if pos >= len(p.output) || !isNL(p.output[pos]) {
41+
break
42+
}
43+
pos++
44+
45+
if blank {
46+
insert = pos
47+
}
48+
}
49+
50+
// If there is a //go:build comment before the place we identified,
51+
// use that point instead. (Earlier in the file is always fine.)
52+
if len(p.goBuild) > 0 && p.goBuild[0] < insert {
53+
insert = p.goBuild[0]
54+
} else if len(p.plusBuild) > 0 && p.plusBuild[0] < insert {
55+
insert = p.plusBuild[0]
56+
}
57+
58+
var x constraint.Expr
59+
switch len(p.goBuild) {
60+
case 0:
61+
// Synthesize //go:build expression from // +build lines.
62+
for _, pos := range p.plusBuild {
63+
y, err := constraint.Parse(p.commentTextAt(pos))
64+
if err != nil {
65+
x = nil
66+
break
67+
}
68+
if x == nil {
69+
x = y
70+
} else {
71+
x = &constraint.AndExpr{X: x, Y: y}
72+
}
73+
}
74+
case 1:
75+
// Parse //go:build expression.
76+
x, _ = constraint.Parse(p.commentTextAt(p.goBuild[0]))
77+
}
78+
79+
var block []byte
80+
if x == nil {
81+
// Don't have a valid //go:build expression to treat as truth.
82+
// Bring all the lines together but leave them alone.
83+
// Note that these are already tabwriter-escaped.
84+
for _, pos := range p.goBuild {
85+
block = append(block, p.lineAt(pos)...)
86+
}
87+
for _, pos := range p.plusBuild {
88+
block = append(block, p.lineAt(pos)...)
89+
}
90+
} else {
91+
block = append(block, tabwriter.Escape)
92+
block = append(block, "//go:build "...)
93+
block = append(block, x.String()...)
94+
block = append(block, tabwriter.Escape, '\n')
95+
if len(p.plusBuild) > 0 {
96+
lines, err := constraint.PlusBuildLines(x)
97+
if err != nil {
98+
lines = []string{"// +build error: " + err.Error()}
99+
}
100+
for _, line := range lines {
101+
block = append(block, tabwriter.Escape)
102+
block = append(block, line...)
103+
block = append(block, tabwriter.Escape, '\n')
104+
}
105+
}
106+
}
107+
block = append(block, '\n')
108+
109+
// Build sorted list of lines to delete from remainder of output.
110+
toDelete := append(p.goBuild, p.plusBuild...)
111+
sort.Ints(toDelete)
112+
113+
// Collect output after insertion point, with lines deleted, into after.
114+
var after []byte
115+
start := insert
116+
for _, end := range toDelete {
117+
if end < start {
118+
continue
119+
}
120+
after = appendLines(after, p.output[start:end])
121+
start = end + len(p.lineAt(end))
122+
}
123+
after = appendLines(after, p.output[start:])
124+
if n := len(after); n >= 2 && isNL(after[n-1]) && isNL(after[n-2]) {
125+
after = after[:n-1]
126+
}
127+
128+
p.output = p.output[:insert]
129+
p.output = append(p.output, block...)
130+
p.output = append(p.output, after...)
131+
}
132+
133+
// appendLines is like append(x, y...)
134+
// but it avoids creating doubled blank lines,
135+
// which would not be gofmt-standard output.
136+
// It assumes that only whole blocks of lines are being appended,
137+
// not line fragments.
138+
func appendLines(x, y []byte) []byte {
139+
if len(y) > 0 && isNL(y[0]) && // y starts in blank line
140+
(len(x) == 0 || len(x) >= 2 && isNL(x[len(x)-1]) && isNL(x[len(x)-2])) { // x is empty or ends in blank line
141+
y = y[1:] // delete y's leading blank line
142+
}
143+
return append(x, y...)
144+
}
145+
146+
func (p *printer) lineAt(start int) []byte {
147+
pos := start
148+
for pos < len(p.output) && !isNL(p.output[pos]) {
149+
pos++
150+
}
151+
if pos < len(p.output) {
152+
pos++
153+
}
154+
return p.output[start:pos]
155+
}
156+
157+
func (p *printer) commentTextAt(start int) string {
158+
if start < len(p.output) && p.output[start] == tabwriter.Escape {
159+
start++
160+
}
161+
pos := start
162+
for pos < len(p.output) && p.output[pos] != tabwriter.Escape && !isNL(p.output[pos]) {
163+
pos++
164+
}
165+
return string(p.output[start:pos])
166+
}
167+
168+
func isNL(b byte) bool {
169+
return b == '\n' || b == '\f'
170+
}

src/go/printer/printer.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package printer
88
import (
99
"fmt"
1010
"go/ast"
11+
"go/build/constraint"
1112
"go/token"
1213
"io"
1314
"os"
@@ -64,6 +65,8 @@ type printer struct {
6465
lastTok token.Token // last token printed (token.ILLEGAL if it's whitespace)
6566
prevOpen token.Token // previous non-brace "open" token (, [, or token.ILLEGAL
6667
wsbuf []whiteSpace // delayed white space
68+
goBuild []int // start index of all //go:build comments in output
69+
plusBuild []int // start index of all // +build comments in output
6770

6871
// Positions
6972
// The out position differs from the pos position when the result
@@ -649,6 +652,11 @@ func (p *printer) writeComment(comment *ast.Comment) {
649652

650653
// shortcut common case of //-style comments
651654
if text[1] == '/' {
655+
if constraint.IsGoBuild(text) {
656+
p.goBuild = append(p.goBuild, len(p.output))
657+
} else if constraint.IsPlusBuild(text) {
658+
p.plusBuild = append(p.plusBuild, len(p.output))
659+
}
652660
p.writeString(pos, trimRight(text), true)
653661
return
654662
}
@@ -1122,6 +1130,8 @@ func (p *printer) printNode(node interface{}) error {
11221130
// get comments ready for use
11231131
p.nextComment()
11241132

1133+
p.print(pmode(0))
1134+
11251135
// format node
11261136
switch n := node.(type) {
11271137
case ast.Expr:
@@ -1313,6 +1323,10 @@ func (cfg *Config) fprint(output io.Writer, fset *token.FileSet, node interface{
13131323
p.impliedSemi = false // EOF acts like a newline
13141324
p.flush(token.Position{Offset: infinity, Line: infinity}, token.EOF)
13151325

1326+
// output is buffered in p.output now.
1327+
// fix //go:build and // +build comments if needed.
1328+
p.fixGoBuildLines()
1329+
13161330
// redirect output through a trimmer to eliminate trailing whitespace
13171331
// (Input to a tabwriter must be untrimmed since trailing tabs provide
13181332
// formatting information. The tabwriter could provide trimming

src/go/printer/printer_test.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,19 @@ func lineAt(text []byte, offs int) []byte {
8888

8989
// diff compares a and b.
9090
func diff(aname, bname string, a, b []byte) error {
91-
var buf bytes.Buffer // holding long error message
91+
if bytes.Equal(a, b) {
92+
return nil
93+
}
9294

95+
var buf bytes.Buffer // holding long error message
9396
// compare lengths
9497
if len(a) != len(b) {
9598
fmt.Fprintf(&buf, "\nlength changed: len(%s) = %d, len(%s) = %d", aname, len(a), bname, len(b))
9699
}
97100

98101
// compare contents
99102
line := 1
100-
offs := 1
103+
offs := 0
101104
for i := 0; i < len(a) && i < len(b); i++ {
102105
ch := a[i]
103106
if ch != b[i] {
@@ -112,10 +115,8 @@ func diff(aname, bname string, a, b []byte) error {
112115
}
113116
}
114117

115-
if buf.Len() > 0 {
116-
return errors.New(buf.String())
117-
}
118-
return nil
118+
fmt.Fprintf(&buf, "\n%s:\n%s\n%s:\n%s", aname, a, bname, b)
119+
return errors.New(buf.String())
119120
}
120121

121122
func runcheck(t *testing.T, source, golden string, mode checkMode) {
@@ -207,6 +208,13 @@ var data = []entry{
207208
{"go2numbers.input", "go2numbers.golden", idempotent},
208209
{"go2numbers.input", "go2numbers.norm", normNumber | idempotent},
209210
{"generics.input", "generics.golden", idempotent},
211+
{"gobuild1.input", "gobuild1.golden", idempotent},
212+
{"gobuild2.input", "gobuild2.golden", idempotent},
213+
{"gobuild3.input", "gobuild3.golden", idempotent},
214+
{"gobuild4.input", "gobuild4.golden", idempotent},
215+
{"gobuild5.input", "gobuild5.golden", idempotent},
216+
{"gobuild6.input", "gobuild6.golden", idempotent},
217+
{"gobuild7.input", "gobuild7.golden", idempotent},
210218
}
211219

212220
func TestFiles(t *testing.T) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//go:build x
2+
// +build x
3+
4+
package p
5+
6+
func f()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package p
2+
3+
//go:build x
4+
5+
func f()
6+
7+
// +build y
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build x
2+
// +build x
3+
4+
// other comment
5+
6+
package p
7+
8+
func f()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// +build y
2+
3+
// other comment
4+
5+
package p
6+
7+
func f()
8+
9+
//go:build x
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// other comment
2+
3+
//go:build x
4+
// +build x
5+
6+
// yet another comment
7+
8+
package p
9+
10+
func f()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// other comment
2+
3+
// +build y
4+
5+
// yet another comment
6+
7+
package p
8+
9+
//go:build x
10+
11+
func f()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//go:build (x || y) && z
2+
// +build x y
3+
// +build z
4+
5+
// doc comment
6+
package p
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// doc comment
2+
package p
3+
4+
// +build x y
5+
// +build z
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//go:build !(x || y) && z
2+
// +build !x,!y,z
3+
4+
package p
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//go:build !(x || y) && z
2+
// +build something else
3+
4+
package p
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//go:build !(x || y) && z
2+
3+
// no +build line
4+
5+
package p
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//go:build !(x || y) && z
2+
// no +build line
3+
4+
package p

0 commit comments

Comments
 (0)