Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / go / analysis / passes / buildtag / buildtag.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 // Package buildtag defines an Analyzer that checks build tags.
6 package buildtag
7
8 import (
9         "bytes"
10         "fmt"
11         "go/ast"
12         "go/parser"
13         "strings"
14         "unicode"
15
16         "golang.org/x/tools/go/analysis"
17         "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
18 )
19
20 const Doc = "check that +build tags are well-formed and correctly located"
21
22 var Analyzer = &analysis.Analyzer{
23         Name: "buildtag",
24         Doc:  Doc,
25         Run:  runBuildTag,
26 }
27
28 func runBuildTag(pass *analysis.Pass) (interface{}, error) {
29         for _, f := range pass.Files {
30                 checkGoFile(pass, f)
31         }
32         for _, name := range pass.OtherFiles {
33                 if err := checkOtherFile(pass, name); err != nil {
34                         return nil, err
35                 }
36         }
37         for _, name := range pass.IgnoredFiles {
38                 if strings.HasSuffix(name, ".go") {
39                         f, err := parser.ParseFile(pass.Fset, name, nil, parser.ParseComments)
40                         if err != nil {
41                                 // Not valid Go source code - not our job to diagnose, so ignore.
42                                 return nil, nil
43                         }
44                         checkGoFile(pass, f)
45                 } else {
46                         if err := checkOtherFile(pass, name); err != nil {
47                                 return nil, err
48                         }
49                 }
50         }
51         return nil, nil
52 }
53
54 func checkGoFile(pass *analysis.Pass, f *ast.File) {
55         pastCutoff := false
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 {
59                         pastCutoff = true
60                 }
61
62                 // "+build" is ignored within or after a /*...*/ comment.
63                 if !strings.HasPrefix(group.List[0].Text, "//") {
64                         pastCutoff = true
65                         continue
66                 }
67
68                 // Check each line of a //-comment.
69                 for _, c := range group.List {
70                         if !strings.Contains(c.Text, "+build") {
71                                 continue
72                         }
73                         if err := checkLine(c.Text, pastCutoff); err != nil {
74                                 pass.Reportf(c.Pos(), "%s", err)
75                         }
76                 }
77         }
78 }
79
80 func checkOtherFile(pass *analysis.Pass, filename string) error {
81         content, tf, err := analysisutil.ReadFile(pass.Fset, filename)
82         if err != nil {
83                 return err
84         }
85
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)
89
90         // Determine cutpoint where +build comments are no longer valid.
91         // They are valid in leading // comments in the file followed by
92         // a blank line.
93         //
94         // This must be done as a separate pass because of the
95         // requirement that the comment be followed by a blank line.
96         var cutoff int
97         for i, line := range lines {
98                 line = bytes.TrimSpace(line)
99                 if !bytes.HasPrefix(line, slashSlash) {
100                         if len(line) > 0 {
101                                 break
102                         }
103                         cutoff = i
104                 }
105         }
106
107         for i, line := range lines {
108                 line = bytes.TrimSpace(line)
109                 if !bytes.HasPrefix(line, slashSlash) {
110                         continue
111                 }
112                 if !bytes.Contains(line, []byte("+build")) {
113                         continue
114                 }
115                 if err := checkLine(string(line), i >= cutoff); err != nil {
116                         pass.Reportf(analysisutil.LineStart(tf, i+1), "%s", err)
117                         continue
118                 }
119         }
120         return nil
121 }
122
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)
127
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")
133                 }
134                 if pastCutoff {
135                         return fmt.Errorf("+build comment must appear before package clause and be followed by a blank line")
136                 }
137                 if err := checkArguments(fields); err != nil {
138                         return err
139                 }
140         } else {
141                 // Comment with +build but not at beginning.
142                 if !pastCutoff {
143                         return fmt.Errorf("possible malformed +build comment")
144                 }
145         }
146         return nil
147 }
148
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)
154                         }
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)
159                                 }
160                         }
161                 }
162         }
163         return nil
164 }
165
166 var (
167         nl         = []byte("\n")
168         slashSlash = []byte("//")
169 )