Skip to content

Commit 0097e74

Browse files
author
Sonny Martin
committed
Invariant cleanup
1 parent f9a09d8 commit 0097e74

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

src/util/simplify_expr.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Author: Daniel Kroening, [email protected]
88

99
#include "simplify_expr.h"
1010

11-
#include <cassert>
1211
#include <algorithm>
1312

1413
#include "arith_tools.h"
@@ -19,6 +18,7 @@ Author: Daniel Kroening, [email protected]
1918
#include "endianness_map.h"
2019
#include "expr_util.h"
2120
#include "fixedbv.h"
21+
#include "invariant.h"
2222
#include "namespace.h"
2323
#include "pointer_offset_size.h"
2424
#include "rational.h"
@@ -225,7 +225,7 @@ bool simplify_exprt::simplify_typecast(exprt &expr)
225225
inequality.add_source_location()=expr.source_location();
226226
inequality.lhs()=expr.op0();
227227
inequality.rhs()=from_integer(0, op_type);
228-
assert(inequality.rhs().is_not_nil());
228+
CHECK_RETURN(inequality.rhs().is_not_nil());
229229
simplify_node(inequality);
230230
expr.swap(inequality);
231231
return false;
@@ -260,7 +260,7 @@ bool simplify_exprt::simplify_typecast(exprt &expr)
260260
inequality.add_source_location()=expr.source_location();
261261
inequality.lhs()=expr.op0();
262262
inequality.rhs()=from_integer(0, op_type);
263-
assert(inequality.rhs().is_not_nil());
263+
CHECK_RETURN(inequality.rhs().is_not_nil());
264264
simplify_node(inequality);
265265
expr.op0()=inequality;
266266
simplify_typecast(expr); // recursive call
@@ -488,13 +488,13 @@ bool simplify_exprt::simplify_typecast(exprt &expr)
488488
if(operand.is_true())
489489
{
490490
expr=from_integer(1, expr_type);
491-
assert(expr.is_not_nil());
491+
CHECK_RETURN(expr.is_not_nil());
492492
return false;
493493
}
494494
else if(operand.is_false())
495495
{
496496
expr=from_integer(0, expr_type);
497-
assert(expr.is_not_nil());
497+
CHECK_RETURN(expr.is_not_nil());
498498
return false;
499499
}
500500
}
@@ -1373,7 +1373,7 @@ bool simplify_exprt::simplify_update(exprt &expr)
13731373
std::size_t number=to_struct_type(value_ptr_type).
13741374
component_number(component_name);
13751375

1376-
assert(number<value_ptr->operands().size());
1376+
CHECK_RETURN(number < value_ptr->operands().size());
13771377

