CBMC
call_graph.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Function Call Graphs
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #ifndef CPROVER_ANALYSES_CALL_GRAPH_H
13 #define CPROVER_ANALYSES_CALL_GRAPH_H
14 
15 #include <iosfwd>
16 #include <map>
17 #include <unordered_set>
18 
19 #include <util/graph.h>
20 
22 
23 class goto_functionst;
24 class goto_modelt;
25 
44 {
45 public:
46  explicit call_grapht(bool collect_callsites=false);
47  explicit call_grapht(const goto_modelt &, bool collect_callsites=false);
48  explicit call_grapht(const goto_functionst &, bool collect_callsites=false);
49 
50  // These two functions build a call graph restricted to functions
51  // reachable from the given root.
52 
54  const goto_modelt &model,
55  const irep_idt &root,
56  bool collect_callsites)
57  {
58  return call_grapht(model, root, collect_callsites);
59  }
60 
62  const goto_functionst &functions,
63  const irep_idt &root,
64  bool collect_callsites)
65  {
66  return call_grapht(functions, root, collect_callsites);
67  }
68 
69  // Constructors used to implement the above:
70 
71 private:
73  const goto_modelt &model,
74  const irep_idt &root,
75  bool collect_callsites);
77  const goto_functionst &functions,
78  const irep_idt &root,
79  bool collect_callsites);
80 
81 public:
82 
83  void output_dot(std::ostream &out) const;
84  void output(std::ostream &out) const;
85  void output_xml(std::ostream &out) const;
86 
88  typedef std::unordered_set<irep_idt, irep_id_hash> nodest;
89 
92  typedef std::multimap<irep_idt, irep_idt> edgest;
93 
95  typedef std::pair<irep_idt, irep_idt> edget;
96 
99 
101  typedef std::set<locationt, goto_programt::target_less_than> locationst;
102 
105  typedef std::map<edget, locationst> callsitest;
106 
114 
117 
118  void add(const irep_idt &caller, const irep_idt &callee);
119  void add(const irep_idt &caller, const irep_idt &callee, locationt callsite);
120 
121  call_grapht get_inverted() const;
122 
125  {
129  };
130 
132  struct function_nodet : public graph_nodet<edge_with_callsitest>
133  {
135  irep_idt function;
136  };
137 
139  class directed_grapht : public ::grapht<function_nodet>
140  {
141  friend class call_grapht;
142 
144  std::unordered_map<irep_idt, node_indext> nodes_by_name;
145 
146  public:
150  std::optional<node_indext> get_node_index(const irep_idt &function) const;
151 
153  typedef std::unordered_map<irep_idt, node_indext> nodes_by_namet;
154 
158  {
159  return nodes_by_name;
160  }
161  };
162 
163  directed_grapht get_directed_graph() const;
164 
165 protected:
166  void add(const irep_idt &function,
167  const goto_programt &body);
168 private:
170  std::string format_callsites(const edget &edge) const;
171 };
172 
173 #endif // CPROVER_ANALYSES_CALL_GRAPH_H
Directed graph representation of this call graph.
Definition: call_graph.h:140
std::optional< node_indext > get_node_index(const irep_idt &function) const
Find the graph node by function name.
Definition: call_graph.cpp:300
std::unordered_map< irep_idt, node_indext > nodes_by_name
Maps function names onto node indices.
Definition: call_graph.h:144
const nodes_by_namet & get_nodes_by_name() const
Get the node name -> node index map.
Definition: call_graph.h:157
std::unordered_map< irep_idt, node_indext > nodes_by_namet
Type of the node name -> node index map.
Definition: call_graph.h:153
A call graph (https://en.wikipedia.org/wiki/Call_graph) for a GOTO model or GOTO functions collection...
Definition: call_graph.h:44
void add(const irep_idt &caller, const irep_idt &callee)
Add edge.
Definition: call_graph.cpp:139
bool collect_callsites
Definition: call_graph.h:169
std::map< edget, locationst > callsitest
Type mapping from call-graph edges onto the set of call instructions that make that call.
Definition: call_graph.h:105
nodest nodes
Definition: call_graph.h:113
std::pair< irep_idt, irep_idt > edget
Type of a call graph edge in edgest
Definition: call_graph.h:95
edgest edges
Call graph, including duplicate key-value pairs when there are parallel edges (see grapht documentati...
Definition: call_graph.h:112
call_grapht get_inverted() const
Returns an inverted copy of this call graph.
Definition: call_graph.cpp:165
void output_dot(std::ostream &out) const
Definition: call_graph.cpp:255
std::string format_callsites(const edget &edge) const
Prints callsites responsible for a graph edge as comma-separated location numbers,...
Definition: call_graph.cpp:241
std::multimap< irep_idt, irep_idt > edgest
Type of the edges in the call graph.
Definition: call_graph.h:92
directed_grapht get_directed_graph() const
Returns a grapht representation of this call graph, suitable for use with generic grapht algorithms.
Definition: call_graph.cpp:209
call_grapht(bool collect_callsites=false)
Create empty call graph.
Definition: call_graph.cpp:21
std::set< locationt, goto_programt::target_less_than > locationst
Type of a set of callsites.
Definition: call_graph.h:101
void output(std::ostream &out) const
Definition: call_graph.cpp:272
static call_grapht create_from_root_function(const goto_functionst &functions, const irep_idt &root, bool collect_callsites)
Definition: call_graph.h:61
callsitest callsites
Map from call-graph edges to a set of callsites that make the given call.
Definition: call_graph.h:116
static call_grapht create_from_root_function(const goto_modelt &model, const irep_idt &root, bool collect_callsites)
Definition: call_graph.h:53
void output_xml(std::ostream &out) const
Definition: call_graph.cpp:282
std::unordered_set< irep_idt, irep_id_hash > nodest
Type of the nodes in the call graph.
Definition: call_graph.h:88
goto_programt::const_targett locationt
Type of a callsite stored in member callsites
Definition: call_graph.h:98
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
A collection of goto functions.
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:73
instructionst::const_iterator const_targett
Definition: goto_program.h:615
This class represents a node in a directed graph.
Definition: graph.h:35
A generic directed graph with a parametric node type.
Definition: graph.h:167
Concrete Goto Program.
A Template Class for Graphs.
Edge of the directed graph representation of this call graph.
Definition: call_graph.h:125
locationst callsites
Callsites responsible for this graph edge.
Definition: call_graph.h:128
Node of the directed graph representation of this call graph.
Definition: call_graph.h:133