cprover
compute_called_functions.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Query Called Functions
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
13 
14 #include <util/std_expr.h>
15 
18  const exprt &src,
19  std::unordered_set<irep_idt> &address_taken)
20 {
21  forall_operands(it, src)
22  compute_address_taken_functions(*it, address_taken);
23 
24  if(src.id() == ID_address_of)
25  {
26  const address_of_exprt &address = to_address_of_expr(src);
27 
28  if(
29  address.type().id() == ID_pointer &&
30  address.type().subtype().id() == ID_code)
31  {
32  const exprt &target = address.object();
33  if(target.id() == ID_symbol)
34  address_taken.insert(to_symbol_expr(target).get_identifier());
35  }
36  }
37 }
38 
41  const exprt &src,
42  std::unordered_set<irep_idt> &address_taken)
43 {
44  forall_operands(it, src)
45  compute_functions(*it, address_taken);
46 
47  if(src.type().id()==ID_code &&
48  src.id()==ID_symbol)
49  address_taken.insert(to_symbol_expr(src).get_identifier());
50 }
51 
54  const goto_programt &goto_program,
55  std::unordered_set<irep_idt> &address_taken)
56 {
57  for(const auto &i : goto_program.instructions)
58  {
59  i.apply([&address_taken](const exprt &expr) {
60  compute_address_taken_functions(expr, address_taken);
61  });
62  }
63 }
64 
67  const goto_functionst &goto_functions,
68  std::unordered_set<irep_idt> &address_taken)
69 {
70  forall_goto_functions(it, goto_functions)
71  compute_address_taken_functions(it->second.body, address_taken);
72 }
73 
75 std::unordered_set<irep_idt>
77 {
78  std::unordered_set<irep_idt> address_taken;
79  compute_address_taken_functions(goto_functions, address_taken);
80  return address_taken;
81 }
82 
84 std::unordered_set<irep_idt>
86 {
87  std::unordered_set<irep_idt> working_queue;
88  std::unordered_set<irep_idt> functions;
89 
90  // start from entry point
91  working_queue.insert(goto_functions.entry_point());
92 
93  while(!working_queue.empty())
94  {
95  irep_idt id=*working_queue.begin();
96  working_queue.erase(working_queue.begin());
97 
98  if(!functions.insert(id).second)
99  continue;
100 
101  const goto_functionst::function_mapt::const_iterator f_it=
102  goto_functions.function_map.find(id);
103 
104  if(f_it==goto_functions.function_map.end())
105  continue;
106 
107  const goto_programt &program=
108  f_it->second.body;
109 
110  compute_address_taken_functions(program, working_queue);
111 
112  forall_goto_program_instructions(i_it, program)
113  {
114  if(i_it->is_function_call())
115  {
117  to_code_function_call(i_it->code).function(),
118  working_queue);
119  }
120  }
121  }
122 
123  return functions;
124 }
125 
127 std::unordered_set<irep_idt>
129 {
130  return compute_called_functions(goto_model.goto_functions);
131 }
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
typet::subtype
const typet & subtype() const
Definition: type.h:47
address_of_exprt::object
exprt & object()
Definition: std_expr.h:2795
exprt
Base class for all expressions.
Definition: expr.h:53
goto_modelt
Definition: goto_model.h:26
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:27
compute_called_functions
std::unordered_set< irep_idt > compute_called_functions(const goto_functionst &goto_functions)
computes the functions that are (potentially) called
Definition: compute_called_functions.cpp:85
compute_functions
void compute_functions(const exprt &src, std::unordered_set< irep_idt > &address_taken)
get all functions in the expression
Definition: compute_called_functions.cpp:40
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:81
to_address_of_expr
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast an exprt to an address_of_exprt.
Definition: std_expr.h:2823
forall_operands
#define forall_operands(it, expr)
Definition: expr.h:18
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:177
irept::id
const irep_idt & id() const
Definition: irep.h:418
to_code_function_call
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:1294
goto_programt::instructions
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:585
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:23
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
compute_address_taken_functions
void compute_address_taken_functions(const exprt &src, std::unordered_set< irep_idt > &address_taken)
get all functions whose address is taken
Definition: compute_called_functions.cpp:17
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:73
forall_goto_functions
#define forall_goto_functions(it, functions)
Definition: goto_functions.h:122
compute_called_functions.h
Query Called Functions.
goto_functionst::entry_point
static irep_idt entry_point()
Get the identifier of the entry point to a goto model.
Definition: goto_functions.h:90
address_of_exprt
Operator to return the address of an object.
Definition: std_expr.h:2786
std_expr.h
API to expression classes.
code_function_callt::function
exprt & function()
Definition: std_code.h:1218
forall_goto_program_instructions
#define forall_goto_program_instructions(it, program)
Definition: goto_program.h:1196