From f488a2c4f5322b3dcbd14b9256812c9f9da0df54 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 15 Nov 2013 09:22:16 -0500 Subject: [PATCH] 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 --- oracle/describe.go | 31 +++++----- oracle/oracle.go | 12 +++- oracle/testdata/src/main/describe-json.golden | 10 ++-- oracle/testdata/src/main/describe.go | 4 ++ oracle/testdata/src/main/describe.golden | 59 +++++++++++-------- oracle/testdata/src/main/imports.golden | 4 +- 6 files changed, 71 insertions(+), 49 deletions(-) diff --git a/oracle/describe.go b/oracle/describe.go index a6cb702f..f03486cb 100644 --- a/oracle/describe.go +++ b/oracle/describe.go @@ -479,10 +479,10 @@ func (r *describeValueResult) display(printf printfFunc) { if r.obj != nil { if r.obj.Pos() == r.expr.Pos() { // 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 { // 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 { printf(def, "defined here") } @@ -494,7 +494,7 @@ func (r *describeValueResult) display(printf printfFunc) { printf(r.expr, "%s%s", desc, suffix) } else { // 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. 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 { var obj types.Object if nt, ok := deref(ptr.typ).(*types.Named); ok { obj = nt.Obj() } 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") } else { - printf(obj, "\t%s", ptr.typ) + printf(obj, "\t%s", r.qpos.TypeString(ptr.typ)) } } } else { @@ -567,7 +567,7 @@ func (r *describeValueResult) toSerial(res *serial.Result, fset *token.FileSet) }) } pts = append(pts, &serial.DescribePointer{ - Type: ptr.typ.String(), + Type: r.qpos.TypeString(ptr.typ), NamePos: namePos, Labels: labels, }) @@ -578,7 +578,7 @@ func (r *describeValueResult) toSerial(res *serial.Result, fset *token.FileSet) Pos: fset.Position(r.expr.Pos()).String(), Detail: "value", Value: &serial.DescribeValue{ - Type: r.typ.String(), + Type: r.qpos.TypeString(r.typ), Value: value, ObjPos: objpos, PTAErr: ptaerr, @@ -620,20 +620,19 @@ func describeType(o *Oracle, qpos *QueryPos, path []ast.Node) (*describeTypeResu t = qpos.info.TypeOf(n) switch t := t.(type) { case *types.Basic: - description = "reference to built-in type " + t.String() + description = "reference to built-in " case *types.Named: isDef := t.Obj().Pos() == n.Pos() // see caveats at isDef above if isDef { - description = "definition of type " + t.String() + description = "definition of " } else { - description = "reference to type " + t.String() + description = "reference to " } } case ast.Expr: t = qpos.info.TypeOf(n) - description = "type " + t.String() default: // Unreachable? @@ -641,14 +640,16 @@ func describeType(o *Oracle, qpos *QueryPos, path []ast.Node) (*describeTypeResu } return &describeTypeResult{ + qpos: qpos, node: path[0], - description: description, + description: description + "type " + qpos.TypeString(t), typ: t, methods: accessibleMethods(t, qpos.info.Pkg), }, nil } type describeTypeResult struct { + qpos *QueryPos node ast.Node description string typ types.Type @@ -660,7 +661,7 @@ func (r *describeTypeResult) display(printf printfFunc) { // 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() { - 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. @@ -688,7 +689,7 @@ func (r *describeTypeResult) toSerial(res *serial.Result, fset *token.FileSet) { Pos: fset.Position(r.node.Pos()).String(), Detail: "type", Type: &serial.DescribeType{ - Type: r.typ.String(), + Type: r.qpos.TypeString(r.typ), NamePos: namePos, NameDef: nameDef, Methods: methodsToSerial(r.methods, fset), diff --git a/oracle/oracle.go b/oracle/oracle.go index dbe7fedc..8dffbd69 100644 --- a/oracle/oracle.go +++ b/oracle/oracle.go @@ -110,6 +110,16 @@ type QueryPos struct { 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. type Result struct { fset *token.FileSet @@ -244,7 +254,7 @@ func newOracle(imp *importer.Importer, args []string, ptalog io.Writer, needs in 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 { start = time.Now() diff --git a/oracle/testdata/src/main/describe-json.golden b/oracle/testdata/src/main/describe-json.golden index e2b26182..702726d4 100644 --- a/oracle/testdata/src/main/describe-json.golden +++ b/oracle/testdata/src/main/describe-json.golden @@ -101,11 +101,11 @@ "pos": "testdata/src/main/describe-json.go:18:6", "detail": "value", "value": { - "type": "describe.I", + "type": "I", "objpos": "testdata/src/main/describe-json.go:14:6", "pts": [ { - "type": "*describe.D", + "type": "*D", "namepos": "testdata/src/main/describe-json.go:28:6", "labels": [ { @@ -115,7 +115,7 @@ ] }, { - "type": "describe.C", + "type": "C", "namepos": "testdata/src/main/describe-json.go:27:6" } ] @@ -133,11 +133,11 @@ { "mode": "describe", "describe": { - "desc": "definition of type describe.C", + "desc": "definition of type C", "pos": "testdata/src/main/describe-json.go:27:6", "detail": "type", "type": { - "type": "describe.C", + "type": "C", "namepos": "testdata/src/main/describe-json.go:27:6", "namedef": "int", "methods": [ diff --git a/oracle/testdata/src/main/describe.go b/oracle/testdata/src/main/describe.go index b7fe77ad..ed4eea6b 100644 --- a/oracle/testdata/src/main/describe.go +++ b/oracle/testdata/src/main/describe.go @@ -8,6 +8,8 @@ package describe // @describe pkgdecl "describe" 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 pie = cake(pi) // @describe const-def-pie "pie" const _ = pi // @describe const-ref-pi "pi" @@ -65,6 +67,8 @@ func main() { // @describe func-def-main "main" defer main() // @describe defer-stmt "defer" go main() // @describe go-stmt "go" + + panic(3) // @describe builtin-ref-panic "panic" } func deadcode() { diff --git a/oracle/testdata/src/main/describe.golden b/oracle/testdata/src/main/describe.golden index 23efffd3..4aa8a975 100644 --- a/oracle/testdata/src/main/describe.golden +++ b/oracle/testdata/src/main/describe.golden @@ -6,6 +6,7 @@ definition of package "describe" method (describe.D) f() type I interface{f()} method (describe.I) f() + const c untyped integer = 0 type cake float64 func deadcode func() var global *string @@ -16,64 +17,67 @@ definition of package "describe" -------- @describe type-ref-builtin -------- reference to built-in type float64 +-------- @describe const-ref-iota -------- +reference to const iota untyped integer of constant value 0 + -------- @describe const-def-pi -------- definition of const pi untyped float -------- @describe const-def-pie -------- -definition of const pie describe.cake +definition of const pie cake -------- @describe const-ref-pi -------- reference to const pi untyped float of constant value 3141/1000 defined here -------- @describe func-def-main -------- -definition of func describe.main() +definition of func main() -------- @describe func-ref-main -------- -reference to func describe.main() +reference to func main() defined here -------- @describe func-ref-*C.f -------- -reference to method func (*describe.C).f() +reference to method func (*C).f() defined here -------- @describe func-ref-D.f -------- -reference to method func (describe.D).f() +reference to method func (D).f() defined here -------- @describe func-ref-I.f -------- -reference to interface method func (describe.I).f() +reference to interface method func (I).f() defined here -------- @describe type-D -------- -reference to type describe.D +reference to type D defined as struct{} Method set: method (describe.D) f() -------- @describe type-I -------- -reference to type describe.I +reference to type I defined as interface{f()} Method set: method (describe.I) f() -------- @describe func-ref-d.f -------- -reference to method func (describe.D).f() +reference to method func (D).f() defined here -------- @describe func-ref-i.f -------- -reference to interface method func (describe.I).f() +reference to interface method func (I).f() defined here -------- @describe ref-lexical-d -------- -reference to var d describe.D +reference to var d D defined here -------- @describe ref-anon -------- reference to var anon func() defined here value may point to these labels: - func@29.10 + func@31.10 -------- @describe ref-global -------- reference to var global *string @@ -105,42 +109,42 @@ value may point to these labels: b -------- @describe var-ref-i-C -------- -reference to var i describe.I +reference to var i I defined here -this describe.I may contain these dynamic types: - *describe.C, may point to: +this I may contain these dynamic types: + *C, may point to: new -------- @describe var-ref-i-D -------- -reference to var i describe.I +reference to var i I defined here -this describe.I may contain these dynamic types: - describe.D +this I may contain these dynamic types: + D -------- @describe var-ref-i -------- -reference to var i describe.I +reference to var i I defined here -this describe.I may contain these dynamic types: - *describe.C, may point to: +this I may contain these dynamic types: + *C, may point to: new - describe.D + D -------- @describe const-local-pi -------- definition of const localpi untyped float -------- @describe const-local-pie -------- -definition of const localpie describe.cake +definition of const localpie cake -------- @describe const-ref-localpi -------- reference to const localpi untyped float of constant value 3141/1000 defined here -------- @describe type-def-T -------- -definition of type describe.T +definition of type T No methods. -------- @describe type-ref-T -------- -reference to type describe.T +reference to type T defined as int No methods. @@ -171,6 +175,9 @@ defer statement -------- @describe go-stmt -------- go statement +-------- @describe builtin-ref-panic -------- +function call (or conversion) of type () + -------- @describe var-decl-stmt -------- 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?) -------- @describe def-iface-I -------- -definition of type describe.I +definition of type I Method set: method (describe.I) f() diff --git a/oracle/testdata/src/main/imports.golden b/oracle/testdata/src/main/imports.golden index c0f77fb5..cf9d881f 100644 --- a/oracle/testdata/src/main/imports.golden +++ b/oracle/testdata/src/main/imports.golden @@ -7,7 +7,7 @@ import of package "lib" var Var int -------- @describe ref-const -------- -reference to const Const untyped integer +reference to const lib.Const untyped integer defined here -------- @describe ref-func -------- @@ -15,7 +15,7 @@ reference to func lib.Func() defined here -------- @describe ref-var -------- -reference to var Var int +reference to var lib.Var int defined here -------- @describe ref-type --------