Skip to content

Commit cba81d9

Browse files
author
Sonny Martin
committed
Cleanup asserts & throws - replace with invariants
Deprecated c-style asserts in favour of cbmc invariants. Some minor refactoring that was relevant to the cleanup.
1 parent f9a09d8 commit cba81d9

File tree

4 files changed

+70
-51
lines changed

4 files changed

+70
-51
lines changed

src/util/simplify_expr.cpp

Lines changed: 25 additions & 30 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
}
@@ -1365,17 +1365,13 @@ bool simplify_exprt::simplify_update(exprt &expr)
13651365
{
13661366
const irep_idt &component_name=
13671367
e.get(ID_component_name);
1368-
1369-
if(!to_struct_type(value_ptr_type).
1370-
has_component(component_name))
1368+
const struct_typet &value_ptr_struct_type =
1369+
to_struct_type(value_ptr_type);
1370+
if(!value_ptr_struct_type.has_component(component_name))
13711371
return true;
1372-
1373-
std::size_t number=to_struct_type(value_ptr_type).
1374-
component_number(component_name);
1375-
1376-
assert(number<value_ptr->operands().size());
1377-
1378-
value_ptr=&value_ptr->operands()[number];
1372+
auto &designator_as_struct_expr = to_struct_expr(*value_ptr);
1373+
value_ptr = &designator_as_struct_expr.component(component_name, ns);
1374+
CHECK_RETURN(value_ptr->is_not_nil());
13791375
}
13801376
else
13811377
return true; // give up, unknown designator
@@ -1407,16 +1403,13 @@ bool simplify_exprt::simplify_object(exprt &expr)
14071403
}
14081404
else if(expr.id()==ID_typecast)
14091405
{
1410-
const typet &op_type=ns.follow(expr.op0().type());
1411-
1412-
assert(expr.operands().size()==1);
1406+
auto const &typecast_expr = to_typecast_expr(expr);
1407+
const typet &op_type = ns.follow(typecast_expr.op().type());
14131408

14141409
if(op_type.id()==ID_pointer)
14151410
{
14161411
// cast from pointer to pointer
1417-
exprt tmp;
1418-
tmp.swap(expr.op0());
1419-
expr.swap(tmp);
1412+
expr = typecast_expr.op();
14201413
simplify_object(expr);
14211414
return false;
14221415
}
@@ -1427,10 +1420,12 @@ bool simplify_exprt::simplify_object(exprt &expr)
14271420
// We do a bit of special treatment for (TYPE *)(a+(int)&o) and
14281421
// (TYPE *)(a+(int)((T*)&o+x)), which are re-written to '&o'.
14291422

1430-
exprt tmp=expr.op0();
1431-
if(tmp.id()==ID_plus && tmp.operands().size()==2)
1423+
const exprt &casted_expr = typecast_expr.op();
1424+
if(casted_expr.id() == ID_plus && casted_expr.operands().size() == 2)
14321425
{
1433-
exprt cand=tmp.op0().id()==ID_typecast?tmp.op0():tmp.op1();
1426+
const exprt &cand = casted_expr.op0().id() == ID_typecast ? casted_expr
1427+
.op0()
1428+
: casted_expr.op1();
14341429

14351430
if(cand.id()==ID_typecast &&
14361431
cand.operands().size()==1 &&
@@ -1545,7 +1540,7 @@ exprt simplify_exprt::bits2expr(
15451540
for(const auto &component : components)
15461541
{
15471542
mp_integer m_size=pointer_offset_bits(component.type(), ns);
1548-
assert(m_size>=0);
1543+
CHECK_RETURN(m_size >= 0);
15491544

15501545
std::string comp_bits=
15511546
std::string(
@@ -1573,7 +1568,7 @@ exprt simplify_exprt::bits2expr(
15731568

15741569
std::size_t el_size=
15751570
integer2size_t(pointer_offset_bits(type.subtype(), ns));
1576-
assert(el_size>0);
1571+
CHECK_RETURN(el_size > 0);
15771572

15781573
array_exprt result(array_type);
15791574
result.reserve_operands(n_el);
@@ -1829,10 +1824,10 @@ bool simplify_exprt::simplify_byte_extract(byte_extract_exprt &expr)
18291824
op_type_ptr->id()==ID_array;
18301825
op_type_ptr=&(ns.follow(*op_type_ptr).subtype()))
18311826
{
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);
1827+
DATA_INVARIANT(el_size > 0, "arrays must not have zero-sized objects");
1828+
DATA_INVARIANT(
1829+
el_size % 8 == 0,
1830+
"array elements have a size in bits which is a multiple of bytes");
18361831
mp_integer el_bytes=el_size/8;
18371832

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

src/util/simplify_expr_boolean.cpp

Lines changed: 5 additions & 8 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,13 +213,10 @@ 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);
217-
exprt tmp;
218-
tmp.swap(op);
219-
expr.swap(tmp);
220-
expr.id(ID_forall);
221-
expr.op1().make_not();
222-
simplify_node(expr.op1());
216+
auto const &op_as_exists = to_exists_expr(op);
217+
forall_exprt rewritten_op(op_as_exists.symbol(), not_exprt(op_as_exists.where()));
218+
simplify_node(rewritten_op.where());
219+
expr = rewritten_op;
223220
return false;
224221
}
225222

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

src/util/std_expr.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4512,6 +4512,24 @@ class exists_exprt:public quantifier_exprt
45124512
}
45134513
};
45144514

4515+
inline const exists_exprt &to_exists_expr(const exprt &expr)
4516+
{
4517+
PRECONDITION(expr.id() == ID_exists);
4518+
DATA_INVARIANT(
4519+
expr.operands().size() == 2,
4520+
"exists expressions have exactly two operands");
4521+
return static_cast<const exists_exprt &>(expr);
4522+
}
4523+
4524+
inline exists_exprt &to_exists_expr(exprt &expr)
4525+
{
4526+
PRECONDITION(expr.id() == ID_exists);
4527+
DATA_INVARIANT(
4528+
expr.operands().size() == 2,
4529+
"exists expressions have exactly two operands");
4530+
return static_cast<exists_exprt &>(expr);
4531+
}
4532+
45154533
/// \brief The popcount (counting the number of bits set to 1) expression
45164534
class popcount_exprt: public unary_exprt
45174535
{

0 commit comments

Comments
 (0)