| 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 internal/checker defines various implementation helpers for |
| 6 | // the singlechecker and multichecker packages, which provide the |
| 7 | // complete main function for an analysis driver executable |
| 8 | // based on go/packages. |
| 9 | // |
| 10 | // (Note: it is not used by the public 'checker' package, since the |
| 11 | // latter provides a set of pure functions for use as building blocks.) |
| 12 | package checkerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/ |
| 13 | |
| 14 | // TODO(adonovan): publish the JSON schema in go/analysis or analysisjson. |
| 15 | |
| 16 | import ( |
| 17 | "flagscip-go gomod github.com/golang/go/src go1.25.0 flag/" |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 19 | "ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 20 | |
| 21 | "logscip-go gomod github.com/golang/go/src go1.25.0 log/" |
| 22 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 23 | "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/" |
| 24 | "runtime/pprofscip-go gomod github.com/golang/go/src go1.25.0 `runtime/pprof`/" |
| 25 | "runtime/tracescip-go gomod github.com/golang/go/src go1.25.0 `runtime/trace`/" |
| 26 | "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 27 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 28 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 29 | |
| 30 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 31 | "golang.org/x/tools/go/analysis/checkerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/" |
| 32 | "golang.org/x/tools/go/analysis/internalscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal`/" |
| 33 | "golang.org/x/tools/go/analysis/internal/analysisflagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/analysisflags`/" |
| 34 | "golang.org/x/tools/go/packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/" |
| 35 | "golang.org/x/tools/internal/analysis/driverutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/" |
| 36 | ) |
| 37 | |
| 38 | var ( |
| 39 | // Debug is a set of single-letter flags: |
| 40 | // |
| 41 | // f show [f]acts as they are created |
| 42 | // p disable [p]arallel execution of analyzers |
| 43 | // s do additional [s]anity checks on fact types and serialization |
| 44 | // t show [t]iming info (NB: use 'p' flag to avoid GC/scheduler noise) |
| 45 | // v show [v]erbose logging |
| 46 | // |
| 47 | Debugscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/Debug. = "" |
| 48 | |
| 49 | // Log files for optional performance tracing. |
| 50 | CPUProfilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/CPUProfile., MemProfilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/MemProfile., Tracescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/Trace. string |
| 51 | |
| 52 | // IncludeTests indicates whether test files should be analyzed too. |
| 53 | IncludeTestsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/IncludeTests. = true |
| 54 | ) |
| 55 | |
| 56 | // RegisterFlags registers command-line flags used by the analysis driver. |
| 57 | func RegisterFlagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/RegisterFlags().() { |
| 58 | // When adding flags here, remember to update |
| 59 | // the list of suppressed flags in analysisflags. |
| 60 | |
| 61 | flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.StringVarscip-go gomod github.com/golang/go/src go1.25.0 flag/StringVar().(&Debugscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/Debug., "debug", Debugscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/Debug., `debug flags, any subset of "fpstv"`) |
| 62 | |
| 63 | flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.StringVarscip-go gomod github.com/golang/go/src go1.25.0 flag/StringVar().(&CPUProfilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/CPUProfile., "cpuprofile", "", "write CPU profile to this file") |
| 64 | flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.StringVarscip-go gomod github.com/golang/go/src go1.25.0 flag/StringVar().(&MemProfilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/MemProfile., "memprofile", "", "write memory profile to this file") |
| 65 | flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.StringVarscip-go gomod github.com/golang/go/src go1.25.0 flag/StringVar().(&Tracescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/Trace., "trace", "", "write trace log to this file") |
| 66 | flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.BoolVarscip-go gomod github.com/golang/go/src go1.25.0 flag/BoolVar().(&IncludeTestsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/IncludeTests., "test", IncludeTestsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/IncludeTests., "indicates whether test files should be analyzed, too") |
| 67 | } |
| 68 | |
| 69 | // Run loads the packages specified by args using go/packages, |
| 70 | // then applies the specified analyzers to them. |
| 71 | // Analysis flags must already have been set. |
| 72 | // Analyzers must be valid according to [analysis.Validate]. |
| 73 | // It provides most of the logic for the main functions of both the |
| 74 | // singlechecker and the multi-analysis commands. |
| 75 | // It returns the appropriate exit code. |
| 76 | // |
| 77 | // TODO(adonovan): tests should not call this function directly. |
| 78 | // Fiddling with global variables (flags such as [analysisflags.Fix]) |
| 79 | // is error-prone and hostile to parallelism. Instead, use unit tests |
| 80 | // of the actual units (e.g. checker.Analyze) and integration tests |
| 81 | // (e.g. TestScript) of whole executables. |
| 82 | func Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/Run().(args []string, analyzers []*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#) (exitcode int) { |
| 83 | // Instead of returning a code directly, |
| 84 | // call this function to monotonically increase the exit code. |
| 85 | // This allows us to keep going in the face of some errors |
| 86 | // without having to remember what code to return. |
| 87 | // |
| 88 | // TODO(adonovan): interpreting exit codes is like reading tea-leaves. |
| 89 | // Instead of wasting effort trying to encode a multidimensional result |
| 90 | // into 7 bits we should just emit structured JSON output, and |
| 91 | // an exit code of 0 or 1 for success or failure. |
| 92 | exitAtLeast := func(code int) { |
| 93 | exitcode = max(code, exitcode) |
| 94 | } |
| 95 | |
| 96 | // Since analysisflags is linked in (for {single,multi}checker), |
| 97 | // the -v flag is registered for complex legacy reasons |
| 98 | // related to cmd/vet CLI. |
| 99 | // Treat it as an undocumented alias for -debug=v. |
| 100 | if v := flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.CommandLinescip-go gomod github.com/golang/go/src go1.25.0 flag/CommandLine..Lookupscip-go gomod github.com/golang/go/src go1.25.0 flag/FlagSet#Lookup().("v"); v != nil && |
| 101 | v.Valuescip-go gomod github.com/golang/go/src go1.25.0 flag/Flag#Value..(flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Getterscip-go gomod github.com/golang/go/src go1.25.0 flag/Getter#).Getscip-go gomod github.com/golang/go/src go1.25.0 flag/Getter#Get().() == true && |
| 102 | !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(Debugscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/Debug., "v") { |
| 103 | Debugscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/Debug. += "v" |
| 104 | } |
| 105 | |
| 106 | if CPUProfilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/CPUProfile. != "" { |
| 107 | f, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Createscip-go gomod github.com/golang/go/src go1.25.0 os/Create().(CPUProfilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/CPUProfile.) |
| 108 | if err != nil { |
| 109 | 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) |
| 110 | } |
| 111 | if err := pprofscip-go gomod github.com/golang/go/src go1.25.0 `runtime/pprof`/.StartCPUProfilescip-go gomod github.com/golang/go/src go1.25.0 `runtime/pprof`/StartCPUProfile().(f); err != nil { |
| 112 | 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) |
| 113 | } |
| 114 | // NB: profile won't be written in case of error. |
| 115 | defer pprofscip-go gomod github.com/golang/go/src go1.25.0 `runtime/pprof`/.StopCPUProfilescip-go gomod github.com/golang/go/src go1.25.0 `runtime/pprof`/StopCPUProfile().() |
| 116 | } |
| 117 | |
| 118 | if Tracescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/Trace. != "" { |
| 119 | f, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Createscip-go gomod github.com/golang/go/src go1.25.0 os/Create().(Tracescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/Trace.) |
| 120 | if err != nil { |
| 121 | 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) |
| 122 | } |
| 123 | if err := tracescip-go gomod github.com/golang/go/src go1.25.0 `runtime/trace`/.Startscip-go gomod github.com/golang/go/src go1.25.0 `runtime/trace`/Start().(f); err != nil { |
| 124 | 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) |
| 125 | } |
| 126 | // NB: trace log won't be written in case of error. |
| 127 | defer func() { |
| 128 | tracescip-go gomod github.com/golang/go/src go1.25.0 `runtime/trace`/.Stopscip-go gomod github.com/golang/go/src go1.25.0 `runtime/trace`/Stop().() |
| 129 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("To view the trace, run:\n$ go tool trace view %s", Tracescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/Trace.) |
| 130 | }() |
| 131 | } |
| 132 | |
| 133 | if MemProfilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/MemProfile. != "" { |
| 134 | f, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Createscip-go gomod github.com/golang/go/src go1.25.0 os/Create().(MemProfilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/MemProfile.) |
| 135 | if err != nil { |
| 136 | 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) |
| 137 | } |
| 138 | // NB: memprofile won't be written in case of error. |
| 139 | defer func() { |
| 140 | runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GCscip-go gomod github.com/golang/go/src go1.25.0 runtime/GC().() // get up-to-date statistics |
| 141 | if err := pprofscip-go gomod github.com/golang/go/src go1.25.0 `runtime/pprof`/.WriteHeapProfilescip-go gomod github.com/golang/go/src go1.25.0 `runtime/pprof`/WriteHeapProfile().(f); err != nil { |
| 142 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 log/Fatalf().("Writing memory profile: %v", err) |
| 143 | } |
| 144 | f.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().() |
| 145 | }() |
| 146 | } |
| 147 | |
| 148 | // Load the packages. |
| 149 | if dbgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/dbg().('v') { |
| 150 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.SetPrefixscip-go gomod github.com/golang/go/src go1.25.0 log/SetPrefix().("") |
| 151 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.SetFlagsscip-go gomod github.com/golang/go/src go1.25.0 log/SetFlags().(logscip-go gomod github.com/golang/go/src go1.25.0 log/.Lmicrosecondsscip-go gomod github.com/golang/go/src go1.25.0 log/Lmicroseconds.) // display timing |
| 152 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("load %s", args) |
| 153 | } |
| 154 | |
| 155 | // Optimization: if the selected analyzers don't produce/consume |
| 156 | // facts, we need source only for the initial packages. |
| 157 | allSyntax := needFactsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/needFacts().(analyzers) |
| 158 | initial, err := loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/load().(args, allSyntax) |
| 159 | if err != nil { |
| 160 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printscip-go gomod github.com/golang/go/src go1.25.0 log/Print().(err) |
| 161 | exitAtLeast(1) |
| 162 | return |
| 163 | } |
| 164 | |
| 165 | // Print package and module errors regardless of RunDespiteErrors. |
| 166 | // Do not exit if there are errors, yet. |
| 167 | if n := 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().(initial); n > 0 { |
| 168 | exitAtLeast(1) |
| 169 | } |
| 170 | |
| 171 | var factLog ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Writerscip-go gomod github.com/golang/go/src go1.25.0 io/Writer# |
| 172 | if dbgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/dbg().('f') { |
| 173 | factLog = 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. |
| 174 | } |
| 175 | |
| 176 | // Run the analysis. |
| 177 | opts := &checkerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/.Optionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Options#{ |
| 178 | SanityCheckscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Options#SanityCheck.: dbgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/dbg().('s'), |
| 179 | Sequentialscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Options#Sequential.: dbgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/dbg().('p'), |
| 180 | FactLogscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Options#FactLog.: factLog, |
| 181 | } |
| 182 | if dbgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/dbg().('v') { |
| 183 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("building graph of analysis passes") |
| 184 | } |
| 185 | graph, 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().(analyzers, initial, opts) |
| 186 | if err != nil { |
| 187 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printscip-go gomod github.com/golang/go/src go1.25.0 log/Print().(err) |
| 188 | exitAtLeast(1) |
| 189 | return |
| 190 | } |
| 191 | |
| 192 | // Don't print the diagnostics, |
| 193 | // but apply all fixes from the root actions. |
| 194 | if analysisflagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/analysisflags`/.Fixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/analysisflags`/Fix. { |
| 195 | fixActions := make([]driverutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/.FixActionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/FixAction#, len(graph.Rootsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Graph#Roots.)) |
| 196 | for i, act := range graph.Rootsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Graph#Roots. { |
| 197 | if 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); pass != nil { |
| 198 | fixActions[i] = driverutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/.FixActionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/FixAction#{ |
| 199 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/FixAction#Name.: act.Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#String().(), |
| 200 | Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/FixAction#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., |
| 201 | Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/FixAction#Files.: 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., |
| 202 | FileSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/FixAction#FileSet.: 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., |
| 203 | ReadFileFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/FixAction#ReadFileFunc.: pass.ReadFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReadFile., |
| 204 | Diagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/FixAction#Diagnostics.: act.Diagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Diagnostics., |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | write := func(filename string, content []byte) error { |
| 209 | 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().(filename, content, 0644) |
| 210 | } |
| 211 | if err := driverutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/.ApplyFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/ApplyFixes().(fixActions, write, analysisflagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/analysisflags`/.Diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/analysisflags`/Diff., dbgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/dbg().('v')); err != nil { |
| 212 | // Fail when applying fixes failed. |
| 213 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printscip-go gomod github.com/golang/go/src go1.25.0 log/Print().(err) |
| 214 | exitAtLeast(1) |
| 215 | return |
| 216 | } |
| 217 | // Don't proceed to print text/JSON, |
| 218 | // and don't report an error |
| 219 | // just because there were diagnostics. |
| 220 | return |
| 221 | } |
| 222 | |
| 223 | // Print the results. If !RunDespiteErrors and there |
| 224 | // are errors in the packages, this will have 0 exit |
| 225 | // code. Otherwise, we prefer to return exit code |
| 226 | // indicating diagnostics. |
| 227 | exitAtLeast(printDiagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/printDiagnostics().(graph)) |
| 228 | |
| 229 | return |
| 230 | } |
| 231 | |
| 232 | // printDiagnostics prints diagnostics in text or JSON form |
| 233 | // and returns the appropriate exit code. |
| 234 | func printDiagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/printDiagnostics().(graph *checkerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/.Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Graph#) (exitcode int) { |
| 235 | // Keep consistent with analogous logic in |
| 236 | // processResults in ../../unitchecker/unitchecker.go. |
| 237 | |
| 238 | // Print the results. |
| 239 | // With -json, the exit code is always zero. |
| 240 | if analysisflagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/analysisflags`/.JSONscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/analysisflags`/JSON. { |
| 241 | if err := graph.PrintJSONscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Graph#PrintJSON().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stdoutscip-go gomod github.com/golang/go/src go1.25.0 os/Stdout.); err != nil { |
| 242 | return 1 |
| 243 | } |
| 244 | } else { |
| 245 | if err := graph.PrintTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Graph#PrintText().(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., analysisflagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/analysisflags`/.Contextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/analysisflags`/Context.); err != nil { |
| 246 | return 1 |
| 247 | } |
| 248 | |
| 249 | // Compute the exit code. |
| 250 | var numErrors, rootDiags int |
| 251 | for act := range graph.Allscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Graph#All().() { |
| 252 | if act.Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Err. != nil { |
| 253 | numErrors++ |
| 254 | } else if act.IsRootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#IsRoot. { |
| 255 | rootDiags += len(act.Diagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Diagnostics.) |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | if numErrors > 0 { |
| 260 | exitcode = 1 // analysis failed, at least partially |
| 261 | } else if rootDiags > 0 { |
| 262 | exitcode = 3 // successfully produced diagnostics |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // Print timing info. |
| 267 | if dbgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/dbg().('t') { |
| 268 | if !dbgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/dbg().('p') { |
| 269 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printlnscip-go gomod github.com/golang/go/src go1.25.0 log/Println().("Warning: times are mostly GC/scheduler noise; use -debug=tp to disable parallelism") |
| 270 | } |
| 271 | |
| 272 | var list []*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# |
| 273 | var total timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration# |
| 274 | for act := range graph.Allscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Graph#All().() { |
| 275 | list = append(list, act) |
| 276 | total += act.Durationscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Duration. |
| 277 | } |
| 278 | |
| 279 | // Print actions accounting for 90% of the total. |
| 280 | 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().(list, func(i, j int) bool { |
| 281 | return list[i].Durationscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Duration. > list[j].Durationscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Duration. |
| 282 | }) |
| 283 | var sum timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration# |
| 284 | for _, act := range list { |
| 285 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(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., "%s\t%s\n", act.Durationscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Duration., act) |
| 286 | sum += act.Durationscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Duration. |
| 287 | if sum >= total*9/10 { |
| 288 | break |
| 289 | } |
| 290 | } |
| 291 | if total > sum { |
| 292 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(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., "%s\tall others\n", total-sum) |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return exitcode |
| 297 | } |
| 298 | |
| 299 | // load loads the initial packages. Returns only top-level loading |
| 300 | // errors. Does not consider errors in packages. |
| 301 | func loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/load().(patterns []string, allSyntax bool) ([]*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) { |
| 302 | mode := packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.LoadSyntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/LoadSyntax. |
| 303 | if allSyntax { |
| 304 | mode = packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.LoadAllSyntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/LoadAllSyntax. |
| 305 | } |
| 306 | mode |= 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. |
| 307 | conf := 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#{ |
| 308 | Modescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Mode.: mode, |
| 309 | // Ensure that child process inherits correct alias of PWD. |
| 310 | // (See discussion at Dir field of [exec.Command].) |
| 311 | // However, this currently breaks some tests. |
| 312 | // TODO(adonovan): Investigate. |
| 313 | // |
| 314 | // Dir: os.Getenv("PWD"), |
| 315 | Testsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Tests.: IncludeTestsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/IncludeTests., |
| 316 | } |
| 317 | initial, 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().(&conf, patterns...) |
| 318 | if err == nil && len(initial) == 0 { |
| 319 | err = 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 matched no packages", 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().(patterns, " ")) |
| 320 | } |
| 321 | return initial, err |
| 322 | } |
| 323 | |
| 324 | // needFacts reports whether any analysis required by the specified set |
| 325 | // needs facts. If so, we must load the entire program from source. |
| 326 | func needFactsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/needFacts().(analyzers []*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#) bool { |
| 327 | seen := make(map[*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#]bool) |
| 328 | var q []*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# // for BFS |
| 329 | q = append(q, analyzers...) |
| 330 | for len(q) > 0 { |
| 331 | a := q[0] |
| 332 | q = q[1:] |
| 333 | if !seen[a] { |
| 334 | seen[a] = true |
| 335 | if len(a.FactTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#FactTypes.) > 0 { |
| 336 | return true |
| 337 | } |
| 338 | q = append(q, a.Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires....) |
| 339 | } |
| 340 | } |
| 341 | return false |
| 342 | } |
| 343 | |
| 344 | func dbgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/dbg().(b byte) bool { return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.IndexBytescip-go gomod github.com/golang/go/src go1.25.0 strings/IndexByte().(Debugscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/Debug., b) >= 0 } |