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. |
4 | |
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" |
8 | |
9 | // TODO(zpavlinovic): update static for how it handles generic function bodies. |
10 | |
11 | import ( |
12 | "golang.org/x/tools/go/callgraph" |
13 | "golang.org/x/tools/go/ssa" |
14 | "golang.org/x/tools/go/ssa/ssautil" |
15 | ) |
16 | |
17 | // CallGraph computes the call graph of the specified program |
18 | // considering only static calls. |
19 | func CallGraph(prog *ssa.Program) *callgraph.Graph { |
20 | cg := callgraph.New(nil) // TODO(adonovan) eliminate concept of rooted callgraph |
21 | |
22 | // TODO(adonovan): opt: use only a single pass over the ssa.Program. |
23 | // TODO(adonovan): opt: this is slower than RTA (perhaps because |
24 | // the lower precision means so many edges are allocated)! |
25 | for f := range ssautil.AllFunctions(prog) { |
26 | fnode := cg.CreateNode(f) |
27 | for _, b := range f.Blocks { |
28 | for _, instr := range b.Instrs { |
29 | if site, ok := instr.(ssa.CallInstruction); ok { |
30 | if g := site.Common().StaticCallee(); g != nil { |
31 | gnode := cg.CreateNode(g) |
32 | callgraph.AddEdge(fnode, site, gnode) |
33 | } |
34 | } |
35 | } |
36 | } |
37 | } |
38 | |
39 | return cg |
40 | } |
41 |
Members