Debian Codegraph @

go/analysis/analysistest/analysistest.go

raw symbols SCIP
1 // Copyright 2018 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 analysistest provides utilities for testing analyzers.
6 package analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/
7
8 import (
9 "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/"
10 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
11 "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/"
12 "go/formatscip-go gomod github.com/golang/go/src go1.25.0 `go/format`/"
13 "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/"
14 "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/"
15 "logscip-go gomod github.com/golang/go/src go1.25.0 log/"
16 "mapsscip-go gomod github.com/golang/go/src go1.25.0 maps/"
17 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
18 "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/"
19 "regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/"
20 "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/"
21 "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/"
22 "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/"
23 "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/"
24 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
25 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
26 "text/scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/"
27
28 "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/"
29 "golang.org/x/tools/go/analysis/checkerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/"
30 "golang.org/x/tools/go/analysis/internalscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal`/"
31 "golang.org/x/tools/go/packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/"
32 "golang.org/x/tools/internal/analysis/driverutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/"
33 "golang.org/x/tools/internal/diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/"
34 "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/"
35 "golang.org/x/tools/txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/"
36 )
37
38 // WriteFiles is a helper function that creates a temporary directory
39 // and populates it with a GOPATH-style project using filemap (which
40 // maps file names to contents). On success it returns the name of the
41 // directory and a cleanup function to delete it.
42 //
43 // TODO(adonovan): provide a newer version that accepts a testing.T,
44 // calls T.TempDir, and calls T.Fatal on any error, avoiding the need
45 // to return cleanup or err:
46 //
47 // func WriteFilesToTmp(t *testing.T filemap map[string]string) string
48 func WriteFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/WriteFiles().(filemap map[string]string) (dir string, cleanup func(), err error) {
49 gopath, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.MkdirTempscip-go gomod github.com/golang/go/src go1.25.0 os/MkdirTemp().("", "analysistest")
50 if err != nil {
51 return "", nil, err
52 }
53 cleanup = func() { osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(gopath) }
54
55 for name, content := range filemap {
56 filename := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(gopath, "src", name)
57 osscip-go gomod github.com/golang/go/src go1.25.0 os/.MkdirAllscip-go gomod github.com/golang/go/src go1.25.0 os/MkdirAll().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Dir().(filename), 0777) // ignore error
58 if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(filename, []byte(content), 0666); err != nil {
59 cleanup()
60 return "", nil, err
61 }
62 }
63 return gopath, cleanup, nil
64 }
65
66 // TestData returns the effective filename of
67 // the program's "testdata" directory.
68 // This function may be overridden by projects using
69 // an alternative build system (such as Blaze) that
70 // does not run a test in its package directory.
71 var TestDatascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/TestData. = func() string {
72 testdata, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Abs().("testdata")
73 if err != nil {
74 logscip-go gomod github.com/golang/go/src go1.25.0 log/.Fatalscip-go gomod github.com/golang/go/src go1.25.0 log/Fatal().(err)
75 }
76 return testdata
77 }
78
79 // Testing is an abstraction of a *testing.T.
80 type Testingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing# interface {
81 Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#Errorf.(format string, args ...any)
82 }
83
84 // RunWithSuggestedFixes behaves like Run, but additionally applies
85 // suggested fixes and verifies their output.
86 //
87 // It uses golden files, placed alongside each source file, to express
88 // the desired output: the expected transformation of file example.go
89 // is specified in file example.go.golden.
90 //
91 // Golden files may be of two forms: a plain Go source file, or a
92 // txtar archive.
93 //
94 // A plain Go source file indicates the expected result of applying
95 // all suggested fixes to the original file.
96 //
97 // A txtar archive specifies, in each section, the expected result of
98 // applying all suggested fixes of a given message to the original
99 // file; the name of the archive section is the fix's message. In this
100 // way, the various alternative fixes offered by a single diagnostic
101 // can be tested independently. Here's an example:
102 //
103 // -- turn into single negation --
104 // package pkg
105 //
106 // func fn(b1, b2 bool) {
107 // if !b1 { // want `negating a boolean twice`
108 // println()
109 // }
110 // }
111 //
112 // -- remove double negation --
113 // package pkg
114 //
115 // func fn(b1, b2 bool) {
116 // if b1 { // want `negating a boolean twice`
117 // println()
118 // }
119 // }
120 //
121 // # Conflicts
122 //
123 // Regardless of the form of the golden file, it is possible for
124 // multiple fixes to conflict, either because they overlap, or are
125 // close enough together that the particular diff algorithm cannot
126 // separate them.
127 //
128 // RunWithSuggestedFixes uses a simple three-way merge to accumulate
129 // fixes, similar to a git merge. The merge algorithm may be able to
130 // coalesce identical edits, for example duplicate imports of the same
131 // package. (Bear in mind that this is an editorial decision. In
132 // general, coalescing identical edits may not be correct: consider
133 // two statements that increment the same counter.)
134 //
135 // If there are conflicts, the test fails. In any case, the
136 // non-conflicting edits will be compared against the expected output.
137 // In this situation, we recommend that you increase the textual
138 // separation between conflicting parts or, if that fails, split
139 // your tests into smaller parts.
140 //
141 // If a diagnostic offers multiple fixes for the same problem, they
142 // are almost certain to conflict, so in this case you should define
143 // the expected output using a multi-section txtar file as described
144 // above.
145 func RunWithSuggestedFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/RunWithSuggestedFixes().(t Testingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#, dir string, a *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#, patterns ...string) []*Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result# {
146 results := Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Run().(t, dir, a, patterns...)
147
148 // If the immediate caller of RunWithSuggestedFixes is in
149 // x/tools, we apply stricter checks as required by gopls.
150 inTools := false
151 {
152 var pcs [1]uintptr
153 n := runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.Callersscip-go gomod github.com/golang/go/src go1.25.0 runtime/Callers().(1, pcs[:])
154 frames := runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.CallersFramesscip-go gomod github.com/golang/go/src go1.25.0 runtime/CallersFrames().(pcs[:n])
155 fr, _ := frames.Nextscip-go gomod github.com/golang/go/src go1.25.0 runtime/Frames#Next().()
156 if fr.Funcscip-go gomod github.com/golang/go/src go1.25.0 runtime/Frame#Func. != nil && stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(fr.Funcscip-go gomod github.com/golang/go/src go1.25.0 runtime/Frame#Func..Namescip-go gomod github.com/golang/go/src go1.25.0 runtime/Func#Name().(), "golang.org/x/tools/") {
157 inTools = true
158 }
159 }
160
161 generated := make(map[*tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#]bool)
162
163 // Process each result (package) separately, matching up the suggested
164 // fixes into a diff, which we will compare to the .golden file. We have
165 // to do this per-result in case a file appears in two packages, such as in
166 // packages with tests, where mypkg/a.go will appear in both mypkg and
167 // mypkg.test. In that case, the analyzer may suggest the same set of
168 // changes to a.go for each package. If we merge all the results, those
169 // changes get doubly applied, which will cause conflicts or mismatches.
170 // Validating the results separately means as long as the two analyses
171 // don't produce conflicting suggestions for a single file, everything
172 // should match up.
173 for _, result := range results {
174 act := result.Actionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Action.
175
176 // Compute set of generated files.
177 for _, file := range internalscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal`/.ActionPassscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal`/ActionPass.(act).Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Files. {
178 // Memoize, since there may be many actions
179 // for the same package (list of files).
180 tokFile := act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Fset..Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(file.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Pos().())
181 if _, seen := generated[tokFile]; !seen {
182 generated[tokFile] = astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.IsGeneratedscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IsGenerated().(file)
183 }
184 }
185
186 // For each fix, split its edits by file and convert to diff form.
187 var (
188 // fixEdits: message -> fixes -> filename -> edits
189 //
190 // TODO(adonovan): this mapping assumes fix.Messages
191 // are unique across analyzers, whereas they are only
192 // unique within a given Diagnostic.
193 fixEdits = make(map[string][]map[string][]diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/.Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#)
194 allFilenames = make(map[string]bool)
195 )
196 for _, diag := range act.Diagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Diagnostics. {
197 // Fixes are validated upon creation in Pass.Report.
198 fixloop:
199 for _, fix := range diag.SuggestedFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#SuggestedFixes. {
200 // Assert that lazy fixes have a Category (#65578, #65087).
201 if inTools && len(fix.TextEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#TextEdits.) == 0 && diag.Categoryscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Category. == "" {
202 t.Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#Errorf.("missing Diagnostic.Category for SuggestedFix without TextEdits (gopls requires the category for the name of the fix command")
203 }
204
205 // Skip any fix that edits a generated file.
206 for _, edit := range fix.TextEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#TextEdits. {
207 file := act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Fset..Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(edit.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.)
208 if generated[file] {
209 continue fixloop
210 }
211 }
212
213 // Convert edits to diff form.
214 // Group fixes by message and file.
215 edits := make(map[string][]diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/.Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#)
216 for _, edit := range fix.TextEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#TextEdits. {
217 file := act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Fset..Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(edit.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.)
218 allFilenames[file.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Name().()] = true
219 edits[file.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Name().()] = append(edits[file.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Name().()], diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/.Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#{
220 Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start.: file.Offsetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Offset().(edit.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.),
221 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End.: file.Offsetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Offset().(edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.),
222 Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New.: string(edit.NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.),
223 })
224 }
225 fixEdits[fix.Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#Message.] = append(fixEdits[fix.Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#Message.], edits)
226 }
227 }
228
229 merge := func(file, message string, x, y []diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/.Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#) []diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/.Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit# {
230 z, ok := diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/.Mergescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Merge().(x, y)
231 if !ok {
232 t.Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#Errorf.("in file %s, conflict applying fix %q", file, message)
233 return x // discard y
234 }
235 return z
236 }
237
238 // Because the checking is driven by original
239 // filenames, there is no way to express that a fix
240 // (e.g. extract declaration) creates a new file.
241 for _, filename := range slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Sortedscip-go gomod github.com/golang/go/src go1.25.0 slices/Sorted().(mapsscip-go gomod github.com/golang/go/src go1.25.0 maps/.Keysscip-go gomod github.com/golang/go/src go1.25.0 maps/Keys().(allFilenames)) {
242 // Read the original file.
243 content, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 os/ReadFile().(filename)
244 if err != nil {
245 t.Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#Errorf.("error reading %s: %v", filename, err)
246 continue
247 }
248
249 // check checks that the accumulated edits applied
250 // to the original content yield the wanted content.
251 check := func(prefix string, accumulated []diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/.Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#, want []byte) {
252 if err := applyDiffsAndComparescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/applyDiffsAndCompare().(result.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Pass..Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Pkg., filename, content, want, accumulated); err != nil {
253 t.Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#Errorf.("%s: %s", prefix, err)
254 }
255 }
256
257 // Read the golden file. It may have one of two forms:
258 // (1) A txtar archive with one section per fix title,
259 // including all fixes of just that title.
260 // (2) The expected output for file.Name after all (?) fixes are applied.
261 // This form requires that no diagnostic has multiple fixes.
262 ar, err := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.ParseFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/ParseFile().(filename + ".golden")
263 if err != nil {
264 t.Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#Errorf.("error reading %s.golden: %v", filename, err)
265 continue
266 }
267 if len(ar.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Files.) > 0 {
268 // Form #1: one archive section per kind of suggested fix.
269 if len(ar.Commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Comment.) > 0 {
270 // Disallow the combination of comment and archive sections.
271 t.Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#Errorf.("%s.golden has leading comment; we don't know what to do with it", filename)
272 continue
273 }
274
275 // Each archive section is named for a fix.Message.
276 // Accumulate the parts of the fix that apply to the current file,
277 // using a simple three-way merge, discarding conflicts,
278 // then apply the merged edits and compare to the archive section.
279 for _, section := range ar.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Files. {
280 message, want := section.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#Name., section.Datascip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#Data.
281 var accumulated []diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/.Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#
282 for _, fix := range fixEdits[message] {
283 accumulated = merge(filename, message, accumulated, fix[filename])
284 }
285 check(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("all fixes of message %q", message), accumulated, want)
286 }
287
288 } else {
289 // Form #2: all suggested fixes are represented by a single file.
290 want := ar.Commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Comment.
291 var accumulated []diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/.Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#
292 for _, message := range slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Sortedscip-go gomod github.com/golang/go/src go1.25.0 slices/Sorted().(mapsscip-go gomod github.com/golang/go/src go1.25.0 maps/.Keysscip-go gomod github.com/golang/go/src go1.25.0 maps/Keys().(fixEdits)) {
293 for _, fix := range fixEdits[message] {
294 accumulated = merge(filename, message, accumulated, fix[filename])
295 }
296 }
297 check("all fixes", accumulated, want)
298 }
299 }
300 }
301
302 return results
303 }
304
305 // applyDiffsAndCompare applies edits to original and compares the results against
306 // want after formatting both. fileName is use solely for error reporting.
307 func applyDiffsAndComparescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/applyDiffsAndCompare().(pkg *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#, filename string, original, want []byte, edits []diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/.Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#) error {
308 // Relativize filename, for tidier errors.
309 if cwd, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getwdscip-go gomod github.com/golang/go/src go1.25.0 os/Getwd().(); err == nil {
310 if rel, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Relscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Rel().(cwd, filename); err == nil {
311 filename = rel
312 }
313 }
314
315 if len(edits) == 0 {
316 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%s: no edits", filename)
317 }
318 fixedBytes, err := diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/.ApplyBytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/ApplyBytes().(original, edits)
319 if err != nil {
320 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%s: error applying fixes: %v (see possible explanations at RunWithSuggestedFixes)", filename, err)
321 }
322 fixed, err := driverutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/.FormatSourceRemoveImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/FormatSourceRemoveImports().(pkg, fixedBytes)
323 if err != nil {
324 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%s: error formatting resulting source: %v\n%s", filename, err, fixedBytes)
325 }
326
327 want, err = formatscip-go gomod github.com/golang/go/src go1.25.0 `go/format`/.Sourcescip-go gomod github.com/golang/go/src go1.25.0 `go/format`/Source().(want)
328 if err != nil {
329 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%s.golden: error formatting golden file: %v\n%s", filename, err, fixed)
330 }
331
332 // Keep error reporting logic below consistent with
333 // TestScript in ../internal/checker/fix_test.go!
334
335 unified := func(xlabel, ylabel string, x, y []byte) string {
336 x = append(slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Clipscip-go gomod github.com/golang/go/src go1.25.0 slices/Clip().(bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 bytes/TrimSpace().(x)), '\n')
337 y = append(slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Clipscip-go gomod github.com/golang/go/src go1.25.0 slices/Clip().(bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 bytes/TrimSpace().(y)), '\n')
338 return diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/.Unifiedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Unified().(xlabel, ylabel, string(x), string(y))
339 }
340
341 if diff := unified(filename+" (fixed)", filename+" (want)", fixed, want); diff != "" {
342 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("unexpected %s content:\n"+
343 "-- original --\n%s\n"+
344 "-- fixed --\n%s\n"+
345 "-- want --\n%s\n"+
346 "-- diff original fixed --\n%s\n"+
347 "-- diff fixed want --\n%s",
348 filename,
349 original,
350 fixed,
351 want,
352 unified(filename+" (original)", filename+" (fixed)", original, fixed),
353 diff)
354 }
355 return nil
356 }
357
358 // Run applies an analysis to the packages denoted by the "go list" patterns.
359 //
360 // It loads the packages from the specified
361 // directory using golang.org/x/tools/go/packages, runs the analysis on
362 // them, and checks that each analysis emits the expected diagnostics
363 // and facts specified by the contents of '// want ...' comments in the
364 // package's source files. It treats a comment of the form
365 // "//...// want..." or "/*...// want... */" as if it starts at 'want'.
366 //
367 // If the directory contains a go.mod file, Run treats it as the root of the
368 // Go module in which to work. Otherwise, Run treats it as the root of a
369 // GOPATH-style tree, with package contained in the src subdirectory.
370 //
371 // An expectation of a Diagnostic is specified by a string literal
372 // containing a regular expression that must match the diagnostic
373 // message. For example:
374 //
375 // fmt.Printf("%s", 1) // want `cannot provide int 1 to %s`
376 //
377 // An expectation of a Fact associated with an object is specified by
378 // 'name:"pattern"', where name is the name of the object, which must be
379 // declared on the same line as the comment, and pattern is a regular
380 // expression that must match the string representation of the fact,
381 // fmt.Sprint(fact). For example:
382 //
383 // func panicf(format string, args interface{}) { // want panicf:"printfWrapper"
384 //
385 // Package facts are specified by the name "package" and appear on
386 // line 1 of the first source file of the package.
387 //
388 // A single 'want' comment may contain a mixture of diagnostic and fact
389 // expectations, including multiple facts about the same object:
390 //
391 // // want "diag" "diag2" x:"fact1" x:"fact2" y:"fact3"
392 //
393 // Unexpected diagnostics and facts, and unmatched expectations, are
394 // reported as errors to the Testing.
395 //
396 // Run reports an error to the Testing if loading or analysis failed.
397 // Run also returns a Result for each package for which analysis was
398 // attempted, even if unsuccessful. It is safe for a test to ignore all
399 // the results, but a test may use it to perform additional checks.
400 func Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Run().(t Testingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#, dir string, a *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#, patterns ...string) []*Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result# {
401 if t, ok := t.(testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#); ok {
402 testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsGoPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsGoPackages().(t)
403 }
404
405 pkgs, err := loadPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/loadPackages().(dir, patterns...)
406 if err != nil {
407 t.Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#Errorf.("loading %s: %v", patterns, err)
408 return nil
409 }
410
411 // Print parse and type errors to the test log.
412 // (Do not print them to stderr, which would pollute
413 // the log in cases where the tests pass.)
414 if t, ok := t.(testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#); ok && !a.RunDespiteErrorsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#RunDespiteErrors. {
415 packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Visitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Visit().(pkgs, nil, func(pkg *packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#) {
416 for _, err := range pkg.Errorsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Errors. {
417 t.Logscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Log().(err)
418 }
419 })
420 }
421
422 res, err := checkerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/.Analyzescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Analyze().([]*analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{a}, pkgs, nil)
423 if err != nil {
424 t.Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#Errorf.("Analyze: %v", err)
425 return nil
426 }
427
428 var results []*Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#
429 for _, act := range res.Rootsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Graph#Roots. {
430 if act.Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Err. != nil {
431 t.Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#Errorf.("error analyzing %s: %v", act, act.Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Err.)
432 } else {
433 checkscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/check().(t, dir, act)
434 }
435
436 // Compute legacy map of facts relating to this package.
437 facts := make(map[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#][]analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Factscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Fact#)
438 for _, objFact := range act.AllObjectFactsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#AllObjectFacts().() {
439 if obj := objFact.Objectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/ObjectFact#Object.; obj.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pkg().() == act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types. {
440 facts[obj] = append(facts[obj], objFact.Factscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/ObjectFact#Fact.)
441 }
442 }
443 for _, pkgFact := range act.AllPackageFactsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#AllPackageFacts().() {
444 if pkgFact.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/PackageFact#Package. == act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types. {
445 facts[nil] = append(facts[nil], pkgFact.Factscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/PackageFact#Fact.)
446 }
447 }
448
449 // Construct the legacy result.
450 results = append(results, &Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#{
451 Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Pass.: internalscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal`/.ActionPassscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal`/ActionPass.(act), // may be nil
452 Diagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Diagnostics.: act.Diagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Diagnostics.,
453 Factsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Facts.: facts,
454 Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Result.: act.Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Result.,
455 Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Err.: act.Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Err.,
456 Actionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Action.: act,
457 })
458 }
459 return results
460 }
461
462 // A Result holds the result of applying an analyzer to a package.
463 //
464 // Facts contains only facts associated with the package and its objects.
465 //
466 // This internal type was inadvertently and regrettably exposed
467 // through a public type alias. It is essentially redundant with
468 // [checker.Action], but must be retained for compatibility. Clients may
469 // access the public fields of the Pass but must not invoke any of
470 // its "verbs", since the pass is already complete.
471 type Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result# struct {
472 Actionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Action. *checkerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/.Actionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#
473
474 // legacy fields (do not use)
475 Factsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Facts. map[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#][]analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Factscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Fact# // nil key => package fact
476 Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Pass. *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass# // nil => action not executed
477 Diagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Diagnostics. []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Diagnosticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic# // see Action.Diagnostics
478 Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Result. any // see Action.Result
479 Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Err. error // see Action.Err
480 }
481
482 // loadPackages uses go/packages to load a specified packages (from source, with
483 // dependencies) from dir, which is the root of a GOPATH-style project tree.
484 // loadPackages returns an error if any package had an error, or the pattern
485 // matched no packages.
486 func loadPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/loadPackages().(dir string, patterns ...string) ([]*packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#, error) {
487 env := []string{"GOPATH=" + dir, "GO111MODULE=off", "GOWORK=off"} // GOPATH mode
488
489 // Undocumented module mode. Will be replaced by something better.
490 if _, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, "go.mod")); err == nil {
491 gowork := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, "go.work")
492 if _, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(gowork); err != nil {
493 gowork = "off"
494 }
495
496 env = []string{"GO111MODULE=on", "GOPROXY=off", "GOWORK=" + gowork} // module mode
497 }
498
499 // packages.Load loads the real standard library, not a minimal
500 // fake version, which would be more efficient, especially if we
501 // have many small tests that import, say, net/http.
502 // However there is no easy way to make go/packages to consume
503 // a list of packages we generate and then do the parsing and
504 // typechecking, though this feature seems to be a recurring need.
505
506 mode := packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedName. | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedFiles. | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedCompiledGoFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedCompiledGoFiles. | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedImports. |
507 packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedTypes. | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedTypesSizesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedTypesSizes. | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedSyntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedSyntax. | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedTypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedTypesInfo. |
508 packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedDepsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedDeps. | packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.NeedModulescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/NeedModule.
509 cfg := &packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#{
510 Modescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Mode.: mode,
511 Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Dir.: dir,
512 Testsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Tests.: true,
513 Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Env.: append(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Environscip-go gomod github.com/golang/go/src go1.25.0 os/Environ().(), env...),
514 }
515 pkgs, err := packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Load().(cfg, patterns...)
516 if err != nil {
517 return nil, err
518 }
519
520 // If any named package couldn't be loaded at all
521 // (e.g. the Name field is unset), fail fast.
522 for _, pkg := range pkgs {
523 if pkg.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Name. == "" {
524 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("failed to load %q: Errors=%v",
525 pkg.PkgPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#PkgPath., pkg.Errorsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Errors.)
526 }
527 }
528
529 if len(pkgs) == 0 {
530 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("no packages matched %s", patterns)
531 }
532 return pkgs, nil
533 }
534
535 // check inspects an analysis pass on which the analysis has already
536 // been run, and verifies that all reported diagnostics and facts match
537 // specified by the contents of "// want ..." comments in the package's
538 // source files, which must have been parsed with comments enabled.
539 func checkscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/check().(t Testingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#, gopath string, act *checkerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/.Actionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#) {
540 type key struct {
541 file string
542 line int
543 }
544
545 want := make(map[key][]expectationscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#)
546
547 // processComment parses expectations out of comments.
548 processComment := func(filename string, linenum int, text string) {
549 text = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(text)
550
551 // Any comment starting with "want" is treated
552 // as an expectation, even without following whitespace.
553 if rest, ok := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.CutPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/CutPrefix().(text, "want"); ok {
554 lineDelta, expects, err := parseExpectationsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/parseExpectations().(rest)
555 if err != nil {
556 t.Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#Errorf.("%s:%d: in 'want' comment: %s", filename, linenum, err)
557 return
558 }
559 if expects != nil {
560 want[key{filename, linenum + lineDelta}] = expects
561 }
562 }
563 }
564
565 // Extract 'want' comments from parsed Go files.
566 for _, f := range act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Syntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Syntax. {
567 for _, cgroup := range f.Commentsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Comments. {
568 for _, c := range cgroup.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#List. {
569
570 text := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimPrefix().(c.Textscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#Text., "//")
571 if text == c.Textscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#Text. { // not a //-comment.
572 text = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimPrefix().(text, "/*")
573 text = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSuffix().(text, "*/")
574 }
575
576 // Hack: treat a comment of the form "//...// want..."
577 // or "/*...// want... */
578 // as if it starts at 'want'.
579 // This allows us to add comments on comments,
580 // as required when testing the buildtag analyzer.
581 if i := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Indexscip-go gomod github.com/golang/go/src go1.25.0 strings/Index().(text, "// want"); i >= 0 {
582 text = text[i+len("// "):]
583 }
584
585 // It's tempting to compute the filename
586 // once outside the loop, but it's
587 // incorrect because it can change due
588 // to //line directives.
589 posn := act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Fset..Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(c.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#Pos().())
590 filename := sanitizescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/sanitize().(gopath, posn.Filenamescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Filename.)
591 processComment(filename, posn.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line., text)
592 }
593 }
594 }
595
596 // Extract 'want' comments from non-Go files.
597 // TODO(adonovan): we may need to handle //line directives.
598 files := act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..OtherFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#OtherFiles.
599
600 // Hack: these analyzers need to extract expectations from
601 // all configurations, so include the files are usually
602 // ignored. (This was previously a hack in the respective
603 // analyzers' tests.)
604 switch act.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Analyzer..Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name. {
605 case "buildtag", "directive", "plusbuild":
606 files = slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Concatscip-go gomod github.com/golang/go/src go1.25.0 slices/Concat().(files, act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..IgnoredFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#IgnoredFiles.)
607 }
608
609 for _, filename := range files {
610 data, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 os/ReadFile().(filename)
611 if err != nil {
612 t.Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#Errorf.("can't read '// want' comments from %s: %v", filename, err)
613 continue
614 }
615 filename := sanitizescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/sanitize().(gopath, filename)
616 linenum := 0
617 for line := range stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.SplitSeqscip-go gomod github.com/golang/go/src go1.25.0 strings/SplitSeq().(string(data), "\n") {
618 linenum++
619
620 // Hack: treat a comment of the form "//...// want..."
621 // or "/*...// want... */
622 // as if it starts at 'want'.
623 // This allows us to add comments on comments,
624 // as required when testing the buildtag analyzer.
625 if i := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Indexscip-go gomod github.com/golang/go/src go1.25.0 strings/Index().(line, "// want"); i >= 0 {
626 line = line[i:]
627 }
628
629 if i := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Indexscip-go gomod github.com/golang/go/src go1.25.0 strings/Index().(line, "//"); i >= 0 {
630 line = line[i+len("//"):]
631 processComment(filename, linenum, line)
632 }
633 }
634 }
635
636 checkMessage := func(posn tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#, kind, name, message string) {
637 posn.Filenamescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Filename. = sanitizescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/sanitize().(gopath, posn.Filenamescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Filename.)
638 k := key{posn.Filenamescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Filename., posn.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line.}
639 expects := want[k]
640 var unmatched []string
641 for i, exp := range expects {
642 if exp.kindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#kind. == kind && exp.namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#name. == name {
643 if exp.rxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#rx..MatchStringscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#MatchString().(message) {
644 // matched: remove the expectation.
645 expects[i] = expects[len(expects)-1]
646 expects = expects[:len(expects)-1]
647 want[k] = expects
648 return
649 }
650 unmatched = append(unmatched, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%#q", exp.rxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#rx.))
651 }
652 }
653 if unmatched == nil {
654 t.Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#Errorf.("%v: unexpected %s: %v", posn, kind, message)
655 } else {
656 t.Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#Errorf.("%v: %s %q does not match pattern %s",
657 posn, kind, message, stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(unmatched, " or "))
658 }
659 }
660
661 // Check the diagnostics match expectations.
662 for _, f := range act.Diagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Diagnostics. {
663 // TODO(matloob): Support ranges in analysistest.
664 posn := act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Fset..Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(f.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Pos.)
665 checkMessage(posn, "diagnostic", "", f.Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Message.)
666 }
667
668 // Check the facts match expectations.
669 // We check only facts relating to the current package.
670 //
671 // We report errors in lexical order for determinism.
672 // (It's only deterministic within each file, not across files,
673 // because go/packages does not guarantee file.Pos is ascending
674 // across the files of a single compilation unit.)
675
676 // package facts: reported at start of first file
677 for _, pkgFact := range act.AllPackageFactsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#AllPackageFacts().() {
678 if pkgFact.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/PackageFact#Package. == act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types. {
679 posn := act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Fset..Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Syntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Syntax.[0].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Pos().())
680 posn.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line., posn.Columnscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Column. = 1, 1
681 checkMessage(posn, "fact", "package", fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprint().(pkgFact))
682 }
683 }
684
685 // object facts: reported at line of object declaration
686 objFacts := act.AllObjectFactsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#AllObjectFacts().()
687 sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Slicescip-go gomod github.com/golang/go/src go1.25.0 sort/Slice().(objFacts, func(i, j int) bool {
688 return objFacts[i].Objectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/ObjectFact#Object..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pos().() < objFacts[j].Objectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/ObjectFact#Object..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pos().()
689 })
690 for _, objFact := range objFacts {
691 if obj := objFact.Objectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/ObjectFact#Object.; obj.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pkg().() == act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types. {
692 posn := act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Fset..Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(obj.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pos().())
693 checkMessage(posn, "fact", obj.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Name().(), fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprint().(objFact.Factscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/ObjectFact#Fact.))
694 }
695 }
696
697 // Reject surplus expectations.
698 //
699 // Sometimes an Analyzer reports two similar diagnostics on a
700 // line with only one expectation. The reader may be confused by
701 // the error message.
702 // TODO(adonovan): print a better error:
703 // "got 2 diagnostics here; each one needs its own expectation".
704 var surplus []string
705 for key, expects := range want {
706 for _, exp := range expects {
707 err := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s:%d: no %s was reported matching %#q", key.file, key.line, exp.kindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#kind., exp.rxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#rx.)
708 surplus = append(surplus, err)
709 }
710 }
711 sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(surplus)
712 for _, err := range surplus {
713 t.Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Testing#Errorf.("%s", err)
714 }
715 }
716
717 type expectationscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation# struct {
718 kindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#kind. string // either "fact" or "diagnostic"
719 namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#name. string // name of object to which fact belongs, or "package" ("fact" only)
720 rxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#rx. *regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#
721 }
722
723 func (ex expectationscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#) Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#String().() string {
724 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s %s:%q", ex.kindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#kind., ex.namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#name., ex.rxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#rx.) // for debugging
725 }
726
727 // parseExpectations parses the content of a "// want ..." comment
728 // and returns the expectations, a mixture of diagnostics ("rx") and
729 // facts (name:"rx").
730 func parseExpectationsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/parseExpectations().(text string) (lineDelta int, expects []expectationscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#, err error) {
731 var scanErr string
732 sc := new(scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.Scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/Scanner#).Initscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/Scanner#Init().(stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.25.0 strings/NewReader().(text))
733 sc.Errorscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/Scanner#Error. = func(s *scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.Scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/Scanner#, msg string) {
734 scanErr = msg // e.g. bad string escape
735 }
736 sc.Modescip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/Scanner#Mode. = scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.ScanIdentsscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/ScanIdents. | scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.ScanStringsscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/ScanStrings. | scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.ScanRawStringsscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/ScanRawStrings. | scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.ScanIntsscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/ScanInts.
737
738 scanRegexp := func(tok rune) (*regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#, error) {
739 if tok != scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.Stringscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/String. && tok != scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.RawStringscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/RawString. {
740 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("got %s, want regular expression",
741 scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.TokenStringscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/TokenString().(tok))
742 }
743 pattern, _ := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Unquotescip-go gomod github.com/golang/go/src go1.25.0 strconv/Unquote().(sc.TokenTextscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/Scanner#TokenText().()) // can't fail
744 return regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.Compilescip-go gomod github.com/golang/go/src go1.25.0 regexp/Compile().(pattern)
745 }
746
747 for {
748 tok := sc.Scanscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/Scanner#Scan().()
749 switch tok {
750 case '+':
751 tok = sc.Scanscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/Scanner#Scan().()
752 if tok != scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.Intscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/Int. {
753 return 0, nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("got +%s, want +Int", scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.TokenStringscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/TokenString().(tok))
754 }
755 lineDelta, _ = strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(sc.TokenTextscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/Scanner#TokenText().())
756 case scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.Stringscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/String., scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.RawStringscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/RawString.:
757 rx, err := scanRegexp(tok)
758 if err != nil {
759 return 0, nil, err
760 }
761 expects = append(expects, expectationscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#{"diagnostic", "", rx})
762
763 case scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/Ident.:
764 name := sc.TokenTextscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/Scanner#TokenText().()
765 tok = sc.Scanscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/Scanner#Scan().()
766 if tok != ':' {
767 return 0, nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("got %s after %s, want ':'",
768 scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.TokenStringscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/TokenString().(tok), name)
769 }
770 tok = sc.Scanscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/Scanner#Scan().()
771 rx, err := scanRegexp(tok)
772 if err != nil {
773 return 0, nil, err
774 }
775 expects = append(expects, expectationscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/expectation#{"fact", name, rx})
776
777 case scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.EOFscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/EOF.:
778 if scanErr != "" {
779 return 0, nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%s", scanErr)
780 }
781 return lineDelta, expects, nil
782
783 default:
784 return 0, nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("unexpected %s", scannerscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/.TokenStringscip-go gomod github.com/golang/go/src go1.25.0 `text/scanner`/TokenString().(tok))
785 }
786 }
787 }
788
789 // sanitize removes the GOPATH portion of the filename,
790 // typically a gnarly /tmp directory, and returns the rest.
791 func sanitizescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/sanitize().(gopath, filename string) string {
792 prefix := gopath + string(osscip-go gomod github.com/golang/go/src go1.25.0 os/.PathSeparatorscip-go gomod github.com/golang/go/src go1.25.0 os/PathSeparator.) + "src" + string(osscip-go gomod github.com/golang/go/src go1.25.0 os/.PathSeparatorscip-go gomod github.com/golang/go/src go1.25.0 os/PathSeparator.)
793 return filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.ToSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/ToSlash().(stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimPrefix().(filename, prefix))
794 }