go.tools/oracle: use TypeString, ObjectString to print relative (unqualified) names when a package is implied by the context.
+ a couple more tests. R=gri, crawshaw CC=golang-dev https://golang.org/cl/26370046
This commit is contained in:
parent
9fcd20e680
commit
f488a2c4f5
|
|
@ -479,10 +479,10 @@ func (r *describeValueResult) display(printf printfFunc) {
|
||||||
if r.obj != nil {
|
if r.obj != nil {
|
||||||
if r.obj.Pos() == r.expr.Pos() {
|
if r.obj.Pos() == r.expr.Pos() {
|
||||||
// defining ident
|
// defining ident
|
||||||
printf(r.expr, "definition of %s%s%s", prefix, r.obj, suffix)
|
printf(r.expr, "definition of %s%s%s", prefix, r.qpos.ObjectString(r.obj), suffix)
|
||||||
} else {
|
} else {
|
||||||
// referring ident
|
// referring ident
|
||||||
printf(r.expr, "reference to %s%s%s", prefix, r.obj, suffix)
|
printf(r.expr, "reference to %s%s%s", prefix, r.qpos.ObjectString(r.obj), suffix)
|
||||||
if def := r.obj.Pos(); def != token.NoPos {
|
if def := r.obj.Pos(); def != token.NoPos {
|
||||||
printf(def, "defined here")
|
printf(def, "defined here")
|
||||||
}
|
}
|
||||||
|
|
@ -494,7 +494,7 @@ func (r *describeValueResult) display(printf printfFunc) {
|
||||||
printf(r.expr, "%s%s", desc, suffix)
|
printf(r.expr, "%s%s", desc, suffix)
|
||||||
} else {
|
} else {
|
||||||
// non-constant expression
|
// non-constant expression
|
||||||
printf(r.expr, "%s of type %s", desc, r.typ)
|
printf(r.expr, "%s of type %s", desc, r.qpos.TypeString(r.typ))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -514,17 +514,17 @@ func (r *describeValueResult) display(printf printfFunc) {
|
||||||
// reflect.Value expression.
|
// reflect.Value expression.
|
||||||
|
|
||||||
if len(r.ptrs) > 0 {
|
if len(r.ptrs) > 0 {
|
||||||
printf(r.qpos, "this %s may contain these dynamic types:", r.typ)
|
printf(r.qpos, "this %s may contain these dynamic types:", r.qpos.TypeString(r.typ))
|
||||||
for _, ptr := range r.ptrs {
|
for _, ptr := range r.ptrs {
|
||||||
var obj types.Object
|
var obj types.Object
|
||||||
if nt, ok := deref(ptr.typ).(*types.Named); ok {
|
if nt, ok := deref(ptr.typ).(*types.Named); ok {
|
||||||
obj = nt.Obj()
|
obj = nt.Obj()
|
||||||
}
|
}
|
||||||
if len(ptr.labels) > 0 {
|
if len(ptr.labels) > 0 {
|
||||||
printf(obj, "\t%s, may point to:", ptr.typ)
|
printf(obj, "\t%s, may point to:", r.qpos.TypeString(ptr.typ))
|
||||||
printLabels(printf, ptr.labels, "\t\t")
|
printLabels(printf, ptr.labels, "\t\t")
|
||||||
} else {
|
} else {
|
||||||
printf(obj, "\t%s", ptr.typ)
|
printf(obj, "\t%s", r.qpos.TypeString(ptr.typ))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -567,7 +567,7 @@ func (r *describeValueResult) toSerial(res *serial.Result, fset *token.FileSet)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pts = append(pts, &serial.DescribePointer{
|
pts = append(pts, &serial.DescribePointer{
|
||||||
Type: ptr.typ.String(),
|
Type: r.qpos.TypeString(ptr.typ),
|
||||||
NamePos: namePos,
|
NamePos: namePos,
|
||||||
Labels: labels,
|
Labels: labels,
|
||||||
})
|
})
|
||||||
|
|
@ -578,7 +578,7 @@ func (r *describeValueResult) toSerial(res *serial.Result, fset *token.FileSet)
|
||||||
Pos: fset.Position(r.expr.Pos()).String(),
|
Pos: fset.Position(r.expr.Pos()).String(),
|
||||||
Detail: "value",
|
Detail: "value",
|
||||||
Value: &serial.DescribeValue{
|
Value: &serial.DescribeValue{
|
||||||
Type: r.typ.String(),
|
Type: r.qpos.TypeString(r.typ),
|
||||||
Value: value,
|
Value: value,
|
||||||
ObjPos: objpos,
|
ObjPos: objpos,
|
||||||
PTAErr: ptaerr,
|
PTAErr: ptaerr,
|
||||||
|
|
@ -620,20 +620,19 @@ func describeType(o *Oracle, qpos *QueryPos, path []ast.Node) (*describeTypeResu
|
||||||
t = qpos.info.TypeOf(n)
|
t = qpos.info.TypeOf(n)
|
||||||
switch t := t.(type) {
|
switch t := t.(type) {
|
||||||
case *types.Basic:
|
case *types.Basic:
|
||||||
description = "reference to built-in type " + t.String()
|
description = "reference to built-in "
|
||||||
|
|
||||||
case *types.Named:
|
case *types.Named:
|
||||||
isDef := t.Obj().Pos() == n.Pos() // see caveats at isDef above
|
isDef := t.Obj().Pos() == n.Pos() // see caveats at isDef above
|
||||||
if isDef {
|
if isDef {
|
||||||
description = "definition of type " + t.String()
|
description = "definition of "
|
||||||
} else {
|
} else {
|
||||||
description = "reference to type " + t.String()
|
description = "reference to "
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case ast.Expr:
|
case ast.Expr:
|
||||||
t = qpos.info.TypeOf(n)
|
t = qpos.info.TypeOf(n)
|
||||||
description = "type " + t.String()
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// Unreachable?
|
// Unreachable?
|
||||||
|
|
@ -641,14 +640,16 @@ func describeType(o *Oracle, qpos *QueryPos, path []ast.Node) (*describeTypeResu
|
||||||
}
|
}
|
||||||
|
|
||||||
return &describeTypeResult{
|
return &describeTypeResult{
|
||||||
|
qpos: qpos,
|
||||||
node: path[0],
|
node: path[0],
|
||||||
description: description,
|
description: description + "type " + qpos.TypeString(t),
|
||||||
typ: t,
|
typ: t,
|
||||||
methods: accessibleMethods(t, qpos.info.Pkg),
|
methods: accessibleMethods(t, qpos.info.Pkg),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type describeTypeResult struct {
|
type describeTypeResult struct {
|
||||||
|
qpos *QueryPos
|
||||||
node ast.Node
|
node ast.Node
|
||||||
description string
|
description string
|
||||||
typ types.Type
|
typ types.Type
|
||||||
|
|
@ -660,7 +661,7 @@ func (r *describeTypeResult) display(printf printfFunc) {
|
||||||
|
|
||||||
// Show the underlying type for a reference to a named type.
|
// Show the underlying type for a reference to a named type.
|
||||||
if nt, ok := r.typ.(*types.Named); ok && r.node.Pos() != nt.Obj().Pos() {
|
if nt, ok := r.typ.(*types.Named); ok && r.node.Pos() != nt.Obj().Pos() {
|
||||||
printf(nt.Obj(), "defined as %s", nt.Underlying())
|
printf(nt.Obj(), "defined as %s", r.qpos.TypeString(nt.Underlying()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the method set, if the type kind is capable of bearing methods.
|
// Print the method set, if the type kind is capable of bearing methods.
|
||||||
|
|
@ -688,7 +689,7 @@ func (r *describeTypeResult) toSerial(res *serial.Result, fset *token.FileSet) {
|
||||||
Pos: fset.Position(r.node.Pos()).String(),
|
Pos: fset.Position(r.node.Pos()).String(),
|
||||||
Detail: "type",
|
Detail: "type",
|
||||||
Type: &serial.DescribeType{
|
Type: &serial.DescribeType{
|
||||||
Type: r.typ.String(),
|
Type: r.qpos.TypeString(r.typ),
|
||||||
NamePos: namePos,
|
NamePos: namePos,
|
||||||
NameDef: nameDef,
|
NameDef: nameDef,
|
||||||
Methods: methodsToSerial(r.methods, fset),
|
Methods: methodsToSerial(r.methods, fset),
|
||||||
|
|
|
||||||
|
|
@ -110,6 +110,16 @@ type QueryPos struct {
|
||||||
path []ast.Node // AST path from query node to root of ast.File
|
path []ast.Node // AST path from query node to root of ast.File
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TypeString prints type T relative to the query position.
|
||||||
|
func (qpos *QueryPos) TypeString(T types.Type) string {
|
||||||
|
return types.TypeString(qpos.info.Pkg, T)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ObjectString prints object obj relative to the query position.
|
||||||
|
func (qpos *QueryPos) ObjectString(obj types.Object) string {
|
||||||
|
return types.ObjectString(qpos.info.Pkg, obj)
|
||||||
|
}
|
||||||
|
|
||||||
// A Result encapsulates the result of an oracle.Query.
|
// A Result encapsulates the result of an oracle.Query.
|
||||||
type Result struct {
|
type Result struct {
|
||||||
fset *token.FileSet
|
fset *token.FileSet
|
||||||
|
|
@ -244,7 +254,7 @@ func newOracle(imp *importer.Importer, args []string, ptalog io.Writer, needs in
|
||||||
o.typeInfo = m
|
o.typeInfo = m
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create SSA package for the initial package and its dependencies.
|
// Create SSA package for the initial packages and their dependencies.
|
||||||
if needs&needSSA != 0 {
|
if needs&needSSA != 0 {
|
||||||
start = time.Now()
|
start = time.Now()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -101,11 +101,11 @@
|
||||||
"pos": "testdata/src/main/describe-json.go:18:6",
|
"pos": "testdata/src/main/describe-json.go:18:6",
|
||||||
"detail": "value",
|
"detail": "value",
|
||||||
"value": {
|
"value": {
|
||||||
"type": "describe.I",
|
"type": "I",
|
||||||
"objpos": "testdata/src/main/describe-json.go:14:6",
|
"objpos": "testdata/src/main/describe-json.go:14:6",
|
||||||
"pts": [
|
"pts": [
|
||||||
{
|
{
|
||||||
"type": "*describe.D",
|
"type": "*D",
|
||||||
"namepos": "testdata/src/main/describe-json.go:28:6",
|
"namepos": "testdata/src/main/describe-json.go:28:6",
|
||||||
"labels": [
|
"labels": [
|
||||||
{
|
{
|
||||||
|
|
@ -115,7 +115,7 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "describe.C",
|
"type": "C",
|
||||||
"namepos": "testdata/src/main/describe-json.go:27:6"
|
"namepos": "testdata/src/main/describe-json.go:27:6"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
@ -133,11 +133,11 @@
|
||||||
{
|
{
|
||||||
"mode": "describe",
|
"mode": "describe",
|
||||||
"describe": {
|
"describe": {
|
||||||
"desc": "definition of type describe.C",
|
"desc": "definition of type C",
|
||||||
"pos": "testdata/src/main/describe-json.go:27:6",
|
"pos": "testdata/src/main/describe-json.go:27:6",
|
||||||
"detail": "type",
|
"detail": "type",
|
||||||
"type": {
|
"type": {
|
||||||
"type": "describe.C",
|
"type": "C",
|
||||||
"namepos": "testdata/src/main/describe-json.go:27:6",
|
"namepos": "testdata/src/main/describe-json.go:27:6",
|
||||||
"namedef": "int",
|
"namedef": "int",
|
||||||
"methods": [
|
"methods": [
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@ package describe // @describe pkgdecl "describe"
|
||||||
|
|
||||||
type cake float64 // @describe type-ref-builtin "float64"
|
type cake float64 // @describe type-ref-builtin "float64"
|
||||||
|
|
||||||
|
const c = iota // @describe const-ref-iota "iota"
|
||||||
|
|
||||||
const pi = 3.141 // @describe const-def-pi "pi"
|
const pi = 3.141 // @describe const-def-pi "pi"
|
||||||
const pie = cake(pi) // @describe const-def-pie "pie"
|
const pie = cake(pi) // @describe const-def-pie "pie"
|
||||||
const _ = pi // @describe const-ref-pi "pi"
|
const _ = pi // @describe const-ref-pi "pi"
|
||||||
|
|
@ -65,6 +67,8 @@ func main() { // @describe func-def-main "main"
|
||||||
|
|
||||||
defer main() // @describe defer-stmt "defer"
|
defer main() // @describe defer-stmt "defer"
|
||||||
go main() // @describe go-stmt "go"
|
go main() // @describe go-stmt "go"
|
||||||
|
|
||||||
|
panic(3) // @describe builtin-ref-panic "panic"
|
||||||
}
|
}
|
||||||
|
|
||||||
func deadcode() {
|
func deadcode() {
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ definition of package "describe"
|
||||||
method (describe.D) f()
|
method (describe.D) f()
|
||||||
type I interface{f()}
|
type I interface{f()}
|
||||||
method (describe.I) f()
|
method (describe.I) f()
|
||||||
|
const c untyped integer = 0
|
||||||
type cake float64
|
type cake float64
|
||||||
func deadcode func()
|
func deadcode func()
|
||||||
var global *string
|
var global *string
|
||||||
|
|
@ -16,64 +17,67 @@ definition of package "describe"
|
||||||
-------- @describe type-ref-builtin --------
|
-------- @describe type-ref-builtin --------
|
||||||
reference to built-in type float64
|
reference to built-in type float64
|
||||||
|
|
||||||
|
-------- @describe const-ref-iota --------
|
||||||
|
reference to const iota untyped integer of constant value 0
|
||||||
|
|
||||||
-------- @describe const-def-pi --------
|
-------- @describe const-def-pi --------
|
||||||
definition of const pi untyped float
|
definition of const pi untyped float
|
||||||
|
|
||||||
-------- @describe const-def-pie --------
|
-------- @describe const-def-pie --------
|
||||||
definition of const pie describe.cake
|
definition of const pie cake
|
||||||
|
|
||||||
-------- @describe const-ref-pi --------
|
-------- @describe const-ref-pi --------
|
||||||
reference to const pi untyped float of constant value 3141/1000
|
reference to const pi untyped float of constant value 3141/1000
|
||||||
defined here
|
defined here
|
||||||
|
|
||||||
-------- @describe func-def-main --------
|
-------- @describe func-def-main --------
|
||||||
definition of func describe.main()
|
definition of func main()
|
||||||
|
|
||||||
-------- @describe func-ref-main --------
|
-------- @describe func-ref-main --------
|
||||||
reference to func describe.main()
|
reference to func main()
|
||||||
defined here
|
defined here
|
||||||
|
|
||||||
-------- @describe func-ref-*C.f --------
|
-------- @describe func-ref-*C.f --------
|
||||||
reference to method func (*describe.C).f()
|
reference to method func (*C).f()
|
||||||
defined here
|
defined here
|
||||||
|
|
||||||
-------- @describe func-ref-D.f --------
|
-------- @describe func-ref-D.f --------
|
||||||
reference to method func (describe.D).f()
|
reference to method func (D).f()
|
||||||
defined here
|
defined here
|
||||||
|
|
||||||
-------- @describe func-ref-I.f --------
|
-------- @describe func-ref-I.f --------
|
||||||
reference to interface method func (describe.I).f()
|
reference to interface method func (I).f()
|
||||||
defined here
|
defined here
|
||||||
|
|
||||||
-------- @describe type-D --------
|
-------- @describe type-D --------
|
||||||
reference to type describe.D
|
reference to type D
|
||||||
defined as struct{}
|
defined as struct{}
|
||||||
Method set:
|
Method set:
|
||||||
method (describe.D) f()
|
method (describe.D) f()
|
||||||
|
|
||||||
-------- @describe type-I --------
|
-------- @describe type-I --------
|
||||||
reference to type describe.I
|
reference to type I
|
||||||
defined as interface{f()}
|
defined as interface{f()}
|
||||||
Method set:
|
Method set:
|
||||||
method (describe.I) f()
|
method (describe.I) f()
|
||||||
|
|
||||||
-------- @describe func-ref-d.f --------
|
-------- @describe func-ref-d.f --------
|
||||||
reference to method func (describe.D).f()
|
reference to method func (D).f()
|
||||||
defined here
|
defined here
|
||||||
|
|
||||||
-------- @describe func-ref-i.f --------
|
-------- @describe func-ref-i.f --------
|
||||||
reference to interface method func (describe.I).f()
|
reference to interface method func (I).f()
|
||||||
defined here
|
defined here
|
||||||
|
|
||||||
-------- @describe ref-lexical-d --------
|
-------- @describe ref-lexical-d --------
|
||||||
reference to var d describe.D
|
reference to var d D
|
||||||
defined here
|
defined here
|
||||||
|
|
||||||
-------- @describe ref-anon --------
|
-------- @describe ref-anon --------
|
||||||
reference to var anon func()
|
reference to var anon func()
|
||||||
defined here
|
defined here
|
||||||
value may point to these labels:
|
value may point to these labels:
|
||||||
func@29.10
|
func@31.10
|
||||||
|
|
||||||
-------- @describe ref-global --------
|
-------- @describe ref-global --------
|
||||||
reference to var global *string
|
reference to var global *string
|
||||||
|
|
@ -105,42 +109,42 @@ value may point to these labels:
|
||||||
b
|
b
|
||||||
|
|
||||||
-------- @describe var-ref-i-C --------
|
-------- @describe var-ref-i-C --------
|
||||||
reference to var i describe.I
|
reference to var i I
|
||||||
defined here
|
defined here
|
||||||
this describe.I may contain these dynamic types:
|
this I may contain these dynamic types:
|
||||||
*describe.C, may point to:
|
*C, may point to:
|
||||||
new
|
new
|
||||||
|
|
||||||
-------- @describe var-ref-i-D --------
|
-------- @describe var-ref-i-D --------
|
||||||
reference to var i describe.I
|
reference to var i I
|
||||||
defined here
|
defined here
|
||||||
this describe.I may contain these dynamic types:
|
this I may contain these dynamic types:
|
||||||
describe.D
|
D
|
||||||
|
|
||||||
-------- @describe var-ref-i --------
|
-------- @describe var-ref-i --------
|
||||||
reference to var i describe.I
|
reference to var i I
|
||||||
defined here
|
defined here
|
||||||
this describe.I may contain these dynamic types:
|
this I may contain these dynamic types:
|
||||||
*describe.C, may point to:
|
*C, may point to:
|
||||||
new
|
new
|
||||||
describe.D
|
D
|
||||||
|
|
||||||
-------- @describe const-local-pi --------
|
-------- @describe const-local-pi --------
|
||||||
definition of const localpi untyped float
|
definition of const localpi untyped float
|
||||||
|
|
||||||
-------- @describe const-local-pie --------
|
-------- @describe const-local-pie --------
|
||||||
definition of const localpie describe.cake
|
definition of const localpie cake
|
||||||
|
|
||||||
-------- @describe const-ref-localpi --------
|
-------- @describe const-ref-localpi --------
|
||||||
reference to const localpi untyped float of constant value 3141/1000
|
reference to const localpi untyped float of constant value 3141/1000
|
||||||
defined here
|
defined here
|
||||||
|
|
||||||
-------- @describe type-def-T --------
|
-------- @describe type-def-T --------
|
||||||
definition of type describe.T
|
definition of type T
|
||||||
No methods.
|
No methods.
|
||||||
|
|
||||||
-------- @describe type-ref-T --------
|
-------- @describe type-ref-T --------
|
||||||
reference to type describe.T
|
reference to type T
|
||||||
defined as int
|
defined as int
|
||||||
No methods.
|
No methods.
|
||||||
|
|
||||||
|
|
@ -171,6 +175,9 @@ defer statement
|
||||||
-------- @describe go-stmt --------
|
-------- @describe go-stmt --------
|
||||||
go statement
|
go statement
|
||||||
|
|
||||||
|
-------- @describe builtin-ref-panic --------
|
||||||
|
function call (or conversion) of type ()
|
||||||
|
|
||||||
-------- @describe var-decl-stmt --------
|
-------- @describe var-decl-stmt --------
|
||||||
definition of var a int
|
definition of var a int
|
||||||
|
|
||||||
|
|
@ -179,7 +186,7 @@ definition of var b *int
|
||||||
no points-to information: PTA did not encounter this expression (dead code?)
|
no points-to information: PTA did not encounter this expression (dead code?)
|
||||||
|
|
||||||
-------- @describe def-iface-I --------
|
-------- @describe def-iface-I --------
|
||||||
definition of type describe.I
|
definition of type I
|
||||||
Method set:
|
Method set:
|
||||||
method (describe.I) f()
|
method (describe.I) f()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import of package "lib"
|
||||||
var Var int
|
var Var int
|
||||||
|
|
||||||
-------- @describe ref-const --------
|
-------- @describe ref-const --------
|
||||||
reference to const Const untyped integer
|
reference to const lib.Const untyped integer
|
||||||
defined here
|
defined here
|
||||||
|
|
||||||
-------- @describe ref-func --------
|
-------- @describe ref-func --------
|
||||||
|
|
@ -15,7 +15,7 @@ reference to func lib.Func()
|
||||||
defined here
|
defined here
|
||||||
|
|
||||||
-------- @describe ref-var --------
|
-------- @describe ref-var --------
|
||||||
reference to var Var int
|
reference to var lib.Var int
|
||||||
defined here
|
defined here
|
||||||
|
|
||||||
-------- @describe ref-type --------
|
-------- @describe ref-type --------
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue