| 1 | // Copyright 2013 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 | /* |
| 6 | Package callgraph defines the call graph and various algorithms |
| 7 | and utilities to operate on it. |
| 8 | |
| 9 | A call graph is a labelled directed graph whose nodes represent |
| 10 | functions and whose edge labels represent syntactic function call |
| 11 | sites. The presence of a labelled edge (caller, site, callee) |
| 12 | indicates that caller may call callee at the specified call site. |
| 13 | |
| 14 | A call graph is a multigraph: it may contain multiple edges (caller, |
| 15 | *, callee) connecting the same pair of nodes, so long as the edges |
| 16 | differ by label; this occurs when one function calls another function |
| 17 | from multiple call sites. Also, it may contain multiple edges |
| 18 | (caller, site, *) that differ only by callee; this indicates a |
| 19 | polymorphic call. |
| 20 | |
| 21 | A SOUND call graph is one that overapproximates the dynamic calling |
| 22 | behaviors of the program in all possible executions. One call graph |
| 23 | is more PRECISE than another if it is a smaller overapproximation of |
| 24 | the dynamic behavior. |
| 25 | |
| 26 | All call graphs have a synthetic root node which is responsible for |
| 27 | calling main() and init(). |
| 28 | |
| 29 | Calls to built-in functions (e.g. panic, println) are not represented |
| 30 | in the call graph; they are treated like built-in operators of the |
| 31 | language. |
| 32 | */ |
| 33 | package callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/ // import "golang.org/x/tools/go/callgraph" |
| 34 | |
| 35 | // TODO(zpavlinovic): decide how callgraphs handle calls to and from generic function bodies. |
| 36 | |
| 37 | import ( |
| 38 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 39 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 40 | |
| 41 | "golang.org/x/tools/go/ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/" |
| 42 | ) |
| 43 | |
| 44 | // A Graph represents a call graph. |
| 45 | // |
| 46 | // A graph may contain nodes that are not reachable from the root. |
| 47 | // If the call graph is sound, such nodes indicate unreachable |
| 48 | // functions. |
| 49 | type Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph# struct { |
| 50 | Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#Root. *Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node# // the distinguished root node (Root.Func may be nil) |
| 51 | Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#Nodes. map[*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#]*Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node# // all nodes by function |
| 52 | } |
| 53 | |
| 54 | // New returns a new Graph with the specified (optional) root node. |
| 55 | func Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/New().(root *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) *Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph# { |
| 56 | g := &Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#{Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#Nodes.: make(map[*ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#]*Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#)} |
| 57 | g.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#Root. = g.CreateNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#CreateNode().(root) |
| 58 | return g |
| 59 | } |
| 60 | |
| 61 | // CreateNode returns the Node for fn, creating it if not present. |
| 62 | // The root node may have fn=nil. |
| 63 | func (g *Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#) CreateNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#CreateNode().(fn *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) *Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node# { |
| 64 | n, ok := g.Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#Nodes.[fn] |
| 65 | if !ok { |
| 66 | n = &Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#{Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#Func.: fn, IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#ID.: len(g.Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#Nodes.)} |
| 67 | g.Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#Nodes.[fn] = n |
| 68 | } |
| 69 | return n |
| 70 | } |
| 71 | |
| 72 | // A Node represents a node in a call graph. |
| 73 | type Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node# struct { |
| 74 | Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#Func. *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function# // the function this node represents |
| 75 | IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#ID. int // 0-based sequence number |
| 76 | Inscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#In. []*Edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge# // unordered set of incoming call edges (n.In[*].Callee == n) |
| 77 | Outscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#Out. []*Edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge# // unordered set of outgoing call edges (n.Out[*].Caller == n) |
| 78 | } |
| 79 | |
| 80 | func (n *Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#) Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#String().() string { |
| 81 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("n%d:%s", n.IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#ID., n.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#Func.) |
| 82 | } |
| 83 | |
| 84 | // A Edge represents an edge in the call graph. |
| 85 | // |
| 86 | // Site is nil for edges originating in synthetic or intrinsic |
| 87 | // functions, e.g. reflect.Value.Call or the root of the call graph. |
| 88 | type Edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge# struct { |
| 89 | Callerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#Caller. *Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node# |
| 90 | Sitescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#Site. ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.CallInstructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction# |
| 91 | Calleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#Callee. *Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node# |
| 92 | } |
| 93 | |
| 94 | func (e Edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#) Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#String().() string { |
| 95 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s --> %s", e.Callerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#Caller., e.Calleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#Callee.) |
| 96 | } |
| 97 | |
| 98 | func (e Edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#) Descriptionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#Description().() string { |
| 99 | var prefix string |
| 100 | switch e.Sitescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#Site..(type) { |
| 101 | case nil: |
| 102 | return "synthetic call" |
| 103 | case *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Goscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Go#: |
| 104 | prefix = "concurrent " |
| 105 | case *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Deferscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Defer#: |
| 106 | prefix = "deferred " |
| 107 | } |
| 108 | return prefix + e.Sitescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#Site..Commonscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#Common.().Descriptionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#Description().() |
| 109 | } |
| 110 | |
| 111 | func (e Edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#) Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#Pos().() tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos# { |
| 112 | if e.Sitescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#Site. == nil { |
| 113 | return tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NoPosscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NoPos. |
| 114 | } |
| 115 | return e.Sitescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#Site..Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Instruction#Pos.() |
| 116 | } |
| 117 | |
| 118 | // AddEdge adds the edge (caller, site, callee) to the call graph. |
| 119 | // Elimination of duplicate edges is the caller's responsibility. |
| 120 | func AddEdgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/AddEdge().(caller *Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#, site ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.CallInstructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#, callee *Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#) { |
| 121 | e := &Edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#{caller, site, callee} |
| 122 | callee.Inscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#In. = append(callee.Inscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#In., e) |
| 123 | caller.Outscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#Out. = append(caller.Outscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#Out., e) |
| 124 | } |