|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: CFG-based information for assigns clause checking simplification |
| 4 | +
|
| 5 | +Author: Remi Delmas |
| 6 | +
|
| 7 | +Date: June 2022 |
| 8 | +
|
| 9 | +\*******************************************************************/ |
| 10 | + |
| 11 | +/// \file |
| 12 | +/// Classes providing CFG-based information about symbols to guide |
| 13 | +/// simplifications in function and loop assigns clause checking |
| 14 | + |
| 15 | +#ifndef CPROVER_GOTO_INSTRUMENT_CONTRACTS_CFG_INFO_H |
| 16 | +#define CPROVER_GOTO_INSTRUMENT_CONTRACTS_CFG_INFO_H |
| 17 | + |
| 18 | +#include <goto-programs/goto_convert_class.h> |
| 19 | + |
| 20 | +#include <util/byte_operators.h> |
| 21 | +#include <util/expr_cast.h> |
| 22 | +#include <util/message.h> |
| 23 | + |
| 24 | +#include <goto-programs/goto_model.h> |
| 25 | + |
| 26 | +#include <analyses/dirty.h> |
| 27 | +#include <analyses/locals.h> |
| 28 | +#include <goto-instrument/havoc_utils.h> |
| 29 | +#include <goto-instrument/loop_utils.h> |
| 30 | + |
| 31 | +#include <set> |
| 32 | +#include <vector> |
| 33 | + |
| 34 | +/// Stores information about a goto function computed from its CFG. |
| 35 | +/// |
| 36 | +/// The methods of this class provide information about identifiers |
| 37 | +/// to allow simplifying assigns clause checking assertions. |
| 38 | +class cfg_infot |
| 39 | +{ |
| 40 | +public: |
| 41 | + /// Returns true iff `ident` is locally declared. |
| 42 | + virtual bool is_local(const irep_idt &ident) const = 0; |
| 43 | + |
| 44 | + /// Returns true iff the given `ident` is either non-locally declared |
| 45 | + /// or is locally-declared but dirty. |
| 46 | + virtual bool is_not_local_or_dirty_local(const irep_idt &ident) const = 0; |
| 47 | + |
| 48 | + /// Returns true iff `expr` is an access to a locally declared symbol |
| 49 | + /// and does not contain `dereference` or `address_of` operations. |
| 50 | + bool is_local_composite_access(const exprt &expr) const |
| 51 | + { |
| 52 | + // case-splitting on the lhs structure copied from symex_assignt::assign_rec |
| 53 | + if(expr.id() == ID_symbol) |
| 54 | + { |
| 55 | + return is_local(to_symbol_expr(expr).get_identifier()); |
| 56 | + } |
| 57 | + else if(expr.id() == ID_index) |
| 58 | + { |
| 59 | + return is_local_composite_access(to_index_expr(expr).array()); |
| 60 | + } |
| 61 | + else if(expr.id() == ID_member) |
| 62 | + { |
| 63 | + const typet &type = to_member_expr(expr).struct_op().type(); |
| 64 | + if( |
| 65 | + type.id() == ID_struct || type.id() == ID_struct_tag || |
| 66 | + type.id() == ID_union || type.id() == ID_union_tag) |
| 67 | + { |
| 68 | + return is_local_composite_access(to_member_expr(expr).compound()); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + throw unsupported_operation_exceptiont( |
| 73 | + "is_local_composite_access: unexpected assignment to member of '" + |
| 74 | + type.id_string() + "'"); |
| 75 | + } |
| 76 | + } |
| 77 | + else if(expr.id() == ID_if) |
| 78 | + { |
| 79 | + return is_local_composite_access(to_if_expr(expr).true_case()) && |
| 80 | + is_local_composite_access(to_if_expr(expr).false_case()); |
| 81 | + } |
| 82 | + else if(expr.id() == ID_typecast) |
| 83 | + { |
| 84 | + return is_local_composite_access(to_typecast_expr(expr).op()); |
| 85 | + } |
| 86 | + else if( |
| 87 | + expr.id() == ID_byte_extract_little_endian || |
| 88 | + expr.id() == ID_byte_extract_big_endian) |
| 89 | + { |
| 90 | + return is_local_composite_access(to_byte_extract_expr(expr).op()); |
| 91 | + } |
| 92 | + else if(expr.id() == ID_complex_real) |
| 93 | + { |
| 94 | + return is_local_composite_access(to_complex_real_expr(expr).op()); |
| 95 | + } |
| 96 | + else if(expr.id() == ID_complex_imag) |
| 97 | + { |
| 98 | + return is_local_composite_access(to_complex_imag_expr(expr).op()); |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + // matches ID_address_of, ID_dereference, etc. |
| 103 | + return false; |
| 104 | + } |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +// For goto_functions |
| 109 | +class function_cfg_infot : public cfg_infot |
| 110 | +{ |
| 111 | +public: |
| 112 | + explicit function_cfg_infot(const goto_functiont &_goto_function) |
| 113 | + : dirty_analysis(_goto_function), locals(_goto_function) |
| 114 | + { |
| 115 | + parameters.insert( |
| 116 | + _goto_function.parameter_identifiers.begin(), |
| 117 | + _goto_function.parameter_identifiers.end()); |
| 118 | + } |
| 119 | + |
| 120 | + /// Returns true iff `ident` is a local or parameter of the goto_function. |
| 121 | + bool is_local(const irep_idt &ident) const override |
| 122 | + { |
| 123 | + return locals.is_local(ident) || |
| 124 | + (parameters.find(ident) != parameters.end()); |
| 125 | + } |
| 126 | + |
| 127 | + /// Returns true iff the given `ident` is either not a goto_function local |
| 128 | + /// or is a local that is dirty. |
| 129 | + bool is_not_local_or_dirty_local(const irep_idt &ident) const override |
| 130 | + { |
| 131 | + if(is_local(ident)) |
| 132 | + return dirty_analysis.get_dirty_ids().find(ident) != |
| 133 | + dirty_analysis.get_dirty_ids().end(); |
| 134 | + else |
| 135 | + return true; |
| 136 | + } |
| 137 | + |
| 138 | +private: |
| 139 | + const dirtyt dirty_analysis; |
| 140 | + const localst locals; |
| 141 | + std::unordered_set<irep_idt> parameters; |
| 142 | +}; |
| 143 | + |
| 144 | +// For a loop in a goto_function |
| 145 | +class loop_cfg_infot : public cfg_infot |
| 146 | +{ |
| 147 | +public: |
| 148 | + loop_cfg_infot(goto_functiont &_goto_function, const loopt &loop) |
| 149 | + : dirty_analysis(_goto_function) |
| 150 | + { |
| 151 | + for(const auto &t : loop) |
| 152 | + { |
| 153 | + if(t->is_decl()) |
| 154 | + locals.insert(t->decl_symbol().get_identifier()); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /// Returns true iff `ident` is a loop local. |
| 159 | + bool is_local(const irep_idt &ident) const override |
| 160 | + { |
| 161 | + return locals.find(ident) != locals.end(); |
| 162 | + } |
| 163 | + |
| 164 | + /// Returns true iff the given `ident` is either not a loop local |
| 165 | + /// or is a loop local that is dirty. |
| 166 | + bool is_not_local_or_dirty_local(const irep_idt &ident) const override |
| 167 | + { |
| 168 | + if(is_local(ident)) |
| 169 | + return dirty_analysis.get_dirty_ids().find(ident) != |
| 170 | + dirty_analysis.get_dirty_ids().end(); |
| 171 | + else |
| 172 | + return true; |
| 173 | + } |
| 174 | + |
| 175 | + void erase_locals(std::set<exprt> &exprs) |
| 176 | + { |
| 177 | + auto it = exprs.begin(); |
| 178 | + while(it != exprs.end()) |
| 179 | + { |
| 180 | + if( |
| 181 | + it->id() == ID_symbol && is_local(to_symbol_expr(*it).get_identifier())) |
| 182 | + { |
| 183 | + it = exprs.erase(it); |
| 184 | + } |
| 185 | + else |
| 186 | + { |
| 187 | + it++; |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | +private: |
| 193 | + const dirtyt dirty_analysis; |
| 194 | + std::unordered_set<irep_idt> locals; |
| 195 | +}; |
| 196 | + |
| 197 | +#endif |
0 commit comments