2013-09-10 12:11:42 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package oracle
|
|
|
|
|
|
|
|
import (
|
2013-09-25 12:34:39 -06:00
|
|
|
"fmt"
|
2013-09-10 12:11:42 -06:00
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"code.google.com/p/go.tools/go/types"
|
2013-09-24 13:08:14 -06:00
|
|
|
"code.google.com/p/go.tools/oracle/serial"
|
2013-09-10 12:11:42 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Referrers reports all identifiers that resolve to the same object
|
|
|
|
// as the queried identifier, within any package in the analysis scope.
|
|
|
|
//
|
go.tools/oracle: refactor Oracle API to allow repeated queries on same scope.
The existing standalone Query function builds an importer, ssa.Program, oracle,
and query position, executes the query and returns the result.
For clients (such as Frederik Zipp's web-based github.com/fzipp/pythia tool)
that wish to load the program once and make several queries, we now expose
these as separate operations too. Here's a client, in pseudocode:
o := oracle.New(...)
for ... {
qpos := o.ParseQueryPos(...)
res := o.Query(mode, qpos)
print result
}
NB: this is a slight deoptimisation in the one-shot case since we have to
build the entire SSA program with debug info, not just the query package,
since we now don't know the query package at that time.
The 'exact' param to ParseQueryPos needs more thought since its
ideal value is a function of the query mode. This will do for now.
Details:
- expose Oracle type, New() func and Query() method.
- expose QueryPos type and ParseQueryPos func.
- improved package doc comment.
- un-exposed the "needs" bits.
- added test.
R=crawshaw
CC=frederik.zipp, golang-dev
https://golang.org/cl/13810043
2013-09-23 13:02:18 -06:00
|
|
|
func referrers(o *Oracle, qpos *QueryPos) (queryResult, error) {
|
|
|
|
id, _ := qpos.path[0].(*ast.Ident)
|
2013-09-10 12:11:42 -06:00
|
|
|
if id == nil {
|
2013-09-25 12:34:39 -06:00
|
|
|
return nil, fmt.Errorf("no identifier here")
|
2013-09-10 12:11:42 -06:00
|
|
|
}
|
|
|
|
|
go.tools/oracle: refactor Oracle API to allow repeated queries on same scope.
The existing standalone Query function builds an importer, ssa.Program, oracle,
and query position, executes the query and returns the result.
For clients (such as Frederik Zipp's web-based github.com/fzipp/pythia tool)
that wish to load the program once and make several queries, we now expose
these as separate operations too. Here's a client, in pseudocode:
o := oracle.New(...)
for ... {
qpos := o.ParseQueryPos(...)
res := o.Query(mode, qpos)
print result
}
NB: this is a slight deoptimisation in the one-shot case since we have to
build the entire SSA program with debug info, not just the query package,
since we now don't know the query package at that time.
The 'exact' param to ParseQueryPos needs more thought since its
ideal value is a function of the query mode. This will do for now.
Details:
- expose Oracle type, New() func and Query() method.
- expose QueryPos type and ParseQueryPos func.
- improved package doc comment.
- un-exposed the "needs" bits.
- added test.
R=crawshaw
CC=frederik.zipp, golang-dev
https://golang.org/cl/13810043
2013-09-23 13:02:18 -06:00
|
|
|
obj := qpos.info.ObjectOf(id)
|
2013-09-10 12:11:42 -06:00
|
|
|
if obj == nil {
|
|
|
|
// Happens for y in "switch y := x.(type)", but I think that's all.
|
2013-09-25 12:34:39 -06:00
|
|
|
return nil, fmt.Errorf("no object for identifier")
|
2013-09-10 12:11:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over all go/types' resolver facts for the entire program.
|
|
|
|
var refs []token.Pos
|
|
|
|
for _, info := range o.typeInfo {
|
|
|
|
for id2, obj2 := range info.Objects {
|
2013-09-13 10:52:57 -06:00
|
|
|
if sameObj(obj, obj2) {
|
2013-09-10 12:11:42 -06:00
|
|
|
if id2.NamePos == obj.Pos() {
|
|
|
|
continue // skip defining ident
|
|
|
|
}
|
|
|
|
refs = append(refs, id2.NamePos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Sort(byPos(refs))
|
|
|
|
|
|
|
|
return &referrersResult{
|
|
|
|
query: id.NamePos,
|
|
|
|
obj: obj,
|
|
|
|
refs: refs,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2013-09-13 10:52:57 -06:00
|
|
|
// same reports whether x and y are identical, or both are PkgNames
|
|
|
|
// referring to the same Package.
|
2013-09-10 12:11:42 -06:00
|
|
|
//
|
2013-09-13 10:52:57 -06:00
|
|
|
func sameObj(x, y types.Object) bool {
|
|
|
|
if x == y {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if _, ok := x.(*types.PkgName); ok {
|
|
|
|
if _, ok := y.(*types.PkgName); ok {
|
|
|
|
return x.Pkg() == y.Pkg()
|
2013-09-10 12:11:42 -06:00
|
|
|
}
|
|
|
|
}
|
2013-09-13 10:52:57 -06:00
|
|
|
return false
|
2013-09-10 12:11:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type referrersResult struct {
|
|
|
|
query token.Pos // identifer of query
|
|
|
|
obj types.Object // object it denotes
|
|
|
|
refs []token.Pos // set of all other references to it
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *referrersResult) display(printf printfFunc) {
|
|
|
|
if r.query != r.obj.Pos() {
|
|
|
|
printf(r.query, "reference to %s", r.obj.Name())
|
|
|
|
}
|
|
|
|
// TODO(adonovan): pretty-print object using same logic as
|
|
|
|
// (*describeValueResult).display.
|
|
|
|
printf(r.obj, "defined here as %s", r.obj)
|
|
|
|
for _, ref := range r.refs {
|
|
|
|
if r.query != ref {
|
|
|
|
printf(ref, "referenced here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-24 13:08:14 -06:00
|
|
|
func (r *referrersResult) toSerial(res *serial.Result, fset *token.FileSet) {
|
|
|
|
referrers := &serial.Referrers{
|
2013-09-10 12:11:42 -06:00
|
|
|
Pos: fset.Position(r.query).String(),
|
|
|
|
Desc: r.obj.String(),
|
|
|
|
}
|
2013-09-13 10:52:57 -06:00
|
|
|
if pos := r.obj.Pos(); pos != token.NoPos { // Package objects have no Pos()
|
2013-09-10 12:11:42 -06:00
|
|
|
referrers.ObjPos = fset.Position(pos).String()
|
|
|
|
}
|
|
|
|
for _, ref := range r.refs {
|
|
|
|
referrers.Refs = append(referrers.Refs, fset.Position(ref).String())
|
|
|
|
}
|
|
|
|
res.Referrers = referrers
|
|
|
|
}
|