GoPLS Viewer

Home|gopls/go/analysis/passes/buildtag/buildtag_old.go
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.
4
5// TODO(rsc): Delete this file once Go 1.17 comes out and we can retire Go 1.15 support.
6
7//go:build !go1.16
8// +build !go1.16
9
10// Package buildtag defines an Analyzer that checks build tags.
11package buildtag
12
13import (
14    "bytes"
15    "fmt"
16    "go/ast"
17    "go/parser"
18    "strings"
19    "unicode"
20
21    "golang.org/x/tools/go/analysis"
22    "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
23)
24
25const Doc = "check that +build tags are well-formed and correctly located"
26
27var Analyzer = &analysis.Analyzer{
28    Name"buildtag",
29    Doc:  Doc,
30    Run:  runBuildTag,
31}
32
33func runBuildTag(pass *analysis.Pass) (interface{}, error) {
34    for _f := range pass.Files {
35        checkGoFile(passf)
36    }
37    for _name := range pass.OtherFiles {
38        if err := checkOtherFile(passname); err != nil {
39            return nilerr
40        }
41    }
42    for _name := range pass.IgnoredFiles {
43        if strings.HasSuffix(name".go") {
44            ferr := parser.ParseFile(pass.Fsetnamenilparser.ParseComments)
45            if err != nil {
46                // Not valid Go source code - not our job to diagnose, so ignore.
47                return nilnil
48            }
49            checkGoFile(passf)
50        } else {
51            if err := checkOtherFile(passname); err != nil {
52                return nilerr
53            }
54        }
55    }
56    return nilnil
57}
58
59func checkGoFile(pass *analysis.Passf *ast.File) {
60    pastCutoff := false
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 {
64            pastCutoff = true
65        }
66
67        // "+build" is ignored within or after a /*...*/ comment.
68        if !strings.HasPrefix(group.List[0].Text"//") {
69            pastCutoff = true
70            continue
71        }
72
73        // Check each line of a //-comment.
74        for _c := range group.List {
75            if !strings.Contains(c.Text"+build") {
76                continue
77            }
78            if err := checkLine(c.TextpastCutoff); err != nil {
79                pass.Reportf(c.Pos(), "%s"err)
80            }
81        }
82    }
83}
84
85func checkOtherFile(pass *analysis.Passfilename stringerror {
86    contenttferr := analysisutil.ReadFile(pass.Fsetfilename)
87    if err != nil {
88        return err
89    }
90
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(contentnl)
94
95    // Determine cutpoint where +build comments are no longer valid.
96    // They are valid in leading // comments in the file followed by
97    // a blank line.
98    //
99    // This must be done as a separate pass because of the
100    // requirement that the comment be followed by a blank line.
101    var cutoff int
102    for iline := range lines {
103        line = bytes.TrimSpace(line)
104        if !bytes.HasPrefix(lineslashSlash) {
105            if len(line) > 0 {
106                break
107            }
108            cutoff = i
109        }
110    }
111
112    for iline := range lines {
113        line = bytes.TrimSpace(line)
114        if !bytes.HasPrefix(lineslashSlash) {
115            continue
116        }
117        if !bytes.Contains(line, []byte("+build")) {
118            continue
119        }
120        if err := checkLine(string(line), i >= cutoff); err != nil {
121            pass.Reportf(analysisutil.LineStart(tfi+1), "%s"err)
122            continue
123        }
124    }
125    return nil
126}
127
128// checkLine checks a line that starts with "//" and contains "+build".
129func checkLine(line stringpastCutoff boolerror {
130    line = strings.TrimPrefix(line"//")
131    line = strings.TrimSpace(line)
132
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")
138        }
139        if pastCutoff {
140            return fmt.Errorf("+build comment must appear before package clause and be followed by a blank line")
141        }
142        if err := checkArguments(fields); err != nil {
143            return err
144        }
145    } else {
146        // Comment with +build but not at beginning.
147        if !pastCutoff {
148            return fmt.Errorf("possible malformed +build comment")
149        }
150    }
151    return nil
152}
153
154func checkArguments(fields []stringerror {
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)
159            }
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)
164                }
165            }
166        }
167    }
168    return nil
169}
170
171var (
172    nl         = []byte("\n")
173    slashSlash = []byte("//")
174)
175
MembersX
checkOtherFile.BlockStmt.RangeStmt_2753.i
checkOtherFile.BlockStmt.RangeStmt_2753.line
checkArguments.RangeStmt_3903.arg
bytes
runBuildTag.BlockStmt.RangeStmt_827.name
checkGoFile.BlockStmt.pastCutoff
checkGoFile.BlockStmt.RangeStmt_1429.group
checkArguments.RangeStmt_3903.BlockStmt.RangeStmt_3938.elem
checkOtherFile.BlockStmt.cutoff
checkOtherFile.BlockStmt.RangeStmt_2753.BlockStmt.err
checkLine
checkLine.pastCutoff
fmt
runBuildTag.BlockStmt.RangeStmt_946.BlockStmt.BlockStmt.err
checkArguments.RangeStmt_3903.BlockStmt.RangeStmt_3938.BlockStmt.RangeStmt_4149.c
checkLine.BlockStmt.err
runBuildTag.BlockStmt.RangeStmt_769.f
checkOtherFile.BlockStmt.tf
checkOtherFile.BlockStmt.RangeStmt_2591.i
checkLine.BlockStmt.fields
checkArguments.fields
checkGoFile.BlockStmt.RangeStmt_1429.BlockStmt.RangeStmt_1791.c
checkOtherFile.BlockStmt.content
checkOtherFile.BlockStmt.err
checkOtherFile.BlockStmt.lines
runBuildTag.BlockStmt.RangeStmt_946.name
runBuildTag.BlockStmt.RangeStmt_946.BlockStmt.BlockStmt.f
checkOtherFile.BlockStmt.RangeStmt_2591.line
runBuildTag.BlockStmt.RangeStmt_827.BlockStmt.err
checkGoFile.BlockStmt.RangeStmt_1429.BlockStmt.RangeStmt_1791.BlockStmt.err
checkLine.line
checkArguments
Members
X