22// Use of this source code is governed by a BSD-style
33// license that can be found in the LICENSE file.
44//
5- // Partial copy of https://github.com/golang/tools/blob/master/go/analysis/internal/checker
6- // FIXME add a commit hash.
5+ // Partial copy of https://github.com/golang/tools/blob/dba5486c2a1d03519930812112b23ed2c45c04fc/go/analysis/internal/checker/checker.go
76
87package goanalysis
98
@@ -28,20 +27,22 @@ import (
2827// package (as different analyzers are applied, either in sequence or
2928// parallel), and across packages (as dependencies are analyzed).
3029type action struct {
31- a * analysis.Analyzer
32- pkg * packages.Package
33- pass * analysis.Pass
34- deps []* action
35- objectFacts map [objectFactKey ]analysis.Fact
36- packageFacts map [packageFactKey ]analysis.Fact
37- result any
38- diagnostics []analysis.Diagnostic
39- err error
30+ a * analysis.Analyzer
31+ pkg * packages.Package
32+ pass * analysis.Pass
33+ isroot bool
34+ deps []* action
35+ objectFacts map [objectFactKey ]analysis.Fact
36+ packageFacts map [packageFactKey ]analysis.Fact
37+ result any
38+ diagnostics []analysis.Diagnostic
39+ err error
40+
41+ // NOTE(ldez) custom fields.
4042 r * runner
4143 analysisDoneCh chan struct {}
4244 loadCachedFactsDone bool
4345 loadCachedFactsOk bool
44- isroot bool
4546 isInitialPkg bool
4647 needAnalyzeSource bool
4748}
@@ -78,12 +79,11 @@ func (act *action) analyze() {
7879 // Report an error if any dependency failures.
7980 var depErrors error
8081 for _ , dep := range act .deps {
81- if dep .err = = nil {
82- continue
82+ if dep .err ! = nil {
83+ depErrors = errors . Join ( depErrors , errors . Unwrap ( dep . err ))
8384 }
84-
85- depErrors = errors .Join (depErrors , errors .Unwrap (dep .err ))
8685 }
86+
8787 if depErrors != nil {
8888 act .err = fmt .Errorf ("failed prerequisites: %w" , depErrors )
8989 return
@@ -92,7 +92,10 @@ func (act *action) analyze() {
9292 // Plumb the output values of the dependencies
9393 // into the inputs of this action. Also facts.
9494 inputs := make (map [* analysis.Analyzer ]any )
95+ act .objectFacts = make (map [objectFactKey ]analysis.Fact )
96+ act .packageFacts = make (map [packageFactKey ]analysis.Fact )
9597 startedAt := time .Now ()
98+
9699 for _ , dep := range act .deps {
97100 if dep .pkg == act .pkg {
98101 // Same package, different analysis (horizontal edge):
@@ -106,17 +109,29 @@ func (act *action) analyze() {
106109 inheritFacts (act , dep )
107110 }
108111 }
112+
109113 factsDebugf ("%s: Inherited facts in %s" , act , time .Since (startedAt ))
110114
115+ module := & analysis.Module {} // possibly empty (non nil) in go/analysis drivers.
116+ if mod := act .pkg .Module ; mod != nil {
117+ module .Path = mod .Path
118+ module .Version = mod .Version
119+ module .GoVersion = mod .GoVersion
120+ }
121+
111122 // Run the analysis.
112123 pass := & analysis.Pass {
113- Analyzer : act .a ,
114- Fset : act .pkg .Fset ,
115- Files : act .pkg .Syntax ,
116- OtherFiles : act .pkg .OtherFiles ,
117- Pkg : act .pkg .Types ,
118- TypesInfo : act .pkg .TypesInfo ,
119- TypesSizes : act .pkg .TypesSizes ,
124+ Analyzer : act .a ,
125+ Fset : act .pkg .Fset ,
126+ Files : act .pkg .Syntax ,
127+ OtherFiles : act .pkg .OtherFiles ,
128+ IgnoredFiles : act .pkg .IgnoredFiles ,
129+ Pkg : act .pkg .Types ,
130+ TypesInfo : act .pkg .TypesInfo ,
131+ TypesSizes : act .pkg .TypesSizes ,
132+ TypeErrors : act .pkg .TypeErrors ,
133+ Module : module ,
134+
120135 ResultOf : inputs ,
121136 Report : func (d analysis.Diagnostic ) { act .diagnostics = append (act .diagnostics , d ) },
122137 ImportObjectFact : act .importObjectFact ,
@@ -126,6 +141,7 @@ func (act *action) analyze() {
126141 AllObjectFacts : act .allObjectFacts ,
127142 AllPackageFacts : act .allPackageFacts ,
128143 }
144+
129145 act .pass = pass
130146 act .r .passToPkgGuard .Lock ()
131147 act .r .passToPkg [pass ] = act .pkg
@@ -138,7 +154,9 @@ func (act *action) analyze() {
138154 act .err = fmt .Errorf ("analysis skipped: %w" , & pkgerrors.IllTypedError {Pkg : act .pkg })
139155 } else {
140156 startedAt = time .Now ()
157+
141158 act .result , act .err = pass .Analyzer .Run (pass )
159+
142160 analyzedIn := time .Since (startedAt )
143161 if analyzedIn > time .Millisecond * 10 {
144162 debugf ("%s: run analyzer in %s" , act , analyzedIn )
@@ -149,7 +167,8 @@ func (act *action) analyze() {
149167 pass .ExportObjectFact = nil
150168 pass .ExportPackageFact = nil
151169
152- if err := act .persistFactsToCache (); err != nil {
170+ err := act .persistFactsToCache ()
171+ if err != nil {
153172 act .r .log .Warnf ("Failed to persist facts to cache: %s" , err )
154173 }
155174}
@@ -172,14 +191,15 @@ func inheritFacts(act, dep *action) {
172191 // Optionally serialize/deserialize fact
173192 // to verify that it works across address spaces.
174193 if serialize {
175- var err error
176- fact , err = codeFact (fact )
194+ encodedFact , err := codeFact (fact )
177195 if err != nil {
178- act .r .log .Panicf ("internal error: encoding of %T fact failed in %v" , fact , act )
196+ act .r .log .Panicf ("internal error: encoding of %T fact failed in %v: %v " , fact , act , err )
179197 }
198+ fact = encodedFact
180199 }
181200
182201 factsInheritDebugf ("%v: inherited %T fact for %s: %s" , act , fact , key .obj , fact )
202+
183203 act .objectFacts [key ] = fact
184204 }
185205
@@ -192,14 +212,15 @@ func inheritFacts(act, dep *action) {
192212 // to verify that it works across address spaces
193213 // and is deterministic.
194214 if serialize {
195- var err error
196- fact , err = codeFact (fact )
215+ encodedFact , err := codeFact (fact )
197216 if err != nil {
198217 act .r .log .Panicf ("internal error: encoding of %T fact failed in %v" , fact , act )
199218 }
219+ fact = encodedFact
200220 }
201221
202222 factsInheritDebugf ("%v: inherited %T fact for %s: %s" , act , fact , key .pkg .Path (), fact )
223+
203224 act .packageFacts [key ] = fact
204225 }
205226}
@@ -248,8 +269,13 @@ func exportedFrom(obj types.Object, pkg *types.Package) bool {
248269 return obj .Exported () && obj .Pkg () == pkg ||
249270 obj .Type ().(* types.Signature ).Recv () != nil
250271 case * types.Var :
251- return obj .Exported () && obj .Pkg () == pkg ||
252- obj .IsField ()
272+ if obj .IsField () {
273+ return true
274+ }
275+ // we can't filter more aggressively than this because we need
276+ // to consider function parameters exported, but have no way
277+ // of telling apart function parameters from local variables.
278+ return obj .Pkg () == pkg
253279 case * types.TypeName , * types.Const :
254280 return true
255281 }
@@ -284,21 +310,19 @@ func (act *action) exportObjectFact(obj types.Object, fact analysis.Fact) {
284310 act .objectFacts [key ] = fact // clobber any existing entry
285311 if isFactsExportDebug {
286312 objstr := types .ObjectString (obj , (* types .Package ).Name )
313+
287314 factsExportDebugf ("%s: object %s has fact %s\n " ,
288315 act .pkg .Fset .Position (obj .Pos ()), objstr , fact )
289316 }
290317}
291318
292319// NOTE(ldez) no alteration.
293320func (act * action ) allObjectFacts () []analysis.ObjectFact {
294- out := make ([]analysis.ObjectFact , 0 , len (act .objectFacts ))
295- for key , fact := range act .objectFacts {
296- out = append (out , analysis.ObjectFact {
297- Object : key .obj ,
298- Fact : fact ,
299- })
321+ facts := make ([]analysis.ObjectFact , 0 , len (act .objectFacts ))
322+ for k := range act .objectFacts {
323+ facts = append (facts , analysis.ObjectFact {Object : k .obj , Fact : act .objectFacts [k ]})
300324 }
301- return out
325+ return facts
302326}
303327
304328// NOTE(ldez) altered: `act.factType`
@@ -322,6 +346,7 @@ func (act *action) importPackageFact(pkg *types.Package, ptr analysis.Fact) bool
322346func (act * action ) exportPackageFact (fact analysis.Fact ) {
323347 key := packageFactKey {act .pass .Pkg , act .factType (fact )}
324348 act .packageFacts [key ] = fact // clobber any existing entry
349+
325350 factsDebugf ("%s: package %s has fact %s\n " ,
326351 act .pkg .Fset .Position (act .pass .Files [0 ].Pos ()), act .pass .Pkg .Path (), fact )
327352}
@@ -330,19 +355,16 @@ func (act *action) exportPackageFact(fact analysis.Fact) {
330355func (act * action ) factType (fact analysis.Fact ) reflect.Type {
331356 t := reflect .TypeOf (fact )
332357 if t .Kind () != reflect .Ptr {
333- act .r .log .Fatalf ("invalid Fact type: got %T, want pointer" , t )
358+ act .r .log .Fatalf ("invalid Fact type: got %T, want pointer" , fact )
334359 }
335360 return t
336361}
337362
338363// NOTE(ldez) no alteration.
339364func (act * action ) allPackageFacts () []analysis.PackageFact {
340- out := make ([]analysis.PackageFact , 0 , len (act .packageFacts ))
341- for key , fact := range act .packageFacts {
342- out = append (out , analysis.PackageFact {
343- Package : key .pkg ,
344- Fact : fact ,
345- })
365+ facts := make ([]analysis.PackageFact , 0 , len (act .packageFacts ))
366+ for k := range act .packageFacts {
367+ facts = append (facts , analysis.PackageFact {Package : k .pkg , Fact : act .packageFacts [k ]})
346368 }
347- return out
369+ return facts
348370}
0 commit comments