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.
5 // TODO(rsc): Delete this file once Go 1.17 comes out and we can retire Go 1.15 support.
10 // Package buildtag defines an Analyzer that checks build tags.
21 "golang.org/x/tools/go/analysis"
22 "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
25 const Doc = "check that +build tags are well-formed and correctly located"
27 var Analyzer = &analysis.Analyzer{
33 func runBuildTag(pass *analysis.Pass) (interface{}, error) {
34 for _, f := range pass.Files {
37 for _, name := range pass.OtherFiles {
38 if err := checkOtherFile(pass, name); err != nil {
42 for _, name := range pass.IgnoredFiles {
43 if strings.HasSuffix(name, ".go") {
44 f, err := parser.ParseFile(pass.Fset, name, nil, parser.ParseComments)
46 // Not valid Go source code - not our job to diagnose, so ignore.
51 if err := checkOtherFile(pass, name); err != nil {
59 func checkGoFile(pass *analysis.Pass, f *ast.File) {
61 for _, group := range f.Comments {
62 // A +build comment is ignored after or adjoining the package declaration.
63 if group.End()+1 >= f.Package {
67 // "+build" is ignored within or after a /*...*/ comment.
68 if !strings.HasPrefix(group.List[0].Text, "//") {
73 // Check each line of a //-comment.
74 for _, c := range group.List {
75 if !strings.Contains(c.Text, "+build") {
78 if err := checkLine(c.Text, pastCutoff); err != nil {
79 pass.Reportf(c.Pos(), "%s", err)
85 func checkOtherFile(pass *analysis.Pass, filename string) error {
86 content, tf, err := analysisutil.ReadFile(pass.Fset, filename)
91 // We must look at the raw lines, as build tags may appear in non-Go
92 // files such as assembly files.
93 lines := bytes.SplitAfter(content, nl)
95 // Determine cutpoint where +build comments are no longer valid.
96 // They are valid in leading // comments in the file followed by
99 // This must be done as a separate pass because of the
100 // requirement that the comment be followed by a blank line.
102 for i, line := range lines {
103 line = bytes.TrimSpace(line)
104 if !bytes.HasPrefix(line, slashSlash) {
112 for i, line := range lines {
113 line = bytes.TrimSpace(line)
114 if !bytes.HasPrefix(line, slashSlash) {
117 if !bytes.Contains(line, []byte("+build")) {
120 if err := checkLine(string(line), i >= cutoff); err != nil {
121 pass.Reportf(analysisutil.LineStart(tf, i+1), "%s", err)
128 // checkLine checks a line that starts with "//" and contains "+build".
129 func checkLine(line string, pastCutoff bool) error {
130 line = strings.TrimPrefix(line, "//")
131 line = strings.TrimSpace(line)
133 if strings.HasPrefix(line, "+build") {
134 fields := strings.Fields(line)
135 if fields[0] != "+build" {
136 // Comment is something like +buildasdf not +build.
137 return fmt.Errorf("possible malformed +build comment")
140 return fmt.Errorf("+build comment must appear before package clause and be followed by a blank line")
142 if err := checkArguments(fields); err != nil {
146 // Comment with +build but not at beginning.
148 return fmt.Errorf("possible malformed +build comment")
154 func checkArguments(fields []string) error {
155 for _, arg := range fields[1:] {
156 for _, elem := range strings.Split(arg, ",") {
157 if strings.HasPrefix(elem, "!!") {
158 return fmt.Errorf("invalid double negative in build constraint: %s", arg)
160 elem = strings.TrimPrefix(elem, "!")
161 for _, c := range elem {
162 if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' {
163 return fmt.Errorf("invalid non-alphanumeric build constraint: %s", arg)
173 slashSlash = []byte("//")