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-07-11 19:05:55 -06:00
|
|
|
files []source.ParseGoHandle
|
2019-10-21 15:25:09 -06:00
|
|
|
errors []*source.Error
|
2019-06-11 15:09:26 -06:00
|
|
|
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-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-10-02 12:45:55 -06:00
|
|
|
func (p *pkg) Files() []source.ParseGoHandle {
|
|
|
|
return p.files
|
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) {
|
|
|
|
for _, ph := range p.Files() {
|
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-10-02 12:45:55 -06:00
|
|
|
for _, ph := range p.files {
|
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-10-24 13:44:41 -06:00
|
|
|
func (s *snapshot) FindAnalysisError(ctx context.Context, id string, diag protocol.Diagnostic) (*source.Error, error) {
|
|
|
|
acts := s.getActionHandles(packageID(id), source.ParseFull)
|
|
|
|
for _, act := range acts {
|
|
|
|
errors, _, err := act.analyze(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-09-24 14:28:59 -06:00
|
|
|
}
|
2019-10-24 13:44:41 -06:00
|
|
|
for _, err := range errors {
|
|
|
|
if err.Category != diag.Source {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err.Message != diag.Message {
|
2019-09-24 14:28:59 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-10-24 13:44:41 -06:00
|
|
|
if protocol.CompareRange(err.Range, diag.Range) != 0 {
|
2019-09-24 14:28:59 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-10-24 13:44:41 -06:00
|
|
|
return err, nil
|
2019-09-24 14:28:59 -06:00
|
|
|
}
|
2019-09-04 11:16:09 -06:00
|
|
|
}
|
2019-10-24 13:44:41 -06:00
|
|
|
return nil, errors.Errorf("no matching diagnostic for %v", diag)
|
2019-06-18 16:25:22 -06:00
|
|
|
}
|
2019-09-09 18:22:42 -06:00
|
|
|
|
2019-10-29 16:13:19 -06:00
|
|
|
func findFileInPackage(ctx context.Context, uri span.URI, pkg source.Package) (source.ParseGoHandle, source.Package, error) {
|
|
|
|
queue := []source.Package{pkg}
|
2019-09-09 18:22:42 -06:00
|
|
|
seen := make(map[string]bool)
|
|
|
|
|
|
|
|
for len(queue) > 0 {
|
|
|
|
pkg := queue[0]
|
|
|
|
queue = queue[1:]
|
|
|
|
seen[pkg.ID()] = true
|
|
|
|
|
2019-10-29 16:13:19 -06:00
|
|
|
for _, ph := range pkg.Files() {
|
2019-09-09 18:22:42 -06:00
|
|
|
if ph.File().Identity().URI == uri {
|
2019-09-16 16:17:51 -06:00
|
|
|
return ph, pkg, nil
|
2019-09-09 18:22:42 -06:00
|
|
|
}
|
|
|
|
}
|
2019-10-29 16:13:19 -06:00
|
|
|
for _, dep := range pkg.Imports() {
|
2019-09-09 18:22:42 -06:00
|
|
|
if !seen[dep.ID()] {
|
|
|
|
queue = append(queue, dep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 16:17:51 -06:00
|
|
|
return nil, nil, errors.Errorf("no file for %s", uri)
|
2019-09-09 18:22:42 -06:00
|
|
|
}
|