2019-05-10 14:35:43 -06: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-03-06 14:33:47 -07:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"go/ast"
|
|
|
|
"go/types"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/analysis"
|
|
|
|
"golang.org/x/tools/go/packages"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
)
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
// pkg contains the type information needed by the source package.
|
|
|
|
type pkg struct {
|
2019-03-06 14:33:47 -07:00
|
|
|
id, pkgPath string
|
|
|
|
files []string
|
|
|
|
syntax []*ast.File
|
|
|
|
errors []packages.Error
|
2019-05-14 21:04:23 -06:00
|
|
|
imports map[string]*pkg
|
2019-03-06 14:33:47 -07:00
|
|
|
types *types.Package
|
|
|
|
typesInfo *types.Info
|
2019-03-29 14:39:22 -06:00
|
|
|
typesSizes types.Sizes
|
2019-03-06 14:33:47 -07:00
|
|
|
|
|
|
|
// The analysis cache holds analysis information for all the packages in a view.
|
|
|
|
// Each graph node (action) is one unit of analysis.
|
|
|
|
// Edges express package-to-package (vertical) dependencies,
|
|
|
|
// and analysis-to-analysis (horizontal) dependencies.
|
|
|
|
mu sync.Mutex
|
|
|
|
analyses map[*analysis.Analyzer]*analysisEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
type analysisEntry struct {
|
2019-04-25 14:26:22 -06:00
|
|
|
done chan struct{}
|
|
|
|
succeeded bool
|
2019-03-06 14:33:47 -07:00
|
|
|
*source.Action
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
func (pkg *pkg) GetActionGraph(ctx context.Context, a *analysis.Analyzer) (*source.Action, error) {
|
2019-03-06 14:33:47 -07:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg.mu.Lock()
|
|
|
|
e, ok := pkg.analyses[a]
|
|
|
|
if ok {
|
|
|
|
// cache hit
|
|
|
|
pkg.mu.Unlock()
|
|
|
|
|
|
|
|
// wait for entry to become ready or the context to be cancelled
|
|
|
|
select {
|
2019-04-25 14:26:22 -06:00
|
|
|
case <-e.done:
|
|
|
|
// If the goroutine we are waiting on was cancelled, we should retry.
|
|
|
|
// If errors other than cancelation/timeout become possible, it may
|
|
|
|
// no longer be appropriate to always retry here.
|
|
|
|
if !e.succeeded {
|
|
|
|
return pkg.GetActionGraph(ctx, a)
|
|
|
|
}
|
2019-03-06 14:33:47 -07:00
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// cache miss
|
|
|
|
e = &analysisEntry{
|
2019-04-25 14:26:22 -06:00
|
|
|
done: make(chan struct{}),
|
2019-03-06 14:33:47 -07:00
|
|
|
Action: &source.Action{
|
|
|
|
Analyzer: a,
|
|
|
|
Pkg: pkg,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
pkg.analyses[a] = e
|
|
|
|
pkg.mu.Unlock()
|
|
|
|
|
2019-04-25 14:26:22 -06:00
|
|
|
defer func() {
|
|
|
|
// If we got an error, clear out our defunct cache entry. We don't cache
|
|
|
|
// errors since they could depend on our dependencies, which can change.
|
|
|
|
// Currently the only possible error is context.Canceled, though, which
|
|
|
|
// should also not be cached.
|
|
|
|
if !e.succeeded {
|
|
|
|
pkg.mu.Lock()
|
|
|
|
delete(pkg.analyses, a)
|
|
|
|
pkg.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always close done so waiters don't get stuck.
|
|
|
|
close(e.done)
|
|
|
|
}()
|
|
|
|
|
2019-03-06 14:33:47 -07:00
|
|
|
// This goroutine becomes responsible for populating
|
|
|
|
// the entry and broadcasting its readiness.
|
|
|
|
|
|
|
|
// Add a dependency on each required analyzers.
|
|
|
|
for _, req := range a.Requires {
|
|
|
|
act, err := pkg.GetActionGraph(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
e.Deps = append(e.Deps, act)
|
|
|
|
}
|
|
|
|
|
|
|
|
// An analysis that consumes/produces facts
|
|
|
|
// must run on the package's dependencies too.
|
|
|
|
if len(a.FactTypes) > 0 {
|
|
|
|
importPaths := make([]string, 0, len(pkg.imports))
|
|
|
|
for importPath := range pkg.imports {
|
|
|
|
importPaths = append(importPaths, importPath)
|
|
|
|
}
|
|
|
|
sort.Strings(importPaths) // for determinism
|
|
|
|
for _, importPath := range importPaths {
|
2019-04-26 22:00:15 -06:00
|
|
|
dep, ok := pkg.imports[importPath]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2019-03-06 14:33:47 -07:00
|
|
|
act, err := dep.GetActionGraph(ctx, a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
e.Deps = append(e.Deps, act)
|
|
|
|
}
|
|
|
|
}
|
2019-04-25 14:26:22 -06:00
|
|
|
e.succeeded = true
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
|
|
|
return e.Action, nil
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
func (pkg *pkg) PkgPath() string {
|
2019-05-01 20:46:07 -06:00
|
|
|
return pkg.pkgPath
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
func (pkg *pkg) GetFilenames() []string {
|
2019-03-06 14:33:47 -07:00
|
|
|
return pkg.files
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
func (pkg *pkg) GetSyntax() []*ast.File {
|
2019-03-06 14:33:47 -07:00
|
|
|
return pkg.syntax
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
func (pkg *pkg) GetErrors() []packages.Error {
|
2019-03-06 14:33:47 -07:00
|
|
|
return pkg.errors
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
func (pkg *pkg) GetTypes() *types.Package {
|
2019-03-06 14:33:47 -07:00
|
|
|
return pkg.types
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
func (pkg *pkg) GetTypesInfo() *types.Info {
|
2019-03-06 14:33:47 -07:00
|
|
|
return pkg.typesInfo
|
|
|
|
}
|
2019-03-11 15:14:55 -06:00
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
func (pkg *pkg) GetTypesSizes() types.Sizes {
|
2019-03-29 14:39:22 -06:00
|
|
|
return pkg.typesSizes
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
func (pkg *pkg) IsIllTyped() bool {
|
2019-03-11 15:14:55 -06:00
|
|
|
return pkg.types == nil && pkg.typesInfo == nil
|
|
|
|
}
|
2019-05-10 08:32:25 -06:00
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
func (pkg *pkg) GetImport(pkgPath string) source.Package {
|
2019-05-31 21:34:02 -06:00
|
|
|
if imp := pkg.imports[pkgPath]; imp != nil {
|
|
|
|
return imp
|
|
|
|
}
|
|
|
|
// Don't return a nil pointer because that still satisfies the interface.
|
|
|
|
return nil
|
2019-05-10 08:32:25 -06:00
|
|
|
}
|