aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/vet/main.go
diff options
context:
space:
mode:
authorKonstantin Shaposhnikov <k.shaposhnikov@gmail.com>2016-06-27 17:13:15 +0800
committerIan Lance Taylor <iant@golang.org>2016-06-28 22:09:00 +0000
commit85a4f44745859ecdd71c034171d40263651f8594 (patch)
treeede9b4b7655ef1be37411cf5b19ae9deb47855b8 /src/cmd/vet/main.go
parent733aefd06e5cf708637308a4ad7a048aa97db5cd (diff)
downloadgo-85a4f44745859ecdd71c034171d40263651f8594.tar.gz
go-85a4f44745859ecdd71c034171d40263651f8594.zip
cmd/vet: make checking example names in _test packages more robust
Prior to this change package "foo" had to be installed in order to check example names in "foo_test" package. However by the time "foo_test" package is checked a parsed "foo" package has been already constructed. Use it to check example names. Also change TestDivergentPackagesExamples test to pass directory of the package to the vet tool as it is the most common way to invoke it. This requires changes to errchk to add support for grabbing source files from a directory. Fixes #16189 Change-Id: Ief103d07b024822282b86c24250835cc591793e8 Reviewed-on: https://go-review.googlesource.com/24488 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/cmd/vet/main.go')
-rw-r--r--src/cmd/vet/main.go22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/cmd/vet/main.go b/src/cmd/vet/main.go
index 8212a14f03..4f3cca8f6d 100644
--- a/src/cmd/vet/main.go
+++ b/src/cmd/vet/main.go
@@ -182,6 +182,9 @@ type File struct {
file *ast.File
b bytes.Buffer // for use by methods
+ // Parsed package "foo" when checking package "foo_test"
+ basePkg *Package
+
// The objects that are receivers of a "String() string" method.
// This is used by the recursiveStringer method in print.go.
stringers map[*ast.Object]bool
@@ -238,7 +241,7 @@ func main() {
}
os.Exit(exitCode)
}
- if !doPackage(".", flag.Args()) {
+ if doPackage(".", flag.Args(), nil) == nil {
warnf("no files checked")
}
os.Exit(exitCode)
@@ -278,12 +281,12 @@ func doPackageDir(directory string) {
names = append(names, pkg.TestGoFiles...) // These are also in the "foo" package.
names = append(names, pkg.SFiles...)
prefixDirectory(directory, names)
- doPackage(directory, names)
+ basePkg := doPackage(directory, names, nil)
// 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(directory, names)
+ doPackage(directory, names, basePkg)
}
}
@@ -299,8 +302,8 @@ type Package struct {
}
// doPackage analyzes the single package constructed from the named files.
-// It returns whether any files were checked.
-func doPackage(directory string, names []string) bool {
+// It returns the parsed Package or nil if none of the files have been checked.
+func doPackage(directory string, names []string, basePkg *Package) *Package {
var files []*File
var astFiles []*ast.File
fs := token.NewFileSet()
@@ -309,7 +312,7 @@ func doPackage(directory string, names []string) bool {
if err != nil {
// Warn but continue to next package.
warnf("%s: %s", name, err)
- return false
+ return nil
}
checkBuildTag(name, data)
var parsedFile *ast.File
@@ -317,14 +320,14 @@ func doPackage(directory string, names []string) bool {
parsedFile, err = parser.ParseFile(fs, name, data, 0)
if err != nil {
warnf("%s: %s", name, err)
- return false
+ return nil
}
astFiles = append(astFiles, parsedFile)
}
files = append(files, &File{fset: fs, content: data, name: name, file: parsedFile})
}
if len(astFiles) == 0 {
- return false
+ return nil
}
pkg := new(Package)
pkg.path = astFiles[0].Name.Name
@@ -346,13 +349,14 @@ func doPackage(directory string, names []string) bool {
}
for _, file := range files {
file.pkg = pkg
+ file.basePkg = basePkg
file.checkers = chk
if file.file != nil {
file.walkFile(file.name, file.file)
}
}
asmCheck(pkg)
- return true
+ return pkg
}
func visit(path string, f os.FileInfo, err error) error {