Commit a7d2d483 authored by Robert Griesemer's avatar Robert Griesemer

cmd/vet: adjust vet to use go/types and friends from std repo

- s|"golang.org/x/tools/go/exact"|"go/constant"|
- s|"golang.org/x/tools/go/types"|"go/types"|
- removed import of gcimporter
- import "go/importer" instead
- trivial adjustments to make use of go/importer
- adjusted import paths for whitelist.go

Change-Id: I43488ff44c329cd869c92dcc31193fb31bebfd29
Reviewed-on: https://go-review.googlesource.com/10695Reviewed-by: 's avatarRob Pike <r@golang.org>
parent 1b8b2c15
...@@ -7,11 +7,10 @@ ...@@ -7,11 +7,10 @@
package main package main
import ( import (
"cmd/vet/whitelist"
"flag" "flag"
"go/ast" "go/ast"
"strings" "strings"
"golang.org/x/tools/cmd/vet/whitelist"
) )
var compositeWhiteList = flag.Bool("compositewhitelist", true, "use composite white list; for testing only") var compositeWhiteList = flag.Bool("compositewhitelist", true, "use composite white list; for testing only")
......
...@@ -11,8 +11,7 @@ import ( ...@@ -11,8 +11,7 @@ import (
"fmt" "fmt"
"go/ast" "go/ast"
"go/token" "go/token"
"go/types"
"golang.org/x/tools/go/types"
) )
func init() { func init() {
......
...@@ -15,14 +15,12 @@ import ( ...@@ -15,14 +15,12 @@ import (
"go/parser" "go/parser"
"go/printer" "go/printer"
"go/token" "go/token"
"go/types"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
_ "golang.org/x/tools/go/gcimporter"
"golang.org/x/tools/go/types"
) )
var ( var (
......
...@@ -12,8 +12,7 @@ package main ...@@ -12,8 +12,7 @@ package main
import ( import (
"go/ast" "go/ast"
"go/token" "go/token"
"go/types"
"golang.org/x/tools/go/types"
) )
func init() { func init() {
......
...@@ -10,13 +10,12 @@ import ( ...@@ -10,13 +10,12 @@ import (
"bytes" "bytes"
"flag" "flag"
"go/ast" "go/ast"
"go/constant"
"go/token" "go/token"
"go/types"
"strconv" "strconv"
"strings" "strings"
"unicode/utf8" "unicode/utf8"
"golang.org/x/tools/go/exact"
"golang.org/x/tools/go/types"
) )
var printfuncs = flag.String("printfuncs", "", "comma-separated list of print function names to check") var printfuncs = flag.String("printfuncs", "", "comma-separated list of print function names to check")
...@@ -160,11 +159,11 @@ func (f *File) checkPrintf(call *ast.CallExpr, name string, formatIndex int) { ...@@ -160,11 +159,11 @@ func (f *File) checkPrintf(call *ast.CallExpr, name string, formatIndex int) {
} }
return return
} }
if lit.Kind() != exact.String { if lit.Kind() != constant.String {
f.Badf(call.Pos(), "constant %v not a string in call to %s", lit, name) f.Badf(call.Pos(), "constant %v not a string in call to %s", lit, name)
return return
} }
format := exact.StringVal(lit) format := constant.StringVal(lit)
firstArg := formatIndex + 1 // Arguments are immediately after format string. firstArg := formatIndex + 1 // Arguments are immediately after format string.
if !strings.Contains(format, "%") { if !strings.Contains(format, "%") {
if len(call.Args) > firstArg { if len(call.Args) > firstArg {
......
...@@ -34,8 +34,7 @@ import ( ...@@ -34,8 +34,7 @@ import (
"flag" "flag"
"go/ast" "go/ast"
"go/token" "go/token"
"go/types"
"golang.org/x/tools/go/types"
) )
var strictShadowing = flag.Bool("shadowstrict", false, "whether to be strict about shadowing; can be noisy") var strictShadowing = flag.Bool("shadowstrict", false, "whether to be strict about shadowing; can be noisy")
......
...@@ -10,10 +10,9 @@ package main ...@@ -10,10 +10,9 @@ package main
import ( import (
"go/ast" "go/ast"
"go/constant"
"go/token" "go/token"
"go/types"
"golang.org/x/tools/go/exact"
"golang.org/x/tools/go/types"
) )
func init() { func init() {
...@@ -46,7 +45,7 @@ func checkLongShift(f *File, node ast.Node, x, y ast.Expr) { ...@@ -46,7 +45,7 @@ func checkLongShift(f *File, node ast.Node, x, y ast.Expr) {
if v == nil { if v == nil {
return return
} }
amt, ok := exact.Int64Val(v) amt, ok := constant.Int64Val(v)
if !ok { if !ok {
return return
} }
......
...@@ -8,14 +8,15 @@ package main ...@@ -8,14 +8,15 @@ package main
import ( import (
"go/ast" "go/ast"
"go/importer"
"go/token" "go/token"
"go/types"
"golang.org/x/tools/go/types"
) )
// imports is the canonical map of imported packages we need for typechecking. // stdImporter is the importer we use to import packages.
// It is created during initialization. // It is created during initialization so that all packages
var imports = make(map[string]*types.Package) // are imported by the same importer.
var stdImporter = importer.Default()
var ( var (
stringerMethodType = types.New("func() string") stringerMethodType = types.New("func() string")
...@@ -35,7 +36,7 @@ func init() { ...@@ -35,7 +36,7 @@ func init() {
// path.name, and adds the respective package to the imports map // path.name, and adds the respective package to the imports map
// as a side effect. // as a side effect.
func importType(path, name string) types.Type { func importType(path, name string) types.Type {
pkg, err := types.DefaultImport(imports, path) pkg, err := stdImporter.Import(path)
if err != nil { if err != nil {
// This can happen if fmt hasn't been compiled yet. // This can happen if fmt hasn't been compiled yet.
// Since nothing uses formatterType anyway, don't complain. // Since nothing uses formatterType anyway, don't complain.
...@@ -56,9 +57,9 @@ func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) error { ...@@ -56,9 +57,9 @@ func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) error {
pkg.spans = make(map[types.Object]Span) pkg.spans = make(map[types.Object]Span)
pkg.types = make(map[ast.Expr]types.TypeAndValue) pkg.types = make(map[ast.Expr]types.TypeAndValue)
config := types.Config{ config := types.Config{
// We provide the same packages map for all imports to ensure // We use the same importer for all imports to ensure that
// that everybody sees identical packages for the given paths. // everybody sees identical packages for the given paths.
Packages: imports, Importer: stdImporter,
// By providing a Config with our own error function, it will continue // By providing a Config with our own error function, it will continue
// past the first error. There is no need for that function to do anything. // past the first error. There is no need for that function to do anything.
Error: func(error) {}, Error: func(error) {},
......
...@@ -9,8 +9,7 @@ package main ...@@ -9,8 +9,7 @@ package main
import ( import (
"go/ast" "go/ast"
"go/token" "go/token"
"go/types"
"golang.org/x/tools/go/types"
) )
func init() { func init() {
......
...@@ -11,9 +11,8 @@ import ( ...@@ -11,9 +11,8 @@ import (
"flag" "flag"
"go/ast" "go/ast"
"go/token" "go/token"
"go/types"
"strings" "strings"
"golang.org/x/tools/go/types"
) )
var unusedFuncsFlag = flag.String("unusedfuncs", var unusedFuncsFlag = flag.String("unusedfuncs",
......
...@@ -87,7 +87,7 @@ func TestTags(t *testing.T) { ...@@ -87,7 +87,7 @@ func TestTags(t *testing.T) {
"-v", // We're going to look at the files it examines. "-v", // We're going to look at the files it examines.
"testdata/tagtest", "testdata/tagtest",
} }
cmd = exec.Command(filepath.Join(".", binary), args...) cmd = exec.Command("./"+binary, args...)
output, err := cmd.CombinedOutput() output, err := cmd.CombinedOutput()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package whitelist defines exceptions for the vet tool. // Package whitelist defines exceptions for the vet tool.
package whitelist // import "golang.org/x/tools/cmd/vet/whitelist" package whitelist // import "cmd/vet/whitelist"
// UnkeyedLiteral are types that are actually slices, but // UnkeyedLiteral are types that are actually slices, but
// syntactically, we cannot tell whether the Typ in pkg.Typ{1, 2, 3} // syntactically, we cannot tell whether the Typ in pkg.Typ{1, 2, 3}
......
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