2018-11-12 12:15:47 -07:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
2018-12-18 13:46:14 -07:00
|
|
|
"bytes"
|
2018-11-12 12:15:47 -07:00
|
|
|
"context"
|
2018-12-18 13:46:14 -07:00
|
|
|
"fmt"
|
2019-05-17 11:45:50 -06:00
|
|
|
"strings"
|
2019-02-07 15:27:10 -07:00
|
|
|
|
2019-02-06 16:47:00 -07:00
|
|
|
"golang.org/x/tools/go/analysis"
|
2019-02-07 15:27:10 -07:00
|
|
|
"golang.org/x/tools/go/analysis/passes/asmdecl"
|
|
|
|
"golang.org/x/tools/go/analysis/passes/assign"
|
|
|
|
"golang.org/x/tools/go/analysis/passes/atomic"
|
|
|
|
"golang.org/x/tools/go/analysis/passes/atomicalign"
|
|
|
|
"golang.org/x/tools/go/analysis/passes/bools"
|
|
|
|
"golang.org/x/tools/go/analysis/passes/buildtag"
|
|
|
|
"golang.org/x/tools/go/analysis/passes/cgocall"
|
|
|
|
"golang.org/x/tools/go/analysis/passes/composite"
|
|
|
|
"golang.org/x/tools/go/analysis/passes/copylock"
|
|
|
|
"golang.org/x/tools/go/analysis/passes/httpresponse"
|
|
|
|
"golang.org/x/tools/go/analysis/passes/loopclosure"
|
2019-02-06 16:47:00 -07:00
|
|
|
"golang.org/x/tools/go/analysis/passes/lostcancel"
|
2019-02-07 15:27:10 -07:00
|
|
|
"golang.org/x/tools/go/analysis/passes/nilfunc"
|
2019-02-06 16:47:00 -07:00
|
|
|
"golang.org/x/tools/go/analysis/passes/printf"
|
2019-02-07 15:27:10 -07:00
|
|
|
"golang.org/x/tools/go/analysis/passes/shift"
|
|
|
|
"golang.org/x/tools/go/analysis/passes/stdmethods"
|
|
|
|
"golang.org/x/tools/go/analysis/passes/structtag"
|
2019-02-06 16:47:00 -07:00
|
|
|
"golang.org/x/tools/go/analysis/passes/tests"
|
2019-02-07 15:27:10 -07:00
|
|
|
"golang.org/x/tools/go/analysis/passes/unmarshal"
|
|
|
|
"golang.org/x/tools/go/analysis/passes/unreachable"
|
|
|
|
"golang.org/x/tools/go/analysis/passes/unsafeptr"
|
|
|
|
"golang.org/x/tools/go/analysis/passes/unusedresult"
|
2018-11-12 12:15:47 -07:00
|
|
|
"golang.org/x/tools/go/packages"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2018-11-12 12:15:47 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Diagnostic struct {
|
2019-02-19 19:11:15 -07:00
|
|
|
span.Span
|
2019-02-27 16:08:04 -07:00
|
|
|
Message string
|
|
|
|
Source string
|
|
|
|
Severity DiagnosticSeverity
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
|
|
|
|
2019-02-27 16:08:04 -07:00
|
|
|
type DiagnosticSeverity int
|
|
|
|
|
|
|
|
const (
|
|
|
|
SeverityWarning DiagnosticSeverity = iota
|
|
|
|
SeverityError
|
|
|
|
)
|
|
|
|
|
2019-05-23 13:03:11 -06:00
|
|
|
func Diagnostics(ctx context.Context, v View, f GoFile) (map[span.URI][]Diagnostic, error) {
|
|
|
|
pkg := f.GetPackage(ctx)
|
2019-03-13 17:31:41 -06:00
|
|
|
if pkg == nil {
|
2019-05-23 13:03:11 -06:00
|
|
|
return singleDiagnostic(f.URI(), "%s is not part of a package", f.URI()), nil
|
2019-03-13 17:31:41 -06:00
|
|
|
}
|
2019-05-29 12:59:35 -06:00
|
|
|
// Prepare the reports we will send for the files in this package.
|
2019-02-19 19:11:15 -07:00
|
|
|
reports := make(map[span.URI][]Diagnostic)
|
2019-03-06 14:33:47 -07:00
|
|
|
for _, filename := range pkg.GetFilenames() {
|
2019-05-15 15:58:16 -06:00
|
|
|
uri := span.FileURI(filename)
|
|
|
|
if v.Ignore(uri) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
reports[uri] = []Diagnostic{}
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
2019-05-17 11:45:50 -06:00
|
|
|
|
2019-05-29 12:59:35 -06:00
|
|
|
// Prepare any additional reports for the errors in this package.
|
2019-05-17 11:45:50 -06:00
|
|
|
for _, pkgErr := range pkg.GetErrors() {
|
|
|
|
reports[packageErrorSpan(pkgErr).URI()] = []Diagnostic{}
|
|
|
|
}
|
|
|
|
|
2019-05-01 20:46:07 -06:00
|
|
|
// Run diagnostics for the package that this URI belongs to.
|
|
|
|
if !diagnostics(ctx, v, pkg, reports) {
|
|
|
|
// If we don't have any list, parse, or type errors, run analyses.
|
|
|
|
if err := analyses(ctx, v, pkg, reports); err != nil {
|
2019-05-23 13:03:11 -06:00
|
|
|
v.Session().Logger().Errorf(ctx, "failed to run analyses for %s: %v", f.URI(), err)
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Updates to the diagnostics for this package may need to be propagated.
|
2019-05-23 13:03:11 -06:00
|
|
|
for _, f := range f.GetActiveReverseDeps(ctx) {
|
2019-05-01 20:46:07 -06:00
|
|
|
pkg := f.GetPackage(ctx)
|
2019-05-14 08:22:10 -06:00
|
|
|
if pkg == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-05-01 20:46:07 -06:00
|
|
|
for _, filename := range pkg.GetFilenames() {
|
|
|
|
reports[span.FileURI(filename)] = []Diagnostic{}
|
|
|
|
}
|
|
|
|
diagnostics(ctx, v, pkg, reports)
|
|
|
|
}
|
|
|
|
return reports, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func diagnostics(ctx context.Context, v View, pkg Package, reports map[span.URI][]Diagnostic) bool {
|
2019-03-11 15:14:55 -06:00
|
|
|
var listErrors, parseErrors, typeErrors []packages.Error
|
2019-03-06 14:33:47 -07:00
|
|
|
for _, err := range pkg.GetErrors() {
|
2018-11-12 12:15:47 -07:00
|
|
|
switch err.Kind {
|
|
|
|
case packages.ParseError:
|
|
|
|
parseErrors = append(parseErrors, err)
|
|
|
|
case packages.TypeError:
|
|
|
|
typeErrors = append(typeErrors, err)
|
|
|
|
default:
|
2019-03-11 15:14:55 -06:00
|
|
|
listErrors = append(listErrors, err)
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
|
|
|
}
|
2019-03-11 15:14:55 -06:00
|
|
|
// Don't report type errors if there are parse errors or list errors.
|
2018-11-12 12:15:47 -07:00
|
|
|
diags := typeErrors
|
|
|
|
if len(parseErrors) > 0 {
|
|
|
|
diags = parseErrors
|
2019-03-11 15:14:55 -06:00
|
|
|
} else if len(listErrors) > 0 {
|
|
|
|
diags = listErrors
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
|
|
|
for _, diag := range diags {
|
2019-05-17 11:45:50 -06:00
|
|
|
spn := packageErrorSpan(diag)
|
2019-02-19 19:11:15 -07:00
|
|
|
if spn.IsPoint() && diag.Kind == packages.TypeError {
|
2019-04-01 18:08:14 -06:00
|
|
|
spn = pointToSpan(ctx, v, spn)
|
2018-12-20 15:31:55 -07:00
|
|
|
}
|
2018-11-12 12:15:47 -07:00
|
|
|
diagnostic := Diagnostic{
|
2019-04-16 18:27:09 -06:00
|
|
|
Source: "LSP",
|
2019-02-19 19:11:15 -07:00
|
|
|
Span: spn,
|
2019-02-27 16:08:04 -07:00
|
|
|
Message: diag.Msg,
|
|
|
|
Severity: SeverityError,
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
if _, ok := reports[spn.URI()]; ok {
|
|
|
|
reports[spn.URI()] = append(reports[spn.URI()], diagnostic)
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
|
|
|
}
|
2019-05-01 20:46:07 -06:00
|
|
|
// Returns true if we've sent non-empty diagnostics.
|
|
|
|
return len(diags) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func analyses(ctx context.Context, v View, pkg Package, reports map[span.URI][]Diagnostic) error {
|
2019-02-06 09:24:10 -07:00
|
|
|
// Type checking and parsing succeeded. Run analyses.
|
2019-04-25 13:50:50 -06:00
|
|
|
if err := runAnalyses(ctx, v, pkg, func(a *analysis.Analyzer, diag analysis.Diagnostic) error {
|
2019-05-28 14:31:28 -06:00
|
|
|
r := span.NewRange(v.Session().Cache().FileSet(), diag.Pos, diag.End)
|
2019-03-15 11:19:43 -06:00
|
|
|
s, err := r.Span()
|
|
|
|
if err != nil {
|
2019-04-22 15:05:43 -06:00
|
|
|
// The diagnostic has an invalid position, so we don't have a valid span.
|
|
|
|
return err
|
2019-03-15 11:19:43 -06:00
|
|
|
}
|
2019-02-06 09:24:10 -07:00
|
|
|
category := a.Name
|
|
|
|
if diag.Category != "" {
|
|
|
|
category += "." + category
|
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
reports[s.URI()] = append(reports[s.URI()], Diagnostic{
|
2019-02-27 16:08:04 -07:00
|
|
|
Source: category,
|
2019-02-19 19:11:15 -07:00
|
|
|
Span: s,
|
2019-04-16 13:03:58 -06:00
|
|
|
Message: diag.Message,
|
2019-02-27 16:08:04 -07:00
|
|
|
Severity: SeverityWarning,
|
2019-02-06 09:24:10 -07:00
|
|
|
})
|
2019-04-22 15:05:43 -06:00
|
|
|
return nil
|
2019-04-25 13:50:50 -06:00
|
|
|
}); err != nil {
|
2019-05-01 20:46:07 -06:00
|
|
|
return err
|
2019-04-25 13:50:50 -06:00
|
|
|
}
|
2019-05-01 20:46:07 -06:00
|
|
|
return nil
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
|
|
|
|
2019-05-17 11:45:50 -06:00
|
|
|
// parseDiagnosticMessage attempts to parse a standard error message by stripping off the trailing error message.
|
|
|
|
// Works only on errors where the message is prefixed by ": ".
|
|
|
|
// e.g.:
|
|
|
|
// attributes.go:13:1: expected 'package', found 'type'
|
|
|
|
func parseDiagnosticMessage(input string) span.Span {
|
|
|
|
input = strings.TrimSpace(input)
|
|
|
|
|
|
|
|
msgIndex := strings.Index(input, ": ")
|
|
|
|
if msgIndex < 0 {
|
|
|
|
return span.Parse(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
return span.Parse(input[:msgIndex])
|
|
|
|
}
|
|
|
|
|
|
|
|
func packageErrorSpan(pkgErr packages.Error) span.Span {
|
|
|
|
if pkgErr.Pos == "" {
|
|
|
|
return parseDiagnosticMessage(pkgErr.Msg)
|
|
|
|
}
|
|
|
|
return span.Parse(pkgErr.Pos)
|
|
|
|
}
|
|
|
|
|
2019-04-01 18:08:14 -06:00
|
|
|
func pointToSpan(ctx context.Context, v View, spn span.Span) span.Span {
|
|
|
|
// Don't set a range if it's anything other than a type error.
|
2019-05-03 22:04:18 -06:00
|
|
|
f, err := v.GetFile(ctx, spn.URI())
|
2019-04-01 18:08:14 -06:00
|
|
|
if err != nil {
|
2019-05-15 10:24:49 -06:00
|
|
|
v.Session().Logger().Errorf(ctx, "Could find file for diagnostic: %v", spn.URI())
|
2019-04-01 18:08:14 -06:00
|
|
|
return spn
|
|
|
|
}
|
2019-05-03 22:04:18 -06:00
|
|
|
diagFile, ok := f.(GoFile)
|
|
|
|
if !ok {
|
2019-05-15 10:24:49 -06:00
|
|
|
v.Session().Logger().Errorf(ctx, "Not a go file: %v", spn.URI())
|
2019-05-03 22:04:18 -06:00
|
|
|
return spn
|
|
|
|
}
|
2019-04-01 18:08:14 -06:00
|
|
|
tok := diagFile.GetToken(ctx)
|
|
|
|
if tok == nil {
|
2019-05-15 10:24:49 -06:00
|
|
|
v.Session().Logger().Errorf(ctx, "Could not find tokens for diagnostic: %v", spn.URI())
|
2019-04-01 18:08:14 -06:00
|
|
|
return spn
|
|
|
|
}
|
2019-05-17 10:15:22 -06:00
|
|
|
fc := diagFile.Content(ctx)
|
|
|
|
if fc.Error != nil {
|
2019-05-15 10:24:49 -06:00
|
|
|
v.Session().Logger().Errorf(ctx, "Could not find content for diagnostic: %v", spn.URI())
|
2019-04-01 18:08:14 -06:00
|
|
|
return spn
|
|
|
|
}
|
2019-05-17 08:51:19 -06:00
|
|
|
c := span.NewTokenConverter(diagFile.FileSet(), tok)
|
2019-04-01 18:08:14 -06:00
|
|
|
s, err := spn.WithOffset(c)
|
|
|
|
//we just don't bother producing an error if this failed
|
|
|
|
if err != nil {
|
2019-05-15 10:24:49 -06:00
|
|
|
v.Session().Logger().Errorf(ctx, "invalid span for diagnostic: %v: %v", spn.URI(), err)
|
2019-04-01 18:08:14 -06:00
|
|
|
return spn
|
|
|
|
}
|
|
|
|
start := s.Start()
|
|
|
|
offset := start.Offset()
|
2019-05-17 10:15:22 -06:00
|
|
|
width := bytes.IndexAny(fc.Data[offset:], " \n,():;[]")
|
2019-04-01 18:08:14 -06:00
|
|
|
if width <= 0 {
|
|
|
|
return spn
|
|
|
|
}
|
|
|
|
return span.New(spn.URI(), start, span.NewPoint(start.Line(), start.Column()+width, offset+width))
|
|
|
|
}
|
|
|
|
|
2019-03-29 14:14:24 -06:00
|
|
|
func singleDiagnostic(uri span.URI, format string, a ...interface{}) map[span.URI][]Diagnostic {
|
|
|
|
return map[span.URI][]Diagnostic{
|
|
|
|
uri: []Diagnostic{{
|
|
|
|
Source: "LSP",
|
|
|
|
Span: span.New(uri, span.Point{}, span.Point{}),
|
|
|
|
Message: fmt.Sprintf(format, a...),
|
|
|
|
Severity: SeverityError,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 15:05:43 -06:00
|
|
|
func runAnalyses(ctx context.Context, v View, pkg Package, report func(a *analysis.Analyzer, diag analysis.Diagnostic) error) error {
|
|
|
|
// The traditional vet suite:
|
2019-02-06 09:24:10 -07:00
|
|
|
analyzers := []*analysis.Analyzer{
|
2019-02-07 15:27:10 -07:00
|
|
|
asmdecl.Analyzer,
|
|
|
|
assign.Analyzer,
|
|
|
|
atomic.Analyzer,
|
|
|
|
atomicalign.Analyzer,
|
|
|
|
bools.Analyzer,
|
|
|
|
buildtag.Analyzer,
|
|
|
|
cgocall.Analyzer,
|
|
|
|
composite.Analyzer,
|
|
|
|
copylock.Analyzer,
|
|
|
|
httpresponse.Analyzer,
|
|
|
|
loopclosure.Analyzer,
|
2019-02-06 16:47:00 -07:00
|
|
|
lostcancel.Analyzer,
|
2019-02-07 15:27:10 -07:00
|
|
|
nilfunc.Analyzer,
|
2019-02-06 16:47:00 -07:00
|
|
|
printf.Analyzer,
|
2019-02-07 15:27:10 -07:00
|
|
|
shift.Analyzer,
|
|
|
|
stdmethods.Analyzer,
|
|
|
|
structtag.Analyzer,
|
|
|
|
tests.Analyzer,
|
|
|
|
unmarshal.Analyzer,
|
|
|
|
unreachable.Analyzer,
|
|
|
|
unsafeptr.Analyzer,
|
|
|
|
unusedresult.Analyzer,
|
2019-02-06 09:24:10 -07:00
|
|
|
}
|
2019-02-07 15:27:10 -07:00
|
|
|
|
2019-04-22 15:05:43 -06:00
|
|
|
roots, err := analyze(ctx, v, []Package{pkg}, analyzers)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-07 15:27:10 -07:00
|
|
|
|
|
|
|
// Report diagnostics and errors from root analyzers.
|
2019-02-06 16:47:00 -07:00
|
|
|
for _, r := range roots {
|
|
|
|
for _, diag := range r.diagnostics {
|
|
|
|
if r.err != nil {
|
|
|
|
// TODO(matloob): This isn't quite right: we might return a failed prerequisites error,
|
|
|
|
// which isn't super useful...
|
|
|
|
return r.err
|
|
|
|
}
|
2019-04-22 15:05:43 -06:00
|
|
|
if err := report(r.Analyzer, diag); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-06 09:24:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|