1 // Copyright 2019 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.
16 "golang.org/x/sync/errgroup"
17 "golang.org/x/tools/go/analysis"
18 "golang.org/x/tools/internal/analysisinternal"
19 "golang.org/x/tools/internal/event"
20 "golang.org/x/tools/internal/lsp/debug/tag"
21 "golang.org/x/tools/internal/lsp/source"
22 "golang.org/x/tools/internal/memoize"
23 "golang.org/x/tools/internal/span"
24 errors "golang.org/x/xerrors"
27 func (s *snapshot) Analyze(ctx context.Context, id string, analyzers []*source.Analyzer) ([]*source.Diagnostic, error) {
28 var roots []*actionHandle
30 for _, a := range analyzers {
32 if !a.IsEnabled(s.view) {
35 ah, err := s.actionHandle(ctx, packageID(id), a.Analyzer)
39 roots = append(roots, ah)
42 // Check if the context has been canceled before running the analyses.
47 var results []*source.Diagnostic
48 for _, ah := range roots {
49 diagnostics, _, err := ah.analyze(ctx, s)
53 results = append(results, diagnostics...)
58 type actionHandleKey string
60 // An action represents one unit of analysis work: the application of
61 // one analysis to one package. Actions form a DAG, both within a
62 // package (as different analyzers are applied, either in sequence or
63 // parallel), and across packages (as dependencies are analyzed).
64 type actionHandle struct {
65 handle *memoize.Handle
67 analyzer *analysis.Analyzer
71 type actionData struct {
72 diagnostics []*source.Diagnostic
74 objectFacts map[objectFactKey]analysis.Fact
75 packageFacts map[packageFactKey]analysis.Fact
79 type objectFactKey struct {
84 type packageFactKey struct {
89 func (s *snapshot) actionHandle(ctx context.Context, id packageID, a *analysis.Analyzer) (*actionHandle, error) {
90 ph, err := s.buildPackageHandle(ctx, id, source.ParseFull)
94 act := s.getActionHandle(id, ph.mode, a)
99 return nil, errors.Errorf("actionHandle: no key for package %s", id)
101 pkg, err := ph.check(ctx, s)
109 var deps []*actionHandle
110 // Add a dependency on each required analyzers.
111 for _, req := range a.Requires {
112 reqActionHandle, err := s.actionHandle(ctx, id, req)
116 deps = append(deps, reqActionHandle)
119 // TODO(golang/go#35089): Re-enable this when we doesn't use ParseExported
120 // mode for dependencies. In the meantime, disable analysis for dependencies,
121 // since we don't get anything useful out of it.
123 // An analysis that consumes/produces facts
124 // must run on the package's dependencies too.
125 if len(a.FactTypes) > 0 {
126 importIDs := make([]string, 0, len(ph.m.deps))
127 for _, importID := range ph.m.deps {
128 importIDs = append(importIDs, string(importID))
130 sort.Strings(importIDs) // for determinism
131 for _, importID := range importIDs {
132 depActionHandle, err := s.actionHandle(ctx, packageID(importID), a)
136 deps = append(deps, depActionHandle)
141 h := s.generation.Bind(buildActionKey(a, ph), func(ctx context.Context, arg memoize.Arg) interface{} {
142 snapshot := arg.(*snapshot)
143 // Analyze dependencies first.
144 results, err := execAll(ctx, snapshot, deps)
150 return runAnalysis(ctx, snapshot, a, pkg, results)
154 act = s.addActionHandle(act)
158 func (act *actionHandle) analyze(ctx context.Context, snapshot *snapshot) ([]*source.Diagnostic, interface{}, error) {
159 d, err := act.handle.Get(ctx, snapshot.generation, snapshot)
163 data, ok := d.(*actionData)
165 return nil, nil, errors.Errorf("unexpected type for %s:%s", act.pkg.ID(), act.analyzer.Name)
168 return nil, nil, errors.Errorf("unexpected nil analysis for %s:%s", act.pkg.ID(), act.analyzer.Name)
170 return data.diagnostics, data.result, data.err
173 func buildActionKey(a *analysis.Analyzer, ph *packageHandle) actionHandleKey {
174 return actionHandleKey(hashContents([]byte(fmt.Sprintf("%p %s", a, string(ph.key)))))
177 func (act *actionHandle) String() string {
178 return fmt.Sprintf("%s@%s", act.analyzer, act.pkg.PkgPath())
181 func execAll(ctx context.Context, snapshot *snapshot, actions []*actionHandle) (map[*actionHandle]*actionData, error) {
183 results := make(map[*actionHandle]*actionData)
185 g, ctx := errgroup.WithContext(ctx)
186 for _, act := range actions {
189 v, err := act.handle.Get(ctx, snapshot.generation, snapshot)
193 data, ok := v.(*actionData)
195 return errors.Errorf("unexpected type for %s: %T", act, v)
205 return results, g.Wait()
208 func runAnalysis(ctx context.Context, snapshot *snapshot, analyzer *analysis.Analyzer, pkg *pkg, deps map[*actionHandle]*actionData) (data *actionData) {
210 objectFacts: make(map[objectFactKey]analysis.Fact),
211 packageFacts: make(map[packageFactKey]analysis.Fact),
214 if r := recover(); r != nil {
215 data.err = errors.Errorf("analysis %s for package %s panicked: %v", analyzer.Name, pkg.PkgPath(), r)
219 // Plumb the output values of the dependencies
220 // into the inputs of this action. Also facts.
221 inputs := make(map[*analysis.Analyzer]interface{})
223 for depHandle, depData := range deps {
224 if depHandle.pkg == pkg {
225 // Same package, different analysis (horizontal edge):
226 // in-memory outputs of prerequisite analyzers
227 // become inputs to this analysis pass.
228 inputs[depHandle.analyzer] = depData.result
229 } else if depHandle.analyzer == analyzer { // (always true)
230 // Same analysis, different package (vertical edge):
231 // serialized facts produced by prerequisite analysis
232 // become available to this analysis pass.
233 for key, fact := range depData.objectFacts {
234 // Filter out facts related to objects
235 // that are irrelevant downstream
236 // (equivalently: not in the compiler export data).
237 if !exportedFrom(key.obj, depHandle.pkg.types) {
240 data.objectFacts[key] = fact
242 for key, fact := range depData.packageFacts {
243 // TODO: filter out facts that belong to
244 // packages not mentioned in the export data
245 // to prevent side channels.
247 data.packageFacts[key] = fact
252 var syntax []*ast.File
253 for _, cgf := range pkg.compiledGoFiles {
254 syntax = append(syntax, cgf.File)
257 var diagnostics []*analysis.Diagnostic
260 pass := &analysis.Pass{
262 Fset: snapshot.view.session.cache.fset,
265 TypesInfo: pkg.GetTypesInfo(),
266 TypesSizes: pkg.GetTypesSizes(),
268 Report: func(d analysis.Diagnostic) {
269 // Prefix the diagnostic category with the analyzer's name.
270 if d.Category == "" {
271 d.Category = analyzer.Name
273 d.Category = analyzer.Name + "." + d.Category
275 diagnostics = append(diagnostics, &d)
277 ImportObjectFact: func(obj types.Object, ptr analysis.Fact) bool {
281 key := objectFactKey{obj, factType(ptr)}
283 if v, ok := data.objectFacts[key]; ok {
284 reflect.ValueOf(ptr).Elem().Set(reflect.ValueOf(v).Elem())
289 ExportObjectFact: func(obj types.Object, fact analysis.Fact) {
290 if obj.Pkg() != pkg.types {
291 panic(fmt.Sprintf("internal error: in analysis %s of package %s: Fact.Set(%s, %T): can't set facts on objects belonging another package",
292 analyzer, pkg.ID(), obj, fact))
294 key := objectFactKey{obj, factType(fact)}
295 data.objectFacts[key] = fact // clobber any existing entry
297 ImportPackageFact: func(pkg *types.Package, ptr analysis.Fact) bool {
301 key := packageFactKey{pkg, factType(ptr)}
302 if v, ok := data.packageFacts[key]; ok {
303 reflect.ValueOf(ptr).Elem().Set(reflect.ValueOf(v).Elem())
308 ExportPackageFact: func(fact analysis.Fact) {
309 key := packageFactKey{pkg.types, factType(fact)}
310 data.packageFacts[key] = fact // clobber any existing entry
312 AllObjectFacts: func() []analysis.ObjectFact {
313 facts := make([]analysis.ObjectFact, 0, len(data.objectFacts))
314 for k := range data.objectFacts {
315 facts = append(facts, analysis.ObjectFact{Object: k.obj, Fact: data.objectFacts[k]})
319 AllPackageFacts: func() []analysis.PackageFact {
320 facts := make([]analysis.PackageFact, 0, len(data.packageFacts))
321 for k := range data.packageFacts {
322 facts = append(facts, analysis.PackageFact{Package: k.pkg, Fact: data.packageFacts[k]})
327 analysisinternal.SetTypeErrors(pass, pkg.typeErrors)
329 if pkg.IsIllTyped() {
330 data.err = errors.Errorf("analysis skipped due to errors in package")
333 data.result, data.err = pass.Analyzer.Run(pass)
338 if got, want := reflect.TypeOf(data.result), pass.Analyzer.ResultType; got != want {
339 data.err = errors.Errorf(
340 "internal error: on package %s, analyzer %s returned a result of type %v, but declared ResultType %v",
341 pass.Pkg.Path(), pass.Analyzer, got, want)
345 // disallow calls after Run
346 pass.ExportObjectFact = func(obj types.Object, fact analysis.Fact) {
347 panic(fmt.Sprintf("%s:%s: Pass.ExportObjectFact(%s, %T) called after Run", analyzer.Name, pkg.PkgPath(), obj, fact))
349 pass.ExportPackageFact = func(fact analysis.Fact) {
350 panic(fmt.Sprintf("%s:%s: Pass.ExportPackageFact(%T) called after Run", analyzer.Name, pkg.PkgPath(), fact))
353 for _, diag := range diagnostics {
354 srcDiags, err := analysisDiagnosticDiagnostics(ctx, snapshot, pkg, analyzer, diag)
356 event.Error(ctx, "unable to compute analysis error position", err, tag.Category.Of(diag.Category), tag.Package.Of(pkg.ID()))
359 if ctx.Err() != nil {
363 data.diagnostics = append(data.diagnostics, srcDiags...)
368 // exportedFrom reports whether obj may be visible to a package that imports pkg.
369 // This includes not just the exported members of pkg, but also unexported
370 // constants, types, fields, and methods, perhaps belonging to oether packages,
371 // that find there way into the API.
372 // This is an overapproximation of the more accurate approach used by
373 // gc export data, which walks the type graph, but it's much simpler.
375 // TODO(adonovan): do more accurate filtering by walking the type graph.
376 func exportedFrom(obj types.Object, pkg *types.Package) bool {
377 switch obj := obj.(type) {
379 return obj.Exported() && obj.Pkg() == pkg ||
380 obj.Type().(*types.Signature).Recv() != nil
382 return obj.Exported() && obj.Pkg() == pkg ||
384 case *types.TypeName, *types.Const:
387 return false // Nil, Builtin, Label, or PkgName
390 func factType(fact analysis.Fact) reflect.Type {
391 t := reflect.TypeOf(fact)
392 if t.Kind() != reflect.Ptr {
393 panic(fmt.Sprintf("invalid Fact type: got %T, want pointer", t))
398 func (s *snapshot) DiagnosePackage(ctx context.Context, spkg source.Package) (map[span.URI][]*source.Diagnostic, error) {
400 // Apply type error analyzers. They augment type error diagnostics with their own fixes.
401 var analyzers []*source.Analyzer
402 for _, a := range s.View().Options().TypeErrorAnalyzers {
403 analyzers = append(analyzers, a)
405 var errorAnalyzerDiag []*source.Diagnostic
406 if pkg.hasTypeErrors {
408 errorAnalyzerDiag, err = s.Analyze(ctx, pkg.ID(), analyzers)
410 // Keep going: analysis failures should not block diagnostics.
411 event.Error(ctx, "type error analysis failed", err, tag.Package.Of(pkg.ID()))
414 diags := map[span.URI][]*source.Diagnostic{}
415 for _, diag := range pkg.diagnostics {
416 for _, eaDiag := range errorAnalyzerDiag {
417 if eaDiag.URI == diag.URI && eaDiag.Range == diag.Range && eaDiag.Message == diag.Message {
418 // Type error analyzers just add fixes and tags. Make a copy,
419 // since we don't own either, and overwrite.
420 // The analyzer itself can't do this merge because
421 // analysis.Diagnostic doesn't have all the fields, and Analyze
422 // can't because it doesn't have the type error, notably its code.
424 clone.SuggestedFixes = eaDiag.SuggestedFixes
425 clone.Tags = eaDiag.Tags
429 diags[diag.URI] = append(diags[diag.URI], diag)