2019-10-04 15:18:43 -06:00
|
|
|
package cache
|
2019-02-06 16:47:00 -07:00
|
|
|
|
|
|
|
import (
|
2019-03-06 14:33:47 -07:00
|
|
|
"context"
|
2019-02-06 16:47:00 -07:00
|
|
|
"fmt"
|
2019-03-06 14:33:47 -07:00
|
|
|
"go/token"
|
2019-02-06 16:47:00 -07:00
|
|
|
"go/types"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
|
2019-04-22 15:05:43 -06:00
|
|
|
"golang.org/x/sync/errgroup"
|
2019-02-06 16:47:00 -07:00
|
|
|
"golang.org/x/tools/go/analysis"
|
2019-10-04 15:18:43 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/memoize"
|
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-06 16:47:00 -07:00
|
|
|
)
|
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
func (s *snapshot) Analyze(ctx context.Context, id string, analyzers []*analysis.Analyzer) (map[*analysis.Analyzer][]*source.Error, error) {
|
2019-10-04 15:18:43 -06:00
|
|
|
var roots []*actionHandle
|
2019-08-15 14:03:47 -06:00
|
|
|
|
2019-10-04 15:18:43 -06:00
|
|
|
for _, a := range analyzers {
|
|
|
|
ah, err := s.actionHandle(ctx, packageID(id), source.ParseFull, a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ah.isroot = true
|
|
|
|
roots = append(roots, ah)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the context has been canceled before running the analyses.
|
2019-04-22 15:05:43 -06:00
|
|
|
if ctx.Err() != nil {
|
2019-08-16 15:29:19 -06:00
|
|
|
return nil, ctx.Err()
|
2019-04-22 15:05:43 -06:00
|
|
|
}
|
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
results := make(map[*analysis.Analyzer][]*source.Error)
|
2019-10-04 15:18:43 -06:00
|
|
|
for _, ah := range roots {
|
|
|
|
diagnostics, _, err := ah.analyze(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(ctx, "no results", err)
|
|
|
|
continue
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
2019-10-04 15:18:43 -06:00
|
|
|
results[ah.analyzer] = diagnostics
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
2019-10-04 15:18:43 -06:00
|
|
|
return results, nil
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// An action represents one unit of analysis work: the application of
|
|
|
|
// one analysis to one package. Actions form a DAG, both within a
|
|
|
|
// package (as different analyzers are applied, either in sequence or
|
|
|
|
// parallel), and across packages (as dependencies are analyzed).
|
2019-10-04 15:18:43 -06:00
|
|
|
type actionHandle struct {
|
|
|
|
handle *memoize.Handle
|
2019-09-24 14:28:59 -06:00
|
|
|
|
2019-10-04 15:18:43 -06:00
|
|
|
analyzer *analysis.Analyzer
|
|
|
|
deps []*actionHandle
|
|
|
|
pkg *pkg
|
2019-02-06 16:47:00 -07:00
|
|
|
pass *analysis.Pass
|
|
|
|
isroot bool
|
|
|
|
objectFacts map[objectFactKey]analysis.Fact
|
|
|
|
packageFacts map[packageFactKey]analysis.Fact
|
2019-10-04 15:18:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type actionData struct {
|
2019-10-21 15:25:09 -06:00
|
|
|
diagnostics []*source.Error
|
2019-10-04 15:18:43 -06:00
|
|
|
result interface{}
|
|
|
|
err error
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type objectFactKey struct {
|
|
|
|
obj types.Object
|
|
|
|
typ reflect.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
type packageFactKey struct {
|
|
|
|
pkg *types.Package
|
|
|
|
typ reflect.Type
|
|
|
|
}
|
|
|
|
|
2019-10-04 15:18:43 -06:00
|
|
|
func (s *snapshot) actionHandle(ctx context.Context, id packageID, mode source.ParseMode, a *analysis.Analyzer) (*actionHandle, error) {
|
2019-10-15 11:27:09 -06:00
|
|
|
ah := s.getAction(id, mode, a)
|
2019-10-14 14:13:06 -06:00
|
|
|
if ah != nil {
|
|
|
|
return ah, nil
|
|
|
|
}
|
2019-10-04 15:18:43 -06:00
|
|
|
cph := s.getPackage(id, mode)
|
|
|
|
if cph == nil {
|
|
|
|
return nil, errors.Errorf("no CheckPackageHandle for %s:%v", id, mode == source.ParseExported)
|
|
|
|
}
|
|
|
|
if len(cph.key) == 0 {
|
|
|
|
return nil, errors.Errorf("no key for CheckPackageHandle %s", id)
|
|
|
|
}
|
|
|
|
pkg, err := cph.check(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-14 14:13:06 -06:00
|
|
|
ah = &actionHandle{
|
2019-10-04 15:18:43 -06:00
|
|
|
analyzer: a,
|
|
|
|
pkg: pkg,
|
|
|
|
}
|
|
|
|
// Add a dependency on each required analyzers.
|
|
|
|
for _, req := range a.Requires {
|
|
|
|
reqActionHandle, err := s.actionHandle(ctx, id, mode, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ah.deps = append(ah.deps, reqActionHandle)
|
|
|
|
}
|
|
|
|
// An analysis that consumes/produces facts
|
|
|
|
// must run on the package's dependencies too.
|
|
|
|
if len(a.FactTypes) > 0 {
|
|
|
|
importIDs := make([]string, 0, len(cph.m.deps))
|
|
|
|
for _, importID := range cph.m.deps {
|
|
|
|
importIDs = append(importIDs, string(importID))
|
|
|
|
}
|
|
|
|
sort.Strings(importIDs) // for determinism
|
|
|
|
for _, importID := range importIDs {
|
|
|
|
depActionHandle, err := s.actionHandle(ctx, packageID(importID), source.ParseExported, a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ah.deps = append(ah.deps, depActionHandle)
|
|
|
|
}
|
|
|
|
}
|
2019-10-14 14:13:06 -06:00
|
|
|
h := s.view.session.cache.store.Bind(buildActionKey(a, cph), func(ctx context.Context) interface{} {
|
2019-10-04 15:18:43 -06:00
|
|
|
data := &actionData{}
|
|
|
|
data.diagnostics, data.result, data.err = ah.exec(ctx, s.view.session.cache.fset)
|
|
|
|
return data
|
|
|
|
})
|
|
|
|
ah.handle = h
|
2019-10-15 11:27:09 -06:00
|
|
|
|
2019-10-14 14:13:06 -06:00
|
|
|
s.addAction(ah)
|
2019-10-04 15:18:43 -06:00
|
|
|
return ah, nil
|
|
|
|
}
|
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
func (act *actionHandle) analyze(ctx context.Context) ([]*source.Error, interface{}, error) {
|
2019-10-16 17:09:37 -06:00
|
|
|
v := act.handle.Get(ctx)
|
2019-10-04 15:18:43 -06:00
|
|
|
if v == nil {
|
2019-10-16 17:09:37 -06:00
|
|
|
return nil, nil, errors.Errorf("no analyses for %s", act.pkg.ID())
|
2019-10-04 15:18:43 -06:00
|
|
|
}
|
|
|
|
data := v.(*actionData)
|
|
|
|
return data.diagnostics, data.result, data.err
|
|
|
|
}
|
|
|
|
|
2019-10-14 14:13:06 -06:00
|
|
|
func buildActionKey(a *analysis.Analyzer, cph *checkPackageHandle) string {
|
2019-10-15 11:27:09 -06:00
|
|
|
return hashContents([]byte(fmt.Sprintf("%p %s", a, string(cph.key))))
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
|
2019-10-04 15:18:43 -06:00
|
|
|
func (act *actionHandle) String() string {
|
|
|
|
return fmt.Sprintf("%s@%s", act.analyzer, act.pkg.PkgPath())
|
|
|
|
}
|
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
func execAll(ctx context.Context, fset *token.FileSet, actions []*actionHandle) (map[*actionHandle][]*source.Error, map[*actionHandle]interface{}, error) {
|
2019-10-04 15:18:43 -06:00
|
|
|
var (
|
|
|
|
mu sync.Mutex
|
2019-10-21 15:25:09 -06:00
|
|
|
diagnostics = make(map[*actionHandle][]*source.Error)
|
2019-10-04 15:18:43 -06:00
|
|
|
results = make(map[*actionHandle]interface{})
|
|
|
|
)
|
|
|
|
|
2019-04-22 15:05:43 -06:00
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
2019-02-06 16:47:00 -07:00
|
|
|
for _, act := range actions {
|
2019-04-22 15:05:43 -06:00
|
|
|
act := act
|
|
|
|
g.Go(func() error {
|
2019-10-04 15:18:43 -06:00
|
|
|
d, r, err := act.analyze(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
diagnostics[act] = d
|
|
|
|
results[act] = r
|
|
|
|
|
|
|
|
return nil
|
2019-04-22 15:05:43 -06:00
|
|
|
})
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
2019-10-04 15:18:43 -06:00
|
|
|
return diagnostics, results, g.Wait()
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
func (act *actionHandle) exec(ctx context.Context, fset *token.FileSet) ([]*source.Error, interface{}, error) {
|
2019-02-06 16:47:00 -07:00
|
|
|
// Analyze dependencies.
|
2019-10-04 15:18:43 -06:00
|
|
|
_, depResults, err := execAll(ctx, fset, act.deps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Plumb the output values of the dependencies
|
|
|
|
// into the inputs of this action. Also facts.
|
|
|
|
inputs := make(map[*analysis.Analyzer]interface{})
|
|
|
|
act.objectFacts = make(map[objectFactKey]analysis.Fact)
|
|
|
|
act.packageFacts = make(map[packageFactKey]analysis.Fact)
|
2019-10-04 15:18:43 -06:00
|
|
|
for _, dep := range act.deps {
|
|
|
|
if dep.pkg == act.pkg {
|
2019-02-06 16:47:00 -07:00
|
|
|
// Same package, different analysis (horizontal edge):
|
|
|
|
// in-memory outputs of prerequisite analyzers
|
|
|
|
// become inputs to this analysis pass.
|
2019-10-04 15:18:43 -06:00
|
|
|
inputs[dep.analyzer] = depResults[dep]
|
|
|
|
} else if dep.analyzer == act.analyzer { // (always true)
|
2019-02-06 16:47:00 -07:00
|
|
|
// Same analysis, different package (vertical edge):
|
|
|
|
// serialized facts produced by prerequisite analysis
|
|
|
|
// become available to this analysis pass.
|
|
|
|
inheritFacts(act, dep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
var diagnostics []*analysis.Diagnostic
|
|
|
|
|
2019-02-06 16:47:00 -07:00
|
|
|
// Run the analysis.
|
|
|
|
pass := &analysis.Pass{
|
2019-10-04 15:18:43 -06:00
|
|
|
Analyzer: act.analyzer,
|
2019-03-29 14:39:22 -06:00
|
|
|
Fset: fset,
|
2019-10-04 15:18:43 -06:00
|
|
|
Files: act.pkg.GetSyntax(ctx),
|
|
|
|
Pkg: act.pkg.GetTypes(),
|
|
|
|
TypesInfo: act.pkg.GetTypesInfo(),
|
|
|
|
TypesSizes: act.pkg.GetTypesSizes(),
|
2019-02-06 16:47:00 -07:00
|
|
|
ResultOf: inputs,
|
2019-10-04 15:18:43 -06:00
|
|
|
Report: func(d analysis.Diagnostic) { diagnostics = append(diagnostics, &d) },
|
2019-02-06 16:47:00 -07:00
|
|
|
ImportObjectFact: act.importObjectFact,
|
|
|
|
ExportObjectFact: act.exportObjectFact,
|
|
|
|
ImportPackageFact: act.importPackageFact,
|
|
|
|
ExportPackageFact: act.exportPackageFact,
|
2019-06-13 08:10:44 -06:00
|
|
|
AllObjectFacts: act.allObjectFacts,
|
|
|
|
AllPackageFacts: act.allPackageFacts,
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
act.pass = pass
|
|
|
|
|
2019-10-04 15:18:43 -06:00
|
|
|
if act.pkg.IsIllTyped() {
|
|
|
|
return nil, nil, errors.Errorf("analysis skipped due to errors in package: %v", act.pkg.GetErrors())
|
2019-10-21 15:25:09 -06:00
|
|
|
}
|
|
|
|
result, err := pass.Analyzer.Run(pass)
|
|
|
|
if err == nil {
|
|
|
|
if got, want := reflect.TypeOf(result), pass.Analyzer.ResultType; got != want {
|
|
|
|
err = errors.Errorf(
|
|
|
|
"internal error: on package %s, analyzer %s returned a result of type %v, but declared ResultType %v",
|
|
|
|
pass.Pkg.Path(), pass.Analyzer, got, want)
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// disallow calls after Run
|
|
|
|
pass.ExportObjectFact = nil
|
|
|
|
pass.ExportPackageFact = nil
|
2019-04-23 12:36:30 -06:00
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
var errors []*source.Error
|
|
|
|
for _, diag := range diagnostics {
|
|
|
|
srcErr, err := sourceError(ctx, act.pkg, diag)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
errors = append(errors, srcErr)
|
|
|
|
}
|
|
|
|
return errors, result, err
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// inheritFacts populates act.facts with
|
|
|
|
// those it obtains from its dependency, dep.
|
2019-10-04 15:18:43 -06:00
|
|
|
func inheritFacts(act, dep *actionHandle) {
|
2019-02-06 16:47:00 -07:00
|
|
|
for key, fact := range dep.objectFacts {
|
|
|
|
// Filter out facts related to objects
|
|
|
|
// that are irrelevant downstream
|
|
|
|
// (equivalently: not in the compiler export data).
|
2019-10-04 15:18:43 -06:00
|
|
|
if !exportedFrom(key.obj, dep.pkg.types) {
|
2019-02-06 16:47:00 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
act.objectFacts[key] = fact
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, fact := range dep.packageFacts {
|
|
|
|
// TODO: filter out facts that belong to
|
|
|
|
// packages not mentioned in the export data
|
|
|
|
// to prevent side channels.
|
|
|
|
|
|
|
|
act.packageFacts[key] = fact
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// exportedFrom reports whether obj may be visible to a package that imports pkg.
|
|
|
|
// This includes not just the exported members of pkg, but also unexported
|
|
|
|
// constants, types, fields, and methods, perhaps belonging to oether packages,
|
|
|
|
// that find there way into the API.
|
|
|
|
// This is an overapproximation of the more accurate approach used by
|
|
|
|
// gc export data, which walks the type graph, but it's much simpler.
|
|
|
|
//
|
|
|
|
// TODO(adonovan): do more accurate filtering by walking the type graph.
|
|
|
|
func exportedFrom(obj types.Object, pkg *types.Package) bool {
|
|
|
|
switch obj := obj.(type) {
|
|
|
|
case *types.Func:
|
|
|
|
return obj.Exported() && obj.Pkg() == pkg ||
|
|
|
|
obj.Type().(*types.Signature).Recv() != nil
|
|
|
|
case *types.Var:
|
|
|
|
return obj.Exported() && obj.Pkg() == pkg ||
|
|
|
|
obj.IsField()
|
|
|
|
case *types.TypeName, *types.Const:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false // Nil, Builtin, Label, or PkgName
|
|
|
|
}
|
|
|
|
|
|
|
|
// importObjectFact implements Pass.ImportObjectFact.
|
|
|
|
// Given a non-nil pointer ptr of type *T, where *T satisfies Fact,
|
|
|
|
// importObjectFact copies the fact value to *ptr.
|
2019-10-04 15:18:43 -06:00
|
|
|
func (act *actionHandle) importObjectFact(obj types.Object, ptr analysis.Fact) bool {
|
2019-02-06 16:47:00 -07:00
|
|
|
if obj == nil {
|
|
|
|
panic("nil object")
|
|
|
|
}
|
|
|
|
key := objectFactKey{obj, factType(ptr)}
|
|
|
|
if v, ok := act.objectFacts[key]; ok {
|
|
|
|
reflect.ValueOf(ptr).Elem().Set(reflect.ValueOf(v).Elem())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// exportObjectFact implements Pass.ExportObjectFact.
|
2019-10-04 15:18:43 -06:00
|
|
|
func (act *actionHandle) exportObjectFact(obj types.Object, fact analysis.Fact) {
|
2019-02-06 16:47:00 -07:00
|
|
|
if act.pass.ExportObjectFact == nil {
|
2019-07-14 21:08:10 -06:00
|
|
|
panic(fmt.Sprintf("%s: Pass.ExportObjectFact(%s, %T) called after Run", act, obj, fact))
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
|
2019-10-04 15:18:43 -06:00
|
|
|
if obj.Pkg() != act.pkg.types {
|
2019-07-14 21:08:10 -06:00
|
|
|
panic(fmt.Sprintf("internal error: in analysis %s of package %s: Fact.Set(%s, %T): can't set facts on objects belonging another package",
|
2019-10-04 15:18:43 -06:00
|
|
|
act.analyzer, act.pkg.ID(), obj, fact))
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
key := objectFactKey{obj, factType(fact)}
|
|
|
|
act.objectFacts[key] = fact // clobber any existing entry
|
|
|
|
}
|
|
|
|
|
2019-06-13 08:10:44 -06:00
|
|
|
// allObjectFacts implements Pass.AllObjectFacts.
|
2019-10-04 15:18:43 -06:00
|
|
|
func (act *actionHandle) allObjectFacts() []analysis.ObjectFact {
|
2019-06-13 08:10:44 -06:00
|
|
|
facts := make([]analysis.ObjectFact, 0, len(act.objectFacts))
|
|
|
|
for k := range act.objectFacts {
|
|
|
|
facts = append(facts, analysis.ObjectFact{Object: k.obj, Fact: act.objectFacts[k]})
|
|
|
|
}
|
|
|
|
return facts
|
|
|
|
}
|
|
|
|
|
2019-02-06 16:47:00 -07:00
|
|
|
// importPackageFact implements Pass.ImportPackageFact.
|
|
|
|
// Given a non-nil pointer ptr of type *T, where *T satisfies Fact,
|
|
|
|
// fact copies the fact value to *ptr.
|
2019-10-04 15:18:43 -06:00
|
|
|
func (act *actionHandle) importPackageFact(pkg *types.Package, ptr analysis.Fact) bool {
|
2019-02-06 16:47:00 -07:00
|
|
|
if pkg == nil {
|
|
|
|
panic("nil package")
|
|
|
|
}
|
|
|
|
key := packageFactKey{pkg, factType(ptr)}
|
|
|
|
if v, ok := act.packageFacts[key]; ok {
|
|
|
|
reflect.ValueOf(ptr).Elem().Set(reflect.ValueOf(v).Elem())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// exportPackageFact implements Pass.ExportPackageFact.
|
2019-10-04 15:18:43 -06:00
|
|
|
func (act *actionHandle) exportPackageFact(fact analysis.Fact) {
|
2019-02-06 16:47:00 -07:00
|
|
|
if act.pass.ExportPackageFact == nil {
|
2019-07-14 21:08:10 -06:00
|
|
|
panic(fmt.Sprintf("%s: Pass.ExportPackageFact(%T) called after Run", act, fact))
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
key := packageFactKey{act.pass.Pkg, factType(fact)}
|
|
|
|
act.packageFacts[key] = fact // clobber any existing entry
|
|
|
|
}
|
|
|
|
|
|
|
|
func factType(fact analysis.Fact) reflect.Type {
|
|
|
|
t := reflect.TypeOf(fact)
|
|
|
|
if t.Kind() != reflect.Ptr {
|
2019-07-14 21:08:10 -06:00
|
|
|
panic(fmt.Sprintf("invalid Fact type: got %T, want pointer", t))
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
2019-06-13 08:10:44 -06:00
|
|
|
|
|
|
|
// allObjectFacts implements Pass.AllObjectFacts.
|
2019-10-04 15:18:43 -06:00
|
|
|
func (act *actionHandle) allPackageFacts() []analysis.PackageFact {
|
2019-06-13 08:10:44 -06:00
|
|
|
facts := make([]analysis.PackageFact, 0, len(act.packageFacts))
|
|
|
|
for k := range act.packageFacts {
|
|
|
|
facts = append(facts, analysis.PackageFact{Package: k.pkg, Fact: act.packageFacts[k]})
|
|
|
|
}
|
|
|
|
return facts
|
|
|
|
}
|