Skip to content

Commit d3a148d

Browse files
Sonny Martinhannes-steffenhagen-diffblue
Sonny Martin
authored andcommitted
Cleanup asserts & throws - replace with invariants
* C-style asserts are deprecated in favor of CBMCs INVARIANT macros * Some minor refactoring that was relevant to the cleanup.
1 parent da8d2ab commit d3a148d

7 files changed

+85
-70
lines changed

src/util/simplify_expr_int.cpp

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ bool simplify_exprt::simplify_bswap(bswap_exprt &expr)
4343
mp_integer new_value=0;
4444
for(std::size_t bit = 0; bit < width; bit += bits_per_byte)
4545
{
46-
assert(!bytes.empty());
46+
INVARIANT(
47+
!bytes.empty(),
48+
"bytes is not empty because we just pushed just as many elements on "
49+
"top of it as we are popping now");
4750
new_value+=bytes.back()<<bit;
4851
bytes.pop_back();
4952
}
@@ -180,7 +183,7 @@ bool simplify_exprt::simplify_mult(exprt &expr)
180183
exprt::operandst::iterator constant;
181184

182185
// true if we have found a constant
183-
bool found = false;
186+
bool constant_found = false;
184187

185188
typet c_sizeof_type=nil_typet();
186189

@@ -212,7 +215,7 @@ bool simplify_exprt::simplify_mult(exprt &expr)
212215
c_sizeof_type=
213216
static_cast<const typet &>(it->find(ID_C_c_sizeof_type));
214217

215-
if(found)
218+
if(constant_found)
216219
{
217220
// update the constant factor
218221
if(!mul_expr(to_constant_expr(*constant), to_constant_expr(*it)))
@@ -222,7 +225,7 @@ bool simplify_exprt::simplify_mult(exprt &expr)
222225
{
223226
// set it as the constant factor if this is the first
224227
constant=it;
225-
found=true;
228+
constant_found = true;
226229
}
227230
}
228231

@@ -238,7 +241,10 @@ bool simplify_exprt::simplify_mult(exprt &expr)
238241

239242
if(c_sizeof_type.is_not_nil())
240243
{
241-
assert(found);
244+
INVARIANT(
245+
constant_found,
246+
"c_sizeof_type is only set to a non-nil value "
247+
"if a constant has been found");
242248
constant->set(ID_C_c_sizeof_type, c_sizeof_type);
243249
}
244250

@@ -252,7 +258,7 @@ bool simplify_exprt::simplify_mult(exprt &expr)
252258
else
253259
{
254260
// if the constant is a one and there are other factors
255-
if(found && constant->is_one())
261+
if(constant_found && constant->is_one())
256262
{
257263
// just delete it
258264
operands.erase(constant);
@@ -440,6 +446,8 @@ bool simplify_exprt::simplify_mod(exprt &expr)
440446

441447
bool simplify_exprt::simplify_plus(exprt &expr)
442448
{
449+
PRECONDITION(expr.id() == ID_plus);
450+
443451
if(!is_number(expr.type()) &&
444452
expr.type().id()!=ID_pointer)
445453
return true;
@@ -448,8 +456,6 @@ bool simplify_exprt::simplify_plus(exprt &expr)
448456

449457
exprt::operandst &operands=expr.operands();
450458

451-
assert(expr.id()==ID_plus);
452-
453459
// floating-point addition is _NOT_ associative; thus,
454460
// there is special case for float
455461

@@ -523,7 +529,7 @@ bool simplify_exprt::simplify_plus(exprt &expr)
523529
to_constant_expr(*it)))
524530
{
525531
*it=from_integer(0, it->type());
526-
assert(it->is_not_nil());
532+
CHECK_RETURN(it->is_not_nil());
527533
result=false;
528534
}
529535
}
@@ -556,7 +562,7 @@ bool simplify_exprt::simplify_plus(exprt &expr)
556562
{
557563
*(itm->second)=from_integer(0, expr.type());
558564
*it=from_integer(0, expr.type());
559-
assert(it->is_not_nil());
565+
CHECK_RETURN(it->is_not_nil());
560566
expr_map.erase(itm);
561567
result=false;
562568
}
@@ -583,7 +589,7 @@ bool simplify_exprt::simplify_plus(exprt &expr)
583589
if(operands.empty())
584590
{
585591
expr=from_integer(0, expr.type());
586-
assert(expr.is_not_nil());
592+
CHECK_RETURN(expr.is_not_nil());
587593
return false;
588594
}
589595
else if(operands.size()==1)
@@ -598,14 +604,14 @@ bool simplify_exprt::simplify_plus(exprt &expr)
598604

599605
bool simplify_exprt::simplify_minus(exprt &expr)
600606
{
607+
PRECONDITION(expr.id() == ID_minus);
608+
601609
if(!is_number(expr.type()) &&
602610
expr.type().id()!=ID_pointer)
603611
return true;
604612

605613
exprt::operandst &operands=expr.operands();
606614

607-
assert(expr.id()==ID_minus);
608-
609615
if(operands.size()!=2)
610616
return true;
611617

@@ -646,7 +652,7 @@ bool simplify_exprt::simplify_minus(exprt &expr)
646652
if(operands[0]==operands[1])
647653
{
648654
exprt zero=from_integer(0, expr.type());
649-
assert(zero.is_not_nil());
655+
CHECK_RETURN(zero.is_not_nil());
650656
expr=zero;
651657
return false;
652658
}
@@ -741,7 +747,9 @@ bool simplify_exprt::simplify_bitwise(exprt &expr)
741747
if(expr.op1().type()!=expr.type())
742748
break;
743749

744-
assert(a_str.size()==b_str.size());
750+
INVARIANT(
751+
a_str.size() == b_str.size(),
752+
"bitvectors of the same type have the same size");
745753

746754
std::string new_value;
747755
new_value.resize(width);
@@ -847,32 +855,40 @@ bool simplify_exprt::simplify_bitwise(exprt &expr)
847855

848856
bool simplify_exprt::simplify_extractbit(exprt &expr)
849857
{
850-
const typet &op0_type=expr.op0().type();
851-
852-
if(!is_bitvector_type(op0_type))
853-
return true;
858+
PRECONDITION(expr.id() == ID_extractbit);
859+
const auto &extractbit_expr = to_extractbit_expr(expr);
854860

855-
std::size_t width=to_bitvector_type(op0_type).get_width();
861+
const typet &src_type = extractbit_expr.src().type();
856862

857-
assert(expr.operands().size()==2);
863+
if(!is_bitvector_type(src_type))
864+
return true;
858865

859-
mp_integer i;
866+
const std::size_t src_bit_width = to_bitvector_type(src_type).get_width();
860867

861-
if(to_integer(expr.op1(), i))
868+
const auto index_converted_to_int =
869+
numeric_cast<mp_integer>(extractbit_expr.index());
870+
if(!index_converted_to_int.has_value())
871+
{
862872
return true;
863-
864-
if(!expr.op0().is_constant())
873+
}
874+
const mp_integer index_as_int = index_converted_to_int.value();
875+
if(!extractbit_expr.src().is_constant())
865876
return true;
866877

867-
if(i<0 || i>=width)
878+
if(index_as_int < 0 || index_as_int >= src_bit_width)
868879
return true;
869880

870-
const irep_idt &value=expr.op0().get(ID_value);
881+
const irep_idt &src_value =
882+
to_constant_expr(extractbit_expr.src()).get_value();
883+
884+
std::string src_value_as_string = id2string(src_value);
871885

872-
if(value.size()!=width)
886+
if(src_value_as_string.size() != src_bit_width)
873887
return true;
874888

875-
bool bit=(id2string(value)[width-integer2size_t(i)-1]=='1');
889+
const bool bit =
890+
(src_value_as_string
891+
[src_bit_width - numeric_cast_v<std::size_t>(index_as_int) - 1] == '1');
876892

877893
expr.make_bool(bit);
878894

@@ -1580,7 +1596,9 @@ bool simplify_exprt::simplify_inequality_not_constant(exprt &expr)
15801596

15811597
// now we only have >=, =
15821598

1583-
assert(expr.id()==ID_ge || expr.id()==ID_equal);
1599+
INVARIANT(
1600+
expr.id() == ID_ge || expr.id() == ID_equal,
1601+
"we previously converted all other cases to >= or ==");
15841602

15851603
// syntactically equal?
15861604

@@ -1662,7 +1680,7 @@ bool simplify_exprt::simplify_inequality_not_constant(exprt &expr)
16621680
bool simplify_exprt::simplify_inequality_constant(exprt &expr)
16631681
{
16641682
// the constant is always on the RHS
1665-
assert(expr.op1().is_constant());
1683+
PRECONDITION(expr.op1().is_constant());
16661684

16671685
if(expr.op0().id()==ID_if && expr.op0().operands().size()==3)
16681686
{
@@ -1766,7 +1784,7 @@ bool simplify_exprt::simplify_inequality_constant(exprt &expr)
17661784
{
17671785
constant+=i;
17681786
*it=from_integer(0, it->type());
1769-
assert(it->is_not_nil());
1787+
CHECK_RETURN(it->is_not_nil());
17701788
changed=true;
17711789
}
17721790
}
@@ -1906,9 +1924,7 @@ bool simplify_exprt::simplify_inequality_constant(exprt &expr)
19061924
}
19071925
else if(expr.id()==ID_gt)
19081926
{
1909-
mp_integer i;
1910-
if(to_integer(expr.op1(), i))
1911-
throw "bit-vector constant unexpectedly non-integer";
1927+
mp_integer i = numeric_cast_v<mp_integer>(expr.op1());
19121928

19131929
if(i==max)
19141930
{
@@ -1932,9 +1948,7 @@ bool simplify_exprt::simplify_inequality_constant(exprt &expr)
19321948
}
19331949
else if(expr.id()==ID_le)
19341950
{
1935-
mp_integer i;
1936-
if(to_integer(expr.op1(), i))
1937-
throw "bit-vector constant unexpectedly non-integer";
1951+
mp_integer i = numeric_cast_v<mp_integer>(expr.op1());
19381952

19391953
if(i==max)
19401954
{

src/util/simplify_expr_pointer.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,8 @@ bool simplify_exprt::simplify_address_of(exprt &expr)
196196
else if(object.id()==ID_dereference)
197197
{
198198
// simplify &*p to p
199-
assert(object.operands().size()==1);
200-
exprt tmp=object.op0();
201-
expr=tmp;
199+
auto const &object_as_dereference_expr = to_dereference_expr(object);
200+
expr = object_as_dereference_expr.pointer();
202201
return false;
203202
}
204203

@@ -410,9 +409,10 @@ bool simplify_exprt::simplify_pointer_offset(exprt &expr)
410409

411410
bool simplify_exprt::simplify_inequality_address_of(exprt &expr)
412411
{
413-
assert(expr.type().id()==ID_bool);
414-
assert(expr.operands().size()==2);
415-
assert(expr.id()==ID_equal || expr.id()==ID_notequal);
412+
PRECONDITION(expr.id() == ID_equal || expr.id() == ID_notequal);
413+
PRECONDITION(expr.type().id() == ID_bool);
414+
DATA_INVARIANT(
415+
expr.operands().size() == 2, "(in)equalities have two operands");
416416

417417
exprt tmp0=expr.op0();
418418
if(tmp0.id()==ID_typecast)
@@ -426,8 +426,8 @@ bool simplify_exprt::simplify_inequality_address_of(exprt &expr)
426426
if(tmp1.op0().id()==ID_index &&
427427
to_index_expr(tmp1.op0()).index().is_zero())
428428
tmp1=address_of_exprt(to_index_expr(tmp1.op0()).array());
429-
assert(tmp0.id()==ID_address_of);
430-
assert(tmp1.id()==ID_address_of);
429+
INVARIANT(tmp0.id() == ID_address_of, "id must be ID_address_of");
430+
INVARIANT(tmp1.id() == ID_address_of, "id must be ID_address_of");
431431

432432
if(tmp0.operands().size()!=1)
433433
return true;
@@ -450,14 +450,15 @@ bool simplify_exprt::simplify_inequality_address_of(exprt &expr)
450450

451451
bool simplify_exprt::simplify_inequality_pointer_object(exprt &expr)
452452
{
453-
assert(expr.type().id()==ID_bool);
454-
assert(expr.operands().size()==2);
455-
assert(expr.id()==ID_equal || expr.id()==ID_notequal);
453+
PRECONDITION(expr.id() == ID_equal || expr.id() == ID_notequal);
454+
PRECONDITION(expr.type().id() == ID_bool);
455+
DATA_INVARIANT(
456+
expr.operands().size() == 2, "(in)equalities have two operands");
456457

457458
forall_operands(it, expr)
458459
{
459-
assert(it->id()==ID_pointer_object);
460-
assert(it->operands().size()==1);
460+
PRECONDITION(it->id() == ID_pointer_object);
461+
PRECONDITION(it->operands().size() == 1);
461462
const exprt &op=it->op0();
462463

463464
if(op.id()==ID_address_of)

src/util/ssa_expr.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ class ssa_exprt:public symbol_exprt
153153
*/
154154
inline const ssa_exprt &to_ssa_expr(const exprt &expr)
155155
{
156-
assert(expr.id()==ID_symbol &&
157-
expr.get_bool(ID_C_SSA_symbol) &&
158-
!expr.has_operands());
156+
PRECONDITION(
157+
expr.id() == ID_symbol && expr.get_bool(ID_C_SSA_symbol) &&
158+
!expr.has_operands());
159159
return static_cast<const ssa_exprt &>(expr);
160160
}
161161

@@ -164,9 +164,9 @@ inline const ssa_exprt &to_ssa_expr(const exprt &expr)
164164
*/
165165
inline ssa_exprt &to_ssa_expr(exprt &expr)
166166
{
167-
assert(expr.id()==ID_symbol &&
168-
expr.get_bool(ID_C_SSA_symbol) &&
169-
!expr.has_operands());
167+
PRECONDITION(
168+
expr.id() == ID_symbol && expr.get_bool(ID_C_SSA_symbol) &&
169+
!expr.has_operands());
170170
return static_cast<ssa_exprt &>(expr);
171171
}
172172

src/util/std_expr.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static void build_object_descriptor_rec(
7474
build_object_descriptor_rec(ns, index.array(), dest);
7575

7676
exprt sub_size=size_of_expr(expr.type(), ns);
77-
assert(sub_size.is_not_nil());
77+
CHECK_RETURN(sub_size.is_not_nil());
7878

7979
dest.offset()=
8080
plus_exprt(dest.offset(),
@@ -89,7 +89,7 @@ static void build_object_descriptor_rec(
8989
build_object_descriptor_rec(ns, struct_op, dest);
9090

9191
exprt offset=member_offset_expr(member, ns);
92-
assert(offset.is_not_nil());
92+
CHECK_RETURN(offset.is_not_nil());
9393

9494
dest.offset()=
9595
plus_exprt(dest.offset(),
@@ -124,15 +124,15 @@ void object_descriptor_exprt::build(
124124
const exprt &expr,
125125
const namespacet &ns)
126126
{
127-
assert(object().id()==ID_unknown);
127+
PRECONDITION(object().id() == ID_unknown);
128128
object()=expr;
129129

130130
if(offset().id()==ID_unknown)
131131
offset()=from_integer(0, index_type());
132132

133133
build_object_descriptor_rec(ns, expr, *this);
134134

135-
assert(root_object().type().id()!=ID_empty);
135+
POSTCONDITION(root_object().type().id() != ID_empty);
136136
}
137137

138138
shift_exprt::shift_exprt(
@@ -158,7 +158,7 @@ extractbits_exprt::extractbits_exprt(
158158
const typet &_type):
159159
exprt(ID_extractbits, _type)
160160
{
161-
assert(_upper>=_lower);
161+
PRECONDITION(_upper >= _lower);
162162
operands().resize(3);
163163
src()=_src;
164164
upper()=from_integer(_upper, integer_typet());
@@ -204,7 +204,8 @@ const exprt &object_descriptor_exprt::root_object() const
204204

205205
while(p->id() == ID_member || p->id() == ID_index)
206206
{
207-
assert(!p->operands().empty());
207+
DATA_INVARIANT(
208+
p->has_operands(), "member and index expressions have operands");
208209
p = &p->op0();
209210
}
210211

0 commit comments

Comments
 (0)