wide/vendor/golang.org/x/tools/cmd/guru/callees.go

258 lines
6.7 KiB
Go
Raw Normal View History

2018-03-13 07:32:44 +03: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.
2018-03-13 08:24:04 +03:00
package main
2018-03-13 07:32:44 +03:00
import (
"fmt"
"go/ast"
"go/token"
2018-03-13 08:24:04 +03:00
"go/types"
2018-03-13 07:32:44 +03:00
"sort"
2018-03-13 08:24:04 +03:00
"golang.org/x/tools/cmd/guru/serial"
2018-03-13 07:32:44 +03:00
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/pointer"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
)
// Callees reports the possible callees of the function call site
// identified by the specified source location.
func callees(q *Query) error {
lconf := loader.Config{Build: q.Build}
if err := setPTAScope(&lconf, q.Scope); err != nil {
return err
}
// Load/parse/type-check the program.
2018-03-13 08:24:04 +03:00
lprog, err := loadWithSoftErrors(&lconf)
2018-03-13 07:32:44 +03:00
if err != nil {
return err
}
qpos, err := parseQueryPos(lprog, q.Pos, true) // needs exact pos
if err != nil {
return err
}
// Determine the enclosing call for the specified position.
var e *ast.CallExpr
for _, n := range qpos.path {
if e, _ = n.(*ast.CallExpr); e != nil {
break
}
}
if e == nil {
return fmt.Errorf("there is no function call here")
}
// 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.
if qpos.info.Types[e.Fun].IsType() {
return fmt.Errorf("this is a type conversion, not a function call")
}
// Deal with obviously static calls before constructing SSA form.
// Some static calls may yet require SSA construction,
// e.g. f := func(){}; f().
switch funexpr := unparen(e.Fun).(type) {
case *ast.Ident:
switch obj := qpos.info.Uses[funexpr].(type) {
case *types.Builtin:
// Reject calls to built-ins.
return fmt.Errorf("this is a call to the built-in '%s' operator", obj.Name())
case *types.Func:
// This is a static function call
2018-03-13 08:24:04 +03:00
q.Output(lprog.Fset, &calleesTypesResult{
2018-03-13 07:32:44 +03:00
site: e,
callee: obj,
2018-03-13 08:24:04 +03:00
})
2018-03-13 07:32:44 +03:00
return nil
}
case *ast.SelectorExpr:
sel := qpos.info.Selections[funexpr]
if sel == nil {
// qualified identifier.
// May refer to top level function variable
// or to top level function.
callee := qpos.info.Uses[funexpr.Sel]
if obj, ok := callee.(*types.Func); ok {
2018-03-13 08:24:04 +03:00
q.Output(lprog.Fset, &calleesTypesResult{
2018-03-13 07:32:44 +03:00
site: e,
callee: obj,
2018-03-13 08:24:04 +03:00
})
2018-03-13 07:32:44 +03:00
return nil
}
} else if sel.Kind() == types.MethodVal {
2018-03-13 08:24:04 +03:00
// Inspect the receiver type of the selected method.
// If it is concrete, the call is statically dispatched.
// (Due to implicit field selections, it is not enough to look
// at sel.Recv(), the type of the actual receiver expression.)
method := sel.Obj().(*types.Func)
recvtype := method.Type().(*types.Signature).Recv().Type()
2018-03-13 07:32:44 +03:00
if !types.IsInterface(recvtype) {
// static method call
2018-03-13 08:24:04 +03:00
q.Output(lprog.Fset, &calleesTypesResult{
2018-03-13 07:32:44 +03:00
site: e,
2018-03-13 08:24:04 +03:00
callee: method,
})
2018-03-13 07:32:44 +03:00
return nil
}
}
}
prog := ssautil.CreateProgram(lprog, ssa.GlobalDebug)
ptaConfig, err := setupPTA(prog, lprog, q.PTALog, q.Reflection)
if err != nil {
return err
}
pkg := prog.Package(qpos.info.Pkg)
if pkg == nil {
return fmt.Errorf("no SSA package")
}
// Defer SSA construction till after errors are reported.
2018-03-13 08:24:04 +03:00
prog.Build()
2018-03-13 07:32:44 +03:00
// Ascertain calling function and call site.
callerFn := ssa.EnclosingFunction(pkg, qpos.path)
if callerFn == nil {
return fmt.Errorf("no SSA function built for this location (dead code?)")
}
// Find the call site.
site, err := findCallSite(callerFn, e)
if err != nil {
return err
}
funcs, err := findCallees(ptaConfig, site)
if err != nil {
return err
}
2018-03-13 08:24:04 +03:00
q.Output(lprog.Fset, &calleesSSAResult{
2018-03-13 07:32:44 +03:00
site: site,
funcs: funcs,
2018-03-13 08:24:04 +03:00
})
2018-03-13 07:32:44 +03:00
return nil
}
func findCallSite(fn *ssa.Function, call *ast.CallExpr) (ssa.CallInstruction, error) {
instr, _ := fn.ValueForExpr(call)
callInstr, _ := instr.(ssa.CallInstruction)
if instr == nil {
return nil, fmt.Errorf("this call site is unreachable in this analysis")
}
return callInstr, nil
}
func findCallees(conf *pointer.Config, 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.
conf.BuildCallGraph = true
cg := ptrAnalysis(conf).CallGraph
cg.DeleteSyntheticNodes()
// Find all call edges from the site.
n := cg.Nodes[site.Parent()]
if n == nil {
return nil, fmt.Errorf("this call site is unreachable in this analysis")
}
calleesMap := make(map[*ssa.Function]bool)
for _, edge := range n.Out {
if edge.Site == site {
calleesMap[edge.Callee.Func] = true
}
}
// De-duplicate and sort.
funcs := make([]*ssa.Function, 0, len(calleesMap))
for f := range calleesMap {
funcs = append(funcs, f)
}
sort.Sort(byFuncPos(funcs))
return funcs, nil
}
type calleesSSAResult struct {
site ssa.CallInstruction
funcs []*ssa.Function
}
type calleesTypesResult struct {
site *ast.CallExpr
callee *types.Func
}
2018-03-13 08:24:04 +03:00
func (r *calleesSSAResult) PrintPlain(printf printfFunc) {
2018-03-13 07:32:44 +03:00
if len(r.funcs) == 0 {
// dynamic call on a provably nil func/interface
printf(r.site, "%s on nil value", r.site.Common().Description())
} else {
printf(r.site, "this %s dispatches to:", r.site.Common().Description())
for _, callee := range r.funcs {
printf(callee, "\t%s", callee)
}
}
}
2018-03-13 08:24:04 +03:00
func (r *calleesSSAResult) JSON(fset *token.FileSet) []byte {
2018-03-13 07:32:44 +03:00
j := &serial.Callees{
Pos: fset.Position(r.site.Pos()).String(),
Desc: r.site.Common().Description(),
}
for _, callee := range r.funcs {
2018-03-13 08:24:04 +03:00
j.Callees = append(j.Callees, &serial.Callee{
2018-03-13 07:32:44 +03:00
Name: callee.String(),
Pos: fset.Position(callee.Pos()).String(),
})
}
2018-03-13 08:24:04 +03:00
return toJSON(j)
2018-03-13 07:32:44 +03:00
}
2018-03-13 08:24:04 +03:00
func (r *calleesTypesResult) PrintPlain(printf printfFunc) {
2018-03-13 07:32:44 +03:00
printf(r.site, "this static function call dispatches to:")
printf(r.callee, "\t%s", r.callee.FullName())
}
2018-03-13 08:24:04 +03:00
func (r *calleesTypesResult) JSON(fset *token.FileSet) []byte {
2018-03-13 07:32:44 +03:00
j := &serial.Callees{
Pos: fset.Position(r.site.Pos()).String(),
Desc: "static function call",
}
2018-03-13 08:24:04 +03:00
j.Callees = []*serial.Callee{
{
2018-03-13 07:32:44 +03:00
Name: r.callee.FullName(),
Pos: fset.Position(r.callee.Pos()).String(),
},
}
2018-03-13 08:24:04 +03:00
return toJSON(j)
2018-03-13 07:32:44 +03:00
}
// NB: byFuncPos is not deterministic across packages since it depends on load order.
// Use lessPos if the tests need it.
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] }