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  forall_goto_program_instructions(it, goto_program)
58  {
59  compute_address_taken_functions(it->guard, address_taken);
60  compute_address_taken_functions(it->code, address_taken);
61  }
62 }
63 
66  const goto_functionst &goto_functions,
67  std::unordered_set<irep_idt> &address_taken)
68 {
69  forall_goto_functions(it, goto_functions)
70  compute_address_taken_functions(it->second.body, address_taken);
71 }
72 
74 std::unordered_set<irep_idt>
76 {
77  std::unordered_set<irep_idt> address_taken;
78  compute_address_taken_functions(goto_functions, address_taken);
79  return address_taken;
80 }
81 
83 std::unordered_set<irep_idt>
85 {
86  std::unordered_set<irep_idt> working_queue;
87  std::unordered_set<irep_idt> functions;
88 
89  // start from entry point
90  working_queue.insert(goto_functions.entry_point());
91 
92  while(!working_queue.empty())
93  {
94  irep_idt id=*working_queue.begin();
95  working_queue.erase(working_queue.begin());
96 
97  if(!functions.insert(id).second)
98  continue;
99 
100  const goto_functionst::function_mapt::const_iterator f_it=
101  goto_functions.function_map.find(id);
102 
103  if(f_it==goto_functions.function_map.end())
104  continue;
105 
106  const goto_programt &program=
107  f_it->second.body;
108 
109  compute_address_taken_functions(program, working_queue);
110 
111  forall_goto_program_instructions(i_it, program)
112  {
113  if(i_it->is_function_call())
114  {
116  to_code_function_call(i_it->code).function(),
117  working_queue);
118  }
119  }
120  }
121 
122  return functions;
123 }
124 
126 std::unordered_set<irep_idt>
128 {
129  return compute_called_functions(goto_model.goto_functions);
130 }
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:35
typet::subtype
const typet & subtype() const
Definition: type.h:38
address_of_exprt::object
exprt & object()
Definition: std_expr.h:3265
exprt
Base class for all expressions.
Definition: expr.h:54
goto_modelt
Definition: goto_model.h:24
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:84
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:68
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:3282
forall_operands
#define forall_operands(it, expr)
Definition: expr.h:20
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:251
irept::id
const irep_idt & id() const
Definition: irep.h:259
to_code_function_call
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:1173
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:22
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:32
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:72
forall_goto_functions
#define forall_goto_functions(it, functions)
Definition: goto_functions.h:149
compute_called_functions.h
goto_functionst::entry_point
static irep_idt entry_point()
Get the identifier of the entry point to a goto model.
Definition: goto_functions.h:101
address_of_exprt
Operator to return the address of an object.
Definition: std_expr.h:3255
std_expr.h
code_function_callt::function
exprt & function()
Definition: std_code.h:1099
forall_goto_program_instructions
#define forall_goto_program_instructions(it, program)
Definition: goto_program.h:804