Skip to content

Commit 85f829d

Browse files
committed
cmd/asm: reject misplaced go:build comments
We are converting from using error-prone ad-hoc syntax // +build lines to less error-prone, standard boolean syntax //go:build lines. The timeline is: Go 1.16: prepare for transition - Builds still use // +build for file selection. - Source files may not contain //go:build without // +build. - Builds fail when a source file contains //go:build lines without // +build lines. <<< Go 1.17: start transition - Builds prefer //go:build for file selection, falling back to // +build for files containing only // +build. - Source files may contain //go:build without // +build (but they won't build with Go 1.16). - Gofmt moves //go:build and // +build lines to proper file locations. - Gofmt introduces //go:build lines into files with only // +build lines. - Go vet rejects files with mismatched //go:build and // +build lines. Go 1.18: complete transition - Go fix removes // +build lines, leaving behind equivalent // +build lines. This CL provides part of the <<< marked line above in the Go 1.16 step: rejecting files containing //go:build but not // +build. Reject any //go:build comments found after actual assembler code (include #include etc directives), because the go command itself doesn't read that far. For #41184. Change-Id: Ib460bfd380cce4239993980dd208afd07deff3f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/240602 Trust: Russ Cox <[email protected]> Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 2c6df2e commit 85f829d

File tree

7 files changed

+64
-15
lines changed

7 files changed

+64
-15
lines changed

src/cmd/asm/internal/asm/endtoend_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@ func isHexes(s string) bool {
257257
return true
258258
}
259259

260-
// It would be nice if the error messages began with
260+
// It would be nice if the error messages always began with
261261
// the standard file:line: prefix,
262262
// but that's not where we are today.
263263
// It might be at the beginning but it might be in the middle of the printed instruction.
264-
var fileLineRE = regexp.MustCompile(`(?:^|\()(testdata[/\\][0-9a-z]+\.s:[0-9]+)(?:$|\))`)
264+
var fileLineRE = regexp.MustCompile(`(?:^|\()(testdata[/\\][0-9a-z]+\.s:[0-9]+)(?:$|\)|:)`)
265265

