CBMC
dirty.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Local variables whose address is taken
4 
5 Author: Daniel Kroening
6 
7 Date: March 2013
8 
9 \*******************************************************************/
10 
13 
14 #include "dirty.h"
15 
16 #include <util/pointer_expr.h>
17 #include <util/std_expr.h>
18 
19 void dirtyt::build(const goto_functiont &goto_function)
20 {
21  for(const auto &i : goto_function.body.instructions)
22  {
23  if(i.is_other())
24  {
25  search_other(i);
26  }
27  else
28  {
29  i.apply([this](const exprt &e) { find_dirty(e); });
30  }
31  }
32 }
33 
35 {
36  INVARIANT(instruction.is_other(), "instruction type must be OTHER");
37  if(instruction.get_other().id() == ID_code)
38  {
39  const codet &code = instruction.get_other();
40  const irep_idt &statement = code.get_statement();
41  if(
42  statement == ID_expression || statement == ID_array_set ||
43  statement == ID_array_equal || statement == ID_array_copy ||
44  statement == ID_array_replace || statement == ID_havoc_object ||
45  statement == ID_input || statement == ID_output)
46  {
47  forall_operands(it, code)
48  find_dirty(*it);
49  }
50  // other possible cases according to goto_programt::instructiont::output
51  // and goto_symext::symex_other:
52  // statement == ID_fence ||
53  // statement == ID_user_specified_predicate ||
54  // statement == ID_user_specified_parameter_predicates ||
55  // statement == ID_user_specified_return_predicates ||
56  // statement == ID_decl || statement == ID_nondet || statement == ID_asm)
57  }
58 }
59 
60 void dirtyt::find_dirty(const exprt &expr)
61 {
62  if(expr.id() == ID_address_of)
63  {
64  const address_of_exprt &address_of_expr = to_address_of_expr(expr);
65  find_dirty_address_of(address_of_expr.object());
66  return;
67  }
68 
69  forall_operands(it, expr)
70  find_dirty(*it);
71 }
72 
74 {
75  if(expr.id() == ID_symbol)
76  {
77  const irep_idt &identifier = to_symbol_expr(expr).get_identifier();
78 
79  dirty.insert(identifier);
80  }
81  else if(expr.id() == ID_member)
82  {
83  find_dirty_address_of(to_member_expr(expr).struct_op());
84  }
85  else if(expr.id() == ID_index)
86  {
87  find_dirty_address_of(to_index_expr(expr).array());
88  find_dirty(to_index_expr(expr).index());
89  }
90  else if(expr.id() == ID_dereference)
91  {
92  find_dirty(to_dereference_expr(expr).pointer());
93  }
94  else if(expr.id() == ID_if)
95  {
96  find_dirty_address_of(to_if_expr(expr).true_case());
97  find_dirty_address_of(to_if_expr(expr).false_case());
98  find_dirty(to_if_expr(expr).cond());
99  }
100 }
101 
102 void dirtyt::output(std::ostream &out) const
103 {
105  for(const auto &d : dirty)
106  out << d << '\n';
107 }
108 
113  const irep_idt &id,
114  const goto_functionst::goto_functiont &function)
115 {
116  auto insert_result = dirty_processed_functions.insert(id);
117  if(insert_result.second)
118  dirty.add_function(function);
119 }
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
goto_functiont::body
goto_programt body
Definition: goto_function.h:26
dirty.h
incremental_dirtyt::dirty
dirtyt dirty
Definition: dirty.h:135
address_of_exprt::object
exprt & object()
Definition: pointer_expr.h:380
goto_programt::instructiont::get_other
const goto_instruction_codet & get_other() const
Get the statement for OTHER.
Definition: goto_program.h:321
goto_programt::instructiont::is_other
bool is_other() const
Definition: goto_program.h:471
to_dereference_expr
const dereference_exprt & to_dereference_expr(const exprt &expr)
Cast an exprt to a dereference_exprt.
Definition: pointer_expr.h:716
to_index_expr
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition: std_expr.h:1478
to_if_expr
const if_exprt & to_if_expr(const exprt &expr)
Cast an exprt to an if_exprt.
Definition: std_expr.h:2403
dirtyt::add_function
void add_function(const goto_functiont &goto_function)
Definition: dirty.h:84
exprt
Base class for all expressions.
Definition: expr.h:55
dirtyt::build
void build(const goto_functionst &goto_functions)
Definition: dirty.h:90
dirtyt::find_dirty
void find_dirty(const exprt &expr)
Definition: dirty.cpp:60
incremental_dirtyt::dirty_processed_functions
std::unordered_set< irep_idt > dirty_processed_functions
Definition: dirty.h:136
forall_operands
#define forall_operands(it, expr)
Definition: expr.h:20
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:142
incremental_dirtyt::populate_dirty_for_function
void populate_dirty_for_function(const irep_idt &id, const goto_functionst::goto_functiont &function)
Analyse the given function with dirtyt if it hasn't been seen before.
Definition: dirty.cpp:112
dirtyt::dirty
std::unordered_set< irep_idt > dirty
Definition: dirty.h:103
pointer_expr.h
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:222
irept::id
const irep_idt & id() const
Definition: irep.h:396
goto_functiont
A goto function, consisting of function body (see body) and parameter identifiers (see parameter_iden...
Definition: goto_function.h:23
goto_functionst::goto_functiont
::goto_functiont goto_functiont
Definition: goto_functions.h:27
goto_programt::instructions
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:592
dirtyt::output
void output(std::ostream &out) const
Definition: dirty.cpp:102
dirtyt::search_other
void search_other(const goto_programt::instructiont &instruction)
Definition: dirty.cpp:34
dirtyt::die_if_uninitialized
void die_if_uninitialized() const
Definition: dirty.h:30
to_member_expr
const member_exprt & to_member_expr(const exprt &expr)
Cast an exprt to a member_exprt.
Definition: std_expr.h:2886
to_address_of_expr
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast an exprt to an address_of_exprt.
Definition: pointer_expr.h:408
address_of_exprt
Operator to return the address of an object.
Definition: pointer_expr.h:370
codet::get_statement
const irep_idt & get_statement() const
Definition: std_code_base.h:65
goto_programt::instructiont
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:180
std_expr.h
dirtyt::find_dirty_address_of
void find_dirty_address_of(const exprt &expr)
Definition: dirty.cpp:73
validation_modet::INVARIANT
@ INVARIANT
codet
Data structure for representing an arbitrary statement in a program.
Definition: std_code_base.h:28