CBMC
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,
20  message_handlert &message_handler)
21 {
22  remove_unused_functions(goto_model.goto_functions, message_handler);
23 }
24 
26  goto_functionst &functions,
27  message_handlert &message_handler)
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  for(const auto &instruction : f_it->second.body.instructions)
75  {
76  if(instruction.is_function_call())
77  {
78  const auto &function = instruction.call_function();
79 
80  const irep_idt &identifier =
81  to_symbol_expr(function).get_identifier();
82 
83  find_used_functions(identifier, functions, seen);
84  }
85  }
86  }
87  }
88 }
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
A collection of goto functions.
function_mapt function_map
static irep_idt entry_point()
Get the identifier of the entry point to a goto model.
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:34
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
mstreamt & statistics() const
Definition: message.h:419
static eomt eom
Definition: message.h:297
const irep_idt & get_identifier() const
Definition: std_expr.h:160
Symbol Table + CFG.
void remove_unused_functions(goto_modelt &goto_model, message_handlert &message_handler)
void find_used_functions(const irep_idt &start, goto_functionst &functions, std::set< irep_idt > &seen)
Unused function removal.
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:272