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"
|
|
|
|
|
2019-09-24 14:28:59 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-03-06 14:33:47 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-09-09 18:22:42 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
errors "golang.org/x/xerrors"
|
2019-03-06 14:33:47 -07:00
|
|
|
)
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
// pkg contains the type information needed by the source package.
|
|
|
|
type pkg struct {
|
2019-06-11 15:09:26 -06:00
|
|
|
// ID and package path have their own types to avoid being used interchangeably.
|
2019-10-15 12:47:05 -06:00
|
|
|
id packageID
|
|
|
|
pkgPath packagePath
|
|
|
|
mode source.ParseMode
|
|
|
|
|
2019-11-21 12:54:31 -07:00
|
|
|
goFiles []source.ParseGoHandle
|
2019-11-20 14:15:00 -07:00
|
|
|
compiledGoFiles []source.ParseGoHandle
|
|
|
|
errors []*source.Error
|
|
|
|
imports map[packagePath]*pkg
|
|
|
|
types *types.Package
|
|
|
|
typesInfo *types.Info
|
|
|
|
typesSizes types.Sizes
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
|
|
|
|
2019-09-19 10:39:03 -06:00
|
|
|
// Declare explicit types for package paths and IDs to ensure that we never use
|
|
|
|
// an ID where a path belongs, and vice versa. If we confused the two, it would
|
|
|
|
// result in confusing errors because package IDs often look like package paths.
|
2019-06-11 15:09:26 -06:00
|
|
|
type packageID string
|
|
|
|
type packagePath string
|
|
|
|
|
2019-12-27 14:44:33 -07:00
|
|
|
// Declare explicit types for files and directories to distinguish between the two.
|
|
|
|
type fileURI span.URI
|
|
|
|
type directoryURI span.URI
|
2020-01-23 23:22:47 -07:00
|
|
|
type viewLoadScope span.URI
|
2019-12-27 14:44:33 -07:00
|
|
|
|
2019-10-02 12:45:55 -06:00
|
|
|
func (p *pkg) ID() string {
|
|
|
|
return string(p.id)
|
2019-06-11 16:06:27 -06:00
|
|
|
}
|
|
|
|
|
2019-10-02 12:45:55 -06:00
|
|
|
func (p *pkg) PkgPath() string {
|
|
|
|
return string(p.pkgPath)
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
|
|
|
|
2019-11-20 14:15:00 -07:00
|
|
|
func (p *pkg) CompiledGoFiles() []source.ParseGoHandle {
|
|
|
|
return p.compiledGoFiles
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
|
|
|
|
2019-10-02 12:45:55 -06:00
|
|
|
func (p *pkg) File(uri span.URI) (source.ParseGoHandle, error) {
|
2019-11-21 12:54:31 -07:00
|
|
|
for _, ph := range p.compiledGoFiles {
|
|
|
|
if ph.File().Identity().URI == uri {
|
|
|
|
return ph, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, ph := range p.goFiles {
|
2019-09-17 09:19:11 -06:00
|
|
|
if ph.File().Identity().URI == uri {
|
|
|
|
return ph, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errors.Errorf("no ParseGoHandle for %s", uri)
|
|
|
|
}
|
|
|
|
|
2019-10-24 13:44:41 -06:00
|
|
|
func (p *pkg) GetSyntax() []*ast.File {
|
2019-06-21 15:00:02 -06:00
|
|
|
var syntax []*ast.File
|
2019-11-20 14:15:00 -07:00
|
|
|
for _, ph := range p.compiledGoFiles {
|
2019-10-24 13:44:41 -06:00
|
|
|
file, _, _, err := ph.Cached()
|
2019-09-17 09:19:11 -06:00
|
|
|
if err == nil {
|
2019-07-11 19:05:55 -06:00
|
|
|
syntax = append(syntax, file)
|
2019-06-21 15:00:02 -06:00
|
|
|
}
|
2019-06-05 15:44:09 -06:00
|
|
|
}
|
|
|
|
return syntax
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
func (p *pkg) GetErrors() []*source.Error {
|
2019-10-02 12:45:55 -06:00
|
|
|
return p.errors
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
|
|
|
|
2019-10-02 12:45:55 -06:00
|
|
|
func (p *pkg) GetTypes() *types.Package {
|
|
|
|
return p.types
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
|
|
|
|
2019-10-02 12:45:55 -06:00
|
|
|
func (p *pkg) GetTypesInfo() *types.Info {
|
|
|
|
return p.typesInfo
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
2019-03-11 15:14:55 -06:00
|
|
|
|
2019-10-02 12:45:55 -06:00
|
|
|
func (p *pkg) GetTypesSizes() types.Sizes {
|
|
|
|
return p.typesSizes
|
2019-03-29 14:39:22 -06:00
|
|
|
}
|
|
|
|
|
2019-10-02 12:45:55 -06:00
|
|
|
func (p *pkg) IsIllTyped() bool {
|
|
|
|
return p.types == nil || p.typesInfo == nil || p.typesSizes == nil
|
2019-03-11 15:14:55 -06:00
|
|
|
}
|
2019-05-10 08:32:25 -06:00
|
|
|
|
2019-10-29 16:13:19 -06:00
|
|
|
func (p *pkg) GetImport(pkgPath string) (source.Package, error) {
|
2019-10-02 12:45:55 -06:00
|
|
|
if imp := p.imports[packagePath(pkgPath)]; imp != nil {
|
2019-09-27 11:17:59 -06:00
|
|
|
return imp, nil
|
|
|
|
}
|
|
|
|
// Don't return a nil pointer because that still satisfies the interface.
|
|
|
|
return nil, errors.Errorf("no imported package for %s", pkgPath)
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:13:19 -06:00
|
|
|
func (p *pkg) Imports() []source.Package {
|
|
|
|
var result []source.Package
|
|
|
|
for _, imp := range p.imports {
|
|
|
|
result = append(result, imp)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:55:49 -07:00
|
|
|
func (s *snapshot) FindAnalysisError(ctx context.Context, pkgID, analyzerName, msg string, rng protocol.Range) (*source.Error, error) {
|
|
|
|
analyzer, ok := s.View().Options().Analyzers[analyzerName]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("unexpected analyzer: %s", analyzerName)
|
2019-11-15 12:47:29 -07:00
|
|
|
}
|
2019-11-21 16:55:49 -07:00
|
|
|
act, err := s.actionHandle(ctx, packageID(pkgID), source.ParseFull, analyzer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
errs, _, err := act.analyze(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, err := range errs {
|
|
|
|
if err.Category != analyzerName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err.Message != msg {
|
|
|
|
continue
|
2019-09-24 14:28:59 -06:00
|
|
|
}
|
2019-11-21 16:55:49 -07:00
|
|
|
if protocol.CompareRange(err.Range, rng) != 0 {
|
|
|
|
continue
|
2019-09-24 14:28:59 -06:00
|
|
|
}
|
2019-11-21 16:55:49 -07:00
|
|
|
return err, nil
|
2019-09-04 11:16:09 -06:00
|
|
|
}
|
2019-11-21 16:55:49 -07:00
|
|
|
return nil, errors.Errorf("no matching diagnostic for %s:%v", pkgID, analyzerName)
|
2019-06-18 16:25:22 -06:00
|
|
|
}
|