2018-12-28 23:15:05 +01:00
|
|
|
// Copyright (c) 2013 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 or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd.
|
|
|
|
|
|
|
|
// Package lintutil provides helpers for writing linter command lines.
|
|
|
|
package lintutil // import "honnef.co/go/tools/lint/lintutil"
|
|
|
|
|
|
|
|
import (
|
2020-05-09 15:44:17 +02:00
|
|
|
"crypto/sha256"
|
2018-12-28 23:15:05 +01:00
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"go/build"
|
|
|
|
"go/token"
|
2020-05-09 15:44:17 +02:00
|
|
|
"io"
|
2019-02-18 20:32:41 +01:00
|
|
|
"log"
|
2018-12-28 23:15:05 +01:00
|
|
|
"os"
|
2020-05-09 15:44:17 +02:00
|
|
|
"os/signal"
|
2019-02-18 20:32:41 +01:00
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
2018-12-28 23:15:05 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-05-09 15:44:17 +02:00
|
|
|
"sync/atomic"
|
2018-12-28 23:15:05 +01:00
|
|
|
|
2019-02-18 20:32:41 +01:00
|
|
|
"honnef.co/go/tools/config"
|
2020-05-09 15:44:17 +02:00
|
|
|
"honnef.co/go/tools/internal/cache"
|
2018-12-28 23:15:05 +01:00
|
|
|
"honnef.co/go/tools/lint"
|
2019-02-18 20:32:41 +01:00
|
|
|
"honnef.co/go/tools/lint/lintutil/format"
|
2018-12-28 23:15:05 +01:00
|
|
|
"honnef.co/go/tools/version"
|
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
"golang.org/x/tools/go/analysis"
|
|
|
|
"golang.org/x/tools/go/buildutil"
|
2019-02-18 20:32:41 +01:00
|
|
|
"golang.org/x/tools/go/packages"
|
2018-12-28 23:15:05 +01:00
|
|
|
)
|
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
func NewVersionFlag() flag.Getter {
|
|
|
|
tags := build.Default.ReleaseTags
|
|
|
|
v := tags[len(tags)-1][2:]
|
|
|
|
version := new(VersionFlag)
|
|
|
|
if err := version.Set(v); err != nil {
|
|
|
|
panic(fmt.Sprintf("internal error: %s", err))
|
2018-12-28 23:15:05 +01:00
|
|
|
}
|
2020-05-09 15:44:17 +02:00
|
|
|
return version
|
2018-12-28 23:15:05 +01:00
|
|
|
}
|
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
type VersionFlag int
|
2018-12-28 23:15:05 +01:00
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
func (v *VersionFlag) String() string {
|
2018-12-28 23:15:05 +01:00
|
|
|
return fmt.Sprintf("1.%d", *v)
|
2020-05-09 15:44:17 +02:00
|
|
|
|
2018-12-28 23:15:05 +01:00
|
|
|
}
|
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
func (v *VersionFlag) Set(s string) error {
|
2018-12-28 23:15:05 +01:00
|
|
|
if len(s) < 3 {
|
|
|
|
return errors.New("invalid Go version")
|
|
|
|
}
|
|
|
|
if s[0] != '1' {
|
|
|
|
return errors.New("invalid Go version")
|
|
|
|
}
|
|
|
|
if s[1] != '.' {
|
|
|
|
return errors.New("invalid Go version")
|
|
|
|
}
|
|
|
|
i, err := strconv.Atoi(s[2:])
|
2020-05-09 15:44:17 +02:00
|
|
|
*v = VersionFlag(i)
|
2018-12-28 23:15:05 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
func (v *VersionFlag) Get() interface{} {
|
2018-12-28 23:15:05 +01:00
|
|
|
return int(*v)
|
|
|
|
}
|
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
func usage(name string, flags *flag.FlagSet) func() {
|
|
|
|
return func() {
|
|
|
|
fmt.Fprintf(os.Stderr, "Usage of %s:\n", name)
|
|
|
|
fmt.Fprintf(os.Stderr, "\t%s [flags] # runs on package in current directory\n", name)
|
|
|
|
fmt.Fprintf(os.Stderr, "\t%s [flags] packages\n", name)
|
|
|
|
fmt.Fprintf(os.Stderr, "\t%s [flags] directory\n", name)
|
|
|
|
fmt.Fprintf(os.Stderr, "\t%s [flags] files... # must be a single package\n", name)
|
|
|
|
fmt.Fprintf(os.Stderr, "Flags:\n")
|
|
|
|
flags.PrintDefaults()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 20:32:41 +01:00
|
|
|
type list []string
|
|
|
|
|
|
|
|
func (list *list) String() string {
|
|
|
|
return `"` + strings.Join(*list, ",") + `"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (list *list) Set(s string) error {
|
|
|
|
if s == "" {
|
|
|
|
*list = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
*list = strings.Split(s, ",")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-28 23:15:05 +01:00
|
|
|
func FlagSet(name string) *flag.FlagSet {
|
|
|
|
flags := flag.NewFlagSet("", flag.ExitOnError)
|
|
|
|
flags.Usage = usage(name, flags)
|
|
|
|
flags.String("tags", "", "List of `build tags`")
|
|
|
|
flags.Bool("tests", true, "Include tests")
|
|
|
|
flags.Bool("version", false, "Print version and exit")
|
|
|
|
flags.Bool("show-ignored", false, "Don't filter ignored problems")
|
2019-02-18 20:32:41 +01:00
|
|
|
flags.String("f", "text", "Output `format` (valid choices are 'stylish', 'text' and 'json')")
|
2019-04-22 12:59:42 +02:00
|
|
|
flags.String("explain", "", "Print description of `check`")
|
2019-02-18 20:32:41 +01:00
|
|
|
|
|
|
|
flags.String("debug.cpuprofile", "", "Write CPU profile to `file`")
|
|
|
|
flags.String("debug.memprofile", "", "Write memory profile to `file`")
|
2020-05-09 15:44:17 +02:00
|
|
|
flags.Bool("debug.version", false, "Print detailed version information about this program")
|
|
|
|
flags.Bool("debug.no-compile-errors", false, "Don't print compile errors")
|
2019-02-18 20:32:41 +01:00
|
|
|
|
|
|
|
checks := list{"inherit"}
|
|
|
|
fail := list{"all"}
|
|
|
|
flags.Var(&checks, "checks", "Comma-separated list of `checks` to enable.")
|
|
|
|
flags.Var(&fail, "fail", "Comma-separated list of `checks` that can cause a non-zero exit status.")
|
2018-12-28 23:15:05 +01:00
|
|
|
|
|
|
|
tags := build.Default.ReleaseTags
|
|
|
|
v := tags[len(tags)-1][2:]
|
2020-05-09 15:44:17 +02:00
|
|
|
version := new(VersionFlag)
|
2018-12-28 23:15:05 +01:00
|
|
|
if err := version.Set(v); err != nil {
|
|
|
|
panic(fmt.Sprintf("internal error: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
flags.Var(version, "go", "Target Go `version` in the format '1.x'")
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
func findCheck(cs []*analysis.Analyzer, check string) (*analysis.Analyzer, bool) {
|
2019-04-22 12:59:42 +02:00
|
|
|
for _, c := range cs {
|
2020-05-09 15:44:17 +02:00
|
|
|
if c.Name == check {
|
|
|
|
return c, true
|
2019-04-22 12:59:42 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-09 15:44:17 +02:00
|
|
|
return nil, false
|
2019-04-22 12:59:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
func ProcessFlagSet(cs []*analysis.Analyzer, cums []lint.CumulativeChecker, fs *flag.FlagSet) {
|
2018-12-28 23:15:05 +01:00
|
|
|
tags := fs.Lookup("tags").Value.(flag.Getter).Get().(string)
|
|
|
|
tests := fs.Lookup("tests").Value.(flag.Getter).Get().(bool)
|
|
|
|
goVersion := fs.Lookup("go").Value.(flag.Getter).Get().(int)
|
2019-02-18 20:32:41 +01:00
|
|
|
formatter := fs.Lookup("f").Value.(flag.Getter).Get().(string)
|
2018-12-28 23:15:05 +01:00
|
|
|
printVersion := fs.Lookup("version").Value.(flag.Getter).Get().(bool)
|
|
|
|
showIgnored := fs.Lookup("show-ignored").Value.(flag.Getter).Get().(bool)
|
2019-04-22 12:59:42 +02:00
|
|
|
explain := fs.Lookup("explain").Value.(flag.Getter).Get().(string)
|
2018-12-28 23:15:05 +01:00
|
|
|
|
2019-02-18 20:32:41 +01:00
|
|
|
cpuProfile := fs.Lookup("debug.cpuprofile").Value.(flag.Getter).Get().(string)
|
|
|
|
memProfile := fs.Lookup("debug.memprofile").Value.(flag.Getter).Get().(string)
|
2020-05-09 15:44:17 +02:00
|
|
|
debugVersion := fs.Lookup("debug.version").Value.(flag.Getter).Get().(bool)
|
|
|
|
debugNoCompile := fs.Lookup("debug.no-compile-errors").Value.(flag.Getter).Get().(bool)
|
2019-02-18 20:32:41 +01:00
|
|
|
|
|
|
|
cfg := config.Config{}
|
|
|
|
cfg.Checks = *fs.Lookup("checks").Value.(*list)
|
|
|
|
|
|
|
|
exit := func(code int) {
|
|
|
|
if cpuProfile != "" {
|
|
|
|
pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
if memProfile != "" {
|
|
|
|
f, err := os.Create(memProfile)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
runtime.GC()
|
|
|
|
pprof.WriteHeapProfile(f)
|
|
|
|
}
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
if cpuProfile != "" {
|
|
|
|
f, err := os.Create(cpuProfile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
}
|
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
if debugVersion {
|
|
|
|
version.Verbose()
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
|
2018-12-28 23:15:05 +01:00
|
|
|
if printVersion {
|
|
|
|
version.Print()
|
2019-02-18 20:32:41 +01:00
|
|
|
exit(0)
|
2018-12-28 23:15:05 +01:00
|
|
|
}
|
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
// Validate that the tags argument is well-formed. go/packages
|
|
|
|
// doesn't detect malformed build flags and returns unhelpful
|
|
|
|
// errors.
|
|
|
|
tf := buildutil.TagsFlag{}
|
|
|
|
if err := tf.Set(tags); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, fmt.Errorf("invalid value %q for flag -tags: %s", tags, err))
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
2019-04-22 12:59:42 +02:00
|
|
|
if explain != "" {
|
2020-05-09 15:44:17 +02:00
|
|
|
var haystack []*analysis.Analyzer
|
|
|
|
haystack = append(haystack, cs...)
|
|
|
|
for _, cum := range cums {
|
|
|
|
haystack = append(haystack, cum.Analyzer())
|
|
|
|
}
|
|
|
|
check, ok := findCheck(haystack, explain)
|
2019-04-22 12:59:42 +02:00
|
|
|
if !ok {
|
|
|
|
fmt.Fprintln(os.Stderr, "Couldn't find check", explain)
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
if check.Doc == "" {
|
|
|
|
fmt.Fprintln(os.Stderr, explain, "has no documentation")
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
fmt.Println(check.Doc)
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
ps, err := Lint(cs, cums, fs.Args(), &Options{
|
|
|
|
Tags: tags,
|
|
|
|
LintTests: tests,
|
|
|
|
GoVersion: goVersion,
|
|
|
|
Config: cfg,
|
2018-12-28 23:15:05 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2019-02-18 20:32:41 +01:00
|
|
|
exit(1)
|
2018-12-28 23:15:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-18 20:32:41 +01:00
|
|
|
var f format.Formatter
|
|
|
|
switch formatter {
|
2018-12-28 23:15:05 +01:00
|
|
|
case "text":
|
2019-02-18 20:32:41 +01:00
|
|
|
f = format.Text{W: os.Stdout}
|
|
|
|
case "stylish":
|
|
|
|
f = &format.Stylish{W: os.Stdout}
|
2018-12-28 23:15:05 +01:00
|
|
|
case "json":
|
2019-02-18 20:32:41 +01:00
|
|
|
f = format.JSON{W: os.Stdout}
|
2018-12-28 23:15:05 +01:00
|
|
|
default:
|
2019-02-18 20:32:41 +01:00
|
|
|
fmt.Fprintf(os.Stderr, "unsupported output format %q\n", formatter)
|
|
|
|
exit(2)
|
2018-12-28 23:15:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-18 20:32:41 +01:00
|
|
|
var (
|
|
|
|
total int
|
|
|
|
errors int
|
|
|
|
warnings int
|
|
|
|
)
|
|
|
|
|
|
|
|
fail := *fs.Lookup("fail").Value.(*list)
|
2020-05-09 15:44:17 +02:00
|
|
|
analyzers := make([]*analysis.Analyzer, len(cs), len(cs)+len(cums))
|
|
|
|
copy(analyzers, cs)
|
|
|
|
for _, cum := range cums {
|
|
|
|
analyzers = append(analyzers, cum.Analyzer())
|
2018-12-28 23:15:05 +01:00
|
|
|
}
|
2020-05-09 15:44:17 +02:00
|
|
|
shouldExit := lint.FilterChecks(analyzers, fail)
|
|
|
|
shouldExit["compile"] = true
|
2019-02-18 20:32:41 +01:00
|
|
|
|
|
|
|
total = len(ps)
|
|
|
|
for _, p := range ps {
|
2020-05-09 15:44:17 +02:00
|
|
|
if p.Check == "compile" && debugNoCompile {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if p.Severity == lint.Ignored && !showIgnored {
|
|
|
|
continue
|
|
|
|
}
|
2019-02-18 20:32:41 +01:00
|
|
|
if shouldExit[p.Check] {
|
|
|
|
errors++
|
|
|
|
} else {
|
|
|
|
p.Severity = lint.Warning
|
|
|
|
warnings++
|
2018-12-28 23:15:05 +01:00
|
|
|
}
|
2019-02-18 20:32:41 +01:00
|
|
|
f.Format(p)
|
|
|
|
}
|
|
|
|
if f, ok := f.(format.Statter); ok {
|
|
|
|
f.Stats(total, errors, warnings)
|
|
|
|
}
|
|
|
|
if errors > 0 {
|
|
|
|
exit(1)
|
2018-12-28 23:15:05 +01:00
|
|
|
}
|
2020-05-09 15:44:17 +02:00
|
|
|
exit(0)
|
2018-12-28 23:15:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Options struct {
|
2019-02-18 20:32:41 +01:00
|
|
|
Config config.Config
|
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
Tags string
|
|
|
|
LintTests bool
|
|
|
|
GoVersion int
|
2018-12-28 23:15:05 +01:00
|
|
|
}
|
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
func computeSalt() ([]byte, error) {
|
|
|
|
if version.Version != "devel" {
|
|
|
|
return []byte(version.Version), nil
|
2019-02-18 20:32:41 +01:00
|
|
|
}
|
2020-05-09 15:44:17 +02:00
|
|
|
p, err := os.Executable()
|
2018-12-28 23:15:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-09 15:44:17 +02:00
|
|
|
f, err := os.Open(p)
|
2018-12-28 23:15:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-09 15:44:17 +02:00
|
|
|
defer f.Close()
|
|
|
|
h := sha256.New()
|
|
|
|
if _, err := io.Copy(h, f); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return h.Sum(nil), nil
|
|
|
|
}
|
2018-12-28 23:15:05 +01:00
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
func Lint(cs []*analysis.Analyzer, cums []lint.CumulativeChecker, paths []string, opt *Options) ([]lint.Problem, error) {
|
|
|
|
salt, err := computeSalt()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not compute salt for cache: %s", err)
|
2018-12-28 23:15:05 +01:00
|
|
|
}
|
2020-05-09 15:44:17 +02:00
|
|
|
cache.SetSalt(salt)
|
2019-02-18 20:32:41 +01:00
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
if opt == nil {
|
|
|
|
opt = &Options{}
|
2019-02-18 20:32:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
l := &lint.Linter{
|
2020-05-09 15:44:17 +02:00
|
|
|
Checkers: cs,
|
|
|
|
CumulativeCheckers: cums,
|
|
|
|
GoVersion: opt.GoVersion,
|
|
|
|
Config: opt.Config,
|
|
|
|
}
|
|
|
|
cfg := &packages.Config{}
|
|
|
|
if opt.LintTests {
|
|
|
|
cfg.Tests = true
|
|
|
|
}
|
|
|
|
if opt.Tags != "" {
|
|
|
|
cfg.BuildFlags = append(cfg.BuildFlags, "-tags", opt.Tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
printStats := func() {
|
|
|
|
// Individual stats are read atomically, but overall there
|
|
|
|
// is no synchronisation. For printing rough progress
|
|
|
|
// information, this doesn't matter.
|
|
|
|
switch atomic.LoadUint32(&l.Stats.State) {
|
|
|
|
case lint.StateInitializing:
|
|
|
|
fmt.Fprintln(os.Stderr, "Status: initializing")
|
|
|
|
case lint.StateGraph:
|
|
|
|
fmt.Fprintln(os.Stderr, "Status: loading package graph")
|
|
|
|
case lint.StateProcessing:
|
|
|
|
fmt.Fprintf(os.Stderr, "Packages: %d/%d initial, %d/%d total; Workers: %d/%d; Problems: %d\n",
|
|
|
|
atomic.LoadUint32(&l.Stats.ProcessedInitialPackages),
|
|
|
|
atomic.LoadUint32(&l.Stats.InitialPackages),
|
|
|
|
atomic.LoadUint32(&l.Stats.ProcessedPackages),
|
|
|
|
atomic.LoadUint32(&l.Stats.TotalPackages),
|
|
|
|
atomic.LoadUint32(&l.Stats.ActiveWorkers),
|
|
|
|
atomic.LoadUint32(&l.Stats.TotalWorkers),
|
|
|
|
atomic.LoadUint32(&l.Stats.Problems),
|
|
|
|
)
|
|
|
|
case lint.StateCumulative:
|
|
|
|
fmt.Fprintln(os.Stderr, "Status: processing cumulative checkers")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(infoSignals) > 0 {
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, infoSignals...)
|
|
|
|
defer signal.Stop(ch)
|
|
|
|
go func() {
|
|
|
|
for range ch {
|
|
|
|
printStats()
|
|
|
|
}
|
|
|
|
}()
|
2019-02-18 20:32:41 +01:00
|
|
|
}
|
|
|
|
|
2020-05-09 15:44:17 +02:00
|
|
|
return l.Lint(cfg, paths)
|
2018-12-28 23:15:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-18 20:32:41 +01:00
|
|
|
var posRe = regexp.MustCompile(`^(.+?):(\d+)(?::(\d+)?)?$`)
|
|
|
|
|
|
|
|
func parsePos(pos string) token.Position {
|
|
|
|
if pos == "-" || pos == "" {
|
|
|
|
return token.Position{}
|
|
|
|
}
|
|
|
|
parts := posRe.FindStringSubmatch(pos)
|
|
|
|
if parts == nil {
|
|
|
|
panic(fmt.Sprintf("internal error: malformed position %q", pos))
|
2018-12-28 23:15:05 +01:00
|
|
|
}
|
2019-02-18 20:32:41 +01:00
|
|
|
file := parts[1]
|
|
|
|
line, _ := strconv.Atoi(parts[2])
|
|
|
|
col, _ := strconv.Atoi(parts[3])
|
|
|
|
return token.Position{
|
|
|
|
Filename: file,
|
|
|
|
Line: line,
|
|
|
|
Column: col,
|
2018-12-28 23:15:05 +01:00
|
|
|
}
|
|
|
|
}
|