CBMC
aggressive_slicer.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Aggressive slicer
4 
5 Author: Elizabeth Polgreen, elizabeth.polgreen@cs.ox.ac.uk
6 
7 \*******************************************************************/
8 
11 
12 #include "aggressive_slicer.h"
13 
14 #include <util/message.h>
15 
18 
20 
22 
23 #include "remove_function.h"
24 
26  const irep_idt &destination_function)
27 {
29  {
34  for(const auto &func :
35  get_reaching_functions(call_graph, destination_function))
36  functions_to_keep.insert(func);
37  }
38  else
39  {
40  for(const auto &func : get_shortest_function_path(
41  call_graph, start_function, destination_function))
42  functions_to_keep.insert(func);
43  }
44 }
45 
47 {
49 
50  for(const auto &fct : goto_model.goto_functions.function_map)
51  {
52  if(!to_code_type(ns.lookup(fct.first).type).get_inlined())
53  {
54  for(const auto &ins : fct.second.body.instructions)
55  if(ins.is_assert())
56  {
57  if(functions_to_keep.insert(fct.first).second)
58  note_functions_to_keep(fct.first);
59  }
60  }
61  }
62 }
63 
65 {
66  for(const auto &name_snippet : name_snippets)
67  {
68  for(const auto &gf_entry : goto_model.goto_functions.function_map)
69  {
70  if(id2string(gf_entry.first).find(name_snippet) != std::string::npos)
71  functions_to_keep.insert(gf_entry.first);
72  }
73  }
74 }
75 
77 {
78  messaget message(message_handler);
79 
82 
83  // if no properties are specified, preserve all functions containing
84  // any property
85  if(user_specified_properties.empty())
86  {
87  message.debug() << "No properties given, so aggressive slicer "
88  << "is running over all possible properties"
89  << messaget::eom;
91  }
92 
93  // if a name snippet is given, get list of all functions containing
94  // name snippet to preserve
95  if(!name_snippets.empty())
97 
98  for(const auto &p : user_specified_properties)
99  {
100  auto property_loc = find_property(p, goto_model.goto_functions);
101  if(!property_loc.has_value())
102  throw "unable to find property in call graph";
103  note_functions_to_keep(property_loc.value().get_function());
104  }
105 
106  // Add functions within distance of shortest path functions
107  // to the list
108  if(call_depth != 0)
109  {
110  for(const auto &func : get_functions_reachable_within_n_steps(
112  functions_to_keep.insert(func);
113  }
114 
115  message.debug() << "Preserving the following functions \n";
116  for(const auto &func : functions_to_keep)
117  message.debug() << func << messaget::eom;
118 
119  for(const auto &gf_entry : goto_model.goto_functions.function_map)
120  {
121  if(functions_to_keep.find(gf_entry.first) == functions_to_keep.end())
122  remove_function(goto_model, gf_entry.first, message_handler);
123  }
124 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:154
get_reaching_functions
std::set< irep_idt > get_reaching_functions(const call_grapht::directed_grapht &graph, const irep_idt &function)
Get functions that can reach a given function.
Definition: call_graph_helpers.cpp:68
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
aggressive_slicert::goto_model
goto_modelt & goto_model
Definition: aggressive_slicer.h:72
goto_model.h
get_functions_reachable_within_n_steps
std::set< irep_idt > get_functions_reachable_within_n_steps(const call_grapht::directed_grapht &graph, const std::set< irep_idt > &start_functions, std::size_t n)
Get either callers or callees reachable from a given list of functions within N steps.
Definition: call_graph_helpers.cpp:74
aggressive_slicert::preserve_all_direct_paths
bool preserve_all_direct_paths
Definition: aggressive_slicer.h:68
messaget::eom
static eomt eom
Definition: message.h:297
aggressive_slicert::note_functions_to_keep
void note_functions_to_keep(const irep_idt &destination_function)
notes functions to keep in the slice based on the program start function and the given destination fu...
Definition: aggressive_slicer.cpp:25
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:29
aggressive_slicert::find_functions_that_contain_name_snippet
void find_functions_that_contain_name_snippet()
Finds all functions whose name contains a name snippet and adds them to the std::unordered_set of ire...
Definition: aggressive_slicer.cpp:64
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:90
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:138
aggressive_slicert::doit
void doit()
Removes the body of all functions except those on the shortest path or functions that are reachable f...
Definition: aggressive_slicer.cpp:76
to_code_type
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:744
remove_function
void remove_function(goto_modelt &goto_model, const irep_idt &identifier, message_handlert &message_handler)
Remove the body of function "identifier" such that an analysis will treat it as a side-effect free fu...
Definition: remove_function.cpp:26
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:47
show_properties.h
INITIALIZE_FUNCTION
#define INITIALIZE_FUNCTION
Definition: static_lifetime_init.h:22
remove_function.h
call_graph_helpers.h
aggressive_slicert::get_all_functions_containing_properties
void get_all_functions_containing_properties()
Finds all functions that contain a property, and adds them to the list of functions to keep.
Definition: aggressive_slicer.cpp:46
aggressive_slicert::call_depth
std::size_t call_depth
Definition: aggressive_slicer.h:66
aggressive_slicert::user_specified_properties
std::list< std::string > user_specified_properties
Definition: aggressive_slicer.h:63
aggressive_slicer.h
aggressive_slicert::message_handler
message_handlert & message_handler
Definition: aggressive_slicer.h:73
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
aggressive_slicert::start_function
const irep_idt start_function
Definition: aggressive_slicer.h:71
get_shortest_function_path
std::list< irep_idt > get_shortest_function_path(const call_grapht::directed_grapht &graph, const irep_idt &src, const irep_idt &dest)
Get list of functions on the shortest path between two functions.
Definition: call_graph_helpers.cpp:107
find_property
optionalt< source_locationt > find_property(const irep_idt &property, const goto_functionst &goto_functions)
Returns a source_locationt that corresponds to the property given by an irep_idt.
Definition: show_properties.cpp:22
aggressive_slicert::name_snippets
std::list< std::string > name_snippets
Definition: aggressive_slicer.h:67
code_typet::get_inlined
bool get_inlined() const
Definition: std_types.h:665
messaget::debug
mstreamt & debug() const
Definition: message.h:429
static_lifetime_init.h
message.h
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
aggressive_slicert::call_graph
call_grapht::directed_grapht call_graph
Definition: aggressive_slicer.h:75
aggressive_slicert::functions_to_keep
std::set< irep_idt > functions_to_keep
Definition: aggressive_slicer.h:74