1
0
mirror of https://github.com/golang/go synced 2024-11-18 23:14:43 -07:00
go/oracle/callstack.go
Alan Donovan 3a4c0462d8 go.tools/oracle: change -mode argument into subcommand.
e.g. "oracle callgraph <package>"

Also: simplified error handling.
Eliminated oracle.errorf because it prepends "file:line:col: "
to the error message so the main function can't safely prepend "Error: ".
The position wasn't interesting though: it was just -pos, more or less.

R=crawshaw, dominik.honnef, r
CC=golang-dev
https://golang.org/cl/13864044
2013-09-25 14:34:39 -04:00

116 lines
2.9 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 (
"fmt"
"go/token"
"code.google.com/p/go.tools/oracle/serial"
"code.google.com/p/go.tools/pointer"
"code.google.com/p/go.tools/ssa"
)
// Callstack displays an arbitrary path from a root of the callgraph
// to the function at the current position.
//
// The information may be misleading in a context-insensitive
// analysis. e.g. the call path X->Y->Z might be infeasible if Y never
// calls Z when it is called from X. TODO(adonovan): think about UI.
//
// TODO(adonovan): permit user to specify a starting point other than
// the analysis root.
//
func callstack(o *Oracle, qpos *QueryPos) (queryResult, error) {
pkg := o.prog.Package(qpos.info.Pkg)
if pkg == nil {
return nil, fmt.Errorf("no SSA package")
}
if !ssa.HasEnclosingFunction(pkg, qpos.path) {
return nil, fmt.Errorf("this position is not inside a function")
}
buildSSA(o)
target := ssa.EnclosingFunction(pkg, qpos.path)
if target == nil {
return nil, fmt.Errorf("no SSA function built for this location (dead code?)")
}
// Run the pointer analysis and build the complete call graph.
callgraph := make(pointer.CallGraph)
o.config.Call = callgraph.AddEdge
root := ptrAnalysis(o)
seen := make(map[pointer.CallGraphNode]bool)
var callstack []pointer.CallSite
// Use depth-first search to find an arbitrary path from a
// root to the target function.
var search func(cgn pointer.CallGraphNode) bool
search = func(cgn pointer.CallGraphNode) bool {
if !seen[cgn] {
seen[cgn] = true
if cgn.Func() == target {
return true
}
for callee, site := range callgraph[cgn] {
if search(callee) {
callstack = append(callstack, site)
return true
}
}
}
return false
}
for toplevel := range callgraph[root] {
if search(toplevel) {
break
}
}
return &callstackResult{
qpos: qpos,
target: target,
callstack: callstack,
}, nil
}
type callstackResult struct {
qpos *QueryPos
target *ssa.Function
callstack []pointer.CallSite
}
func (r *callstackResult) display(printf printfFunc) {
if r.callstack != nil {
printf(r.qpos, "Found a call path from root to %s", r.target)
printf(r.target, "%s", r.target)
for _, site := range r.callstack {
printf(site, "%s from %s", site.Description(), site.Caller().Func())
}
} else {
printf(r.target, "%s is unreachable in this analysis scope", r.target)
}
}
func (r *callstackResult) toSerial(res *serial.Result, fset *token.FileSet) {
var callers []serial.Caller
for _, site := range r.callstack {
callers = append(callers, serial.Caller{
Pos: fset.Position(site.Pos()).String(),
Caller: site.Caller().Func().String(),
Desc: site.Description(),
})
}
res.Callstack = &serial.CallStack{
Pos: fset.Position(r.target.Pos()).String(),
Target: r.target.String(),
Callers: callers,
}
}