cmd/guru: what: include imported package names in sameids

+ Test.

Change-Id: Ib7ef99786f5b60bb3e56ced9588d2ba5725576e1
Reviewed-on: https://go-review.googlesource.com/24949
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Alan Donovan 2016-07-15 12:04:53 -04:00
parent 92480e4760
commit ef6b6ebf3b
5 changed files with 104 additions and 19 deletions

View File

@ -113,6 +113,19 @@
} }
] ]
} }
{
"package": "what-json",
"refs": [
{
"pos": "testdata/src/what-json/main.go:13:7",
"text": "var _ lib.Var // @what pkg \"lib\""
},
{
"pos": "testdata/src/what-json/main.go:14:8",
"text": "type _ lib.T"
}
]
}
-------- @referrers ref-method -------- -------- @referrers ref-method --------
{ {
"objpos": "testdata/src/lib/lib.go:5:13", "objpos": "testdata/src/lib/lib.go:5:13",

View File

@ -28,6 +28,8 @@ references to package lib
var v lib.Type = lib.Const // @referrers ref-package "lib" var v lib.Type = lib.Const // @referrers ref-package "lib"
var v lib.Type = lib.Const // @referrers ref-package "lib" var v lib.Type = lib.Const // @referrers ref-package "lib"
var x lib.T // @definition lexical-pkgname "lib" var x lib.T // @definition lexical-pkgname "lib"
type _ lib.T
var _ lib.Var // @what pkg "lib"
-------- @referrers ref-method -------- -------- @referrers ref-method --------
references to func (Type).Method(x *int) *int references to func (Type).Method(x *int) *int

View File

@ -1,5 +1,7 @@
package main package main
import "lib"
// Tests of 'what' queries, -format=json. // Tests of 'what' queries, -format=json.
// See go.tools/guru/guru_test.go for explanation. // See go.tools/guru/guru_test.go for explanation.
// See what-json.golden for expected query results. // See what-json.golden for expected query results.
@ -7,3 +9,6 @@ package main
func main() { func main() {
f() // @what call "f" f() // @what call "f"
} }
var _ lib.Var // @what pkg "lib"
type _ lib.T

View File

@ -3,33 +3,33 @@
"enclosing": [ "enclosing": [
{ {
"desc": "identifier", "desc": "identifier",
"start": 175, "start": 189,
"end": 176 "end": 190
}, },
{ {
"desc": "function call", "desc": "function call",
"start": 175, "start": 189,
"end": 178 "end": 192
}, },
{ {
"desc": "expression statement", "desc": "expression statement",
"start": 175, "start": 189,
"end": 178 "end": 192
}, },
{ {
"desc": "block", "desc": "block",
"start": 172, "start": 186,
"end": 198 "end": 212
}, },
{ {
"desc": "function declaration", "desc": "function declaration",
"start": 160, "start": 174,
"end": 198 "end": 212
}, },
{ {
"desc": "source file", "desc": "source file",
"start": 0, "start": 0,
"end": 198 "end": 259
} }
], ],
"modes": [ "modes": [
@ -46,3 +46,48 @@
"srcdir": "testdata/src", "srcdir": "testdata/src",
"importpath": "what-json" "importpath": "what-json"
} }
-------- @what pkg --------
{
"enclosing": [
{
"desc": "identifier",
"start": 220,
"end": 223
},
{
"desc": "selector",
"start": 220,
"end": 227
},
{
"desc": "value specification",
"start": 218,
"end": 227
},
{
"desc": "variable declaration",
"start": 214,
"end": 227
},
{
"desc": "source file",
"start": 0,
"end": 259
}
],
"modes": [
"definition",
"describe",
"freevars",
"implements",
"pointsto",
"referrers"
],
"srcdir": "testdata/src",
"importpath": "what-json",
"object": "lib",
"sameids": [
"$GOPATH/src/what-json/main.go:13:7",
"$GOPATH/src/what-json/main.go:14:8"
]
}

View File

@ -117,7 +117,26 @@ func what(q *Query) error {
// it uses the best-effort name resolution done by go/parser. // it uses the best-effort name resolution done by go/parser.
var sameids []token.Pos var sameids []token.Pos
var object string var object string
if id, ok := qpos.path[0].(*ast.Ident); ok && id.Obj != nil { if id, ok := qpos.path[0].(*ast.Ident); ok {
if id.Obj == nil {
// An unresolved identifier is potentially a package name.
// Resolve them with a simple importer (adds ~100µs).
importer := func(imports map[string]*ast.Object, path string) (*ast.Object, error) {
pkg, ok := imports[path]
if !ok {
pkg = &ast.Object{
Kind: ast.Pkg,
Name: filepath.Base(path), // a guess
}
imports[path] = pkg
}
return pkg, nil
}
f := qpos.path[len(qpos.path)-1].(*ast.File)
ast.NewPackage(qpos.fset, map[string]*ast.File{"": f}, importer, nil)
}
if id.Obj != nil {
object = id.Obj.Name object = id.Obj.Name
decl := qpos.path[len(qpos.path)-1] decl := qpos.path[len(qpos.path)-1]
ast.Inspect(decl, func(n ast.Node) bool { ast.Inspect(decl, func(n ast.Node) bool {
@ -127,6 +146,7 @@ func what(q *Query) error {
return true return true
}) })
} }
}
q.Output(qpos.fset, &whatResult{ q.Output(qpos.fset, &whatResult{
path: qpos.path, path: qpos.path,