mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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>
This commit is contained in:
parent
a8dc43edd1
commit
d390283ff4
3 changed files with 35 additions and 9 deletions
|
|
@ -195,7 +195,7 @@ func TestLineDirectives(t *testing.T) {
|
||||||
{`//line :x`, "invalid line number: x", "", 1, 8},
|
{`//line :x`, "invalid line number: x", "", 1, 8},
|
||||||
{`//line foo :`, "invalid line number: ", "", 1, 12},
|
{`//line foo :`, "invalid line number: ", "", 1, 12},
|
||||||
{`//line foo:123abc`, "invalid line number: 123abc", "", 1, 11},
|
{`//line foo:123abc`, "invalid line number: 123abc", "", 1, 11},
|
||||||
{`/**///line foo:x`, "invalid line number: x", "", 1, 15},
|
{`/**///line foo:x`, "syntax error: package statement must be first", "", 1, 16}, //line directive not at start of line - ignored
|
||||||
{`//line foo:0`, "invalid line number: 0", "", 1, 11},
|
{`//line foo:0`, "invalid line number: 0", "", 1, 11},
|
||||||
{fmt.Sprintf(`//line foo:%d`, lineMax+1), fmt.Sprintf("invalid line number: %d", lineMax+1), "", 1, 11},
|
{fmt.Sprintf(`//line foo:%d`, lineMax+1), fmt.Sprintf("invalid line number: %d", lineMax+1), "", 1, 11},
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,10 +45,11 @@ func (s *scanner) init(src io.Reader, errh, pragh func(line, col uint, msg strin
|
||||||
// calls the error handler installed with init. The handler
|
// calls the error handler installed with init. The handler
|
||||||
// must exist.
|
// must exist.
|
||||||
//
|
//
|
||||||
// If a //line or //go: directive is encountered, next
|
// If a //line or //go: directive is encountered at the start
|
||||||
// calls the pragma handler installed with init, if not nil.
|
// of a line, next calls the directive handler pragh installed
|
||||||
|
// with init, if not nil.
|
||||||
//
|
//
|
||||||
// The (line, col) position passed to the error and pragma
|
// The (line, col) position passed to the error and directive
|
||||||
// handler is always at or after the current source reading
|
// handler is always at or after the current source reading
|
||||||
// position.
|
// position.
|
||||||
func (s *scanner) next() {
|
func (s *scanner) next() {
|
||||||
|
|
@ -561,13 +562,14 @@ func (s *scanner) skipLine(r rune) {
|
||||||
|
|
||||||
func (s *scanner) lineComment() {
|
func (s *scanner) lineComment() {
|
||||||
r := s.getr()
|
r := s.getr()
|
||||||
if s.pragh == nil || (r != 'g' && r != 'l') {
|
// directives must start at the beginning of the line (s.col == 0)
|
||||||
|
if s.col != 0 || s.pragh == nil || (r != 'g' && r != 'l') {
|
||||||
s.skipLine(r)
|
s.skipLine(r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// s.pragh != nil && (r == 'g' || r == 'l')
|
// s.col == 0 && s.pragh != nil && (r == 'g' || r == 'l')
|
||||||
|
|
||||||
// recognize pragmas
|
// recognize directives
|
||||||
prefix := "go:"
|
prefix := "go:"
|
||||||
if r == 'l' {
|
if r == 'l' {
|
||||||
prefix = "line "
|
prefix = "line "
|
||||||
|
|
@ -580,7 +582,7 @@ func (s *scanner) lineComment() {
|
||||||
r = s.getr()
|
r = s.getr()
|
||||||
}
|
}
|
||||||
|
|
||||||
// pragma text without line ending (which may be "\r\n" if Windows),
|
// directive text without line ending (which may be "\r\n" if Windows),
|
||||||
s.startLit()
|
s.startLit()
|
||||||
s.skipLine(r)
|
s.skipLine(r)
|
||||||
text := s.stopLit()
|
text := s.stopLit()
|
||||||
|
|
@ -588,7 +590,7 @@ func (s *scanner) lineComment() {
|
||||||
text = text[:i]
|
text = text[:i]
|
||||||
}
|
}
|
||||||
|
|
||||||
s.pragh(s.line, s.col+2, prefix+string(text)) // +2 since pragma text starts after //
|
s.pragh(s.line, s.col+2, prefix+string(text)) // +2 since directive text starts after //
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *scanner) fullComment() {
|
func (s *scanner) fullComment() {
|
||||||
|
|
|
||||||
24
test/fixedbugs/issue18393.go
Normal file
24
test/fixedbugs/issue18393.go
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
// errorcheck
|
||||||
|
|
||||||
|
// Copyright 2017 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Test that compiler directives are ignored if they
|
||||||
|
// don't start at the beginning of the line.
|
||||||
|
|
||||||
|
package p
|
||||||
|
|
||||||
|
//line issue18393.go:20
|
||||||
|
import 42 // error on line 20
|
||||||
|
|
||||||
|
|
||||||
|
/* //line not at start of line: ignored */ //line issue18393.go:30
|
||||||
|
var x // error on line 24, not 30
|
||||||
|
|
||||||
|
|
||||||
|
// ERROR "missing import path"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ERROR "syntax error: unexpected newline, expecting type"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue