2013-08-27 16:49:13 -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.
|
|
|
|
|
2013-08-27 15:58:26 -06:00
|
|
|
package oracle
|
|
|
|
|
|
|
|
import (
|
2013-09-25 12:34:39 -06:00
|
|
|
"fmt"
|
2013-08-27 15:58:26 -06:00
|
|
|
"go/ast"
|
2013-09-03 13:29:02 -06:00
|
|
|
"go/token"
|
|
|
|
"sort"
|
2013-08-27 15:58:26 -06:00
|
|
|
|
|
|
|
"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-03 13:29:02 -06:00
|
|
|
"code.google.com/p/go.tools/ssa"
|
2013-08-27 15:58:26 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Callees reports the possible callees of the function call site
|
|
|
|
// identified by the specified source location.
|
|
|
|
//
|
|
|
|
// TODO(adonovan): if a callee is a wrapper, show the callee's callee.
|
|
|
|
//
|
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 callees(o *Oracle, qpos *QueryPos) (queryResult, error) {
|
2013-09-25 15:17:42 -06:00
|
|
|
pkg := o.prog.Package(qpos.info.Pkg)
|
|
|
|
if pkg == nil {
|
|
|
|
return nil, fmt.Errorf("no SSA package")
|
|
|
|
}
|
|
|
|
|
2013-08-27 15:58:26 -06:00
|
|
|
// Determine the enclosing call for the specified position.
|
2013-09-25 15:17:42 -06:00
|
|
|
var e *ast.CallExpr
|
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
|
|
|
for _, n := range qpos.path {
|
2013-09-25 15:17:42 -06:00
|
|
|
if e, _ = n.(*ast.CallExpr); e != nil {
|
2013-08-27 15:58:26 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2013-09-25 15:17:42 -06:00
|
|
|
if e == nil {
|
2013-09-25 12:34:39 -06:00
|
|
|
return nil, fmt.Errorf("there is no function call here")
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
// TODO(adonovan): issue an error if the call is "too far
|
|
|
|
// away" from the current selection, as this most likely is
|
|
|
|
// not what the user intended.
|
|
|
|
|
|
|
|
// Reject type conversions.
|
2013-09-25 15:17:42 -06:00
|
|
|
if qpos.info.IsType(e.Fun) {
|
2013-09-25 12:34:39 -06:00
|
|
|
return nil, fmt.Errorf("this is a type conversion, not a function call")
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reject calls to built-ins.
|
2013-09-25 15:17:42 -06:00
|
|
|
if id, ok := unparen(e.Fun).(*ast.Ident); ok {
|
2013-09-23 16:18:35 -06:00
|
|
|
if b, ok := qpos.info.ObjectOf(id).(*types.Builtin); ok {
|
2013-09-25 12:34:39 -06:00
|
|
|
return nil, fmt.Errorf("this is a call to the built-in '%s' operator", b.Name())
|
2013-09-23 16:18:35 -06:00
|
|
|
}
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
buildSSA(o)
|
|
|
|
|
2013-09-25 15:17:42 -06:00
|
|
|
// Ascertain calling function and call site.
|
|
|
|
callerFn := ssa.EnclosingFunction(pkg, qpos.path)
|
|
|
|
if callerFn == nil {
|
|
|
|
return nil, fmt.Errorf("no SSA function built for this location (dead code?)")
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
2013-09-25 15:17:42 -06:00
|
|
|
|
2013-12-10 08:16:35 -07:00
|
|
|
// Find the call site.
|
|
|
|
site, err := findCallSite(callerFn, e.Lparen)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
funcs, err := findCallees(o, site)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &calleesResult{
|
|
|
|
site: site,
|
|
|
|
funcs: funcs,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func findCallSite(fn *ssa.Function, lparen token.Pos) (ssa.CallInstruction, error) {
|
|
|
|
for _, b := range fn.Blocks {
|
|
|
|
for _, instr := range b.Instrs {
|
|
|
|
if site, ok := instr.(ssa.CallInstruction); ok && instr.Pos() == lparen {
|
|
|
|
return site, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("this call site is unreachable in this analysis")
|
|
|
|
}
|
|
|
|
|
|
|
|
func findCallees(o *Oracle, site ssa.CallInstruction) ([]*ssa.Function, error) {
|
|
|
|
// Avoid running the pointer analysis for static calls.
|
|
|
|
if callee := site.Common().StaticCallee(); callee != nil {
|
|
|
|
switch callee.String() {
|
|
|
|
case "runtime.SetFinalizer", "(reflect.Value).Call":
|
|
|
|
// The PTA treats calls to these intrinsics as dynamic.
|
|
|
|
// TODO(adonovan): avoid reliance on PTA internals.
|
|
|
|
|
|
|
|
default:
|
|
|
|
return []*ssa.Function{callee}, nil // singleton
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dynamic call: use pointer analysis.
|
2013-12-13 08:04:55 -07:00
|
|
|
o.ptaConfig.BuildCallGraph = true
|
2013-09-25 15:17:42 -06:00
|
|
|
callgraph := ptrAnalysis(o).CallGraph
|
|
|
|
|
2013-12-10 08:16:35 -07:00
|
|
|
// Find all call edges from the site.
|
2013-09-25 15:17:42 -06:00
|
|
|
calleesMap := make(map[*ssa.Function]bool)
|
2013-12-10 08:16:35 -07:00
|
|
|
var foundCGNode bool
|
2013-09-25 15:17:42 -06:00
|
|
|
for _, n := range callgraph.Nodes() {
|
2013-12-10 08:16:35 -07:00
|
|
|
if n.Func() == site.Parent() {
|
|
|
|
foundCGNode = true
|
2013-09-25 15:17:42 -06:00
|
|
|
for _, edge := range n.Edges() {
|
|
|
|
if edge.Site == site {
|
|
|
|
calleesMap[edge.Callee.Func()] = true
|
|
|
|
}
|
|
|
|
}
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
}
|
2013-12-10 08:16:35 -07:00
|
|
|
if !foundCGNode {
|
|
|
|
return nil, fmt.Errorf("this call site is unreachable in this analysis")
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
|
|
|
|
2013-09-25 15:17:42 -06:00
|
|
|
// Discard context, de-duplicate and sort.
|
|
|
|
funcs := make([]*ssa.Function, 0, len(calleesMap))
|
|
|
|
for f := range calleesMap {
|
2013-09-03 13:29:02 -06:00
|
|
|
funcs = append(funcs, f)
|
|
|
|
}
|
|
|
|
sort.Sort(byFuncPos(funcs))
|
2013-12-10 08:16:35 -07:00
|
|
|
return funcs, nil
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type calleesResult struct {
|
2013-09-25 15:17:42 -06:00
|
|
|
site ssa.CallInstruction
|
2013-09-03 13:29:02 -06:00
|
|
|
funcs []*ssa.Function
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
2013-09-03 13:29:02 -06:00
|
|
|
func (r *calleesResult) display(printf printfFunc) {
|
|
|
|
if len(r.funcs) == 0 {
|
|
|
|
// dynamic call on a provably nil func/interface
|
2013-09-25 15:17:42 -06:00
|
|
|
printf(r.site, "%s on nil value", r.site.Common().Description())
|
2013-09-03 13:29:02 -06:00
|
|
|
} else {
|
2013-09-25 15:17:42 -06:00
|
|
|
printf(r.site, "this %s dispatches to:", r.site.Common().Description())
|
2013-09-03 13:29:02 -06:00
|
|
|
for _, callee := range r.funcs {
|
|
|
|
printf(callee, "\t%s", callee)
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
|
|
|
}
|
2013-08-27 15:58:26 -06:00
|
|
|
|
2013-09-24 13:08:14 -06:00
|
|
|
func (r *calleesResult) toSerial(res *serial.Result, fset *token.FileSet) {
|
|
|
|
j := &serial.Callees{
|
2013-09-10 12:11:42 -06:00
|
|
|
Pos: fset.Position(r.site.Pos()).String(),
|
2013-09-25 15:17:42 -06:00
|
|
|
Desc: r.site.Common().Description(),
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
|
|
|
for _, callee := range r.funcs {
|
2013-09-24 13:08:14 -06:00
|
|
|
j.Callees = append(j.Callees, &serial.CalleesItem{
|
2013-09-03 13:29:02 -06:00
|
|
|
Name: callee.String(),
|
2013-09-10 12:11:42 -06:00
|
|
|
Pos: fset.Position(callee.Pos()).String(),
|
2013-09-03 13:29:02 -06:00
|
|
|
})
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
2013-09-03 13:29:02 -06:00
|
|
|
res.Callees = j
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
2013-09-03 13:29:02 -06:00
|
|
|
|
|
|
|
type byFuncPos []*ssa.Function
|
|
|
|
|
|
|
|
func (a byFuncPos) Len() int { return len(a) }
|
|
|
|
func (a byFuncPos) Less(i, j int) bool { return a[i].Pos() < a[j].Pos() }
|
|
|
|
func (a byFuncPos) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|