2019-12-17 13:43:36 -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.
|
|
|
|
|
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"
|
2020-03-10 08:14:56 -06:00
|
|
|
"golang.org/x/tools/internal/analysisinternal"
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
2020-03-10 21:09:39 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/debug/tag"
|
2019-10-04 15:18:43 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/memoize"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-06 16:47:00 -07:00
|
|
|
)
|
|
|
|
|
2020-03-31 21:53:42 -06:00
|
|
|
func (s *snapshot) Analyze(ctx context.Context, id string, analyzers ...*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 {
|
2020-01-30 18:52:23 -07:00
|
|
|
ah, err := s.actionHandle(ctx, packageID(id), a)
|
2019-10-04 15:18:43 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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-24 16:15:23 -06:00
|
|
|
var results []*source.Error
|
2019-10-04 15:18:43 -06:00
|
|
|
for _, ah := range roots {
|
|
|
|
diagnostics, _, err := ah.analyze(ctx)
|
|
|
|
if err != nil {
|
2019-12-05 14:37:47 -07:00
|
|
|
return nil, err
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
2019-10-24 16:15:23 -06:00
|
|
|
results = append(results, 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
|
|
|
}
|
|
|
|
|
2020-02-03 15:07:45 -07:00
|
|
|
type actionHandleKey string
|
|
|
|
|
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-31 15:40:18 -06:00
|
|
|
analyzer *analysis.Analyzer
|
|
|
|
pkg *pkg
|
2019-10-04 15:18:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type actionData struct {
|
2019-10-31 15:40:18 -06:00
|
|
|
diagnostics []*source.Error
|
|
|
|
result interface{}
|
|
|
|
objectFacts map[objectFactKey]analysis.Fact
|
|
|
|
packageFacts map[packageFactKey]analysis.Fact
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-30 18:52:23 -07:00
|
|
|
func (s *snapshot) actionHandle(ctx context.Context, id packageID, a *analysis.Analyzer) (*actionHandle, error) {
|
2020-02-07 12:38:36 -07:00
|
|
|
ph := s.getPackage(id, source.ParseFull)
|
2020-01-30 18:52:23 -07:00
|
|
|
if ph == nil {
|
|
|
|
return nil, errors.Errorf("no PackageHandle for %s", id)
|
|
|
|
}
|
|
|
|
act := s.getActionHandle(id, ph.mode, a)
|
2019-11-21 16:55:49 -07:00
|
|
|
if act != nil {
|
|
|
|
return act, nil
|
2019-10-14 14:13:06 -06:00
|
|
|
}
|
2019-11-29 23:17:57 -07:00
|
|
|
if len(ph.key) == 0 {
|
2020-01-14 16:29:21 -07:00
|
|
|
return nil, errors.Errorf("no key for PackageHandle %s", id)
|
2019-10-04 15:18:43 -06:00
|
|
|
}
|
2019-11-29 23:17:57 -07:00
|
|
|
pkg, err := ph.check(ctx)
|
2019-10-04 15:18:43 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-21 16:55:49 -07:00
|
|
|
act = &actionHandle{
|
2019-10-04 15:18:43 -06:00
|
|
|
analyzer: a,
|
|
|
|
pkg: pkg,
|
|
|
|
}
|
2019-10-31 15:40:18 -06:00
|
|
|
var deps []*actionHandle
|
2019-10-04 15:18:43 -06:00
|
|
|
// Add a dependency on each required analyzers.
|
|
|
|
for _, req := range a.Requires {
|
2020-01-30 18:52:23 -07:00
|
|
|
reqActionHandle, err := s.actionHandle(ctx, id, req)
|
2019-10-04 15:18:43 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-31 15:40:18 -06:00
|
|
|
deps = append(deps, reqActionHandle)
|
2019-10-04 15:18:43 -06:00
|
|
|
}
|
2019-11-21 18:17:28 -07:00
|
|
|
|
|
|
|
// TODO(golang/go#35089): Re-enable this when we doesn't use ParseExported
|
|
|
|
// mode for dependencies. In the meantime, disable analysis for dependencies,
|
|
|
|
// since we don't get anything useful out of it.
|
|
|
|
if false {
|
|
|
|
// An analysis that consumes/produces facts
|
|
|
|
// must run on the package's dependencies too.
|
|
|
|
if len(a.FactTypes) > 0 {
|
2019-11-29 23:17:57 -07:00
|
|
|
importIDs := make([]string, 0, len(ph.m.deps))
|
|
|
|
for _, importID := range ph.m.deps {
|
2019-11-21 18:17:28 -07:00
|
|
|
importIDs = append(importIDs, string(importID))
|
|
|
|
}
|
|
|
|
sort.Strings(importIDs) // for determinism
|
|
|
|
for _, importID := range importIDs {
|
2020-01-30 18:52:23 -07:00
|
|
|
depActionHandle, err := s.actionHandle(ctx, packageID(importID), a)
|
2019-11-21 18:17:28 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
deps = append(deps, depActionHandle)
|
2019-10-04 15:18:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 15:40:18 -06:00
|
|
|
|
|
|
|
fset := s.view.session.cache.fset
|
|
|
|
|
2019-11-29 23:17:57 -07:00
|
|
|
h := s.view.session.cache.store.Bind(buildActionKey(a, ph), func(ctx context.Context) interface{} {
|
2019-11-01 13:04:15 -06:00
|
|
|
// Analyze dependencies first.
|
2020-03-27 10:52:40 -06:00
|
|
|
results, err := execAll(ctx, deps)
|
2019-10-31 15:40:18 -06:00
|
|
|
if err != nil {
|
2019-11-01 13:04:15 -06:00
|
|
|
return &actionData{
|
|
|
|
err: err,
|
|
|
|
}
|
2019-10-31 15:40:18 -06:00
|
|
|
}
|
2019-11-04 12:35:09 -07:00
|
|
|
return runAnalysis(ctx, fset, a, pkg, results)
|
2019-10-04 15:18:43 -06:00
|
|
|
})
|
2019-11-21 16:55:49 -07:00
|
|
|
act.handle = h
|
2019-10-15 11:27:09 -06:00
|
|
|
|
2020-05-07 12:57:44 -06:00
|
|
|
act = s.addActionHandle(act)
|
2019-11-21 16:55:49 -07:00
|
|
|
return act, nil
|
2019-10-04 15:18:43 -06:00
|
|
|
}
|
|
|
|
|
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-12-05 14:37:47 -07:00
|
|
|
return nil, nil, ctx.Err()
|
2019-10-04 15:18:43 -06:00
|
|
|
}
|
2019-11-04 12:35:09 -07:00
|
|
|
data, ok := v.(*actionData)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, errors.Errorf("unexpected type for %s:%s", act.pkg.ID(), act.analyzer.Name)
|
|
|
|
}
|
2019-11-07 11:35:46 -07:00
|
|
|
if data == nil {
|
|
|
|
return nil, nil, errors.Errorf("unexpected nil analysis for %s:%s", act.pkg.ID(), act.analyzer.Name)
|
|
|
|
}
|
2019-10-04 15:18:43 -06:00
|
|
|
return data.diagnostics, data.result, data.err
|
|
|
|
}
|
|
|
|
|
2020-02-03 15:07:45 -07:00
|
|
|
func buildActionKey(a *analysis.Analyzer, ph *packageHandle) actionHandleKey {
|
|
|
|
return actionHandleKey(hashContents([]byte(fmt.Sprintf("%p %s", a, string(ph.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())
|
|
|
|
}
|
|
|
|
|
2020-03-27 10:52:40 -06:00
|
|
|
func execAll(ctx context.Context, actions []*actionHandle) (map[*actionHandle]*actionData, error) {
|
2019-10-31 15:40:18 -06:00
|
|
|
var mu sync.Mutex
|
|
|
|
results := make(map[*actionHandle]*actionData)
|
2019-10-04 15:18:43 -06:00
|
|
|
|
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-31 15:40:18 -06:00
|
|
|
v := act.handle.Get(ctx)
|
|
|
|
if v == nil {
|
|
|
|
return errors.Errorf("no analyses for %s", act.pkg.ID())
|
|
|
|
}
|
|
|
|
data, ok := v.(*actionData)
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("unexpected type for %s: %T", act, v)
|
2019-10-04 15:18:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
2019-10-31 15:40:18 -06:00
|
|
|
results[act] = data
|
2019-10-04 15:18:43 -06:00
|
|
|
|
|
|
|
return nil
|
2019-04-22 15:05:43 -06:00
|
|
|
})
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
2019-10-31 15:40:18 -06:00
|
|
|
return results, g.Wait()
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
|
2019-11-01 13:04:15 -06:00
|
|
|
func runAnalysis(ctx context.Context, fset *token.FileSet, analyzer *analysis.Analyzer, pkg *pkg, deps map[*actionHandle]*actionData) (data *actionData) {
|
|
|
|
data = &actionData{
|
|
|
|
objectFacts: make(map[objectFactKey]analysis.Fact),
|
|
|
|
packageFacts: make(map[packageFactKey]analysis.Fact),
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2020-04-20 10:14:12 -06:00
|
|
|
event.Log(ctx, fmt.Sprintf("analysis panicked: %s", r), tag.Package.Of(pkg.PkgPath()))
|
2020-01-09 16:18:27 -07:00
|
|
|
data.err = errors.Errorf("analysis %s for package %s panicked: %v", analyzer.Name, pkg.PkgPath(), r)
|
2019-11-01 13:04:15 -06:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
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{})
|
2019-10-31 15:40:18 -06:00
|
|
|
|
|
|
|
for depHandle, depData := range deps {
|
|
|
|
if depHandle.pkg == 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-31 15:40:18 -06:00
|
|
|
inputs[depHandle.analyzer] = depData.result
|
|
|
|
} else if depHandle.analyzer == 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.
|
2019-10-31 15:40:18 -06:00
|
|
|
for key, fact := range depData.objectFacts {
|
|
|
|
// Filter out facts related to objects
|
|
|
|
// that are irrelevant downstream
|
|
|
|
// (equivalently: not in the compiler export data).
|
|
|
|
if !exportedFrom(key.obj, depHandle.pkg.types) {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-07 11:35:46 -07:00
|
|
|
data.objectFacts[key] = fact
|
2019-10-31 15:40:18 -06:00
|
|
|
}
|
|
|
|
for key, fact := range depData.packageFacts {
|
|
|
|
// TODO: filter out facts that belong to
|
|
|
|
// packages not mentioned in the export data
|
|
|
|
// to prevent side channels.
|
|
|
|
|
2019-11-07 11:35:46 -07:00
|
|
|
data.packageFacts[key] = fact
|
2019-10-31 15:40:18 -06:00
|
|
|
}
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-31 15:40:18 -06:00
|
|
|
Analyzer: analyzer,
|
2019-10-24 13:44:41 -06:00
|
|
|
Fset: fset,
|
2019-10-31 15:40:18 -06:00
|
|
|
Files: pkg.GetSyntax(),
|
|
|
|
Pkg: pkg.GetTypes(),
|
|
|
|
TypesInfo: pkg.GetTypesInfo(),
|
|
|
|
TypesSizes: pkg.GetTypesSizes(),
|
2019-10-24 13:44:41 -06:00
|
|
|
ResultOf: inputs,
|
|
|
|
Report: func(d analysis.Diagnostic) {
|
|
|
|
// Prefix the diagnostic category with the analyzer's name.
|
|
|
|
if d.Category == "" {
|
2019-10-31 15:40:18 -06:00
|
|
|
d.Category = analyzer.Name
|
2019-10-24 13:44:41 -06:00
|
|
|
} else {
|
2019-10-31 15:40:18 -06:00
|
|
|
d.Category = analyzer.Name + "." + d.Category
|
2019-10-24 13:44:41 -06:00
|
|
|
}
|
|
|
|
diagnostics = append(diagnostics, &d)
|
|
|
|
},
|
2019-10-31 15:40:18 -06:00
|
|
|
ImportObjectFact: func(obj types.Object, ptr analysis.Fact) bool {
|
|
|
|
if obj == nil {
|
|
|
|
panic("nil object")
|
|
|
|
}
|
|
|
|
key := objectFactKey{obj, factType(ptr)}
|
|
|
|
|
2019-11-07 11:35:46 -07:00
|
|
|
if v, ok := data.objectFacts[key]; ok {
|
2019-10-31 15:40:18 -06:00
|
|
|
reflect.ValueOf(ptr).Elem().Set(reflect.ValueOf(v).Elem())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
ExportObjectFact: func(obj types.Object, fact analysis.Fact) {
|
|
|
|
if obj.Pkg() != pkg.types {
|
|
|
|
panic(fmt.Sprintf("internal error: in analysis %s of package %s: Fact.Set(%s, %T): can't set facts on objects belonging another package",
|
|
|
|
analyzer, pkg.ID(), obj, fact))
|
|
|
|
}
|
|
|
|
key := objectFactKey{obj, factType(fact)}
|
2019-11-07 11:35:46 -07:00
|
|
|
data.objectFacts[key] = fact // clobber any existing entry
|
2019-10-31 15:40:18 -06:00
|
|
|
},
|
|
|
|
ImportPackageFact: func(pkg *types.Package, ptr analysis.Fact) bool {
|
|
|
|
if pkg == nil {
|
|
|
|
panic("nil package")
|
|
|
|
}
|
|
|
|
key := packageFactKey{pkg, factType(ptr)}
|
2019-11-07 11:35:46 -07:00
|
|
|
if v, ok := data.packageFacts[key]; ok {
|
2019-10-31 15:40:18 -06:00
|
|
|
reflect.ValueOf(ptr).Elem().Set(reflect.ValueOf(v).Elem())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
ExportPackageFact: func(fact analysis.Fact) {
|
|
|
|
key := packageFactKey{pkg.types, factType(fact)}
|
2019-11-07 11:35:46 -07:00
|
|
|
data.packageFacts[key] = fact // clobber any existing entry
|
2019-10-31 15:40:18 -06:00
|
|
|
},
|
|
|
|
AllObjectFacts: func() []analysis.ObjectFact {
|
2019-11-07 11:35:46 -07:00
|
|
|
facts := make([]analysis.ObjectFact, 0, len(data.objectFacts))
|
|
|
|
for k := range data.objectFacts {
|
|
|
|
facts = append(facts, analysis.ObjectFact{Object: k.obj, Fact: data.objectFacts[k]})
|
2019-10-31 15:40:18 -06:00
|
|
|
}
|
|
|
|
return facts
|
|
|
|
},
|
|
|
|
AllPackageFacts: func() []analysis.PackageFact {
|
2019-11-07 11:35:46 -07:00
|
|
|
facts := make([]analysis.PackageFact, 0, len(data.packageFacts))
|
|
|
|
for k := range data.packageFacts {
|
|
|
|
facts = append(facts, analysis.PackageFact{Package: k.pkg, Fact: data.packageFacts[k]})
|
2019-10-31 15:40:18 -06:00
|
|
|
}
|
|
|
|
return facts
|
|
|
|
},
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
2020-03-10 08:14:56 -06:00
|
|
|
analysisinternal.SetTypeErrors(pass, pkg.typeErrors)
|
2019-02-06 16:47:00 -07:00
|
|
|
|
2019-10-31 15:40:18 -06:00
|
|
|
if pkg.IsIllTyped() {
|
2019-11-01 13:04:15 -06:00
|
|
|
data.err = errors.Errorf("analysis skipped due to errors in package: %v", pkg.GetErrors())
|
|
|
|
return data
|
2019-10-21 15:25:09 -06:00
|
|
|
}
|
2019-11-01 13:04:15 -06:00
|
|
|
data.result, data.err = pass.Analyzer.Run(pass)
|
2020-01-28 18:07:25 -07:00
|
|
|
if data.err != nil {
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := reflect.TypeOf(data.result), pass.Analyzer.ResultType; got != want {
|
|
|
|
data.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)
|
|
|
|
return data
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// disallow calls after Run
|
2019-10-24 09:30:18 -06:00
|
|
|
pass.ExportObjectFact = func(obj types.Object, fact analysis.Fact) {
|
2019-10-31 15:40:18 -06:00
|
|
|
panic(fmt.Sprintf("%s:%s: Pass.ExportObjectFact(%s, %T) called after Run", analyzer.Name, pkg.PkgPath(), obj, fact))
|
2019-10-24 09:30:18 -06:00
|
|
|
}
|
|
|
|
pass.ExportPackageFact = func(fact analysis.Fact) {
|
2019-10-31 15:40:18 -06:00
|
|
|
panic(fmt.Sprintf("%s:%s: Pass.ExportPackageFact(%T) called after Run", analyzer.Name, pkg.PkgPath(), fact))
|
2019-10-24 09:30:18 -06:00
|
|
|
}
|
2019-04-23 12:36:30 -06:00
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
for _, diag := range diagnostics {
|
2019-10-31 15:40:18 -06:00
|
|
|
srcErr, err := sourceError(ctx, fset, pkg, diag)
|
2019-10-21 15:25:09 -06:00
|
|
|
if err != nil {
|
2020-03-10 21:25:10 -06:00
|
|
|
event.Error(ctx, "unable to compute analysis error position", err, tag.Category.Of(diag.Category), tag.Package.Of(pkg.ID()))
|
2020-01-28 18:07:25 -07:00
|
|
|
continue
|
2019-10-21 15:25:09 -06:00
|
|
|
}
|
2020-03-30 10:45:15 -06:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
data.err = ctx.Err()
|
|
|
|
return data
|
|
|
|
}
|
2019-11-01 13:04:15 -06:00
|
|
|
data.diagnostics = append(data.diagnostics, srcErr)
|
2019-10-21 15:25:09 -06:00
|
|
|
}
|
2019-11-01 13:04:15 -06:00
|
|
|
return data
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|