Commit ddbee9ab authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

cmd/vet: support importing from source

Add a -source flag to cmd/vet that instructs
it to typecheck purely from source code.

Updates #16086
Fixes #19332

Change-Id: Ic83d0f14d5bb837a329d539b2873aeccdf7bf669
Reviewed-on: https://go-review.googlesource.com/37690Reviewed-by: default avatarRob Pike <r@golang.org>
parent 7e74d432
...@@ -34,6 +34,9 @@ If any flags are explicitly set to true, only those tests are run. Conversely, i ...@@ -34,6 +34,9 @@ If any flags are explicitly set to true, only those tests are run. Conversely, i
any flag is explicitly set to false, only those tests are disabled. Thus -printf=true any flag is explicitly set to false, only those tests are disabled. Thus -printf=true
runs the printf check, -printf=false runs all checks except the printf check. runs the printf check, -printf=false runs all checks except the printf check.
By default vet uses the object files generated by 'go install some/pkg' to typecheck the code.
If the -source flag is provided, vet uses only source code.
Available checks: Available checks:
Assembly declarations Assembly declarations
......
...@@ -25,6 +25,7 @@ import ( ...@@ -25,6 +25,7 @@ import (
var ( var (
verbose = flag.Bool("v", false, "verbose") verbose = flag.Bool("v", false, "verbose")
source = flag.Bool("source", false, "import from source instead of compiled object files")
tags = flag.String("tags", "", "space-separated list of build tags to apply when parsing") tags = flag.String("tags", "", "space-separated list of build tags to apply when parsing")
tagList = []string{} // exploded version of tags flag; set in main tagList = []string{} // exploded version of tags flag; set in main
) )
......
...@@ -61,7 +61,11 @@ func importType(path, name string) types.Type { ...@@ -61,7 +61,11 @@ func importType(path, name string) types.Type {
func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) error { func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) error {
if stdImporter == nil { if stdImporter == nil {
stdImporter = importer.Default() if *source {
stdImporter = importer.For("source", nil)
} else {
stdImporter = importer.Default()
}
inittypes() inittypes()
} }
pkg.defs = make(map[*ast.Ident]types.Object) pkg.defs = make(map[*ast.Ident]types.Object)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment