OLD | NEW |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Vet is a simple checker for static errors in Go source code. | 5 // Vet is a simple checker for static errors in Go source code. |
6 // See doc.go for more information. | 6 // See doc.go for more information. |
7 package main | 7 package main |
8 | 8 |
9 import ( | 9 import ( |
10 "bytes" | 10 "bytes" |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 if dirs { | 130 if dirs { |
131 for _, name := range flag.Args() { | 131 for _, name := range flag.Args() { |
132 walkDir(name) | 132 walkDir(name) |
133 } | 133 } |
134 return | 134 return |
135 } | 135 } |
136 doPackage(flag.Args()) | 136 doPackage(flag.Args()) |
137 os.Exit(exitCode) | 137 os.Exit(exitCode) |
138 } | 138 } |
139 | 139 |
140 // doPackageDir analyzes the single package found in the directory, if there is
one. | 140 // prefixDirectory places the directory name on the beginning of each name in th
e list. |
| 141 func prefixDirectory(directory string, names []string) { |
| 142 » if directory != "." { |
| 143 » » for i, name := range names { |
| 144 » » » names[i] = filepath.Join(directory, name) |
| 145 » » } |
| 146 » } |
| 147 } |
| 148 |
| 149 // doPackageDir analyzes the single package found in the directory, if there is
one, |
| 150 // plus a test package, if there is one. |
141 func doPackageDir(directory string) { | 151 func doPackageDir(directory string) { |
142 pkg, err := build.Default.ImportDir(directory, 0) | 152 pkg, err := build.Default.ImportDir(directory, 0) |
143 if err != nil { | 153 if err != nil { |
144 // If it's just that there are no go source files, that's fine. | 154 // If it's just that there are no go source files, that's fine. |
145 if _, nogo := err.(*build.NoGoError); nogo { | 155 if _, nogo := err.(*build.NoGoError); nogo { |
146 return | 156 return |
147 } | 157 } |
148 // Non-fatal: we are doing a recursive walk and there may be oth
er directories. | 158 // Non-fatal: we are doing a recursive walk and there may be oth
er directories. |
149 warnf("cannot process directory %s: %s", directory, err) | 159 warnf("cannot process directory %s: %s", directory, err) |
150 return | 160 return |
151 } | 161 } |
152 names := append(pkg.GoFiles, pkg.CgoFiles...) | 162 names := append(pkg.GoFiles, pkg.CgoFiles...) |
153 » // Prefix file names with directory names. | 163 » names = append(names, pkg.TestGoFiles...) // These are also in the "foo"
package. |
154 » if directory != "." { | 164 » prefixDirectory(directory, names) |
155 » » for i, name := range names { | 165 » doPackage(names) |
156 » » » names[i] = filepath.Join(directory, name) | 166 » // Is there also a "foo_test" package? If so, do that one as well. |
157 » » } | 167 » if len(pkg.XTestGoFiles) > 0 { |
| 168 » » names = pkg.XTestGoFiles |
| 169 » » prefixDirectory(directory, names) |
| 170 » » doPackage(names) |
158 } | 171 } |
159 doPackage(names) | |
160 } | 172 } |
161 | 173 |
162 type Package struct { | 174 type Package struct { |
163 types map[ast.Expr]types.Type | 175 types map[ast.Expr]types.Type |
164 values map[ast.Expr]interface{} | 176 values map[ast.Expr]interface{} |
165 } | 177 } |
166 | 178 |
167 // doPackage analyzes the single package constructed from the named files. | 179 // doPackage analyzes the single package constructed from the named files. |
168 func doPackage(names []string) { | 180 func doPackage(names []string) { |
169 var files []*File | 181 var files []*File |
(...skipping 24 matching lines...) Expand all Loading... |
194 pkg.types[x] = typ | 206 pkg.types[x] = typ |
195 if val != nil { | 207 if val != nil { |
196 pkg.values[x] = val | 208 pkg.values[x] = val |
197 } | 209 } |
198 } | 210 } |
199 context := types.Context{ | 211 context := types.Context{ |
200 Expr: exprFn, | 212 Expr: exprFn, |
201 } | 213 } |
202 // Type check the package. | 214 // Type check the package. |
203 _, err := context.Check(fs, astFiles) | 215 _, err := context.Check(fs, astFiles) |
204 » if err != nil { | 216 » if err != nil && *verbose { |
205 warnf("%s", err) | 217 warnf("%s", err) |
206 } | 218 } |
207 for _, file := range files { | 219 for _, file := range files { |
208 file.pkg = pkg | 220 file.pkg = pkg |
209 file.walkFile(file.name, file.file) | 221 file.walkFile(file.name, file.file) |
210 } | 222 } |
211 } | 223 } |
212 | 224 |
213 func visit(path string, f os.FileInfo, err error) error { | 225 func visit(path string, f os.FileInfo, err error) error { |
214 if err != nil { | 226 if err != nil { |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 func (f *File) walkRangeStmt(n *ast.RangeStmt) { | 386 func (f *File) walkRangeStmt(n *ast.RangeStmt) { |
375 checkRangeLoop(f, n) | 387 checkRangeLoop(f, n) |
376 } | 388 } |
377 | 389 |
378 // goFmt returns a string representation of the expression | 390 // goFmt returns a string representation of the expression |
379 func (f *File) gofmt(x ast.Expr) string { | 391 func (f *File) gofmt(x ast.Expr) string { |
380 f.b.Reset() | 392 f.b.Reset() |
381 printer.Fprint(&f.b, f.fset, x) | 393 printer.Fprint(&f.b, f.fset, x) |
382 return f.b.String() | 394 return f.b.String() |
383 } | 395 } |
OLD | NEW |