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 }
Aggressive slicer Consider the control flow graph of the goto program and a criterion description of ...
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.
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.
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.
Function Call Graph Helpers.
goto_modelt & goto_model
message_handlert & message_handler
std::list< std::string > user_specified_properties
call_grapht::directed_grapht call_graph
void doit()
Removes the body of all functions except those on the shortest path or functions that are reachable f...
std::list< std::string > name_snippets
void get_all_functions_containing_properties()
Finds all functions that contain a property, and adds them to the list of 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...
const irep_idt start_function
std::set< irep_idt > functions_to_keep
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...
bool get_inlined() const
Definition: std_types.h:709
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
function_mapt function_map
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:31
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:34
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:154
mstreamt & debug() const
Definition: message.h:421
static eomt eom
Definition: message.h:289
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:148
Symbol Table + CFG.
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
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...
Remove function definition.
std::optional< 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.
Show the properties.
#define INITIALIZE_FUNCTION
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:788