| 1 |
// Copyright 2023 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 unitchecker_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker_test`/ |
| 6 |
|
| 7 |
// This file illustrates separate analysis with an example. |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 11 |
"encoding/jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/" |
| 12 |
"fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 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 |
"ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 16 |
"osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 17 |
"path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 18 |
"sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 19 |
"stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 20 |
"sync/atomicscip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/" |
| 21 |
"testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 22 |
|
| 23 |
"golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 24 |
"golang.org/x/tools/go/analysis/passes/printfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/printf`/" |
| 25 |
"golang.org/x/tools/go/analysis/unitcheckerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/" |
| 26 |
"golang.org/x/tools/go/gcexportdatascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/gcexportdata`/" |
| 27 |
"golang.org/x/tools/go/packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/" |
| 28 |
"golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/" |
| 29 |
"golang.org/x/tools/internal/testfilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/" |
| 30 |
"golang.org/x/tools/txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/" |
| 31 |
) |
| 32 |
|
| 33 |
// TestExampleSeparateAnalysis demonstrates the principle of separate |
| 34 |
// analysis, the distribution of units of type-checking and analysis |
| 35 |
// work across several processes, using serialized summaries to |
| 36 |
// communicate between them. |
| 37 |
// |
| 38 |
// It uses two different kinds of task, "manager" and "worker": |
| 39 |
// |
| 40 |
// - The manager computes the graph of package dependencies, and makes |
| 41 |
// a request to the worker for each package. It does not parse, |
| 42 |
// type-check, or analyze Go code. It is analogous "go vet". |
| 43 |
// |
| 44 |
// - The worker, which contains the Analyzers, reads each request, |
| 45 |
// loads, parses, and type-checks the files of one package, |
| 46 |
// applies all necessary analyzers to the package, then writes |
| 47 |
// its results to a file. It is a unitchecker-based driver, |
| 48 |
// analogous to the program specified by go vet -vettool= flag. |
| 49 |
// |
| 50 |
// In practice these would be separate executables, but for simplicity |
| 51 |
// of this example they are provided by one executable in two |
| 52 |
// different modes: the Example function is the manager, and the same |
| 53 |
// executable invoked with ENTRYPOINT=worker is the worker. |
| 54 |
// (See TestIntegration for how this happens.) |
| 55 |
// |
| 56 |
// Unfortunately this can't be a true Example because of the skip, |
| 57 |
// which requires a testing.T. |
| 58 |
func TestExampleSeparateAnalysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker_test`/TestExampleSeparateAnalysis().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 59 |
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) |
| 60 |
|
| 61 |
// src is an archive containing a module with a printf mistake. |
| 62 |
const src = ` |
| 63 |
-- go.mod -- |
| 64 |
module separate |
| 65 |
go 1.18 |
| 66 |
|
| 67 |
-- main/main.go -- |
| 68 |
package main |
| 69 |
|
| 70 |
import "separate/lib" |
| 71 |
|
| 72 |
func main() { |
| 73 |
lib.MyPrintf("%s", 123) |
| 74 |
} |
| 75 |
|
| 76 |
-- lib/lib.go -- |
| 77 |
package lib |
| 78 |
|
| 79 |
import "fmt" |
| 80 |
|
| 81 |
func MyPrintf(format string, args ...any) { |
| 82 |
fmt.Printf(format, args...) |
| 83 |
} |
| 84 |
` |
| 85 |
|
| 86 |
// Expand archive into tmp tree. |
| 87 |
fs, err := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.FSscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/FS().(txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.Parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Parse().([]byte(src))) |
| 88 |
if err != nil { |
| 89 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 90 |
} |
| 91 |
tmpdir := testfilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/.CopyToTmpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/CopyToTmp().(t, fs) |
| 92 |
|
| 93 |
// Load metadata for the main package and all its dependencies. |
| 94 |
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#{ |
| 95 |
Modescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#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. | 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., |
| 96 |
Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Dir.: tmpdir, |
| 97 |
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().(), |
| •98 |
"GO111MODULE=on", // needs Go module to work |
| 99 |
"GOPROXY=off", // disable network |
| 100 |
"GOWORK=off", // an ambient GOWORK value would break package loading |
| 101 |
), |
| 102 |
Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Logf.: t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf()., |
| 103 |
} |
| 104 |
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, "separate/main") |
| 105 |
if err != nil { |
| 106 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 107 |
} |
| 108 |
// Stop if any package had a metadata error. |
| 109 |
if packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.PrintErrorsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/PrintErrors().(pkgs) > 0 { |
| 110 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("there were errors among loaded packages") |
| 111 |
} |
| 112 |
|
| 113 |
// Now we have loaded the import graph, |
| 114 |
// let's begin the proper work of the manager. |
| 115 |
|
| 116 |
// Gather root packages. They will get all analyzers, |
| 117 |
// whereas dependencies get only the subset that |
| 118 |
// produce facts or are required by them. |
| 119 |
roots := make(map[*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#]bool) |
| 120 |
for _, pkg := range pkgs { |
| 121 |
roots[pkg] = true |
| 122 |
} |
| 123 |
|
| 124 |
// nextID generates sequence numbers for each unit of work. |
| 125 |
// We use it to create names of temporary files. |
| 126 |
var nextID atomicscip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/.Int32scip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/Int32# |
| 127 |
|
| 128 |
var allDiagnostics []string |
| 129 |
|
| 130 |
// Visit all packages in postorder: dependencies first. |
| 131 |
// TODO(adonovan): opt: use parallel postorder. |
| 132 |
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#) { |
| 133 |
if pkg.PkgPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#PkgPath. == "unsafe" { |
| 134 |
return |
| 135 |
} |
| 136 |
|
| 137 |
// Choose a unique prefix for temporary files |
| 138 |
// (.cfg .types .facts) produced by this package. |
| 139 |
// We stow it in an otherwise unused field of |
| 140 |
// Package so it can be accessed by our importers. |
| 141 |
prefix := 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", tmpdir, nextID.Addscip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/Int32#Add().(1)) |
| 142 |
pkg.ExportFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#ExportFile. = prefix |
| 143 |
|
| 144 |
// Construct the request to the worker. |
| 145 |
var ( |
| 146 |
importMap = make(map[string]string) |
| 147 |
packageFile = make(map[string]string) |
| 148 |
packageVetx = make(map[string]string) |
| 149 |
) |
| 150 |
for importPath, dep := range pkg.Importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Imports. { |
| 151 |
importMap[importPath] = dep.PkgPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#PkgPath. |
| 152 |
if depPrefix := dep.ExportFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#ExportFile.; depPrefix != "" { // skip "unsafe" |
| 153 |
packageFile[dep.PkgPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#PkgPath.] = depPrefix + ".types" |
| 154 |
packageVetx[dep.PkgPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#PkgPath.] = depPrefix + ".facts" |
| 155 |
} |
| 156 |
} |
| 157 |
cfg := unitcheckerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#{ |
| 158 |
IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#ID.: pkg.IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#ID., |
| 159 |
ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#ImportPath.: pkg.PkgPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#PkgPath., |
| 160 |
GoFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#GoFiles.: pkg.CompiledGoFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#CompiledGoFiles., |
| 161 |
NonGoFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#NonGoFiles.: pkg.OtherFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#OtherFiles., |
| 162 |
IgnoredFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#IgnoredFiles.: pkg.IgnoredFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#IgnoredFiles., |
| 163 |
ImportMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#ImportMap.: importMap, |
| 164 |
PackageFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#PackageFile.: packageFile, |
| 165 |
PackageVetxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#PackageVetx.: packageVetx, |
| 166 |
VetxOnlyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#VetxOnly.: !roots[pkg], |
| 167 |
VetxOutputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#VetxOutput.: prefix + ".facts", |
| 168 |
} |
| 169 |
if pkg.Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Module. != nil { |
| 170 |
if v := pkg.Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Module..GoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Module#GoVersion.; v != "" { |
| 171 |
cfg.GoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#GoVersion. = "go" + v |
| 172 |
} |
| 173 |
cfg.ModulePathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#ModulePath. = pkg.Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Module..Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Module#Path. |
| 174 |
cfg.ModuleVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#ModuleVersion. = pkg.Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Module..Versionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Module#Version. |
| 175 |
cfg.Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#Module. = analysisModuleFromPackagesModulescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker_test`/analysisModuleFromPackagesModule().(pkg.Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Module.) |
| 176 |
} |
| 177 |
|
| 178 |
// Write the JSON configuration message to a file. |
| 179 |
cfgData, err := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Marshal().(cfg) |
| 180 |
if err != nil { |
| 181 |
t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("internal error in json.Marshal: %v", err) |
| 182 |
} |
| 183 |
cfgFile := prefix + ".cfg" |
| 184 |
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().(cfgFile, cfgData, 0666); err != nil { |
| 185 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 186 |
} |
| 187 |
|
| 188 |
// Send the request to the worker. |
| 189 |
cmd := testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.Commandscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/Command().(t, osscip-go gomod github.com/golang/go/src go1.25.0 os/.Argsscip-go gomod github.com/golang/go/src go1.25.0 os/Args.[0], "-json", cfgFile) |
| 190 |
cmd.Stderrscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stderr. = osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr. |
| 191 |
cmd.Stdoutscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stdout. = new(bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#) |
| 192 |
cmd.Envscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#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().(), "ENTRYPOINT=worker") |
| 193 |
if err := cmd.Runscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Run().(); err != nil { |
| 194 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 195 |
} |
| 196 |
|
| 197 |
// Parse JSON output and gather in allDiagnostics. |
| 198 |
dec := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.NewDecoderscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/NewDecoder().(cmd.Stdoutscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stdout..(ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Readerscip-go gomod github.com/golang/go/src go1.25.0 io/Reader#)) |
| 199 |
for { |
| 200 |
type jsonDiagnostic struct { |
| 201 |
Posn string `json:"posn"` |
| 202 |
Message string `json:"message"` |
| 203 |
} |
| 204 |
// 'results' maps Package.Path -> Analyzer.Name -> diagnostics |
| 205 |
var results map[string]map[string][]jsonDiagnostic |
| 206 |
if err := dec.Decodescip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Decoder#Decode().(&results); err != nil { |
| 207 |
if err == ioscip-go gomod github.com/golang/go/src go1.25.0 io/.EOFscip-go gomod github.com/golang/go/src go1.25.0 io/EOF. { |
| 208 |
break |
| 209 |
} |
| 210 |
t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("internal error decoding JSON: %v", err) |
| 211 |
} |
| 212 |
for _, result := range results { |
| 213 |
for analyzer, diags := range result { |
| 214 |
for _, diag := range diags { |
| 215 |
rel := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.0 strings/ReplaceAll().(diag.Posn, tmpdir, "") |
| 216 |
rel = 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().(rel) |
| 217 |
msg := 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] %s", rel, analyzer, diag.Message) |
| 218 |
allDiagnostics = append(allDiagnostics, msg) |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
}) |
| 224 |
|
| 225 |
// Observe that the example produces a fact-based diagnostic |
| 226 |
// from separate analysis of "main", "lib", and "fmt": |
| 227 |
|
| 228 |
const want = `/main/main.go:6:16: [printf] separate/lib.MyPrintf format %s has arg 123 of wrong type int` |
| 229 |
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().(allDiagnostics) |
| 230 |
if got := 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().(allDiagnostics, "\n"); got != want { |
| 231 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Got: %s\nWant: %s", got, want) |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
// -- worker process -- |
| 236 |
|
| 237 |
// worker is the main entry point for a unitchecker-based driver |
| 238 |
// with only a single analyzer, for illustration. |
| 239 |
func workerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker_test`/worker().() { |
| 240 |
// Currently the unitchecker API doesn't allow clients to |
| 241 |
// control exactly how and where fact and type information |
| 242 |
// is produced and consumed. |
| 243 |
// |
| 244 |
// So, for example, it assumes that type information has |
| 245 |
// already been produced by the compiler, which is true when |
| 246 |
// running under "go vet", but isn't necessary. It may be more |
| 247 |
// convenient and efficient for a distributed analysis system |
| 248 |
// if the worker generates both of them, which is the approach |
| 249 |
// taken in this example; they could even be saved as two |
| 250 |
// sections of a single file. |
| 251 |
// |
| 252 |
// Consequently, this test currently needs special access to |
| 253 |
// private hooks in unitchecker to control how and where facts |
| 254 |
// and types are produced and consumed. In due course this |
| 255 |
// will become a respectable public API. In the meantime, it |
| 256 |
// should at least serve as a demonstration of how one could |
| 257 |
// fork unitchecker to achieve separate analysis without go vet. |
| 258 |
unitcheckerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/.SetTypeImportExportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/SetTypeImportExport().(makeTypesImporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker_test`/makeTypesImporter()., exportTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker_test`/exportTypes().) |
| 259 |
|
| 260 |
unitcheckerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/.Mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Main().(printfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/printf`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/printf`/Analyzer.) |
| 261 |
} |
| 262 |
|
| 263 |
func makeTypesImporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker_test`/makeTypesImporter().(cfg *unitcheckerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#, fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#) typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Importerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Importer# { |
| 264 |
imports := make(map[string]*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#) |
| 265 |
return importerFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker_test`/importerFunc#(func(importPath string) (*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#, error) { |
| 266 |
// Resolve import path to package path (vendoring, etc) |
| 267 |
path, ok := cfg.ImportMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#ImportMap.[importPath] |
| 268 |
if !ok { |
| 269 |
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().("can't resolve import %q", path) |
| 270 |
} |
| 271 |
if path == "unsafe" { |
| 272 |
return typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Unsafescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Unsafe., nil |
| 273 |
} |
| 274 |
|
| 275 |
// Find, read, and decode file containing type information. |
| 276 |
file, ok := cfg.PackageFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#PackageFile.[path] |
| 277 |
if !ok { |
| 278 |
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 package file for %q", path) |
| 279 |
} |
| 280 |
f, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Openscip-go gomod github.com/golang/go/src go1.25.0 os/Open().(file) |
| 281 |
if err != nil { |
| 282 |
return nil, err |
| 283 |
} |
| 284 |
defer f.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().() // ignore error |
| 285 |
return gcexportdatascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/gcexportdata`/.Readscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/gcexportdata`/Read().(f, fset, imports, path) |
| 286 |
}) |
| 287 |
} |
| 288 |
|
| 289 |
func exportTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker_test`/exportTypes().(cfg *unitcheckerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#, fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#, 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#) error { |
| 290 |
var out bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer# |
| 291 |
if err := gcexportdatascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/gcexportdata`/.Writescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/gcexportdata`/Write().(&out, fset, pkg); err != nil { |
| 292 |
return err |
| 293 |
} |
| 294 |
typesFile := 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().(cfg.VetxOutputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker`/Config#VetxOutput., ".facts") + ".types" |
| 295 |
return 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().(typesFile, out.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().(), 0666) |
| 296 |
} |
| 297 |
|
| 298 |
// -- helpers -- |
| 299 |
|
| 300 |
func analysisModuleFromPackagesModulescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker_test`/analysisModuleFromPackagesModule().(mod *packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Module#) *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Module# { |
| 301 |
if mod == nil { |
| 302 |
return nil |
| 303 |
} |
| 304 |
var modErr *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.ModuleErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/ModuleError# |
| 305 |
if mod.Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Module#Error. != nil { |
| 306 |
modErr = &analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.ModuleErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/ModuleError#{Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/ModuleError#Err.: mod.Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Module#Error..Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/ModuleError#Err.} |
| 307 |
} |
| 308 |
return &analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Module#{ |
| 309 |
Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Module#Path.: mod.Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Module#Path., |
| 310 |
Versionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Module#Version.: mod.Versionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Module#Version., |
| 311 |
Replacescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Module#Replace.: analysisModuleFromPackagesModulescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker_test`/analysisModuleFromPackagesModule().(mod.Replacescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Module#Replace.), |
| 312 |
Timescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Module#Time.: mod.Timescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Module#Time., |
| 313 |
Mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Module#Main.: mod.Mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Module#Main., |
| 314 |
Indirectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Module#Indirect.: mod.Indirectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Module#Indirect., |
| 315 |
Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Module#Dir.: mod.Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Module#Dir., |
| 316 |
GoModscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Module#GoMod.: mod.GoModscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Module#GoMod., |
| 317 |
GoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Module#GoVersion.: mod.GoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Module#GoVersion., |
| 318 |
Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Module#Error.: modErr, |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
type importerFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker_test`/importerFunc# func(path string) (*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#, error) |
| 323 |
|
| 324 |
func (f importerFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker_test`/importerFunc#) Importscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/unitchecker_test`/importerFunc#Import().(path string) (*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#, error) { return f(path) } |