summaryrefslogtreecommitdiff
path: root/libgo/go/cmd/vet/buildtag.go
blob: ba3a361b9113bb01d547d8614da0586afb865b1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2013 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.

package main

import (
	"bytes"
	"fmt"
	"os"
	"strings"
	"unicode"
)

var (
	nl         = []byte("\n")
	slashSlash = []byte("//")
	plusBuild  = []byte("+build")
)

func badfLine(f *File, line int, format string, args ...interface{}) {
	msg := fmt.Sprintf(format, args...)
	fmt.Fprintf(os.Stderr, "%s:%d: %s\n", f.name, line, msg)
	setExit(1)
}

// checkBuildTag checks that build tags are in the correct location and well-formed.
func checkBuildTag(f *File) {
	if !vet("buildtags") {
		return
	}

	// we must look at the raw lines, as build tags may appear in non-Go
	// files such as assembly files.
	lines := bytes.SplitAfter(f.content, nl)

	// lineWithComment reports whether a line corresponds to a comment in
	// the source file. If the source file wasn't Go, the function always
	// returns true.
	lineWithComment := func(line int) bool {
		if f.file == nil {
			// Current source file is not Go, so be conservative.
			return true
		}
		for _, group := range f.file.Comments {
			startLine := f.fset.Position(group.Pos()).Line
			endLine := f.fset.Position(group.End()).Line
			if startLine <= line && line <= endLine {
				return true
			}
		}
		return false
	}

	// Determine cutpoint where +build comments are no longer valid.
	// They are valid in leading // comments in the file followed by
	// a blank line.
	var cutoff int
	for i, line := range lines {
		line = bytes.TrimSpace(line)
		if len(line) == 0 {
			cutoff = i
			continue
		}
		if bytes.HasPrefix(line, slashSlash) {
			continue
		}
		break
	}

	for i, line := range lines {
		line = bytes.TrimSpace(line)
		if !bytes.HasPrefix(line, slashSlash) {
			continue
		}
		if !bytes.Contains(line, plusBuild) {
			// Check that the comment contains "+build" early, to
			// avoid unnecessary lineWithComment calls that may
			// incur linear searches.
			continue
		}
		if !lineWithComment(i + 1) {
			// This is a line in a Go source file that looks like a
			// comment, but actually isn't - such as part of a raw
			// string.
			continue
		}

		text := bytes.TrimSpace(line[2:])
		if bytes.HasPrefix(text, plusBuild) {
			fields := bytes.Fields(text)
			if !bytes.Equal(fields[0], plusBuild) {
				// Comment is something like +buildasdf not +build.
				badfLine(f, i+1, "possible malformed +build comment")
				continue
			}
			if i >= cutoff {
				badfLine(f, i+1, "+build comment must appear before package clause and be followed by a blank line")
				continue
			}
			// Check arguments.
		Args:
			for _, arg := range fields[1:] {
				for _, elem := range strings.Split(string(arg), ",") {
					if strings.HasPrefix(elem, "!!") {
						badfLine(f, i+1, "invalid double negative in build constraint: %s", arg)
						break Args
					}
					elem = strings.TrimPrefix(elem, "!")
					for _, c := range elem {
						if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' {
							badfLine(f, i+1, "invalid non-alphanumeric build constraint: %s", arg)
							break Args
						}
					}
				}
			}
			continue
		}
		// Comment with +build but not at beginning.
		if i < cutoff {
			badfLine(f, i+1, "possible malformed +build comment")
			continue
		}
	}
}