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 (
|
2014-02-21 08:46:02 -07:00
|
|
|
"fmt"
|
2013-09-03 13:29:02 -06:00
|
|
|
"go/token"
|
2013-11-13 07:11:10 -07:00
|
|
|
"sort"
|
2013-08-27 15:58:26 -06:00
|
|
|
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/go/callgraph"
|
|
|
|
"golang.org/x/tools/go/ssa"
|
|
|
|
"golang.org/x/tools/go/types"
|
|
|
|
"golang.org/x/tools/oracle/serial"
|
2013-08-27 15:58:26 -06:00
|
|
|
)
|
|
|
|
|
2014-02-21 08:46:02 -07:00
|
|
|
// doCallgraph displays the entire callgraph of the current program,
|
|
|
|
// or if a query -pos was provided, the query package.
|
|
|
|
func doCallgraph(o *Oracle, qpos *QueryPos) (queryResult, error) {
|
2013-08-27 15:58:26 -06:00
|
|
|
buildSSA(o)
|
|
|
|
|
2014-02-21 08:46:02 -07:00
|
|
|
// Run the pointer analysis and build the callgraph.
|
2013-12-13 08:04:55 -07:00
|
|
|
o.ptaConfig.BuildCallGraph = true
|
2014-02-21 08:46:02 -07:00
|
|
|
cg := ptrAnalysis(o).CallGraph
|
|
|
|
cg.DeleteSyntheticNodes()
|
2013-09-03 13:29:02 -06:00
|
|
|
|
2014-02-21 08:46:02 -07:00
|
|
|
var qpkg *types.Package
|
2014-06-10 10:36:40 -06:00
|
|
|
var isQueryPkg func(fn *ssa.Function) bool
|
|
|
|
var keep, remove, roots []*callgraph.Node
|
2014-02-21 08:46:02 -07:00
|
|
|
if qpos == nil {
|
|
|
|
// No -pos provided: show complete callgraph.
|
|
|
|
roots = append(roots, cg.Root)
|
2014-06-10 10:36:40 -06:00
|
|
|
isQueryPkg = func(fn *ssa.Function) bool { return true }
|
2014-02-21 08:46:02 -07:00
|
|
|
} else {
|
|
|
|
// A query -pos was provided: restrict result to
|
|
|
|
// functions belonging to the query package.
|
|
|
|
qpkg = qpos.info.Pkg
|
2014-06-10 10:36:40 -06:00
|
|
|
isQueryPkg = func(fn *ssa.Function) bool {
|
2014-02-21 08:46:02 -07:00
|
|
|
return fn.Pkg != nil && fn.Pkg.Object == qpkg
|
|
|
|
}
|
2014-06-10 10:36:40 -06:00
|
|
|
}
|
2014-02-21 08:46:02 -07:00
|
|
|
|
2014-06-10 10:36:40 -06:00
|
|
|
// First compute the nodes to keep and remove.
|
|
|
|
for fn, cgn := range cg.Nodes {
|
|
|
|
if isQueryPkg(fn) {
|
|
|
|
keep = append(keep, cgn)
|
|
|
|
} else {
|
|
|
|
remove = append(remove, cgn)
|
2014-02-21 08:46:02 -07:00
|
|
|
}
|
2014-06-10 10:36:40 -06:00
|
|
|
}
|
2014-02-21 08:46:02 -07:00
|
|
|
|
2014-06-10 10:36:40 -06:00
|
|
|
// Compact the Node.ID sequence of the kept nodes,
|
|
|
|
// preserving the original order.
|
|
|
|
sort.Sort(nodesByID(keep))
|
|
|
|
for i, cgn := range keep {
|
|
|
|
cgn.ID = i
|
|
|
|
}
|
2014-02-21 08:46:02 -07:00
|
|
|
|
2014-06-10 10:36:40 -06:00
|
|
|
// Compute the set of roots:
|
|
|
|
// in-package nodes with out-of-package callers.
|
|
|
|
// For determinism, roots are ordered by original Node.ID.
|
|
|
|
for _, cgn := range keep {
|
|
|
|
for _, e := range cgn.In {
|
|
|
|
if !isQueryPkg(e.Caller.Func) {
|
|
|
|
roots = append(roots, cgn)
|
|
|
|
break
|
2014-02-21 08:46:02 -07:00
|
|
|
}
|
|
|
|
}
|
2014-06-10 10:36:40 -06:00
|
|
|
}
|
2014-02-21 08:46:02 -07:00
|
|
|
|
2014-06-10 10:36:40 -06:00
|
|
|
// Finally, discard all out-of-package nodes.
|
|
|
|
for _, cgn := range remove {
|
|
|
|
cg.DeleteNode(cgn)
|
2014-02-21 08:46:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return &callgraphResult{qpkg, cg.Nodes, roots}, nil
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type callgraphResult struct {
|
2014-02-21 08:46:02 -07:00
|
|
|
qpkg *types.Package
|
|
|
|
nodes map[*ssa.Function]*callgraph.Node
|
|
|
|
roots []*callgraph.Node
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
2013-09-03 13:29:02 -06:00
|
|
|
func (r *callgraphResult) display(printf printfFunc) {
|
2014-02-21 08:46:02 -07:00
|
|
|
descr := "the entire program"
|
|
|
|
if r.qpkg != nil {
|
|
|
|
descr = fmt.Sprintf("package %s", r.qpkg.Path())
|
|
|
|
}
|
|
|
|
|
2013-09-03 13:29:02 -06:00
|
|
|
printf(nil, `
|
2014-02-21 08:46:02 -07:00
|
|
|
Below is a call graph of %s.
|
2013-08-27 15:58:26 -06:00
|
|
|
The numbered nodes form a spanning tree.
|
|
|
|
Non-numbered nodes indicate back- or cross-edges to the node whose
|
|
|
|
number follows in parentheses.
|
2014-02-21 08:46:02 -07:00
|
|
|
`, descr)
|
2013-11-13 07:11:10 -07:00
|
|
|
|
2014-02-20 09:57:48 -07:00
|
|
|
printed := make(map[*callgraph.Node]int)
|
|
|
|
var print func(caller *callgraph.Node, indent int)
|
|
|
|
print = func(caller *callgraph.Node, indent int) {
|
2013-11-13 07:11:10 -07:00
|
|
|
if num, ok := printed[caller]; !ok {
|
|
|
|
num = len(printed)
|
|
|
|
printed[caller] = num
|
|
|
|
|
|
|
|
// Sort the children into name order for deterministic* output.
|
|
|
|
// (*mostly: anon funcs' names are not globally unique.)
|
|
|
|
var funcs funcsByName
|
2014-02-20 09:57:48 -07:00
|
|
|
for callee := range callgraph.CalleesOf(caller) {
|
|
|
|
funcs = append(funcs, callee.Func)
|
2013-11-13 07:11:10 -07:00
|
|
|
}
|
|
|
|
sort.Sort(funcs)
|
|
|
|
|
2014-02-21 08:46:02 -07:00
|
|
|
printf(caller.Func, "%d\t%*s%s", num, 4*indent, "", caller.Func.RelString(r.qpkg))
|
2013-11-13 07:11:10 -07:00
|
|
|
for _, callee := range funcs {
|
2014-02-21 08:46:02 -07:00
|
|
|
print(r.nodes[callee], indent+1)
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
|
|
|
} else {
|
2014-02-21 08:46:02 -07:00
|
|
|
printf(caller.Func, "\t%*s%s (%d)", 4*indent, "", caller.Func.RelString(r.qpkg), num)
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
|
|
|
}
|
2014-02-21 08:46:02 -07:00
|
|
|
for _, root := range r.roots {
|
|
|
|
print(root, 0)
|
|
|
|
}
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
|
|
|
|
2014-02-21 08:46:02 -07:00
|
|
|
type nodesByID []*callgraph.Node
|
|
|
|
|
|
|
|
func (s nodesByID) Len() int { return len(s) }
|
|
|
|
func (s nodesByID) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
func (s nodesByID) Less(i, j int) bool { return s[i].ID < s[j].ID }
|
|
|
|
|
2013-11-13 07:11:10 -07:00
|
|
|
type funcsByName []*ssa.Function
|
|
|
|
|
|
|
|
func (s funcsByName) Len() int { return len(s) }
|
|
|
|
func (s funcsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
func (s funcsByName) Less(i, j int) bool { return s[i].String() < s[j].String() }
|
|
|
|
|
2013-09-24 13:08:14 -06:00
|
|
|
func (r *callgraphResult) toSerial(res *serial.Result, fset *token.FileSet) {
|
2014-02-21 08:46:02 -07:00
|
|
|
cg := make([]serial.CallGraph, len(r.nodes))
|
|
|
|
for _, n := range r.nodes {
|
2014-02-20 09:57:48 -07:00
|
|
|
j := &cg[n.ID]
|
|
|
|
fn := n.Func
|
2013-09-03 13:29:02 -06:00
|
|
|
j.Name = fn.String()
|
2013-09-10 12:11:42 -06:00
|
|
|
j.Pos = fset.Position(fn.Pos()).String()
|
2014-01-16 12:04:19 -07:00
|
|
|
for callee := range callgraph.CalleesOf(n) {
|
2014-02-20 09:57:48 -07:00
|
|
|
j.Children = append(j.Children, callee.ID)
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
2014-01-15 20:55:52 -07:00
|
|
|
sort.Ints(j.Children)
|
2013-09-03 13:29:02 -06:00
|
|
|
}
|
|
|
|
res.Callgraph = cg
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|