1
// Copyright 2013 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.
17
slashSlash = []byte("//")
18
plusBuild = []byte("+build")
21
// checkBuildTag checks that build tags are in the correct location and well-formed.
22
func checkBuildTag(name string, data []byte) {
23
if !vet("buildtags") {
26
lines := bytes.SplitAfter(data, nl)
28
// Determine cutpoint where +build comments are no longer valid.
29
// They are valid in leading // comments in the file followed by
32
for i, line := range lines {
33
line = bytes.TrimSpace(line)
38
if bytes.HasPrefix(line, slashSlash) {
44
for i, line := range lines {
45
line = bytes.TrimSpace(line)
46
if !bytes.HasPrefix(line, slashSlash) {
49
text := bytes.TrimSpace(line[2:])
50
if bytes.HasPrefix(text, plusBuild) {
51
fields := bytes.Fields(text)
52
if !bytes.Equal(fields[0], plusBuild) {
53
// Comment is something like +buildasdf not +build.
54
fmt.Fprintf(os.Stderr, "%s:%d: possible malformed +build comment\n", name, i+1)
58
fmt.Fprintf(os.Stderr, "%s:%d: +build comment appears too late in file\n", name, i+1)
64
for _, arg := range fields[1:] {
65
for _, elem := range strings.Split(string(arg), ",") {
66
if strings.HasPrefix(elem, "!!") {
67
fmt.Fprintf(os.Stderr, "%s:%d: invalid double negative in build constraint: %s\n", name, i+1, arg)
71
if strings.HasPrefix(elem, "!") {
74
for _, c := range elem {
75
if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' {
76
fmt.Fprintf(os.Stderr, "%s:%d: invalid non-alphanumeric build constraint: %s\n", name, i+1, arg)
85
// Comment with +build but not at beginning.
86
if bytes.Contains(line, plusBuild) && i < cutoff {
87
fmt.Fprintf(os.Stderr, "%s:%d: possible malformed +build comment\n", name, i+1)