1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package static computes the call graph of a Go program containing
6 // only static call edges.
7 package static // import "golang.org/x/tools/go/callgraph/static"
10 "golang.org/x/tools/go/callgraph"
11 "golang.org/x/tools/go/ssa"
12 "golang.org/x/tools/go/ssa/ssautil"
15 // CallGraph computes the call graph of the specified program
16 // considering only static calls.
18 func CallGraph(prog *ssa.Program) *callgraph.Graph {
19 cg := callgraph.New(nil) // TODO(adonovan) eliminate concept of rooted callgraph
21 // TODO(adonovan): opt: use only a single pass over the ssa.Program.
22 // TODO(adonovan): opt: this is slower than RTA (perhaps because
23 // the lower precision means so many edges are allocated)!
24 for f := range ssautil.AllFunctions(prog) {
25 fnode := cg.CreateNode(f)
26 for _, b := range f.Blocks {
27 for _, instr := range b.Instrs {
28 if site, ok := instr.(ssa.CallInstruction); ok {
29 if g := site.Common().StaticCallee(); g != nil {
30 gnode := cg.CreateNode(g)
31 callgraph.AddEdge(fnode, site, gnode)