2013-05-17 14:20:39 -06:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-05-20 13:20:05 -06:00
|
|
|
// This file contains the pieces of the tool that use typechecking from the go/types package.
|
2013-05-17 14:20:39 -06:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2015-04-13 21:53:55 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/go/types"
|
2013-05-17 14:20:39 -06:00
|
|
|
)
|
|
|
|
|
2015-04-13 21:53:55 -06:00
|
|
|
// imports is the canonical map of imported packages we need for typechecking.
|
|
|
|
// It is created during initialization.
|
|
|
|
var imports = make(map[string]*types.Package)
|
2014-09-24 14:54:50 -06:00
|
|
|
|
|
|
|
var (
|
|
|
|
stringerMethodType = types.New("func() string")
|
|
|
|
errorType = types.New("error").Underlying().(*types.Interface)
|
2014-10-14 13:55:46 -06:00
|
|
|
stringerType = types.New("interface{ String() string }").(*types.Interface)
|
|
|
|
formatterType *types.Interface
|
2014-09-24 14:54:50 -06:00
|
|
|
)
|
|
|
|
|
2014-10-14 13:55:46 -06:00
|
|
|
func init() {
|
|
|
|
typ := importType("fmt", "Formatter")
|
|
|
|
if typ != nil {
|
|
|
|
formatterType = typ.Underlying().(*types.Interface)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 14:54:50 -06:00
|
|
|
// importType returns the type denoted by the qualified identifier
|
|
|
|
// path.name, and adds the respective package to the imports map
|
|
|
|
// as a side effect.
|
|
|
|
func importType(path, name string) types.Type {
|
2015-04-13 21:53:55 -06:00
|
|
|
pkg, err := types.DefaultImport(imports, path)
|
2014-09-24 14:54:50 -06:00
|
|
|
if err != nil {
|
2015-02-23 08:57:19 -07:00
|
|
|
// This can happen if fmt hasn't been compiled yet.
|
|
|
|
// Since nothing uses formatterType anyway, don't complain.
|
|
|
|
//warnf("import failed: %v", err)
|
2014-10-14 23:56:09 -06:00
|
|
|
return nil
|
2014-09-24 14:54:50 -06:00
|
|
|
}
|
|
|
|
if obj, ok := pkg.Scope().Lookup(name).(*types.TypeName); ok {
|
|
|
|
return obj.Type()
|
|
|
|
}
|
2014-10-14 23:56:09 -06:00
|
|
|
warnf("invalid type name %q", name)
|
|
|
|
return nil
|
2014-09-24 14:54:50 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 14:20:39 -06:00
|
|
|
func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) error {
|
2014-02-27 11:21:59 -07:00
|
|
|
pkg.defs = make(map[*ast.Ident]types.Object)
|
|
|
|
pkg.uses = make(map[*ast.Ident]types.Object)
|
2015-03-03 13:01:37 -07:00
|
|
|
pkg.selectors = make(map[*ast.SelectorExpr]*types.Selection)
|
2013-06-21 12:27:53 -06:00
|
|
|
pkg.spans = make(map[types.Object]Span)
|
2014-01-28 14:46:24 -07:00
|
|
|
pkg.types = make(map[ast.Expr]types.TypeAndValue)
|
2013-07-18 18:07:44 -06:00
|
|
|
config := types.Config{
|
2015-04-13 21:53:55 -06:00
|
|
|
// We provide the same packages map for all imports to ensure
|
|
|
|
// that everybody sees identical packages for the given paths.
|
|
|
|
Packages: imports,
|
2014-09-24 14:54:50 -06:00
|
|
|
// 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.
|
2013-05-17 14:20:39 -06:00
|
|
|
Error: func(error) {},
|
|
|
|
}
|
2013-07-18 14:09:03 -06:00
|
|
|
info := &types.Info{
|
2015-03-03 13:01:37 -07:00
|
|
|
Selections: pkg.selectors,
|
|
|
|
Types: pkg.types,
|
|
|
|
Defs: pkg.defs,
|
|
|
|
Uses: pkg.uses,
|
2013-07-18 14:09:03 -06:00
|
|
|
}
|
2013-12-09 21:14:28 -07:00
|
|
|
typesPkg, err := config.Check(pkg.path, fs, astFiles, info)
|
|
|
|
pkg.typesPkg = typesPkg
|
2013-07-18 14:09:03 -06:00
|
|
|
// update spans
|
2014-02-27 11:21:59 -07:00
|
|
|
for id, obj := range pkg.defs {
|
|
|
|
pkg.growSpan(id, obj)
|
|
|
|
}
|
|
|
|
for id, obj := range pkg.uses {
|
2013-07-18 14:09:03 -06:00
|
|
|
pkg.growSpan(id, obj)
|
|
|
|
}
|
2013-05-17 14:20:39 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// isStruct reports whether the composite literal c is a struct.
|
|
|
|
// If it is not (probably a struct), it returns a printable form of the type.
|
|
|
|
func (pkg *Package) isStruct(c *ast.CompositeLit) (bool, string) {
|
2013-08-02 20:19:59 -06:00
|
|
|
// Check that the CompositeLit's type is a slice or array (which needs no field keys), if possible.
|
2014-01-28 14:46:24 -07:00
|
|
|
typ := pkg.types[c].Type
|
2013-05-20 13:20:05 -06:00
|
|
|
// If it's a named type, pull out the underlying type. If it's not, the Underlying
|
|
|
|
// method returns the type itself.
|
2013-05-17 14:20:39 -06:00
|
|
|
actual := typ
|
2013-05-20 13:20:05 -06:00
|
|
|
if actual != nil {
|
|
|
|
actual = actual.Underlying()
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
|
|
|
if actual == nil {
|
|
|
|
// No type information available. Assume true, so we do the check.
|
|
|
|
return true, ""
|
|
|
|
}
|
|
|
|
switch actual.(type) {
|
|
|
|
case *types.Struct:
|
|
|
|
return true, typ.String()
|
|
|
|
default:
|
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 16:37:49 -06:00
|
|
|
// matchArgType reports an error if printf verb t is not appropriate
|
|
|
|
// for operand arg.
|
|
|
|
//
|
|
|
|
// typ is used only for recursive calls; external callers must supply nil.
|
|
|
|
//
|
|
|
|
// (Recursion arises from the compound types {map,chan,slice} which
|
|
|
|
// may be printed with %d etc. if that is appropriate for their element
|
|
|
|
// types.)
|
|
|
|
func (f *File) matchArgType(t printfArgType, typ types.Type, arg ast.Expr) bool {
|
2013-09-17 20:57:59 -06:00
|
|
|
return f.matchArgTypeInternal(t, typ, arg, make(map[types.Type]bool))
|
|
|
|
}
|
|
|
|
|
|
|
|
// matchArgTypeInternal is the internal version of matchArgType. It carries a map
|
|
|
|
// remembering what types are in progress so we don't recur when faced with recursive
|
|
|
|
// types or mutually recursive types.
|
|
|
|
func (f *File) matchArgTypeInternal(t printfArgType, typ types.Type, arg ast.Expr, inProgress map[types.Type]bool) bool {
|
2013-07-29 21:15:31 -06:00
|
|
|
// %v, %T accept any argument type.
|
|
|
|
if t == anyType {
|
|
|
|
return true
|
|
|
|
}
|
2013-05-17 14:20:39 -06:00
|
|
|
if typ == nil {
|
2013-07-15 16:37:49 -06:00
|
|
|
// external call
|
2014-01-28 14:46:24 -07:00
|
|
|
typ = f.pkg.types[arg].Type
|
2013-07-15 16:37:49 -06:00
|
|
|
if typ == nil {
|
|
|
|
return true // probably a type check problem
|
|
|
|
}
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
2013-08-27 19:24:43 -06:00
|
|
|
// If the type implements fmt.Formatter, we have nothing to check.
|
|
|
|
// But (see issue 6259) that's not easy to verify, so instead we see
|
|
|
|
// if its method set contains a Format function. We could do better,
|
|
|
|
// even now, but we don't need to be 100% accurate. Wait for 6259 to
|
|
|
|
// be fixed instead. TODO.
|
2014-03-12 18:19:34 -06:00
|
|
|
if f.hasMethod(typ, "Format") {
|
2013-08-27 19:24:43 -06:00
|
|
|
return true
|
|
|
|
}
|
2013-10-09 15:17:25 -06:00
|
|
|
// If we can use a string, might arg (dynamically) implement the Stringer or Error interface?
|
2013-07-12 15:52:00 -06:00
|
|
|
if t&argString != 0 {
|
2014-02-03 17:02:56 -07:00
|
|
|
if types.AssertableTo(errorType, typ) || types.AssertableTo(stringerType, typ) {
|
2013-06-06 17:05:14 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2013-07-15 16:37:49 -06:00
|
|
|
|
2013-09-17 20:57:59 -06:00
|
|
|
typ = typ.Underlying()
|
|
|
|
if inProgress[typ] {
|
|
|
|
// We're already looking at this type. The call that started it will take care of it.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
inProgress[typ] = true
|
|
|
|
|
|
|
|
switch typ := typ.(type) {
|
2013-07-15 16:37:49 -06:00
|
|
|
case *types.Signature:
|
|
|
|
return t&argPointer != 0
|
|
|
|
|
|
|
|
case *types.Map:
|
2013-07-29 22:00:52 -06:00
|
|
|
// Recur: map[int]int matches %d.
|
2013-07-15 16:37:49 -06:00
|
|
|
return t&argPointer != 0 ||
|
2013-09-17 20:57:59 -06:00
|
|
|
(f.matchArgTypeInternal(t, typ.Key(), arg, inProgress) && f.matchArgTypeInternal(t, typ.Elem(), arg, inProgress))
|
2013-07-15 16:37:49 -06:00
|
|
|
|
|
|
|
case *types.Chan:
|
|
|
|
return t&argPointer != 0
|
|
|
|
|
2013-07-29 22:00:52 -06:00
|
|
|
case *types.Array:
|
|
|
|
// Same as slice.
|
2014-01-28 11:57:56 -07:00
|
|
|
if types.Identical(typ.Elem().Underlying(), types.Typ[types.Byte]) && t&argString != 0 {
|
2013-07-29 22:00:52 -06:00
|
|
|
return true // %s matches []byte
|
|
|
|
}
|
|
|
|
// Recur: []int matches %d.
|
2013-09-17 20:57:59 -06:00
|
|
|
return t&argPointer != 0 || f.matchArgTypeInternal(t, typ.Elem().Underlying(), arg, inProgress)
|
2013-07-29 22:00:52 -06:00
|
|
|
|
2013-07-15 16:37:49 -06:00
|
|
|
case *types.Slice:
|
2013-07-29 22:00:52 -06:00
|
|
|
// Same as array.
|
2014-01-28 11:57:56 -07:00
|
|
|
if types.Identical(typ.Elem().Underlying(), types.Typ[types.Byte]) && t&argString != 0 {
|
2013-07-15 16:37:49 -06:00
|
|
|
return true // %s matches []byte
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
2013-09-17 20:57:59 -06:00
|
|
|
// Recur: []int matches %d. But watch out for
|
|
|
|
// type T []T
|
|
|
|
// If the element is a pointer type (type T[]*T), it's handled fine by the Pointer case below.
|
|
|
|
return t&argPointer != 0 || f.matchArgTypeInternal(t, typ.Elem(), arg, inProgress)
|
2013-07-15 16:37:49 -06:00
|
|
|
|
2013-07-29 21:15:31 -06:00
|
|
|
case *types.Pointer:
|
2013-07-29 21:41:07 -06:00
|
|
|
// Ugly, but dealing with an edge case: a known pointer to an invalid type,
|
|
|
|
// probably something from a failed import.
|
|
|
|
if typ.Elem().String() == "invalid type" {
|
|
|
|
if *verbose {
|
|
|
|
f.Warnf(arg.Pos(), "printf argument %v is pointer to invalid or unknown type", f.gofmt(arg))
|
|
|
|
}
|
|
|
|
return true // special case
|
|
|
|
}
|
2013-08-02 06:52:38 -06:00
|
|
|
// If it's actually a pointer with %p, it prints as one.
|
|
|
|
if t == argPointer {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// If it's pointer to struct, that's equivalent in our analysis to whether we can print the struct.
|
|
|
|
if str, ok := typ.Elem().Underlying().(*types.Struct); ok {
|
2013-09-17 20:57:59 -06:00
|
|
|
return f.matchStructArgType(t, str, arg, inProgress)
|
2013-08-02 06:52:38 -06:00
|
|
|
}
|
|
|
|
// The rest can print with %p as pointers, or as integers with %x etc.
|
|
|
|
return t&(argInt|argPointer) != 0
|
|
|
|
|
|
|
|
case *types.Struct:
|
2013-09-17 20:57:59 -06:00
|
|
|
return f.matchStructArgType(t, typ, arg, inProgress)
|
2013-07-29 21:15:31 -06:00
|
|
|
|
2013-07-29 23:51:35 -06:00
|
|
|
case *types.Interface:
|
|
|
|
// If the static type of the argument is empty interface, there's little we can do.
|
|
|
|
// Example:
|
|
|
|
// func f(x interface{}) { fmt.Printf("%s", x) }
|
|
|
|
// Whether x is valid for %s depends on the type of the argument to f. One day
|
|
|
|
// we will be able to do better. For now, we assume that empty interface is OK
|
|
|
|
// but non-empty interfaces, with Stringer and Error handled above, are errors.
|
|
|
|
return typ.NumMethods() == 0
|
|
|
|
|
2013-07-15 16:37:49 -06:00
|
|
|
case *types.Basic:
|
|
|
|
switch typ.Kind() {
|
|
|
|
case types.UntypedBool,
|
|
|
|
types.Bool:
|
|
|
|
return t&argBool != 0
|
|
|
|
|
|
|
|
case types.UntypedInt,
|
|
|
|
types.Int,
|
|
|
|
types.Int8,
|
|
|
|
types.Int16,
|
|
|
|
types.Int32,
|
|
|
|
types.Int64,
|
|
|
|
types.Uint,
|
|
|
|
types.Uint8,
|
|
|
|
types.Uint16,
|
|
|
|
types.Uint32,
|
|
|
|
types.Uint64,
|
|
|
|
types.Uintptr:
|
|
|
|
return t&argInt != 0
|
|
|
|
|
|
|
|
case types.UntypedFloat,
|
|
|
|
types.Float32,
|
|
|
|
types.Float64:
|
|
|
|
return t&argFloat != 0
|
|
|
|
|
|
|
|
case types.UntypedComplex,
|
|
|
|
types.Complex64,
|
|
|
|
types.Complex128:
|
|
|
|
return t&argComplex != 0
|
|
|
|
|
|
|
|
case types.UntypedString,
|
|
|
|
types.String:
|
|
|
|
return t&argString != 0
|
|
|
|
|
|
|
|
case types.UnsafePointer:
|
|
|
|
return t&(argPointer|argInt) != 0
|
|
|
|
|
|
|
|
case types.UntypedRune:
|
|
|
|
return t&(argInt|argRune) != 0
|
|
|
|
|
|
|
|
case types.UntypedNil:
|
|
|
|
return t&argPointer != 0 // TODO?
|
|
|
|
|
|
|
|
case types.Invalid:
|
|
|
|
if *verbose {
|
|
|
|
f.Warnf(arg.Pos(), "printf argument %v has invalid or unknown type", f.gofmt(arg))
|
|
|
|
}
|
|
|
|
return true // Probably a type check problem.
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
2013-07-15 16:37:49 -06:00
|
|
|
panic("unreachable")
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
2013-07-15 16:37:49 -06:00
|
|
|
|
2013-05-17 14:20:39 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-05-15 13:32:51 -06:00
|
|
|
// hasBasicType reports whether x's type is a types.Basic with the given kind.
|
|
|
|
func (f *File) hasBasicType(x ast.Expr, kind types.BasicKind) bool {
|
|
|
|
t := f.pkg.types[x].Type
|
|
|
|
if t != nil {
|
|
|
|
t = t.Underlying()
|
|
|
|
}
|
|
|
|
b, ok := t.(*types.Basic)
|
|
|
|
return ok && b.Kind() == kind
|
|
|
|
}
|
|
|
|
|
2013-08-02 06:52:38 -06:00
|
|
|
// matchStructArgType reports whether all the elements of the struct match the expected
|
|
|
|
// type. For instance, with "%d" all the elements must be printable with the "%d" format.
|
2013-09-17 20:57:59 -06:00
|
|
|
func (f *File) matchStructArgType(t printfArgType, typ *types.Struct, arg ast.Expr, inProgress map[types.Type]bool) bool {
|
2013-08-02 06:52:38 -06:00
|
|
|
for i := 0; i < typ.NumFields(); i++ {
|
2013-09-17 20:57:59 -06:00
|
|
|
if !f.matchArgTypeInternal(t, typ.Field(i).Type(), arg, inProgress) {
|
2013-08-02 06:52:38 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-05-17 14:20:39 -06:00
|
|
|
// numArgsInSignature tells how many formal arguments the function type
|
|
|
|
// being called has.
|
|
|
|
func (f *File) numArgsInSignature(call *ast.CallExpr) int {
|
|
|
|
// Check the type of the function or method declaration
|
2014-01-28 14:46:24 -07:00
|
|
|
typ := f.pkg.types[call.Fun].Type
|
2013-05-17 14:20:39 -06:00
|
|
|
if typ == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
// The type must be a signature, but be sure for safety.
|
|
|
|
sig, ok := typ.(*types.Signature)
|
|
|
|
if !ok {
|
|
|
|
return 0
|
|
|
|
}
|
2013-05-20 13:20:05 -06:00
|
|
|
return sig.Params().Len()
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// isErrorMethodCall reports whether the call is of a method with signature
|
|
|
|
// func Error() string
|
|
|
|
// where "string" is the universe's string type. We know the method is called "Error".
|
|
|
|
func (f *File) isErrorMethodCall(call *ast.CallExpr) bool {
|
2014-01-28 14:46:24 -07:00
|
|
|
typ := f.pkg.types[call].Type
|
2013-07-12 15:52:00 -06:00
|
|
|
if typ != nil {
|
|
|
|
// We know it's called "Error", so just check the function signature.
|
2014-01-28 14:46:24 -07:00
|
|
|
return types.Identical(f.pkg.types[call.Fun].Type, stringerMethodType)
|
2013-07-12 15:52:00 -06:00
|
|
|
}
|
|
|
|
// Without types, we can still check by hand.
|
2013-05-17 14:20:39 -06:00
|
|
|
// Is it a selector expression? Otherwise it's a function call, not a method call.
|
|
|
|
sel, ok := call.Fun.(*ast.SelectorExpr)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// The package is type-checked, so if there are no arguments, we're done.
|
|
|
|
if len(call.Args) > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Check the type of the method declaration
|
2014-01-28 14:46:24 -07:00
|
|
|
typ = f.pkg.types[sel].Type
|
2013-05-17 14:20:39 -06:00
|
|
|
if typ == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// The type must be a signature, but be sure for safety.
|
|
|
|
sig, ok := typ.(*types.Signature)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// There must be a receiver for it to be a method call. Otherwise it is
|
|
|
|
// a function, not something that satisfies the error interface.
|
2013-08-20 00:11:01 -06:00
|
|
|
if sig.Recv() == nil {
|
2013-05-17 14:20:39 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
// There must be no arguments. Already verified by type checking, but be thorough.
|
2013-05-20 13:20:05 -06:00
|
|
|
if sig.Params().Len() > 0 {
|
2013-05-17 14:20:39 -06:00
|
|
|
return false
|
|
|
|
}
|
2013-08-18 19:12:44 -06:00
|
|
|
// Finally the real questions.
|
|
|
|
// There must be one result.
|
|
|
|
if sig.Results().Len() != 1 {
|
2013-05-17 14:20:39 -06:00
|
|
|
return false
|
|
|
|
}
|
2013-08-18 19:12:44 -06:00
|
|
|
// It must have return type "string" from the universe.
|
|
|
|
return sig.Results().At(0).Type() == types.Typ[types.String]
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
2013-08-27 19:24:43 -06:00
|
|
|
|
|
|
|
// hasMethod reports whether the type contains a method with the given name.
|
|
|
|
// It is part of the workaround for Formatters and should be deleted when
|
2014-03-12 18:19:34 -06:00
|
|
|
// that workaround is no longer necessary.
|
|
|
|
// TODO: This could be better once issue 6259 is fixed.
|
|
|
|
func (f *File) hasMethod(typ types.Type, name string) bool {
|
2014-08-28 14:03:13 -06:00
|
|
|
// assume we have an addressable variable of type typ
|
|
|
|
obj, _, _ := types.LookupFieldOrMethod(typ, true, f.pkg.typesPkg, name)
|
2014-03-12 18:19:34 -06:00
|
|
|
_, ok := obj.(*types.Func)
|
|
|
|
return ok
|
2013-08-27 19:24:43 -06:00
|
|
|
}
|