2
2
// Use of this source code is governed by a BSD-style
3
3
// license that can be found in the LICENSE file.
4
4
//
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
7
6
8
7
package goanalysis
9
8
@@ -28,20 +27,22 @@ import (
28
27
// package (as different analyzers are applied, either in sequence or
29
28
// parallel), and across packages (as dependencies are analyzed).
30
29
type 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.
40
42
r * runner
41
43
analysisDoneCh chan struct {}
42
44
loadCachedFactsDone bool
43
45
loadCachedFactsOk bool
44
- isroot bool
45
46
isInitialPkg bool
46
47
needAnalyzeSource bool
47
48
}
@@ -78,12 +79,11 @@ func (act *action) analyze() {
78
79
// Report an error if any dependency failures.
79
80
var depErrors error
80
81
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 ))
83
84
}
84
-
85
- depErrors = errors .Join (depErrors , errors .Unwrap (dep .err ))
86
85
}
86
+
87
87
if depErrors != nil {
88
88
act .err = fmt .Errorf ("failed prerequisites: %w" , depErrors )
89
89
return
@@ -92,7 +92,10 @@ func (act *action) analyze() {
92
92
// Plumb the output values of the dependencies
93
93
// into the inputs of this action. Also facts.
94
94
inputs := make (map [* analysis.Analyzer ]any )
95
+ act .objectFacts = make (map [objectFactKey ]analysis.Fact )
96
+ act .packageFacts = make (map [packageFactKey ]analysis.Fact )
95
97
startedAt := time .Now ()
98
+
96
99
for _ , dep := range act .deps {
97
100
if dep .pkg == act .pkg {
98
101
// Same package, different analysis (horizontal edge):
@@ -106,17 +109,29 @@ func (act *action) analyze() {
106
109
inheritFacts (act , dep )
107
110
}
108
111
}
112
+
109
113
factsDebugf ("%s: Inherited facts in %s" , act , time .Since (startedAt ))
110
114
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
+
111
122
// Run the analysis.
112
123
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
+
120
135
ResultOf : inputs ,
121
136
Report : func (d analysis.Diagnostic ) { act .diagnostics = append (act .diagnostics , d ) },
122
137
ImportObjectFact : act .importObjectFact ,
@@ -126,6 +141,7 @@ func (act *action) analyze() {
126
141
AllObjectFacts : act .allObjectFacts ,
127
142
AllPackageFacts : act .allPackageFacts ,
128
143
}
144
+
129
145
act .pass = pass
130
146
act .r .passToPkgGuard .Lock ()
131
147
act .r .passToPkg [pass ] = act .pkg
@@ -138,7 +154,9 @@ func (act *action) analyze() {
138
154
act .err = fmt .Errorf ("analysis skipped: %w" , & pkgerrors.IllTypedError {Pkg : act .pkg })
139
155
} else {
140
156
startedAt = time .Now ()
157
+
141
158
act .result , act .err = pass .Analyzer .Run (pass )
159
+
142
160
analyzedIn := time .Since (startedAt )
143
161
if analyzedIn > time .Millisecond * 10 {
144
162
debugf ("%s: run analyzer in %s" , act , analyzedIn )
@@ -149,7 +167,8 @@ func (act *action) analyze() {
149
167
pass .ExportObjectFact = nil
150
168
pass .ExportPackageFact = nil
151
169
152
- if err := act .persistFactsToCache (); err != nil {
170
+ err := act .persistFactsToCache ()
171
+ if err != nil {
153
172
act .r .log .Warnf ("Failed to persist facts to cache: %s" , err )
154
173
}
155
174
}
@@ -172,14 +191,15 @@ func inheritFacts(act, dep *action) {
172
191
// Optionally serialize/deserialize fact
173
192
// to verify that it works across address spaces.
174
193
if serialize {
175
- var err error
176
- fact , err = codeFact (fact )
194
+ encodedFact , err := codeFact (fact )
177
195
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 )
179
197
}
198
+ fact = encodedFact
180
199
}
181
200
182
201
factsInheritDebugf ("%v: inherited %T fact for %s: %s" , act , fact , key .obj , fact )
202
+
183
203
act .objectFacts [key ] = fact
184
204
}
185
205
@@ -192,14 +212,15 @@ func inheritFacts(act, dep *action) {
192
212
// to verify that it works across address spaces
193
213
// and is deterministic.
194
214
if serialize {
195
- var err error
196
- fact , err = codeFact (fact )
215
+ encodedFact , err := codeFact (fact )
197
216
if err != nil {
198
217
act .r .log .Panicf ("internal error: encoding of %T fact failed in %v" , fact , act )
199
218
}
219
+ fact = encodedFact
200
220
}
201
221
202
222
factsInheritDebugf ("%v: inherited %T fact for %s: %s" , act , fact , key .pkg .Path (), fact )
223
+
203
224
act .packageFacts [key ] = fact
204
225
}
205
226
}
@@ -248,8 +269,13 @@ func exportedFrom(obj types.Object, pkg *types.Package) bool {
248
269
return obj .Exported () && obj .Pkg () == pkg ||
249
270
obj .Type ().(* types.Signature ).Recv () != nil
250
271
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
253
279
case * types.TypeName , * types.Const :
254
280
return true
255
281
}
@@ -284,21 +310,19 @@ func (act *action) exportObjectFact(obj types.Object, fact analysis.Fact) {
284
310
act .objectFacts [key ] = fact // clobber any existing entry
285
311
if isFactsExportDebug {
286
312
objstr := types .ObjectString (obj , (* types .Package ).Name )
313
+
287
314
factsExportDebugf ("%s: object %s has fact %s\n " ,
288
315
act .pkg .Fset .Position (obj .Pos ()), objstr , fact )
289
316
}
290
317
}
291
318
292
319
// NOTE(ldez) no alteration.
293
320
func (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 ]})
300
324
}
301
- return out
325
+ return facts
302
326
}
303
327
304
328
// NOTE(ldez) altered: `act.factType`
@@ -322,6 +346,7 @@ func (act *action) importPackageFact(pkg *types.Package, ptr analysis.Fact) bool
322
346
func (act * action ) exportPackageFact (fact analysis.Fact ) {
323
347
key := packageFactKey {act .pass .Pkg , act .factType (fact )}
324
348
act .packageFacts [key ] = fact // clobber any existing entry
349
+
325
350
factsDebugf ("%s: package %s has fact %s\n " ,
326
351
act .pkg .Fset .Position (act .pass .Files [0 ].Pos ()), act .pass .Pkg .Path (), fact )
327
352
}
@@ -330,19 +355,16 @@ func (act *action) exportPackageFact(fact analysis.Fact) {
330
355
func (act * action ) factType (fact analysis.Fact ) reflect.Type {
331
356
t := reflect .TypeOf (fact )
332
357
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 )
334
359
}
335
360
return t
336
361
}
337
362
338
363
// NOTE(ldez) no alteration.
339
364
func (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 ]})
346
368
}
347
- return out
369
+ return facts
348
370
}
0 commit comments