mirror of
https://github.com/golang/go
synced 2024-11-19 03:44:40 -07:00
25a0cc4bfd
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
91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
// 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 (
|
|
"go/token"
|
|
|
|
"code.google.com/p/go.tools/oracle/json"
|
|
"code.google.com/p/go.tools/pointer"
|
|
"code.google.com/p/go.tools/ssa"
|
|
)
|
|
|
|
// Callers reports the possible callers of the function
|
|
// immediately enclosing the specified source location.
|
|
//
|
|
// TODO(adonovan): if a caller is a wrapper, show the caller's caller.
|
|
//
|
|
func callers(o *Oracle, qpos *QueryPos) (queryResult, error) {
|
|
pkg := o.prog.Package(qpos.info.Pkg)
|
|
if pkg == nil {
|
|
return nil, o.errorf(qpos.path[0], "no SSA package")
|
|
}
|
|
if !ssa.HasEnclosingFunction(pkg, qpos.path) {
|
|
return nil, o.errorf(qpos.path[0], "this position is not inside a function")
|
|
}
|
|
|
|
buildSSA(o)
|
|
|
|
target := ssa.EnclosingFunction(pkg, qpos.path)
|
|
if target == nil {
|
|
return nil, o.errorf(qpos.path[0], "no SSA function built for this location (dead code?)")
|
|
}
|
|
|
|
// Run the pointer analysis, recording each
|
|
// call found to originate from target.
|
|
var calls []pointer.CallSite
|
|
o.config.Call = func(site pointer.CallSite, callee pointer.CallGraphNode) {
|
|
if callee.Func() == target {
|
|
calls = append(calls, site)
|
|
}
|
|
}
|
|
// TODO(adonovan): sort calls, to ensure test determinism.
|
|
|
|
root := ptrAnalysis(o)
|
|
|
|
return &callersResult{
|
|
target: target,
|
|
root: root,
|
|
calls: calls,
|
|
}, nil
|
|
}
|
|
|
|
type callersResult struct {
|
|
target *ssa.Function
|
|
root pointer.CallGraphNode
|
|
calls []pointer.CallSite
|
|
}
|
|
|
|
func (r *callersResult) display(printf printfFunc) {
|
|
if r.calls == nil {
|
|
printf(r.target, "%s is not reachable in this program.", r.target)
|
|
} else {
|
|
printf(r.target, "%s is called from these %d sites:", r.target, len(r.calls))
|
|
for _, site := range r.calls {
|
|
if site.Caller() == r.root {
|
|
printf(r.target, "the root of the call graph")
|
|
} else {
|
|
printf(site, "\t%s from %s", site.Description(), site.Caller().Func())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (r *callersResult) toJSON(res *json.Result, fset *token.FileSet) {
|
|
var callers []json.Caller
|
|
for _, site := range r.calls {
|
|
var c json.Caller
|
|
c.Caller = site.Caller().Func().String()
|
|
if site.Caller() == r.root {
|
|
c.Desc = "synthetic call"
|
|
} else {
|
|
c.Pos = fset.Position(site.Pos()).String()
|
|
c.Desc = site.Description()
|
|
}
|
|
callers = append(callers, c)
|
|
}
|
|
res.Callers = callers
|
|
}
|