Skip to content

Commit d390283

Browse files
committed
cmd/compile/internal/syntax: compiler directives must start at beginning of line
- ignore them, if they don't. - added tests Fixes #18393. Change-Id: I13f87b81ac6b9138ab5031bb3dd6bebc4c548156 Reviewed-on: https://go-review.googlesource.com/37020 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
1 parent a8dc43e commit d390283

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/cmd/compile/internal/syntax/parser_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func TestLineDirectives(t *testing.T) {
195195
{`//line :x`, "invalid line number: x", "", 1, 8},
196196
{`//line foo :`, "invalid line number: ", "", 1, 12},
197197
{`//line foo:123abc`, "invalid line number: 123abc", "", 1, 11},
198-
{`/**///line foo:x`, "invalid line number: x", "", 1, 15},
198+
{`/**///line foo:x`, "syntax error: package statement must be first", "", 1, 16}, //line directive not at start of line - ignored
199199
{`//line foo:0`, "invalid line number: 0", "", 1, 11},
200200
{fmt.Sprintf(`//line foo:%d`, lineMax+1), fmt.Sprintf("invalid line number: %d", lineMax+1), "", 1, 11},
201201

src/cmd/compile/internal/syntax/scanner.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ func (s *scanner) init(src io.Reader, errh, pragh func(line, col uint, msg strin
4545
// calls the error handler installed with init. The handler
4646
// must exist.
4747
//
48-
// If a //line or //go: directive is encountered, next
49-
// calls the pragma handler installed with init, if not nil.
48+
// If a //line or //go: directive is encountered at the start
49+
// of a line, next calls the directive handler pragh installed
50+
// with init, if not nil.
5051
//
51-
// The (line, col) position passed to the error and pragma
52+
// The (line, col) position passed to the error and directive
5253
// handler is always at or after the current source reading
5354
// position.
5455
func (s *scanner) next() {
@@ -561,13 +562,14 @@ func (s *scanner) skipLine(r rune) {
561562

562563
func (s *scanner) lineComment() {
563564
r := s.getr()
564-
if s.pragh == nil || (r != 'g' && r != 'l') {
565+
// directives must start at the beginning of the line (s.col == 0)
566+
if s.col != 0 || s.pragh == nil || (r != 'g' && r != 'l') {
565567
s.skipLine(r)
566568
return
567569
}
568-
// s.pragh != nil && (r == 'g' || r == 'l')
570+
// s.col == 0 && s.pragh != nil && (r == 'g' || r == 'l')
569571

570-
// recognize pragmas
572+
// recognize directives
571573
prefix := "go:"
572574
if r == 'l' {
573575
prefix = "line "
@@ -580,15 +582,15 @@ func (s *scanner) lineComment() {
580582
r = s.getr()
581583
}
582584

583-
// pragma text without line ending (which may be "\r\n" if Windows),
585+
// directive text without line ending (which may be "\r\n" if Windows),
584586
s.startLit()
585587
s.skipLine(r)
586588
text := s.stopLit()
587589
if i := len(text) - 1; i >= 0 && text[i] == '\r' {
588590
text = text[:i]
589591
}
590592

591-
s.pragh(s.line, s.col+2, prefix+string(text)) // +2 since pragma text starts after //
593+
s.pragh(s.line, s.col+2, prefix+string(text)) // +2 since directive text starts after //
592594
}
593595

594596
func (s *scanner) fullComment() {

test/fixedbugs/issue18393.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// errorcheck
2+
3+
// Copyright 2017 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Test that compiler directives are ignored if they
8+
// don't start at the beginning of the line.
9+
10+
package p
11+
12+
//line issue18393.go:20
13+
import 42 // error on line 20
14+
15+
16+
/* //line not at start of line: ignored */ //line issue18393.go:30
17+
var x // error on line 24, not 30
18+
19+
20+
// ERROR "missing import path"
21+
22+
23+
24+
// ERROR "syntax error: unexpected newline, expecting type"

0 commit comments

Comments
 (0)