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> 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  optionalt<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
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
call_grapht::edgest
std::multimap< irep_idt, irep_idt > edgest
Type of the edges in the call graph.
Definition: call_graph.h:92
call_grapht::collect_callsites
bool collect_callsites
Definition: call_graph.h:169
call_grapht::directed_grapht::get_nodes_by_name
const nodes_by_namet & get_nodes_by_name() const
Get the node name -> node index map.
Definition: call_graph.h:157
call_grapht::callsitest
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
call_grapht::get_inverted
call_grapht get_inverted() const
Returns an inverted copy of this call graph.
Definition: call_graph.cpp:165
call_grapht::directed_grapht::get_node_index
optionalt< node_indext > get_node_index(const irep_idt &function) const
Find the graph node by function name.
Definition: call_graph.cpp:299
call_grapht::call_grapht
call_grapht(bool collect_callsites=false)
Create empty call graph.
Definition: call_graph.cpp:21
grapht
A generic directed graph with a parametric node type.
Definition: graph.h:166
call_grapht::edge_with_callsitest::callsites
locationst callsites
Callsites responsible for this graph edge.
Definition: call_graph.h:128
goto_modelt
Definition: goto_model.h:25
call_grapht::output_xml
void output_xml(std::ostream &out) const
Definition: call_graph.cpp:282
call_grapht::output_dot
void output_dot(std::ostream &out) const
Definition: call_graph.cpp:255
call_grapht::create_from_root_function
static call_grapht create_from_root_function(const goto_modelt &model, const irep_idt &root, bool collect_callsites)
Definition: call_graph.h:53
call_grapht::nodest
std::unordered_set< irep_idt, irep_id_hash > nodest
Type of the nodes in the call graph.
Definition: call_graph.h:88
call_grapht::create_from_root_function
static call_grapht create_from_root_function(const goto_functionst &functions, const irep_idt &root, bool collect_callsites)
Definition: call_graph.h:61
call_grapht::directed_grapht
Directed graph representation of this call graph.
Definition: call_graph.h:139
call_grapht::directed_grapht::nodes_by_name
std::unordered_map< irep_idt, node_indext > nodes_by_name
Maps function names onto node indices.
Definition: call_graph.h:144
call_grapht::get_directed_graph
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
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
call_grapht::format_callsites
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
goto_program.h
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:24
call_grapht
A call graph (https://en.wikipedia.org/wiki/Call_graph) for a GOTO model or GOTO functions collection...
Definition: call_graph.h:43
call_grapht::function_nodet
Node of the directed graph representation of this call graph.
Definition: call_graph.h:132
call_grapht::edget
std::pair< irep_idt, irep_idt > edget
Type of a call graph edge in edgest
Definition: call_graph.h:95
graph.h
call_grapht::callsites
callsitest callsites
Map from call-graph edges to a set of callsites that make the given call.
Definition: call_graph.h:116
graph_nodet
This class represents a node in a directed graph.
Definition: graph.h:34
call_grapht::output
void output(std::ostream &out) const
Definition: call_graph.cpp:272
call_grapht::directed_grapht::nodes_by_namet
std::unordered_map< irep_idt, node_indext > nodes_by_namet
Type of the node name -> node index map.
Definition: call_graph.h:153
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:72
goto_programt::const_targett
instructionst::const_iterator const_targett
Definition: goto_program.h:587
call_grapht::locationst
std::set< locationt > locationst
Type of a set of callsites.
Definition: call_graph.h:101
call_grapht::add
void add(const irep_idt &caller, const irep_idt &callee)
Add edge.
Definition: call_graph.cpp:139
call_grapht::locationt
goto_programt::const_targett locationt
Type of a callsite stored in member callsites
Definition: call_graph.h:98
call_grapht::nodes
nodest nodes
Definition: call_graph.h:113
call_grapht::edge_with_callsitest
Edge of the directed graph representation of this call graph.
Definition: call_graph.h:124
call_grapht::edges
edgest edges
Call graph, including duplicate key-value pairs when there are parallel edges (see grapht documentati...
Definition: call_graph.h:112