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 // Package buildtag defines an Analyzer that checks build tags.
16 "golang.org/x/tools/go/analysis"
17 "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
20 const Doc = "check that +build tags are well-formed and correctly located"
22 var Analyzer = &analysis.Analyzer{
28 func runBuildTag(pass *analysis.Pass) (interface{}, error) {
29 for _, f := range pass.Files {
32 for _, name := range pass.OtherFiles {
33 if err := checkOtherFile(pass, name); err != nil {
37 for _, name := range pass.IgnoredFiles {
38 if strings.HasSuffix(name, ".go") {
39 f, err := parser.ParseFile(pass.Fset, name, nil, parser.ParseComments)
41 // Not valid Go source code - not our job to diagnose, so ignore.
46 if err := checkOtherFile(pass, name); err != nil {
54 func checkGoFile(pass *analysis.Pass, f *ast.File) {
56 for _, group := range f.Comments {
57 // A +build comment is ignored after or adjoining the package declaration.
58 if group.End()+1 >= f.Package {
62 // "+build" is ignored within or after a /*...*/ comment.
63 if !strings.HasPrefix(group.List[0].Text, "//") {
68 // Check each line of a //-comment.
69 for _, c := range group.List {
70 if !strings.Contains(c.Text, "+build") {
73 if err := checkLine(c.Text, pastCutoff); err != nil {
74 pass.Reportf(c.Pos(), "%s", err)
80 func checkOtherFile(pass *analysis.Pass, filename string) error {
81 content, tf, err := analysisutil.ReadFile(pass.Fset, filename)
86 // We must look at the raw lines, as build tags may appear in non-Go
87 // files such as assembly files.
88 lines := bytes.SplitAfter(content, nl)
90 // Determine cutpoint where +build comments are no longer valid.
91 // They are valid in leading // comments in the file followed by
94 // This must be done as a separate pass because of the
95 // requirement that the comment be followed by a blank line.
97 for i, line := range lines {
98 line = bytes.TrimSpace(line)
99 if !bytes.HasPrefix(line, slashSlash) {
107 for i, line := range lines {
108 line = bytes.TrimSpace(line)
109 if !bytes.HasPrefix(line, slashSlash) {
112 if !bytes.Contains(line, []byte("+build")) {
115 if err := checkLine(string(line), i >= cutoff); err != nil {
116 pass.Reportf(analysisutil.LineStart(tf, i+1), "%s", err)
123 // checkLine checks a line that starts with "//" and contains "+build".
124 func checkLine(line string, pastCutoff bool) error {
125 line = strings.TrimPrefix(line, "//")
126 line = strings.TrimSpace(line)
128 if strings.HasPrefix(line, "+build") {
129 fields := strings.Fields(line)
130 if fields[0] != "+build" {
131 // Comment is something like +buildasdf not +build.
132 return fmt.Errorf("possible malformed +build comment")
135 return fmt.Errorf("+build comment must appear before package clause and be followed by a blank line")
137 if err := checkArguments(fields); err != nil {
141 // Comment with +build but not at beginning.
143 return fmt.Errorf("possible malformed +build comment")
149 func checkArguments(fields []string) error {
150 for _, arg := range fields[1:] {
151 for _, elem := range strings.Split(arg, ",") {
152 if strings.HasPrefix(elem, "!!") {
153 return fmt.Errorf("invalid double negative in build constraint: %s", arg)
155 elem = strings.TrimPrefix(elem, "!")
156 for _, c := range elem {
157 if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' {
158 return fmt.Errorf("invalid non-alphanumeric build constraint: %s", arg)
168 slashSlash = []byte("//")