mirror of
https://github.com/golang/go.git
synced 2025-05-06 08:03:03 +00:00
Features: More robust: silently ignore type errors in modes that don't need SSA form: describe, referrers, implements, freevars, description. This makes the tool much more robust for everyday queries. Less configuration: don't require a scope argument for all queries. Only queries that do pointer analysis need it. For the rest, the initial position is enough for importQueryPackage to deduce the scope. It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles. (It no longer works for ad-hoc main packages like $GOROOT/src/net/http/triv.go) More complete: "referrers" computes the scope automatically by scanning the import graph of the entire workspace, using gorename's refactor/importgraph package. This requires two passes at loading. Faster: simplified start-up logic avoids unnecessary package loading and SSA construction (a consequence of bad abstraction) in many cases. "callgraph": remove it. Unlike all the other commands it isn't related to the current selection, and we have golang.org/x/tools/cmdcallgraph now. Internals: Drop support for long-running clients (i.e., Pythia), since godoc -analysis supports all the same features except "pointsto", and precomputes all the results so latency is much lower. Get rid of various unhelpful abstractions introduced to support long-running clients. Expand out the set-up logic for each subcommand. This is simpler, easier to read, and gives us more control, at a small cost in duplication---the familiar story of abstractions. Discard PTA warnings. We weren't showing them (nor should we). Split tests into separate directories (so that importgraph works). Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237 Reviewed-on: https://go-review.googlesource.com/8243 Reviewed-by: David Crawshaw <crawshaw@golang.org>
131 lines
3.3 KiB
Go
131 lines
3.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 (
|
|
"fmt"
|
|
"go/token"
|
|
|
|
"golang.org/x/tools/go/callgraph"
|
|
"golang.org/x/tools/go/loader"
|
|
"golang.org/x/tools/go/ssa"
|
|
"golang.org/x/tools/oracle/serial"
|
|
)
|
|
|
|
// 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(conf *Query) error {
|
|
fset := token.NewFileSet()
|
|
lconf := loader.Config{Fset: fset, Build: conf.Build}
|
|
|
|
// Determine initial packages for PTA.
|
|
args, err := lconf.FromArgs(conf.Scope, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(args) > 0 {
|
|
return fmt.Errorf("surplus arguments: %q", args)
|
|
}
|
|
|
|
// Load/parse/type-check the program.
|
|
lprog, err := lconf.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
qpos, err := parseQueryPos(lprog, conf.Pos, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
prog := ssa.Create(lprog, 0)
|
|
|
|
ptaConfig, err := setupPTA(prog, lprog, conf.PTALog, conf.Reflection)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pkg := prog.Package(qpos.info.Pkg)
|
|
if pkg == nil {
|
|
return fmt.Errorf("no SSA package")
|
|
}
|
|
|
|
if !ssa.HasEnclosingFunction(pkg, qpos.path) {
|
|
return fmt.Errorf("this position is not inside a function")
|
|
}
|
|
|
|
// Defer SSA construction till after errors are reported.
|
|
prog.BuildAll()
|
|
|
|
target := ssa.EnclosingFunction(pkg, qpos.path)
|
|
if target == nil {
|
|
return fmt.Errorf("no SSA function built for this location (dead code?)")
|
|
}
|
|
|
|
// Run the pointer analysis and build the complete call graph.
|
|
ptaConfig.BuildCallGraph = true
|
|
cg := ptrAnalysis(ptaConfig).CallGraph
|
|
cg.DeleteSyntheticNodes()
|
|
|
|
// Search for an arbitrary path from a root to the target function.
|
|
isEnd := func(n *callgraph.Node) bool { return n.Func == target }
|
|
callpath := callgraph.PathSearch(cg.Root, isEnd)
|
|
if callpath != nil {
|
|
callpath = callpath[1:] // remove synthetic edge from <root>
|
|
}
|
|
|
|
conf.Fset = fset
|
|
conf.result = &callstackResult{
|
|
qpos: qpos,
|
|
target: target,
|
|
callpath: callpath,
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type callstackResult struct {
|
|
qpos *queryPos
|
|
target *ssa.Function
|
|
callpath []*callgraph.Edge
|
|
}
|
|
|
|
func (r *callstackResult) display(printf printfFunc) {
|
|
if r.callpath != nil {
|
|
printf(r.qpos, "Found a call path from root to %s", r.target)
|
|
printf(r.target, "%s", r.target)
|
|
for i := len(r.callpath) - 1; i >= 0; i-- {
|
|
edge := r.callpath[i]
|
|
printf(edge, "%s from %s", edge.Description(), edge.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 i := len(r.callpath) - 1; i >= 0; i-- { // (innermost first)
|
|
edge := r.callpath[i]
|
|
callers = append(callers, serial.Caller{
|
|
Pos: fset.Position(edge.Pos()).String(),
|
|
Caller: edge.Caller.Func.String(),
|
|
Desc: edge.Description(),
|
|
})
|
|
}
|
|
res.Callstack = &serial.CallStack{
|
|
Pos: fset.Position(r.target.Pos()).String(),
|
|
Target: r.target.String(),
|
|
Callers: callers,
|
|
}
|
|
}
|