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 --------
{
"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 x lib.T // @definition lexical-pkgname "lib"
type _ lib.T
var _ lib.Var // @what pkg "lib"
-------- @referrers ref-method --------
references to func (Type).Method(x *int) *int

View File

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

View File

@ -3,33 +3,33 @@
"enclosing": [
{
"desc": "identifier",
"start": 175,
"end": 176
"start": 189,
"end": 190
},
{
"desc": "function call",
"start": 175,
"end": 178
"start": 189,
"end": 192
},
{
"desc": "expression statement",
"start": 175,
"end": 178
"start": 189,
"end": 192
},
{
"desc": "block",
"start": 172,
"end": 198
"start": 186,
"end": 212
},
{
"desc": "function declaration",
"start": 160,
"end": 198
"start": 174,
"end": 212
},
{
"desc": "source file",
"start": 0,
"end": 198
"end": 259
}
],
"modes": [
@ -46,3 +46,48 @@
"srcdir": "testdata/src",
"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,15 +117,35 @@ func what(q *Query) error {
// it uses the best-effort name resolution done by go/parser.
var sameids []token.Pos
var object string
if id, ok := qpos.path[0].(*ast.Ident); ok && id.Obj != nil {
object = id.Obj.Name
decl := qpos.path[len(qpos.path)-1]
ast.Inspect(decl, func(n ast.Node) bool {
if n, ok := n.(*ast.Ident); ok && n.Obj == id.Obj {
sameids = append(sameids, n.Pos())
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
}
return true
})
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
decl := qpos.path[len(qpos.path)-1]
ast.Inspect(decl, func(n ast.Node) bool {
if n, ok := n.(*ast.Ident); ok && n.Obj == id.Obj {
sameids = append(sameids, n.Pos())
}
return true
})
}
}
q.Output(qpos.fset, &whatResult{