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::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  src.type().id()==ID_pointer &&
26  src.type().subtype().id()==ID_code)
27  {
28  assert(src.operands().size()==1);
29  const exprt &op=src.op0();
30  if(op.id()==ID_symbol)
31  address_taken.insert(to_symbol_expr(op).get_identifier());
32  }
33 }
34 
37  const exprt &src,
38  std::set<irep_idt> &address_taken)
39 {
40  forall_operands(it, src)
41  compute_functions(*it, address_taken);
42 
43  if(src.type().id()==ID_code &&
44  src.id()==ID_symbol)
45  address_taken.insert(to_symbol_expr(src).get_identifier());
46 }
47 
50  const goto_programt &goto_program,
51  std::set<irep_idt> &address_taken)
52 {
53  forall_goto_program_instructions(it, goto_program)
54  {
55  compute_address_taken_functions(it->guard, address_taken);
56  compute_address_taken_functions(it->code, address_taken);
57  }
58 }
59 
62  const goto_functionst &goto_functions,
63  std::set<irep_idt> &address_taken)
64 {
65  forall_goto_functions(it, goto_functions)
66  compute_address_taken_functions(it->second.body, address_taken);
67 }
68 
71  const goto_functionst &goto_functions,
72  std::set<irep_idt> &functions)
73 {
74  std::set<irep_idt> working_queue;
75  std::set<irep_idt> done;
76 
77  // start from entry point
78  working_queue.insert(goto_functions.entry_point());
79 
80  while(!working_queue.empty())
81  {
82  irep_idt id=*working_queue.begin();
83  working_queue.erase(working_queue.begin());
84 
85  if(done.find(id)!=done.end())
86  continue;
87 
88  functions.insert(id);
89  done.insert(id);
90 
91  const goto_functionst::function_mapt::const_iterator f_it=
92  goto_functions.function_map.find(id);
93 
94  if(f_it==goto_functions.function_map.end())
95  continue;
96 
97  const goto_programt &program=
98  f_it->second.body;
99 
100  compute_address_taken_functions(program, working_queue);
101 
102  forall_goto_program_instructions(i_it, program)
103  {
104  if(i_it->is_function_call())
105  {
107  to_code_function_call(i_it->code).function(),
108  working_queue);
109  }
110  }
111  }
112 }
113 
116  const goto_modelt &goto_model,
117  std::set<irep_idt> &functions)
118 {
119  compute_called_functions(goto_model.goto_functions, functions);
120 }
void compute_address_taken_functions(const exprt &src, std::set< irep_idt > &address_taken)
get all functions whose address is taken
exprt & op0()
Definition: expr.h:84
void compute_functions(const exprt &src, std::set< irep_idt > &address_taken)
get all functions in the expression
typet & type()
Definition: expr.h:60
const irep_idt & id() const
Definition: irep.h:189
API to expression classes.
#define forall_operands(it, expr)
Definition: expr.h:17
Query Called Functions.
A specialization of goto_program_templatet over goto programs in which instructions have codet type...
Definition: goto_program.h:24
void compute_called_functions(const goto_functionst &goto_functions, std::set< irep_idt > &functions)
computes the functions that are (potentially) called
exprt & function()
Definition: std_code.h:677
Base class for all expressions.
Definition: expr.h:46
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast a generic exprt to a symbol_exprt.
Definition: std_expr.h:202
const typet & subtype() const
Definition: type.h:31
#define forall_goto_functions(it, functions)
operandst & operands()
Definition: expr.h:70
#define forall_goto_program_instructions(it, program)
Definition: goto_program.h:68
goto_functionst goto_functions
Definition: goto_model.h:26
const irep_idt & get_identifier() const
Definition: std_types.h:126
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:700