GoPLS Viewer

Home|gopls/go/callgraph/static/static.go
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.
7package static // import "golang.org/x/tools/go/callgraph/static"
8
9// TODO(zpavlinovic): update static for how it handles generic function bodies.
10
11import (
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.
19func 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 siteok := instr.(ssa.CallInstruction); ok {
30                    if g := site.Common().StaticCallee(); g != nil {
31                        gnode := cg.CreateNode(g)
32                        callgraph.AddEdge(fnodesitegnode)
33                    }
34                }
35            }
36        }
37    }
38
39    return cg
40}
41
MembersX
CallGraph.prog
CallGraph.cg
CallGraph.RangeStmt_947.f
CallGraph.RangeStmt_947.BlockStmt.fnode
CallGraph.RangeStmt_947.BlockStmt.RangeStmt_1021.b
CallGraph.RangeStmt_947.BlockStmt.RangeStmt_1021.BlockStmt.RangeStmt_1053.BlockStmt.BlockStmt.BlockStmt.gnode
callgraph
CallGraph
CallGraph.RangeStmt_947.BlockStmt.RangeStmt_1021.BlockStmt.RangeStmt_1053.instr
CallGraph.RangeStmt_947.BlockStmt.RangeStmt_1021.BlockStmt.RangeStmt_1053.BlockStmt.BlockStmt.g
ssa
ssautil
Members
X