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"
|
|
|
|
"go/parser"
|
2013-01-30 08:57:11 -07:00
|
|
|
"go/printer"
|
2010-12-09 10:37:18 -07:00
|
|
|
"go/token"
|
2011-11-02 13:54:16 -06:00
|
|
|
"io"
|
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
|
|
|
|
|
2012-07-16 15:03:11 -06:00
|
|
|
// Flags to control which checks to perform
|
|
|
|
var (
|
|
|
|
vetAll = flag.Bool("all", true, "check everything; disabled if any explicit check is requested")
|
|
|
|
vetMethods = flag.Bool("methods", false, "check that canonically named methods are canonically defined")
|
|
|
|
vetPrintf = flag.Bool("printf", false, "check printf-like invocations")
|
|
|
|
vetStructTags = flag.Bool("structtags", false, "check that struct field tags have canonical format")
|
|
|
|
vetUntaggedLiteral = flag.Bool("composites", false, "check that composite literals used type-tagged elements")
|
2012-09-18 15:19:31 -06:00
|
|
|
vetRangeLoops = flag.Bool("rangeloops", false, "check that range loop variables are used correctly")
|
2013-01-30 08:57:11 -07:00
|
|
|
vetAtomic = flag.Bool("atomic", false, "check for common mistaken usages of the sync/atomic package")
|
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])
|
|
|
|
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 {
|
2011-10-19 14:06:16 -06:00
|
|
|
fset *token.FileSet
|
|
|
|
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.
|
2012-09-18 15:19:31 -06:00
|
|
|
if *vetMethods || *vetPrintf || *vetStructTags || *vetUntaggedLiteral || *vetRangeLoops {
|
2012-07-16 15:03:11 -06:00
|
|
|
*vetAll = false
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
doFile("stdin", os.Stdin)
|
|
|
|
} else {
|
2010-12-14 18:09:24 -07:00
|
|
|
for _, name := range flag.Args() {
|
|
|
|
// Is it a directory?
|
2011-11-30 10:04:16 -07:00
|
|
|
if fi, err := os.Stat(name); err == nil && fi.IsDir() {
|
2010-12-14 18:09:24 -07:00
|
|
|
walkDir(name)
|
|
|
|
} else {
|
|
|
|
doFile(name, nil)
|
|
|
|
}
|
2010-12-09 10:37:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// doFile analyzes one file. If the reader is nil, the source code is read from the
|
|
|
|
// named file.
|
|
|
|
func doFile(name string, reader io.Reader) {
|
|
|
|
fs := token.NewFileSet()
|
|
|
|
parsedFile, err := parser.ParseFile(fs, name, reader, 0)
|
|
|
|
if err != nil {
|
2011-06-29 07:52:34 -06:00
|
|
|
errorf("%s: %s", name, err)
|
2010-12-14 15:12:22 -07:00
|
|
|
return
|
2010-12-09 10:37:18 -07:00
|
|
|
}
|
2011-10-19 14:06:16 -06:00
|
|
|
file := &File{fset: fs, file: parsedFile}
|
2011-12-15 14:44:35 -07:00
|
|
|
file.walkFile(name, parsedFile)
|
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 {
|
|
|
|
errorf("walk error: %s", err)
|
|
|
|
return nil
|
|
|
|
}
|
2011-11-30 10:04:16 -07:00
|
|
|
if !f.IsDir() && strings.HasSuffix(path, ".go") {
|
2010-12-14 18:09:24 -07:00
|
|
|
doFile(path, nil)
|
|
|
|
}
|
2011-09-13 18:47:59 -06:00
|
|
|
return nil
|
2010-12-14 18:09:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// walkDir recursively walks the tree looking for .go files.
|
|
|
|
func walkDir(root string) {
|
2011-09-13 18:47:59 -06:00
|
|
|
filepath.Walk(root, visit)
|
2010-12-14 18:09:24 -07:00
|
|
|
}
|
|
|
|
|
2010-12-14 15:12:22 -07:00
|
|
|
// error formats the error to standard error, adding program
|
|
|
|
// identification and a newline
|
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...)
|
2010-12-14 15:12:22 -07:00
|
|
|
setExit(2)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Warn reports an error but does not set the exit code.
|
|
|
|
func (f *File) Warn(pos token.Pos, args ...interface{}) {
|
2011-10-19 14:06:16 -06:00
|
|
|
loc := f.fset.Position(pos).String() + ": "
|
2010-12-09 10:37:18 -07:00
|
|
|
fmt.Fprint(os.Stderr, loc+fmt.Sprintln(args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warnf reports a formatted error but does not set the exit code.
|
|
|
|
func (f *File) Warnf(pos token.Pos, format string, args ...interface{}) {
|
2011-10-19 14:06:16 -06:00
|
|
|
loc := f.fset.Position(pos).String() + ": "
|
2010-12-09 10:37:18 -07:00
|
|
|
fmt.Fprintf(os.Stderr, loc+format+"\n", args...)
|
|
|
|
}
|
|
|
|
|
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-01-30 08:57:11 -07:00
|
|
|
// walkCall walks an assignment statement
|
|
|
|
func (f *File) walkAssignStmt(stmt *ast.AssignStmt) {
|
|
|
|
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
|
|
|
|
|
|
|
// goFmt returns a string representation of the expression
|
|
|
|
func (f *File) gofmt(x ast.Expr) string {
|
|
|
|
f.b.Reset()
|
|
|
|
printer.Fprint(&f.b, f.fset, x)
|
|
|
|
return f.b.String()
|
|
|
|
}
|