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"
|
2018-11-12 12:15:47 -07:00
|
|
|
"go/token"
|
|
|
|
"strconv"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Diagnostic struct {
|
2018-12-18 13:46:14 -07:00
|
|
|
Range
|
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
|
|
|
|
)
|
|
|
|
|
2018-12-18 14:18:03 -07:00
|
|
|
func Diagnostics(ctx context.Context, v View, uri URI) (map[string][]Diagnostic, error) {
|
|
|
|
f, err := v.GetFile(ctx, uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-13 11:23:28 -07:00
|
|
|
pkg := f.GetPackage()
|
2018-11-12 12:15:47 -07:00
|
|
|
// Prepare the reports we will send for this package.
|
|
|
|
reports := make(map[string][]Diagnostic)
|
|
|
|
for _, filename := range pkg.GoFiles {
|
|
|
|
reports[filename] = []Diagnostic{}
|
|
|
|
}
|
|
|
|
var parseErrors, typeErrors []packages.Error
|
|
|
|
for _, err := range pkg.Errors {
|
|
|
|
switch err.Kind {
|
|
|
|
case packages.ParseError:
|
|
|
|
parseErrors = append(parseErrors, err)
|
|
|
|
case packages.TypeError:
|
|
|
|
typeErrors = append(typeErrors, err)
|
|
|
|
default:
|
|
|
|
// ignore other types of errors
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Don't report type errors if there are parse errors.
|
|
|
|
diags := typeErrors
|
|
|
|
if len(parseErrors) > 0 {
|
|
|
|
diags = parseErrors
|
|
|
|
}
|
|
|
|
for _, diag := range diags {
|
2018-12-05 15:00:36 -07:00
|
|
|
pos := errorPos(diag)
|
2018-12-18 14:18:03 -07:00
|
|
|
diagFile, err := v.GetFile(ctx, ToURI(pos.Filename))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-02-13 11:23:28 -07:00
|
|
|
diagTok := diagFile.GetToken()
|
2019-02-13 14:08:29 -07:00
|
|
|
end, err := identifierEnd(diagFile.GetContent(), pos.Line, pos.Column)
|
2018-12-18 13:46:14 -07:00
|
|
|
// Don't set a range if it's anything other than a type error.
|
|
|
|
if err != nil || diag.Kind != packages.TypeError {
|
|
|
|
end = 0
|
|
|
|
}
|
2018-12-20 15:31:55 -07:00
|
|
|
startPos := fromTokenPosition(diagTok, pos.Line, pos.Column)
|
|
|
|
if !startPos.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
endPos := fromTokenPosition(diagTok, pos.Line, pos.Column+end)
|
|
|
|
if !endPos.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
2018-11-12 12:15:47 -07:00
|
|
|
diagnostic := Diagnostic{
|
2018-12-18 13:46:14 -07:00
|
|
|
Range: Range{
|
2018-12-20 15:31:55 -07:00
|
|
|
Start: startPos,
|
|
|
|
End: endPos,
|
2018-12-18 13:46:14 -07:00
|
|
|
},
|
2019-02-27 16:08:04 -07:00
|
|
|
Message: diag.Msg,
|
|
|
|
Severity: SeverityError,
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
2018-12-05 15:00:36 -07:00
|
|
|
if _, ok := reports[pos.Filename]; ok {
|
|
|
|
reports[pos.Filename] = append(reports[pos.Filename], diagnostic)
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-06 09:24:10 -07:00
|
|
|
if len(diags) > 0 {
|
|
|
|
return reports, nil
|
|
|
|
}
|
|
|
|
// Type checking and parsing succeeded. Run analyses.
|
2019-02-06 16:47:00 -07:00
|
|
|
runAnalyses(v.GetAnalysisCache(), pkg, func(a *analysis.Analyzer, diag analysis.Diagnostic) {
|
2019-02-06 09:24:10 -07:00
|
|
|
pos := pkg.Fset.Position(diag.Pos)
|
|
|
|
category := a.Name
|
|
|
|
if diag.Category != "" {
|
|
|
|
category += "." + category
|
|
|
|
}
|
|
|
|
|
|
|
|
reports[pos.Filename] = append(reports[pos.Filename], Diagnostic{
|
2019-02-27 16:08:04 -07:00
|
|
|
Source: category,
|
|
|
|
Range: Range{Start: diag.Pos, End: diag.Pos},
|
|
|
|
Message: fmt.Sprintf(diag.Message),
|
|
|
|
Severity: SeverityWarning,
|
2019-02-06 09:24:10 -07:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-12 12:15:47 -07:00
|
|
|
return reports, nil
|
|
|
|
}
|
|
|
|
|
2018-12-18 13:46:14 -07:00
|
|
|
// fromTokenPosition converts a token.Position (1-based line and column
|
|
|
|
// number) to a token.Pos (byte offset value). This requires the token.File
|
|
|
|
// to which the token.Pos belongs.
|
|
|
|
func fromTokenPosition(f *token.File, line, col int) token.Pos {
|
|
|
|
linePos := lineStart(f, line)
|
|
|
|
// TODO: This is incorrect, as pos.Column represents bytes, not characters.
|
|
|
|
// This needs to be handled to address golang.org/issue/29149.
|
|
|
|
return linePos + token.Pos(col-1)
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func errorPos(pkgErr packages.Error) token.Position {
|
2018-11-12 12:15:47 -07:00
|
|
|
remainder1, first, hasLine := chop(pkgErr.Pos)
|
|
|
|
remainder2, second, hasColumn := chop(remainder1)
|
|
|
|
var pos token.Position
|
|
|
|
if hasLine && hasColumn {
|
|
|
|
pos.Filename = remainder2
|
|
|
|
pos.Line = second
|
|
|
|
pos.Column = first
|
|
|
|
} else if hasLine {
|
|
|
|
pos.Filename = remainder1
|
|
|
|
pos.Line = first
|
|
|
|
}
|
2018-12-05 15:00:36 -07:00
|
|
|
return pos
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func chop(text string) (remainder string, value int, ok bool) {
|
|
|
|
i := strings.LastIndex(text, ":")
|
|
|
|
if i < 0 {
|
|
|
|
return text, 0, false
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseInt(text[i+1:], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return text, 0, false
|
|
|
|
}
|
|
|
|
return text[:i], int(v), true
|
|
|
|
}
|
2018-12-18 13:46:14 -07:00
|
|
|
|
|
|
|
// identifierEnd returns the length of an identifier within a string,
|
|
|
|
// given the starting line and column numbers of the identifier.
|
|
|
|
func identifierEnd(content []byte, l, c int) (int, error) {
|
|
|
|
lines := bytes.Split(content, []byte("\n"))
|
|
|
|
if len(lines) < l {
|
|
|
|
return 0, fmt.Errorf("invalid line number: got %v, but only %v lines", l, len(lines))
|
|
|
|
}
|
|
|
|
line := lines[l-1]
|
|
|
|
if len(line) < c {
|
|
|
|
return 0, fmt.Errorf("invalid column number: got %v, but the length of the line is %v", c, len(line))
|
|
|
|
}
|
|
|
|
return bytes.IndexAny(line[c-1:], " \n,():;[]"), nil
|
|
|
|
}
|
2019-02-06 09:24:10 -07:00
|
|
|
|
2019-02-06 16:47:00 -07:00
|
|
|
func runAnalyses(c *AnalysisCache, pkg *packages.Package, report func(a *analysis.Analyzer, diag analysis.Diagnostic)) 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-02-06 16:47:00 -07:00
|
|
|
roots := c.analyze([]*packages.Package{pkg}, analyzers)
|
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
|
|
|
|
}
|
|
|
|
report(r.a, diag)
|
2019-02-06 09:24:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|