PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/cmd/vet/main.go

http://github.com/tav/go
Go | 239 lines | 179 code | 28 blank | 32 comment | 36 complexity | a2b67fae8f5183af2d782df2126249d5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Vet is a simple checker for static errors in Go source code.
  5. // See doc.go for more information.
  6. package main
  7. import (
  8. "bytes"
  9. "flag"
  10. "fmt"
  11. "go/ast"
  12. "go/parser"
  13. "go/token"
  14. "io"
  15. "os"
  16. "path/filepath"
  17. "strconv"
  18. "strings"
  19. )
  20. var verbose = flag.Bool("v", false, "verbose")
  21. var exitCode = 0
  22. // setExit sets the value for os.Exit when it is called, later. It
  23. // remembers the highest value.
  24. func setExit(err int) {
  25. if err > exitCode {
  26. exitCode = err
  27. }
  28. }
  29. // Usage is a replacement usage function for the flags package.
  30. func Usage() {
  31. fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
  32. flag.PrintDefaults()
  33. os.Exit(2)
  34. }
  35. // File is a wrapper for the state of a file used in the parser.
  36. // The parse tree walkers are all methods of this type.
  37. type File struct {
  38. fset *token.FileSet
  39. file *ast.File
  40. b bytes.Buffer // for use by methods
  41. }
  42. func main() {
  43. flag.Usage = Usage
  44. flag.Parse()
  45. if *printfuncs != "" {
  46. for _, name := range strings.Split(*printfuncs, ",") {
  47. if len(name) == 0 {
  48. flag.Usage()
  49. }
  50. skip := 0
  51. if colon := strings.LastIndex(name, ":"); colon > 0 {
  52. var err error
  53. skip, err = strconv.Atoi(name[colon+1:])
  54. if err != nil {
  55. errorf(`illegal format for "Func:N" argument %q; %s`, name, err)
  56. }
  57. name = name[:colon]
  58. }
  59. name = strings.ToLower(name)
  60. if name[len(name)-1] == 'f' {
  61. printfList[name] = skip
  62. } else {
  63. printList[name] = skip
  64. }
  65. }
  66. }
  67. if flag.NArg() == 0 {
  68. doFile("stdin", os.Stdin)
  69. } else {
  70. for _, name := range flag.Args() {
  71. // Is it a directory?
  72. if fi, err := os.Stat(name); err == nil && fi.IsDir() {
  73. walkDir(name)
  74. } else {
  75. doFile(name, nil)
  76. }
  77. }
  78. }
  79. os.Exit(exitCode)
  80. }
  81. // doFile analyzes one file. If the reader is nil, the source code is read from the
  82. // named file.
  83. func doFile(name string, reader io.Reader) {
  84. fs := token.NewFileSet()
  85. parsedFile, err := parser.ParseFile(fs, name, reader, 0)
  86. if err != nil {
  87. errorf("%s: %s", name, err)
  88. return
  89. }
  90. file := &File{fset: fs, file: parsedFile}
  91. file.walkFile(name, parsedFile)
  92. }
  93. func visit(path string, f os.FileInfo, err error) error {
  94. if err != nil {
  95. errorf("walk error: %s", err)
  96. return nil
  97. }
  98. if !f.IsDir() && strings.HasSuffix(path, ".go") {
  99. doFile(path, nil)
  100. }
  101. return nil
  102. }
  103. // walkDir recursively walks the tree looking for .go files.
  104. func walkDir(root string) {
  105. filepath.Walk(root, visit)
  106. }
  107. // error formats the error to standard error, adding program
  108. // identification and a newline
  109. func errorf(format string, args ...interface{}) {
  110. fmt.Fprintf(os.Stderr, "vet: "+format+"\n", args...)
  111. setExit(2)
  112. }
  113. // Println is fmt.Println guarded by -v.
  114. func Println(args ...interface{}) {
  115. if !*verbose {
  116. return
  117. }
  118. fmt.Println(args...)
  119. }
  120. // Printf is fmt.Printf guarded by -v.
  121. func Printf(format string, args ...interface{}) {
  122. if !*verbose {
  123. return
  124. }
  125. fmt.Printf(format+"\n", args...)
  126. }
  127. // Bad reports an error and sets the exit code..
  128. func (f *File) Bad(pos token.Pos, args ...interface{}) {
  129. f.Warn(pos, args...)
  130. setExit(1)
  131. }
  132. // Badf reports a formatted error and sets the exit code.
  133. func (f *File) Badf(pos token.Pos, format string, args ...interface{}) {
  134. f.Warnf(pos, format, args...)
  135. setExit(1)
  136. }
  137. // Warn reports an error but does not set the exit code.
  138. func (f *File) Warn(pos token.Pos, args ...interface{}) {
  139. loc := f.fset.Position(pos).String() + ": "
  140. fmt.Fprint(os.Stderr, loc+fmt.Sprintln(args...))
  141. }
  142. // Warnf reports a formatted error but does not set the exit code.
  143. func (f *File) Warnf(pos token.Pos, format string, args ...interface{}) {
  144. loc := f.fset.Position(pos).String() + ": "
  145. fmt.Fprintf(os.Stderr, loc+format+"\n", args...)
  146. }
  147. // walkFile walks the file's tree.
  148. func (f *File) walkFile(name string, file *ast.File) {
  149. Println("Checking file", name)
  150. ast.Walk(f, file)
  151. }
  152. // Visit implements the ast.Visitor interface.
  153. func (f *File) Visit(node ast.Node) ast.Visitor {
  154. switch n := node.(type) {
  155. case *ast.CallExpr:
  156. f.walkCallExpr(n)
  157. case *ast.CompositeLit:
  158. f.walkCompositeLit(n)
  159. case *ast.Field:
  160. f.walkFieldTag(n)
  161. case *ast.FuncDecl:
  162. f.walkMethodDecl(n)
  163. case *ast.InterfaceType:
  164. f.walkInterfaceType(n)
  165. }
  166. return f
  167. }
  168. // walkCall walks a call expression.
  169. func (f *File) walkCall(call *ast.CallExpr, name string) {
  170. f.checkFmtPrintfCall(call, name)
  171. }
  172. // walkCompositeLit walks a composite literal.
  173. func (f *File) walkCompositeLit(c *ast.CompositeLit) {
  174. f.checkUntaggedLiteral(c)
  175. }
  176. // walkFieldTag walks a struct field tag.
  177. func (f *File) walkFieldTag(field *ast.Field) {
  178. if field.Tag == nil {
  179. return
  180. }
  181. f.checkCanonicalFieldTag(field)
  182. }
  183. // walkMethodDecl walks the method's signature.
  184. func (f *File) walkMethod(id *ast.Ident, t *ast.FuncType) {
  185. f.checkCanonicalMethod(id, t)
  186. }
  187. // walkMethodDecl walks the method signature in the declaration.
  188. func (f *File) walkMethodDecl(d *ast.FuncDecl) {
  189. if d.Recv == nil {
  190. // not a method
  191. return
  192. }
  193. f.walkMethod(d.Name, d.Type)
  194. }
  195. // walkInterfaceType walks the method signatures of an interface.
  196. func (f *File) walkInterfaceType(t *ast.InterfaceType) {
  197. for _, field := range t.Methods.List {
  198. for _, id := range field.Names {
  199. f.walkMethod(id, field.Type.(*ast.FuncType))
  200. }
  201. }
  202. }
  203. // walkCallExpr walks a call expression.
  204. func (f *File) walkCallExpr(call *ast.CallExpr) {
  205. switch x := call.Fun.(type) {
  206. case *ast.Ident:
  207. f.walkCall(call, x.Name)
  208. case *ast.SelectorExpr:
  209. f.walkCall(call, x.Sel.Name)
  210. }
  211. }