13781378
value_ptr=&value_ptr->operands()[number];
13791379
}
@@ -1409,7 +1409,9 @@ bool simplify_exprt::simplify_object(exprt &expr)
14091409
{
14101410
const typet &op_type=ns.follow(expr.op0().type());
14111411

1412-
assert(expr.operands().size()==1);
1412+
DATA_INVARIANT(
1413+
expr.operands().size() == 1,
1414+
"typecasts must have exactly one argument");
14131415

14141416
if(op_type.id()==ID_pointer)
14151417
{
@@ -1545,7 +1547,7 @@ exprt simplify_exprt::bits2expr(
15451547
for(const auto &component : components)
15461548
{
15471549
mp_integer m_size=pointer_offset_bits(component.type(), ns);
1548-
assert(m_size>=0);
1550+
CHECK_RETURN(m_size >= 0);
15491551

15501552
std::string comp_bits=
15511553
std::string(
@@ -1573,7 +1575,7 @@ exprt simplify_exprt::bits2expr(
15731575

15741576
std::size_t el_size=
15751577
integer2size_t(pointer_offset_bits(type.subtype(), ns));
1576-
assert(el_size>0);
1578+
CHECK_RETURN(el_size > 0);
15771579

15781580
array_exprt result(array_type);
15791581
result.reserve_operands(n_el);
@@ -1829,10 +1831,10 @@ bool simplify_exprt::simplify_byte_extract(byte_extract_exprt &expr)
18291831
op_type_ptr->id()==ID_array;
18301832
op_type_ptr=&(ns.follow(*op_type_ptr).subtype()))
18311833
{
1832-
// no arrays of zero-sized objects
1833-
assert(el_size>0);
1834-
// no arrays of non-byte sized objects
1835-
assert(el_size%8==0);
1834+
DATA_INVARIANT(el_size > 0, "arrays must not have zero-sized objects");
1835+
DATA_INVARIANT(
1836+
el_size % 8 == 0,
1837+
"array elements have a size in bits which is a multiple of bytes");
18361838
mp_integer el_bytes=el_size/8;
18371839

18381840
if(base_type_eq(expr.type(), op_type_ptr->subtype(), ns) ||

src/util/simplify_expr_boolean.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Author: Daniel Kroening, [email protected]
88

99
#include "simplify_expr_class.h"
1010

11-
#include <cassert>
1211
#include <unordered_set>
1312

1413
#include "expr.h"
14+
#include "invariant.h"
1515
#include "namespace.h"
1616
#include "std_expr.h"
1717

@@ -213,7 +213,9 @@ bool simplify_exprt::simplify_not(exprt &expr)
213213
}
214214
else if(op.id()==ID_exists) // !(exists: a) <-> forall: not a
215215
{
216-
assert(op.operands().size()==2);
216+
DATA_INVARIANT(
217+
op.operands().size() == 2,
218+
"exists expression has two parameters");
217219
exprt tmp;
218220
tmp.swap(op);
219221
expr.swap(tmp);

src/util/simplify_expr_floatbv.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ Author: Daniel Kroening, [email protected]
88

99
#include "simplify_expr_class.h"
1010

11-
#include <cassert>
12-
11+
#include "arith_tools.h"
1312
#include "expr.h"
14-
#include "namespace.h"
1513
#include "ieee_float.h"
14+
#include "invariant.h"
15+
#include "namespace.h"
1616
#include "std_expr.h"
17-
#include "arith_tools.h"
1817

1918
bool simplify_exprt::simplify_isinf(exprt &expr)
2019
{
@@ -142,7 +141,9 @@ bool simplify_exprt::simplify_floatbv_typecast(exprt &expr)
142141
{
143142
// These casts usually reduce precision, and thus, usually round.
144143

145-
assert(expr.operands().size()==2);
144+
PRECONDITION(expr.id() == ID_floatbv_typecast);
145+
DATA_INVARIANT(
146+
expr.operands().size() == 2, "float typecast has two operands");
146147

147148
const typet &dest_type=ns.follow(expr.type());
148149
const typet &src_type=ns.follow(expr.op0().type());
@@ -286,17 +287,25 @@ bool simplify_exprt::simplify_floatbv_op(exprt &expr)
286287
{
287288
const typet &type=ns.follow(expr.type());
288289

289-
if(type.id()!=ID_floatbv)
290-
return true;
291-
292-
assert(expr.operands().size()==3);
290+
PRECONDITION(type.id() == ID_floatbv);
291+
PRECONDITION(
292+
expr.id() == ID_floatbv_plus || expr.id() == ID_floatbv_minus ||
293+
expr.id() == ID_floatbv_mult || expr.id() == ID_floatbv_div);
294+
DATA_INVARIANT(
295+
expr.operands().size() == 3,
296+
"binary operations have two operands, here an addtional parameter "
297+
"is for the rounding mode");
293298

294299
exprt op0=expr.op0();
295300
exprt op1=expr.op1();
296301
exprt op2=expr.op2(); // rounding mode
297302

298-
assert(ns.follow(op0.type())==type);
299-
assert(ns.follow(op1.type())==type);
303+
INVARIANT(
304+
ns.follow(op0.type()) == type,
305+
"expression type of operand must match type of expression");
306+
INVARIANT(
307+
ns.follow(op1.type()) == type,
308+
"expression type of operand must match type of expression");
300309

301310
// Remember that floating-point addition is _NOT_ associative.
302311
// Thus, we don't re-sort the operands.
@@ -347,8 +356,8 @@ bool simplify_exprt::simplify_floatbv_op(exprt &expr)
347356

348357
bool simplify_exprt::simplify_ieee_float_relation(exprt &expr)
349358
{
350-
assert(expr.id()==ID_ieee_float_equal ||
351-
expr.id()==ID_ieee_float_notequal);
359+
PRECONDITION(
360+
expr.id() == ID_ieee_float_equal || expr.id() == ID_ieee_float_notequal);
352361

353362
exprt::operandst &operands=expr.operands();
354363

0 commit comments

Comments
 (0)