2019-02-06 16:47:00 -07:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// This file is largely based on go/analysis/internal/checker/checker.go.
|
|
|
|
|
|
|
|
package source
|
|
|
|
|
|
|
|
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"
|
|
|
|
"log"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
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-06-21 14:28:23 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry/trace"
|
2019-02-06 16:47:00 -07:00
|
|
|
)
|
|
|
|
|
2019-04-22 15:05:43 -06:00
|
|
|
func analyze(ctx context.Context, v View, pkgs []Package, analyzers []*analysis.Analyzer) ([]*Action, error) {
|
2019-06-21 14:28:23 -06:00
|
|
|
ctx, ts := trace.StartSpan(ctx, "source.analyze")
|
|
|
|
defer ts.End()
|
2019-04-22 15:05:43 -06:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
|
2019-02-06 16:47:00 -07:00
|
|
|
// Build nodes for initial packages.
|
2019-03-06 14:33:47 -07:00
|
|
|
var roots []*Action
|
2019-02-06 16:47:00 -07:00
|
|
|
for _, a := range analyzers {
|
|
|
|
for _, pkg := range pkgs {
|
2019-03-06 14:33:47 -07:00
|
|
|
root, err := pkg.GetActionGraph(ctx, a)
|
|
|
|
if err != nil {
|
2019-04-22 15:05:43 -06:00
|
|
|
return nil, err
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
2019-02-06 16:47:00 -07:00
|
|
|
root.isroot = true
|
2019-04-23 12:36:30 -06:00
|
|
|
root.view = v
|
2019-02-06 16:47:00 -07:00
|
|
|
roots = append(roots, root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the graph in parallel.
|
2019-05-17 08:51:19 -06:00
|
|
|
if err := execAll(ctx, v.Session().Cache().FileSet(), roots); err != nil {
|
2019-04-23 12:36:30 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-22 15:05:43 -06:00
|
|
|
return roots, 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-03-06 14:33:47 -07:00
|
|
|
type Action struct {
|
2019-02-06 16:47:00 -07:00
|
|
|
once sync.Once
|
2019-03-06 14:33:47 -07:00
|
|
|
Analyzer *analysis.Analyzer
|
|
|
|
Pkg Package
|
|
|
|
Deps []*Action
|
2019-02-06 16:47:00 -07:00
|
|
|
pass *analysis.Pass
|
|
|
|
isroot bool
|
|
|
|
objectFacts map[objectFactKey]analysis.Fact
|
|
|
|
packageFacts map[packageFactKey]analysis.Fact
|
|
|
|
inputs map[*analysis.Analyzer]interface{}
|
|
|
|
result interface{}
|
|
|
|
diagnostics []analysis.Diagnostic
|
|
|
|
err error
|
|
|
|
duration time.Duration
|
2019-04-23 12:36:30 -06:00
|
|
|
view View
|
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-03-06 14:33:47 -07:00
|
|
|
func (act *Action) String() string {
|
2019-05-23 13:03:11 -06:00
|
|
|
return fmt.Sprintf("%s@%s", act.Analyzer, act.Pkg.PkgPath())
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
|
2019-04-22 15:05:43 -06:00
|
|
|
func execAll(ctx context.Context, fset *token.FileSet, actions []*Action) error {
|
|
|
|
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-04-23 12:36:30 -06:00
|
|
|
return act.exec(ctx, fset)
|
2019-04-22 15:05:43 -06:00
|
|
|
})
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
2019-04-22 15:05:43 -06:00
|
|
|
return g.Wait()
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
|
2019-04-23 12:36:30 -06:00
|
|
|
func (act *Action) exec(ctx context.Context, fset *token.FileSet) error {
|
|
|
|
var err error
|
2019-03-06 14:33:47 -07:00
|
|
|
act.once.Do(func() {
|
2019-04-23 12:36:30 -06:00
|
|
|
err = act.execOnce(ctx, fset)
|
2019-03-06 14:33:47 -07:00
|
|
|
})
|
2019-04-23 12:36:30 -06:00
|
|
|
return err
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
2019-02-06 16:47:00 -07:00
|
|
|
|
2019-04-23 12:36:30 -06:00
|
|
|
func (act *Action) execOnce(ctx context.Context, fset *token.FileSet) error {
|
2019-02-06 16:47:00 -07:00
|
|
|
// Analyze dependencies.
|
2019-04-23 12:36:30 -06:00
|
|
|
if err := execAll(ctx, fset, act.Deps); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-06 16:47:00 -07:00
|
|
|
|
|
|
|
// Report an error if any dependency failed.
|
|
|
|
var failed []string
|
2019-03-06 14:33:47 -07:00
|
|
|
for _, dep := range act.Deps {
|
2019-02-06 16:47:00 -07:00
|
|
|
if dep.err != nil {
|
2019-05-23 13:03:11 -06:00
|
|
|
failed = append(failed, fmt.Sprintf("%s: %v", dep.String(), dep.err))
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if failed != nil {
|
|
|
|
sort.Strings(failed)
|
|
|
|
act.err = fmt.Errorf("failed prerequisites: %s", strings.Join(failed, ", "))
|
2019-04-23 12:36:30 -06:00
|
|
|
return act.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-03-06 14:33:47 -07: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-03-06 14:33:47 -07:00
|
|
|
inputs[dep.Analyzer] = dep.result
|
2019-02-06 16:47:00 -07:00
|
|
|
|
2019-03-06 14:33:47 -07:00
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the analysis.
|
|
|
|
pass := &analysis.Pass{
|
2019-03-29 14:39:22 -06:00
|
|
|
Analyzer: act.Analyzer,
|
|
|
|
Fset: fset,
|
|
|
|
Files: act.Pkg.GetSyntax(),
|
|
|
|
Pkg: act.Pkg.GetTypes(),
|
|
|
|
TypesInfo: act.Pkg.GetTypesInfo(),
|
|
|
|
TypesSizes: act.Pkg.GetTypesSizes(),
|
2019-02-06 16:47:00 -07:00
|
|
|
ResultOf: inputs,
|
|
|
|
Report: func(d analysis.Diagnostic) { act.diagnostics = append(act.diagnostics, d) },
|
|
|
|
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-05-28 13:17:07 -06:00
|
|
|
if act.Pkg.IsIllTyped() && !pass.Analyzer.RunDespiteErrors {
|
2019-05-23 13:03:11 -06:00
|
|
|
act.err = fmt.Errorf("analysis skipped due to errors in package: %v", act.Pkg.GetErrors())
|
2019-02-06 16:47:00 -07:00
|
|
|
} else {
|
2019-04-23 12:36:30 -06:00
|
|
|
act.result, act.err = pass.Analyzer.Run(pass)
|
|
|
|
if act.err == nil {
|
2019-02-06 16:47:00 -07:00
|
|
|
if got, want := reflect.TypeOf(act.result), pass.Analyzer.ResultType; got != want {
|
2019-04-23 12:36:30 -06:00
|
|
|
act.err = fmt.Errorf(
|
2019-02-06 16:47:00 -07:00
|
|
|
"internal error: on package %s, analyzer %s returned a result of type %v, but declared ResultType %v",
|
|
|
|
pass.Pkg.Path(), pass.Analyzer, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// disallow calls after Run
|
|
|
|
pass.ExportObjectFact = nil
|
|
|
|
pass.ExportPackageFact = nil
|
2019-04-23 12:36:30 -06:00
|
|
|
|
|
|
|
return act.err
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// inheritFacts populates act.facts with
|
|
|
|
// those it obtains from its dependency, dep.
|
2019-03-06 14:33:47 -07:00
|
|
|
func inheritFacts(act, dep *Action) {
|
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-03-06 14:33:47 -07:00
|
|
|
if !exportedFrom(key.obj, dep.Pkg.GetTypes()) {
|
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-03-06 14:33:47 -07:00
|
|
|
func (act *Action) 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-03-06 14:33:47 -07:00
|
|
|
func (act *Action) exportObjectFact(obj types.Object, fact analysis.Fact) {
|
2019-02-06 16:47:00 -07:00
|
|
|
if act.pass.ExportObjectFact == nil {
|
|
|
|
log.Panicf("%s: Pass.ExportObjectFact(%s, %T) called after Run", act, obj, fact)
|
|
|
|
}
|
|
|
|
|
2019-03-06 14:33:47 -07:00
|
|
|
if obj.Pkg() != act.Pkg.GetTypes() {
|
2019-02-06 16:47:00 -07:00
|
|
|
log.Panicf("internal error: in analysis %s of package %s: Fact.Set(%s, %T): can't set facts on objects belonging another package",
|
2019-03-06 14:33:47 -07:00
|
|
|
act.Analyzer, act.Pkg, 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.
|
|
|
|
func (act *Action) allObjectFacts() []analysis.ObjectFact {
|
|
|
|
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-03-06 14:33:47 -07:00
|
|
|
func (act *Action) 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-03-06 14:33:47 -07:00
|
|
|
func (act *Action) exportPackageFact(fact analysis.Fact) {
|
2019-02-06 16:47:00 -07:00
|
|
|
if act.pass.ExportPackageFact == nil {
|
|
|
|
log.Panicf("%s: Pass.ExportPackageFact(%T) called after Run", act, fact)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
log.Fatalf("invalid Fact type: got %T, want pointer", t)
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
2019-06-13 08:10:44 -06:00
|
|
|
|
|
|
|
// allObjectFacts implements Pass.AllObjectFacts.
|
|
|
|
func (act *Action) allPackageFacts() []analysis.PackageFact {
|
|
|
|
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
|
|
|
|
}
|