CBMC
utils.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Utility functions for code contracts.
4 
5 Author: Saswat Padhi, saspadhi@amazon.com
6 
7 Date: September 2021
8 
9 \*******************************************************************/
10 
11 #ifndef CPROVER_GOTO_INSTRUMENT_CONTRACTS_UTILS_H
12 #define CPROVER_GOTO_INSTRUMENT_CONTRACTS_UTILS_H
13 
14 // clang-format off
15 #include <vector>
16 
17 #include <analyses/dirty.h>
18 #include <analyses/locals.h>
19 
21 
24 
25 #include <util/expr_cast.h>
26 #include <util/byte_operators.h>
27 #include <util/message.h>
28 // clang-format on
29 
34 {
35 public:
36  havoc_if_validt(const assignst &mod, const namespacet &ns)
37  : havoc_utilst(mod), ns(ns)
38  {
39  }
40 
42  const source_locationt location,
43  const exprt &expr,
44  goto_programt &dest) const override;
45 
47  const source_locationt location,
48  const exprt &expr,
49  goto_programt &dest) const override;
50 
51 protected:
52  const namespacet &ns;
53 };
54 
58 {
59 public:
61  : havoc_if_validt(mod, ns)
62  {
63  }
64 
66  const source_locationt location,
67  const exprt &expr,
68  goto_programt &dest) const override;
69 };
70 
86 exprt all_dereferences_are_valid(const exprt &expr, const namespacet &ns);
87 
98  const std::vector<symbol_exprt> &lhs,
99  const std::vector<symbol_exprt> &rhs);
100 
114  goto_programt &destination,
115  goto_programt::targett &target,
116  goto_programt &payload);
117 
127 const symbolt &new_tmp_symbol(
128  const typet &type,
129  const source_locationt &location,
130  const irep_idt &mode,
131  symbol_table_baset &symtab,
132  std::string suffix = "tmp_cc",
133  bool is_auxiliary = true);
134 
137 void simplify_gotos(goto_programt &goto_program, namespacet &ns);
138 
141 bool is_loop_free(
142  const goto_programt &goto_program,
143  namespacet &ns,
144  messaget &log);
145 
147 class cleanert : public goto_convertt
148 {
149 public:
151  symbol_table_baset &_symbol_table,
152  message_handlert &_message_handler)
153  : goto_convertt(_symbol_table, _message_handler)
154  {
155  }
156 
157  void clean(exprt &guard, goto_programt &dest, const irep_idt &mode)
158  {
159  goto_convertt::clean_expr(guard, dest, mode, true);
160  }
161 
163  const symbol_exprt &function,
164  const exprt::operandst &arguments,
165  goto_programt &dest,
166  const irep_idt &mode)
167  {
168  goto_convertt::do_havoc_slice(nil_exprt{}, function, arguments, dest, mode);
169  }
170 };
171 
175  const exprt &target,
176  const irep_idt &function_id,
177  const namespacet &ns);
178 
182 
183 #endif // CPROVER_GOTO_INSTRUMENT_CONTRACTS_UTILS_H
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:154
goto_convertt::do_havoc_slice
void do_havoc_slice(const exprt &lhs, const symbol_exprt &function, const exprt::operandst &arguments, goto_programt &dest, const irep_idt &mode)
Definition: builtin_functions.cpp:645
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
dirty.h
havoc_if_validt::havoc_if_validt
havoc_if_validt(const assignst &mod, const namespacet &ns)
Definition: utils.h:36
typet
The type of an expression, extends irept.
Definition: type.h:28
goto_convertt::clean_expr
void clean_expr(exprt &expr, goto_programt &dest, const irep_idt &mode, bool result_is_used=true)
Definition: goto_clean_expr.cpp:166
cleanert::cleanert
cleanert(symbol_table_baset &_symbol_table, message_handlert &_message_handler)
Definition: utils.h:150
havoc_utilst
Definition: havoc_utils.h:47
is_loop_free
bool is_loop_free(const goto_programt &goto_program, namespacet &ns, messaget &log)
Returns true iff the given program is loop-free, i.e.
Definition: utils.cpp:173
goto_convert_class.h
goto_model.h
exprt
Base class for all expressions.
Definition: expr.h:55
havoc_utils.h
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:112
new_tmp_symbol
const symbolt & new_tmp_symbol(const typet &type, const source_locationt &location, const irep_idt &mode, symbol_table_baset &symtab, std::string suffix="tmp_cc", bool is_auxiliary=true)
Adds a fresh and uniquely named symbol to the symbol table.
Definition: utils.cpp:148
cleanert::clean
void clean(exprt &guard, goto_programt &dest, const irep_idt &mode)
Definition: utils.h:157
cleanert
Allows to clean expressions of side effects.
Definition: utils.h:147
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:90
byte_operators.h
Expression classes for byte-level operators.
all_dereferences_are_valid
exprt all_dereferences_are_valid(const exprt &expr, const namespacet &ns)
Generate a validity check over all dereferences in an expression.
Definition: utils.cpp:80
insert_before_swap_and_advance
void insert_before_swap_and_advance(goto_programt &destination, goto_programt::targett &target, goto_programt &payload)
Insert a goto program before a target instruction iterator and advance the iterator.
Definition: utils.cpp:138
goto_convertt
Definition: goto_convert_class.h:30
havoc_if_validt::append_object_havoc_code_for_expr
void append_object_havoc_code_for_expr(const source_locationt location, const exprt &expr, goto_programt &dest) const override
Append goto instructions to havoc the underlying object of expr
Definition: utils.cpp:45
havoc_assigns_targetst
A class that further overrides the "safe" havoc utilities, and adds support for havocing pointer_obje...
Definition: utils.h:57
havoc_if_validt::ns
const namespacet & ns
Definition: utils.h:52
locals.h
symbol_table_baset
The symbol table base class interface.
Definition: symbol_table_base.h:21
nil_exprt
The NIL expression.
Definition: std_expr.h:3025
generate_lexicographic_less_than_check
exprt generate_lexicographic_less_than_check(const std::vector< symbol_exprt > &lhs, const std::vector< symbol_exprt > &rhs)
Generate a lexicographic less-than comparison over ordered tuples.
Definition: utils.cpp:94
message_handlert
Definition: message.h:27
exprt::operandst
std::vector< exprt > operandst
Definition: expr.h:58
assignst
std::set< exprt > assignst
Definition: havoc_utils.h:23
make_assigns_clause_replacement_tracking_comment
irep_idt make_assigns_clause_replacement_tracking_comment(const exprt &target, const irep_idt &function_id, const namespacet &ns)
Returns an irep_idt that essentially says that target was assigned by the contract of function_id.
Definition: utils.cpp:231
havoc_assigns_targetst::havoc_assigns_targetst
havoc_assigns_targetst(const assignst &mod, const namespacet &ns)
Definition: utils.h:60
havoc_if_validt::append_scalar_havoc_code_for_expr
void append_scalar_havoc_code_for_expr(const source_locationt location, const exprt &expr, goto_programt &dest) const override
Append goto instructions to havoc the value of expr
Definition: utils.cpp:55
simplify_gotos
void simplify_gotos(goto_programt &goto_program, namespacet &ns)
Turns goto instructions IF cond GOTO label where the condition statically simplifies to false into SK...
Definition: utils.cpp:162
source_locationt
Definition: source_location.h:18
cleanert::do_havoc_slice
void do_havoc_slice(const symbol_exprt &function, const exprt::operandst &arguments, goto_programt &dest, const irep_idt &mode)
Definition: utils.h:162
is_assigns_clause_replacement_tracking_comment
bool is_assigns_clause_replacement_tracking_comment(const irep_idt &comment)
Returns true if the given comment matches the type of comments created by make_assigns_clause_replace...
Definition: utils.cpp:240
symbolt
Symbol table entry.
Definition: symbol.h:27
expr_cast.h
Templated functions to cast to specific exprt-derived classes.
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:72
havoc_assigns_targetst::append_havoc_code_for_expr
void append_havoc_code_for_expr(const source_locationt location, const exprt &expr, goto_programt &dest) const override
Append goto instructions to havoc a single expression expr
Definition: utils.cpp:65
havoc_if_validt
A class that overrides the low-level havocing functions in the base utility class,...
Definition: utils.h:33
message.h
comment
static std::string comment(const rw_set_baset::entryt &entry, bool write)
Definition: race_check.cpp:109
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:586