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 
16 #include "goto_model.h"
17 
19  goto_modelt &goto_model,
21 {
23 }
24 
26  goto_functionst &functions,
28 {
29  std::set<irep_idt> used_functions;
30  std::list<goto_functionst::function_mapt::iterator> unused_functions;
32  goto_functionst::entry_point(), functions, used_functions);
33 
34  for(goto_functionst::function_mapt::iterator it=
35  functions.function_map.begin();
36  it!=functions.function_map.end();
37  it++)
38  {
39  if(used_functions.find(it->first)==used_functions.end())
40  unused_functions.push_back(it);
41  }
42 
43  messaget message(message_handler);
44 
45  if(!unused_functions.empty())
46  {
47  message.statistics()
48  << "Dropping " << unused_functions.size() << " of " <<
49  functions.function_map.size() << " functions (" <<
50  used_functions.size() << " used)" << messaget::eom;
51  }
52 
53  for(const auto &f : unused_functions)
54  functions.function_map.erase(f);
55 }
56 
58  const irep_idt &start,
59  goto_functionst &functions,
60  std::set<irep_idt> &seen)
61 {
62  std::pair<std::set<irep_idt>::const_iterator, bool> res =
63  seen.insert(start);
64 
65  if(!res.second)
66  return;
67  else
68  {
69  goto_functionst::function_mapt::const_iterator f_it =
70  functions.function_map.find(start);
71 
72  if(f_it!=functions.function_map.end())
73  {
74  forall_goto_program_instructions(it, f_it->second.body)
75  {
76  if(it->type==FUNCTION_CALL)
77  {
78  const code_function_callt &call =
79  to_code_function_call(to_code(it->code));
80 
81  // check that this is actually a simple call
82  assert(call.function().id()==ID_symbol);
83 
84  const irep_idt &identifier =
85  to_symbol_expr(call.function()).get_identifier();
86 
87  find_used_functions(identifier, functions, seen);
88  }
89  }
90  }
91  }
92 }
void remove_unused_functions(goto_modelt &goto_model, message_handlert &message_handler)
function_mapt function_map
static mstreamt & eom(mstreamt &m)
Definition: message.h:272
Unused function removal.
const irep_idt & id() const
Definition: irep.h:189
Symbol Table + CFG.
A function call.
Definition: std_code.h:828
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast a generic exprt to a symbol_exprt.
Definition: std_expr.h:210
static irep_idt entry_point()
exprt & function()
Definition: std_code.h:848
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:74
goto_programt coverage_criteriont message_handlert & message_handler
Definition: cover.cpp:66
#define forall_goto_program_instructions(it, program)
Definition: goto_program.h:735
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:32
mstreamt & statistics() const
Definition: message.h:322
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:879