From ef6b6ebf3bae8fcf7070dc621fa12c7ea08ea3c7 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 15 Jul 2016 12:04:53 -0400 Subject: [PATCH] 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 Run-TryBot: Robert Griesemer TryBot-Result: Gobot Gobot --- .../testdata/src/referrers-json/main.golden | 13 ++++ cmd/guru/testdata/src/referrers/main.golden | 2 + cmd/guru/testdata/src/what-json/main.go | 5 ++ cmd/guru/testdata/src/what-json/main.golden | 67 ++++++++++++++++--- cmd/guru/what.go | 36 +++++++--- 5 files changed, 104 insertions(+), 19 deletions(-) diff --git a/cmd/guru/testdata/src/referrers-json/main.golden b/cmd/guru/testdata/src/referrers-json/main.golden index 9c3ee98b..a779a190 100644 --- a/cmd/guru/testdata/src/referrers-json/main.golden +++ b/cmd/guru/testdata/src/referrers-json/main.golden @@ -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", diff --git a/cmd/guru/testdata/src/referrers/main.golden b/cmd/guru/testdata/src/referrers/main.golden index 9cac5040..060d9834 100644 --- a/cmd/guru/testdata/src/referrers/main.golden +++ b/cmd/guru/testdata/src/referrers/main.golden @@ -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 diff --git a/cmd/guru/testdata/src/what-json/main.go b/cmd/guru/testdata/src/what-json/main.go index 49fd0a84..27177ff5 100644 --- a/cmd/guru/testdata/src/what-json/main.go +++ b/cmd/guru/testdata/src/what-json/main.go @@ -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 diff --git a/cmd/guru/testdata/src/what-json/main.golden b/cmd/guru/testdata/src/what-json/main.golden index 639084e8..5008d67a 100644 --- a/cmd/guru/testdata/src/what-json/main.golden +++ b/cmd/guru/testdata/src/what-json/main.golden @@ -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" + ] +} diff --git a/cmd/guru/what.go b/cmd/guru/what.go index 06f44dc0..4f888b02 100644 --- a/cmd/guru/what.go +++ b/cmd/guru/what.go @@ -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{