266266
// Same as in test/run.go
267267
var (
@@ -281,6 +281,7 @@ func testErrors(t *testing.T, goarch, file string) {
281281
defer ctxt.Bso.Flush()
282282
failed := false
283283
var errBuf bytes.Buffer
284+
parser.errorWriter = &errBuf
284285
ctxt.DiagFunc = func(format string, args ...interface{}) {
285286
failed = true
286287
s := fmt.Sprintf(format, args...)
@@ -292,7 +293,7 @@ func testErrors(t *testing.T, goarch, file string) {
292293
pList.Firstpc, ok = parser.Parse()
293294
obj.Flushplist(ctxt, pList, nil, "")
294295
if ok && !failed {
295-
t.Errorf("asm: %s had no errors", goarch)
296+
t.Errorf("asm: %s had no errors", file)
296297
}
297298

298299
errors := map[string]string{}
@@ -368,6 +369,10 @@ func TestARMEndToEnd(t *testing.T) {
368369
}
369370
}
370371

372+
func TestGoBuildErrors(t *testing.T) {
373+
testErrors(t, "amd64", "buildtagerror")
374+
}
375+
371376
func TestARMErrors(t *testing.T) {
372377
testErrors(t, "arm", "armerror")
373378
}

src/cmd/asm/internal/asm/parse.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Parser struct {
2929
lineNum int // Line number in source file.
3030
errorLine int // Line number of last error.
3131
errorCount int // Number of errors.
32+
sawCode bool // saw code in this file (as opposed to comments and blank lines)
3233
pc int64 // virtual PC; count of Progs; doesn't advance for GLOBL or DATA.
3334
input []lex.Token
3435
inputPos int
@@ -132,6 +133,30 @@ func (p *Parser) ParseSymABIs(w io.Writer) bool {
132133
return p.errorCount == 0
133134
}
134135

136+
// nextToken returns the next non-build-comment token from the lexer.
137+
// It reports misplaced //go:build comments but otherwise discards them.
138+
func (p *Parser) nextToken() lex.ScanToken {
139+
for {
140+
tok := p.lex.Next()
141+
if tok == lex.BuildComment {
142+
if p.sawCode {
143+
p.errorf("misplaced //go:build comment")
144+
}
145+
continue
146+
}
147+
if tok != '\n' {
148+
p.sawCode = true
149+
}
150+
if tok == '#' {
151+
// A leftover wisp of a #include/#define/etc,
152+
// to let us know that p.sawCode should be true now.
153+
// Otherwise ignored.
154+
continue
155+
}
156+
return tok
157+
}
158+
}
159+
135160
// line consumes a single assembly line from p.lex of the form
136161
//
137162
// {label:} WORD[.cond] [ arg {, arg} ] (';' | '\n')
@@ -146,7 +171,7 @@ next:
146171
// Skip newlines.
147172
var tok lex.ScanToken
148173
for {
149-
tok = p.lex.Next()
174+
tok = p.nextToken()
150175
// We save the line number here so error messages from this instruction
151176
// are labeled with this line. Otherwise we complain after we've absorbed
152177
// the terminating newline and the line numbers are off by one in errors.
@@ -179,11 +204,11 @@ next:
179204
items = make([]lex.Token, 0, 3)
180205
}
181206
for {
182-
tok = p.lex.Next()
207+
tok = p.nextToken()
183208
if len(operands) == 0 && len(items) == 0 {
184209
if p.arch.InFamily(sys.ARM, sys.ARM64, sys.AMD64, sys.I386) && tok == '.' {
185210
// Suffixes: ARM conditionals or x86 modifiers.
186-
tok = p.lex.Next()
211+
tok = p.nextToken()
187212
str := p.lex.Text()
188213
if tok != scanner.Ident {
189214
p.errorf("instruction suffix expected identifier, found %s", str)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
#define X 1
6+
7+
//go:build x // ERROR "misplaced //go:build comment"
8+

src/cmd/asm/internal/lex/input.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ func (in *Input) Next() ScanToken {
109109
in.Error("'#' must be first item on line")
110110
}
111111
in.beginningOfLine = in.hash()
112+
in.text = "#"
113+
return '#'
114+
112115
case scanner.Ident:
113116
// Is it a macro name?
114117
name := in.Stack.Text()

src/cmd/asm/internal/lex/lex.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ type ScanToken rune
2222
const (
2323
// Asm defines some two-character lexemes. We make up
2424
// a rune/ScanToken value for them - ugly but simple.
25-
LSH ScanToken = -1000 - iota // << Left shift.
26-
RSH // >> Logical right shift.
27-
ARR // -> Used on ARM for shift type 3, arithmetic right shift.
28-
ROT // @> Used on ARM for shift type 4, rotate right.
29-
macroName // name of macro that should not be expanded
25+
LSH ScanToken = -1000 - iota // << Left shift.
26+
RSH // >> Logical right shift.
27+
ARR // -> Used on ARM for shift type 3, arithmetic right shift.
28+
ROT // @> Used on ARM for shift type 4, rotate right.
29+
Include // included file started here
30+
BuildComment // //go:build or +build comment
31+
macroName // name of macro that should not be expanded
3032
)
3133

3234
// IsRegisterShift reports whether the token is one of the ARM register shift operators.

src/cmd/asm/internal/lex/lex_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ func drain(input *Input) string {
281281
if tok == scanner.EOF {
282282
return buf.String()
283283
}
284+
if tok == '#' {
285+
continue
286+
}
284287
if buf.Len() > 0 {
285288
buf.WriteByte('.')
286289
}

src/cmd/asm/internal/lex/tokenizer.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,13 @@ func (t *Tokenizer) Next() ScanToken {
107107
if t.tok != scanner.Comment {
108108
break
109109
}
110-
length := strings.Count(s.TokenText(), "\n")
111-
t.line += length
112-
// TODO: If we ever have //go: comments in assembly, will need to keep them here.
113-
// For now, just discard all comments.
110+
text := s.TokenText()
111+
t.line += strings.Count(text, "\n")
112+
// TODO: Use constraint.IsGoBuild once it exists.
113+
if strings.HasPrefix(text, "//go:build") {
114+
t.tok = BuildComment
115+
break
116+
}
114117
}
115118
switch t.tok {
116119
case '\n':

0 commit comments

Comments
 (0)