2010-12-09 10:37:18 -07:00
|
|
|
// Copyright 2010 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.
|
|
|
|
|
2012-01-29 12:07:25 -07:00
|
|
|
// Vet is a simple checker for static errors in Go source code.
|
2010-12-09 10:37:18 -07:00
|
|
|
// See doc.go for more information.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2011-10-19 14:06:16 -06:00
|
|
|
"bytes"
|
2010-12-09 10:37:18 -07:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
2013-02-22 14:32:43 -07:00
|
|
|
"go/build"
|
2010-12-09 10:37:18 -07:00
|
|
|
"go/parser"
|
2013-01-30 08:57:11 -07:00
|
|
|
"go/printer"
|
2010-12-09 10:37:18 -07:00
|
|
|
"go/token"
|
2013-01-31 14:52:27 -07:00
|
|
|
"io/ioutil"
|
2010-12-09 10:37:18 -07:00
|
|
|
"os"
|
2011-03-06 15:33:23 -07:00
|
|
|
"path/filepath"
|
2010-12-09 10:37:18 -07:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var verbose = flag.Bool("v", false, "verbose")
|
|
|
|
var exitCode = 0
|
|
|
|
|
2013-02-11 14:33:11 -07:00
|
|
|
// Flags to control which checks to perform. "all" is set to true here, and disabled later if
|
|
|
|
// a flag is set explicitly.
|
|
|
|
var report = map[string]*bool{
|
|
|
|
"all": flag.Bool("all", true, "check everything; disabled if any explicit check is requested"),
|
2013-03-05 15:55:04 -07:00
|
|
|
"assign": flag.Bool("assign", false, "check for useless assignments"),
|
2013-02-11 14:33:11 -07:00
|
|
|
"atomic": flag.Bool("atomic", false, "check for common mistaken usages of the sync/atomic package"),
|
|
|
|
"buildtags": flag.Bool("buildtags", false, "check that +build tags are valid"),
|
|
|
|
"composites": flag.Bool("composites", false, "check that composite literals used type-tagged elements"),
|
|
|
|
"methods": flag.Bool("methods", false, "check that canonically named methods are canonically defined"),
|
|
|
|
"printf": flag.Bool("printf", false, "check printf-like invocations"),
|
|
|
|
"structtags": flag.Bool("structtags", false, "check that struct field tags have canonical format"),
|
|
|
|
"rangeloops": flag.Bool("rangeloops", false, "check that range loop variables are used correctly"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// vet tells whether to report errors for the named check, a flag name.
|
|
|
|
func vet(name string) bool {
|
|
|
|
return *report["all"] || *report[name]
|
|
|
|
}
|
2012-07-16 15:03:11 -06:00
|
|
|
|
2010-12-14 15:12:22 -07:00
|
|
|
// setExit sets the value for os.Exit when it is called, later. It
|
|
|
|
// remembers the highest value.
|
|
|
|
func setExit(err int) {
|
|
|
|
if err > exitCode {
|
|
|
|
exitCode = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-09 10:37:18 -07:00
|
|
|
// Usage is a replacement usage function for the flags package.
|
|
|
|
func Usage() {
|
|
|
|
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
2013-02-22 14:32:43 -07:00
|
|
|
fmt.Fprintf(os.Stderr, "\tvet [flags] directory...\n")
|
|
|
|
fmt.Fprintf(os.Stderr, "\tvet [flags] files... # Must be a single package\n")
|
2010-12-09 10:37:18 -07:00
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// File is a wrapper for the state of a file used in the parser.
|
|
|
|
// The parse tree walkers are all methods of this type.
|
|
|
|
type File struct {
|
2013-02-22 18:16:31 -07:00
|
|
|
pkg *Package
|
2011-10-19 14:06:16 -06:00
|
|
|
fset *token.FileSet
|
2013-02-22 14:32:43 -07:00
|
|
|
name string
|
2011-10-19 14:06:16 -06:00
|
|
|
file *ast.File
|
|
|
|
b bytes.Buffer // for use by methods
|
2010-12-09 10:37:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Usage = Usage
|
|
|
|
flag.Parse()
|
|
|
|
|
2012-07-16 15:03:11 -06:00
|
|
|
// If a check is named explicitly, turn off the 'all' flag.
|
2013-02-11 14:33:11 -07:00
|
|
|
for name, ptr := range report {
|
|
|
|
if name != "all" && *ptr {
|
|
|
|
*report["all"] = false
|
|
|
|
break
|
|
|
|
}
|
2012-07-16 15:03:11 -06:00
|
|
|
}
|
|
|
|
|
2010-12-09 10:37:18 -07:00
|
|
|
if *printfuncs != "" {
|
2011-06-27 17:43:14 -06:00
|
|
|
for _, name := range strings.Split(*printfuncs, ",") {
|
2010-12-09 10:37:18 -07:00
|
|
|
if len(name) == 0 {
|
|
|
|
flag.Usage()
|
|
|
|
}
|
|
|
|
skip := 0
|
|
|
|
if colon := strings.LastIndex(name, ":"); colon > 0 {
|
2011-11-01 20:06:05 -06:00
|
|
|
var err error
|
2010-12-09 10:37:18 -07:00
|
|
|
skip, err = strconv.Atoi(name[colon+1:])
|
|
|
|
if err != nil {
|
2011-06-29 07:52:34 -06:00
|
|
|
errorf(`illegal format for "Func:N" argument %q; %s`, name, err)
|
2010-12-09 10:37:18 -07:00
|
|
|
}
|
|
|
|
name = name[:colon]
|
|
|
|
}
|
2011-04-13 17:57:44 -06:00
|
|
|
name = strings.ToLower(name)
|
2010-12-09 10:37:18 -07:00
|
|
|
if name[len(name)-1] == 'f' {
|
|
|
|
printfList[name] = skip
|
|
|
|
} else {
|
|
|
|
printList[name] = skip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if flag.NArg() == 0 {
|
2013-02-22 14:32:43 -07:00
|
|
|
Usage()
|
|
|
|
}
|
|
|
|
dirs := false
|
|
|
|
files := false
|
|
|
|
for _, name := range flag.Args() {
|
|
|
|
// Is it a directory?
|
|
|
|
fi, err := os.Stat(name)
|
|
|
|
if err != nil {
|
|
|
|
warnf("error walking tree: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fi.IsDir() {
|
|
|
|
dirs = true
|
|
|
|
} else {
|
|
|
|
files = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dirs && files {
|
|
|
|
Usage()
|
|
|
|
}
|
|
|
|
if dirs {
|
2010-12-14 18:09:24 -07:00
|
|
|
for _, name := range flag.Args() {
|
2013-02-22 14:32:43 -07:00
|
|
|
walkDir(name)
|
2010-12-09 10:37:18 -07:00
|
|
|
}
|
2013-02-22 14:32:43 -07:00
|
|
|
return
|
2010-12-09 10:37:18 -07:00
|
|
|
}
|
2013-02-22 14:32:43 -07:00
|
|
|
doPackage(flag.Args())
|
2010-12-09 10:37:18 -07:00
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
|
2013-02-25 17:29:09 -07:00
|
|
|
// prefixDirectory places the directory name on the beginning of each name in the list.
|
|
|
|
func prefixDirectory(directory string, names []string) {
|
|
|
|
if directory != "." {
|
|
|
|
for i, name := range names {
|
|
|
|
names[i] = filepath.Join(directory, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// doPackageDir analyzes the single package found in the directory, if there is one,
|
|
|
|
// plus a test package, if there is one.
|
2013-02-22 14:32:43 -07:00
|
|
|
func doPackageDir(directory string) {
|
|
|
|
pkg, err := build.Default.ImportDir(directory, 0)
|
|
|
|
if err != nil {
|
|
|
|
// If it's just that there are no go source files, that's fine.
|
|
|
|
if _, nogo := err.(*build.NoGoError); nogo {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Non-fatal: we are doing a recursive walk and there may be other directories.
|
|
|
|
warnf("cannot process directory %s: %s", directory, err)
|
|
|
|
return
|
|
|
|
}
|
2013-02-25 17:29:09 -07:00
|
|
|
var names []string
|
2013-02-27 16:43:33 -07:00
|
|
|
names = append(names, pkg.GoFiles...)
|
2013-02-25 17:29:09 -07:00
|
|
|
names = append(names, pkg.CgoFiles...)
|
|
|
|
names = append(names, pkg.TestGoFiles...) // These are also in the "foo" package.
|
|
|
|
prefixDirectory(directory, names)
|
2013-02-22 14:32:43 -07:00
|
|
|
doPackage(names)
|
2013-02-25 17:29:09 -07:00
|
|
|
// Is there also a "foo_test" package? If so, do that one as well.
|
|
|
|
if len(pkg.XTestGoFiles) > 0 {
|
|
|
|
names = pkg.XTestGoFiles
|
|
|
|
prefixDirectory(directory, names)
|
|
|
|
doPackage(names)
|
|
|
|
}
|
2013-02-22 14:32:43 -07:00
|
|
|
}
|
|
|
|
|
2013-02-22 18:16:31 -07:00
|
|
|
type Package struct {
|
2013-03-06 13:49:56 -07:00
|
|
|
types map[ast.Expr]Type
|
2013-02-23 16:08:36 -07:00
|
|
|
values map[ast.Expr]interface{}
|
2013-02-22 18:16:31 -07:00
|
|
|
}
|
|
|
|
|
2013-02-22 14:32:43 -07:00
|
|
|
// doPackage analyzes the single package constructed from the named files.
|
|
|
|
func doPackage(names []string) {
|
|
|
|
var files []*File
|
|
|
|
var astFiles []*ast.File
|
|
|
|
fs := token.NewFileSet()
|
|
|
|
for _, name := range names {
|
2013-01-31 14:52:27 -07:00
|
|
|
f, err := os.Open(name)
|
|
|
|
if err != nil {
|
2013-03-05 15:31:17 -07:00
|
|
|
// Warn but continue to next package.
|
|
|
|
warnf("%s: %s", name, err)
|
|
|
|
return
|
2013-01-31 14:52:27 -07:00
|
|
|
}
|
|
|
|
defer f.Close()
|
2013-02-22 14:32:43 -07:00
|
|
|
data, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
2013-03-05 15:31:17 -07:00
|
|
|
warnf("%s: %s", name, err)
|
|
|
|
return
|
2013-02-22 14:32:43 -07:00
|
|
|
}
|
|
|
|
checkBuildTag(name, data)
|
|
|
|
parsedFile, err := parser.ParseFile(fs, name, bytes.NewReader(data), 0)
|
|
|
|
if err != nil {
|
2013-03-05 15:31:17 -07:00
|
|
|
warnf("%s: %s", name, err)
|
|
|
|
return
|
2013-02-22 14:32:43 -07:00
|
|
|
}
|
|
|
|
files = append(files, &File{fset: fs, name: name, file: parsedFile})
|
|
|
|
astFiles = append(astFiles, parsedFile)
|
2013-01-31 14:52:27 -07:00
|
|
|
}
|
2013-02-22 18:16:31 -07:00
|
|
|
pkg := new(Package)
|
2013-02-22 14:32:43 -07:00
|
|
|
// Type check the package.
|
2013-03-06 13:49:56 -07:00
|
|
|
err := pkg.check(fs, astFiles)
|
2013-02-25 17:29:09 -07:00
|
|
|
if err != nil && *verbose {
|
2013-02-22 14:32:43 -07:00
|
|
|
warnf("%s", err)
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
2013-02-22 18:16:31 -07:00
|
|
|
file.pkg = pkg
|
2013-02-22 14:32:43 -07:00
|
|
|
file.walkFile(file.name, file.file)
|
2010-12-09 10:37:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-30 10:04:16 -07:00
|
|
|
func visit(path string, f os.FileInfo, err error) error {
|
2011-09-13 18:47:59 -06:00
|
|
|
if err != nil {
|
2013-03-05 15:31:17 -07:00
|
|
|
warnf("walk error: %s", err)
|
|
|
|
return err
|
2011-09-13 18:47:59 -06:00
|
|
|
}
|
2013-02-22 14:32:43 -07:00
|
|
|
// One package per directory. Ignore the files themselves.
|
|
|
|
if !f.IsDir() {
|
|
|
|
return nil
|
2010-12-14 18:09:24 -07:00
|
|
|
}
|
2013-02-22 14:32:43 -07:00
|
|
|
doPackageDir(path)
|
2011-09-13 18:47:59 -06:00
|
|
|
return nil
|
2010-12-14 18:09:24 -07:00
|
|
|
}
|
|
|
|
|
2013-03-05 15:31:17 -07:00
|
|
|
// walkDir recursively walks the tree looking for Go packages.
|
2010-12-14 18:09:24 -07:00
|
|
|
func walkDir(root string) {
|
2011-09-13 18:47:59 -06:00
|
|
|
filepath.Walk(root, visit)
|
2010-12-14 18:09:24 -07:00
|
|
|
}
|
|
|
|
|
2013-02-22 14:32:43 -07:00
|
|
|
// errorf formats the error to standard error, adding program
|
|
|
|
// identification and a newline, and exits.
|
2011-06-29 07:52:34 -06:00
|
|
|
func errorf(format string, args ...interface{}) {
|
2012-01-29 12:07:25 -07:00
|
|
|
fmt.Fprintf(os.Stderr, "vet: "+format+"\n", args...)
|
2013-02-22 14:32:43 -07:00
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// warnf formats the error to standard error, adding program
|
|
|
|
// identification and a newline, but does not exit.
|
|
|
|
func warnf(format string, args ...interface{}) {
|
|
|
|
fmt.Fprintf(os.Stderr, "vet: "+format+"\n", args...)
|
|
|
|
setExit(1)
|
2010-12-09 10:37:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Println is fmt.Println guarded by -v.
|
|
|
|
func Println(args ...interface{}) {
|
|
|
|
if !*verbose {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Printf is fmt.Printf guarded by -v.
|
|
|
|
func Printf(format string, args ...interface{}) {
|
|
|
|
if !*verbose {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf(format+"\n", args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad reports an error and sets the exit code..
|
|
|
|
func (f *File) Bad(pos token.Pos, args ...interface{}) {
|
|
|
|
f.Warn(pos, args...)
|
2010-12-14 15:12:22 -07:00
|
|
|
setExit(1)
|
2010-12-09 10:37:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Badf reports a formatted error and sets the exit code.
|
|
|
|
func (f *File) Badf(pos token.Pos, format string, args ...interface{}) {
|
|
|
|
f.Warnf(pos, format, args...)
|
2010-12-14 15:12:22 -07:00
|
|
|
setExit(1)
|
2010-12-09 10:37:18 -07:00
|
|
|
}
|
|
|
|
|
2013-02-13 20:34:37 -07:00
|
|
|
func (f *File) loc(pos token.Pos) string {
|
|
|
|
// Do not print columns. Because the pos often points to the start of an
|
|
|
|
// expression instead of the inner part with the actual error, the
|
|
|
|
// precision can mislead.
|
|
|
|
posn := f.fset.Position(pos)
|
|
|
|
return fmt.Sprintf("%s:%d: ", posn.Filename, posn.Line)
|
|
|
|
}
|
|
|
|
|
2010-12-09 10:37:18 -07:00
|
|
|
// Warn reports an error but does not set the exit code.
|
|
|
|
func (f *File) Warn(pos token.Pos, args ...interface{}) {
|
2013-02-13 20:34:37 -07:00
|
|
|
fmt.Fprint(os.Stderr, f.loc(pos)+fmt.Sprintln(args...))
|
2010-12-09 10:37:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Warnf reports a formatted error but does not set the exit code.
|
|
|
|
func (f *File) Warnf(pos token.Pos, format string, args ...interface{}) {
|
2013-02-13 20:34:37 -07:00
|
|
|
fmt.Fprintf(os.Stderr, f.loc(pos)+format+"\n", args...)
|
2010-12-09 10:37:18 -07:00
|
|
|
}
|
|
|
|
|
2011-12-15 14:44:35 -07:00
|
|
|
// walkFile walks the file's tree.
|
|
|
|
func (f *File) walkFile(name string, file *ast.File) {
|
2010-12-14 18:09:24 -07:00
|
|
|
Println("Checking file", name)
|
2010-12-09 10:37:18 -07:00
|
|
|
ast.Walk(f, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visit implements the ast.Visitor interface.
|
2010-12-09 11:22:01 -07:00
|
|
|
func (f *File) Visit(node ast.Node) ast.Visitor {
|
2010-12-09 10:37:18 -07:00
|
|
|
switch n := node.(type) {
|
2013-01-30 08:57:11 -07:00
|
|
|
case *ast.AssignStmt:
|
|
|
|
f.walkAssignStmt(n)
|
2010-12-09 10:37:18 -07:00
|
|
|
case *ast.CallExpr:
|
2011-12-15 14:44:35 -07:00
|
|
|
f.walkCallExpr(n)
|
2012-02-02 20:33:41 -07:00
|
|
|
case *ast.CompositeLit:
|
|
|
|
f.walkCompositeLit(n)
|
2011-06-29 07:52:34 -06:00
|
|
|
case *ast.Field:
|
2011-12-15 14:44:35 -07:00
|
|
|
f.walkFieldTag(n)
|
2011-10-19 14:06:16 -06:00
|
|
|
case *ast.FuncDecl:
|
2011-12-15 14:44:35 -07:00
|
|
|
f.walkMethodDecl(n)
|
2011-10-19 14:06:16 -06:00
|
|
|
case *ast.InterfaceType:
|
2011-12-15 14:44:35 -07:00
|
|
|
f.walkInterfaceType(n)
|
2012-09-18 15:19:31 -06:00
|
|
|
case *ast.RangeStmt:
|
|
|
|
f.walkRangeStmt(n)
|
2010-12-09 10:37:18 -07:00
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2013-02-22 14:32:43 -07:00
|
|
|
// walkAssignStmt walks an assignment statement
|
2013-01-30 08:57:11 -07:00
|
|
|
func (f *File) walkAssignStmt(stmt *ast.AssignStmt) {
|
2013-03-05 15:55:04 -07:00
|
|
|
f.checkAssignStmt(stmt)
|
2013-01-30 08:57:11 -07:00
|
|
|
f.checkAtomicAssignment(stmt)
|
|
|
|
}
|
|
|
|
|
2011-12-15 14:44:35 -07:00
|
|
|
// walkCall walks a call expression.
|
|
|
|
func (f *File) walkCall(call *ast.CallExpr, name string) {
|
|
|
|
f.checkFmtPrintfCall(call, name)
|
2011-10-19 14:06:16 -06:00
|
|
|
}
|
|
|
|
|
2012-09-18 15:19:31 -06:00
|
|
|
// walkCallExpr walks a call expression.
|
|
|
|
func (f *File) walkCallExpr(call *ast.CallExpr) {
|
|
|
|
switch x := call.Fun.(type) {
|
|
|
|
case *ast.Ident:
|
|
|
|
f.walkCall(call, x.Name)
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
f.walkCall(call, x.Sel.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-02 20:33:41 -07:00
|
|
|
// walkCompositeLit walks a composite literal.
|
|
|
|
func (f *File) walkCompositeLit(c *ast.CompositeLit) {
|
|
|
|
f.checkUntaggedLiteral(c)
|
|
|
|
}
|
|
|
|
|
2011-12-15 14:44:35 -07:00
|
|
|
// walkFieldTag walks a struct field tag.
|
|
|
|
func (f *File) walkFieldTag(field *ast.Field) {
|
|
|
|
if field.Tag == nil {
|
2011-10-19 14:06:16 -06:00
|
|
|
return
|
|
|
|
}
|
2011-12-15 14:44:35 -07:00
|
|
|
f.checkCanonicalFieldTag(field)
|
2011-10-19 14:06:16 -06:00
|
|
|
}
|
|
|
|
|
2011-12-15 14:44:35 -07:00
|
|
|
// walkMethodDecl walks the method's signature.
|
|
|
|
func (f *File) walkMethod(id *ast.Ident, t *ast.FuncType) {
|
|
|
|
f.checkCanonicalMethod(id, t)
|
2011-10-19 14:06:16 -06:00
|
|
|
}
|
|
|
|
|
2011-12-15 14:44:35 -07:00
|
|
|
// walkMethodDecl walks the method signature in the declaration.
|
|
|
|
func (f *File) walkMethodDecl(d *ast.FuncDecl) {
|
|
|
|
if d.Recv == nil {
|
|
|
|
// not a method
|
|
|
|
return
|
2011-10-19 14:06:16 -06:00
|
|
|
}
|
2011-12-15 14:44:35 -07:00
|
|
|
f.walkMethod(d.Name, d.Type)
|
2011-10-19 14:06:16 -06:00
|
|
|
}
|
|
|
|
|
2011-12-15 14:44:35 -07:00
|
|
|
// walkInterfaceType walks the method signatures of an interface.
|
|
|
|
func (f *File) walkInterfaceType(t *ast.InterfaceType) {
|
|
|
|
for _, field := range t.Methods.List {
|
|
|
|
for _, id := range field.Names {
|
|
|
|
f.walkMethod(id, field.Type.(*ast.FuncType))
|
2011-10-19 14:06:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-18 12:04:09 -07:00
|
|
|
// walkRangeStmt walks a range statement.
|
2012-09-18 15:19:31 -06:00
|
|
|
func (f *File) walkRangeStmt(n *ast.RangeStmt) {
|
|
|
|
checkRangeLoop(f, n)
|
2011-04-13 17:57:44 -06:00
|
|
|
}
|
2013-01-30 08:57:11 -07:00
|
|
|
|
2013-03-01 13:30:09 -07:00
|
|
|
// gofmt returns a string representation of the expression.
|
2013-01-30 08:57:11 -07:00
|
|
|
func (f *File) gofmt(x ast.Expr) string {
|
|
|
|
f.b.Reset()
|
|
|
|
printer.Fprint(&f.b, f.fset, x)
|
|
|
|
return f.b.String()
|
|
|
|
}
|