cprover
remove_unused_functions.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Unused function removal
4 
5 Author: CM Wintersteiger
6 
7 \*******************************************************************/
8 
11 
13 
14 #include <util/message.h>
15 
17  goto_functionst &functions,
18  message_handlert &message_handler)
19 {
20  std::set<irep_idt> used_functions;
21  std::list<goto_functionst::function_mapt::iterator> unused_functions;
23  goto_functionst::entry_point(), functions, used_functions);
24 
25  for(goto_functionst::function_mapt::iterator it=
26  functions.function_map.begin();
27  it!=functions.function_map.end();
28  it++)
29  {
30  if(used_functions.find(it->first)==used_functions.end())
31  unused_functions.push_back(it);
32  }
33 
34  messaget message(message_handler);
35 
36  if(!unused_functions.empty())
37  {
38  message.statistics()
39  << "Dropping " << unused_functions.size() << " of " <<
40  functions.function_map.size() << " functions (" <<
41  used_functions.size() << " used)" << messaget::eom;
42  }
43 
44  for(const auto &f : unused_functions)
45  functions.function_map.erase(f);
46 }
47 
49  const irep_idt &start,
50  goto_functionst &functions,
51  std::set<irep_idt> &seen)
52 {
53  std::pair<std::set<irep_idt>::const_iterator, bool> res =
54  seen.insert(start);
55 
56  if(!res.second)
57  return;
58  else
59  {
60  goto_functionst::function_mapt::const_iterator f_it =
61  functions.function_map.find(start);
62 
63  if(f_it!=functions.function_map.end())
64  {
65  forall_goto_program_instructions(it, f_it->second.body)
66  {
67  if(it->type==FUNCTION_CALL)
68  {
69  const code_function_callt &call =
70  to_code_function_call(to_code(it->code));
71 
72  // check that this is actually a simple call
73  assert(call.function().id()==ID_symbol);
74 
75  find_used_functions(call.function().get(ID_identifier),
76  functions,
77  seen);
78  }
79  }
80  }
81  }
82 }
void remove_unused_functions(goto_functionst &functions, message_handlert &message_handler)
static mstreamt & eom(mstreamt &m)
Definition: message.h:193
Unused function removal.
const irep_idt & id() const
Definition: irep.h:189
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:213
A function call.
Definition: std_code.h:657
mstreamt & statistics()
Definition: message.h:243
exprt & function()
Definition: std_code.h:677
void find_used_functions(const irep_idt &start, goto_functionst &functions, std::set< irep_idt > &seen)
const codet & to_code(const exprt &expr)
Definition: std_code.h:49
#define forall_goto_program_instructions(it, program)
Definition: goto_program.h:68
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:700