Skip to content

Commit cbe7de3

Browse files
author
martin
committed
Replace assertions in some core libraries.
These also serve as examples of which annotations to use.
1 parent f77b843 commit cbe7de3

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

src/util/expr.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ Author: Daniel Kroening, [email protected]
99
/// \file
1010
/// Expression Representation
1111

12-
#include <cassert>
13-
1412
#include <stack>
1513

1614
#include "string2int.h"
1715
#include "mp_arith.h"
1816
#include "fixedbv.h"
1917
#include "ieee_float.h"
18+
#include "invariant.h"
2019
#include "expr.h"
2120
#include "rational.h"
2221
#include "rational_tools.h"
@@ -203,15 +202,16 @@ void exprt::negate()
203202
else
204203
{
205204
make_nil();
206-
assert(false);
205+
UNREACHABLE;
207206
}
208207
}
209208
else
210209
{
211210
if(id()==ID_unary_minus)
212211
{
213212
exprt tmp;
214-
assert(operands().size()==1);
213+
DATA_INVARIANT(operands().size()==1,
214+
"Unary minus must have one operand");
215215
tmp.swap(op0());
216216
swap(tmp);
217217
}
@@ -245,7 +245,7 @@ bool exprt::is_zero() const
245245
{
246246
rationalt rat_value;
247247
if(to_rational(*this, rat_value))
248-
assert(false);
248+
CHECK_RETURN(false);
249249
return rat_value.is_zero();
250250
}
251251
else if(type_id==ID_unsignedbv ||
@@ -291,7 +291,7 @@ bool exprt::is_one() const
291291
{
292292
rationalt rat_value;
293293
if(to_rational(*this, rat_value))
294-
assert(false);
294+
CHECK_RETURN(false);
295295
return rat_value.is_one();
296296
}
297297
else if(type_id==ID_unsignedbv || type_id==ID_signedbv)

src/util/expr_util.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ exprt make_binary(const exprt &expr)
3636
const typet &type=expr.type();
3737

3838
exprt previous=operands.front();
39-
assert(previous.type()==type);
39+
PRECONDITION(previous.type()==type);
4040

4141
for(exprt::operandst::const_iterator
4242
it=++operands.begin();
4343
it!=operands.end();
4444
++it)
4545
{
46-
assert(it->type()==type);
46+
PRECONDITION(it->type()==type);
4747

4848
exprt tmp=expr;
4949
tmp.operands().clear();
@@ -59,7 +59,7 @@ exprt make_binary(const exprt &expr)
5959
with_exprt make_with_expr(const update_exprt &src)
6060
{
6161
const exprt::operandst &designator=src.designator();
62-
assert(!designator.empty());
62+
PRECONDITION(!designator.empty());
6363

6464
with_exprt result;
6565
exprt *dest=&result;
@@ -78,7 +78,7 @@ with_exprt make_with_expr(const update_exprt &src)
7878
// to_member_designator(*it).get_component_name();
7979
}
8080
else
81-
assert(false);
81+
UNREACHABLE;
8282

8383
*dest=tmp;
8484
dest=&to_with_expr(*dest).new_value();
@@ -108,7 +108,7 @@ exprt is_not_zero(
108108
src_type.id()==ID_floatbv?ID_ieee_float_notequal:ID_notequal;
109109

110110
exprt zero=from_integer(0, src_type);
111-
assert(zero.is_not_nil());
111+
CHECK_RETURN(zero.is_not_nil());
112112

113113
binary_exprt comparison(src, id, zero, bool_typet());
114114
comparison.add_source_location()=src.source_location();
@@ -142,8 +142,8 @@ bool has_subexpr(const exprt &src, const irep_idt &id)
142142

143143
if_exprt lift_if(const exprt &src, std::size_t operand_number)
144144
{
145-
assert(operand_number<src.operands().size());
146-
assert(src.operands()[operand_number].id()==ID_if);
145+
PRECONDITION(operand_number<src.operands().size());
146+
PRECONDITION(src.operands()[operand_number].id()==ID_if);
147147

148148
const if_exprt if_expr=to_if_expr(src.operands()[operand_number]);
149149
const exprt true_case=if_expr.true_case();

src/util/irep.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ Author: Daniel Kroening, [email protected]
99
/// \file
1010
/// Internal Representation
1111

12-
#include <cassert>
1312
#include <ostream>
1413

14+
#include "invariant.h"
15+
1516
#include "string2int.h"
1617
#include "irep.h"
1718
#include "string_hash.h"
@@ -87,7 +88,7 @@ void irept::detach()
8788
remove_ref(old_data);
8889
}
8990

90-
assert(data->ref_count==1);
91+
POSTCONDITION(data->ref_count==1);
9192

9293
#ifdef IREP_DEBUG
9394
std::cout << "DETACH2: " << data << '\n';
@@ -105,7 +106,7 @@ void irept::remove_ref(dt *old_data)
105106
nonrecursive_destructor(old_data);
106107
#else
107108

108-
assert(old_data->ref_count!=0);
109+
PRECONDITION(old_data->ref_count!=0);
109110

110111
#ifdef IREP_DEBUG
111112
std::cout << "R: " << old_data << " " << old_data->ref_count << '\n';
@@ -147,7 +148,7 @@ void irept::nonrecursive_destructor(dt *old_data)
147148
if(d==&empty_d)
148149
continue;
149150

150-
assert(d->ref_count!=0);
151+
INVARIANT(d->ref_count!=0, "All contents of the stack must be in use");
151152
d->ref_count--;
152153

153154
if(d->ref_count==0)
@@ -456,7 +457,8 @@ bool irept::ordering(const irept &other) const
456457
return false;
457458
}
458459

459-
assert(it1==X.sub.end() && it2==Y.sub.end());
460+
INVARIANT(it1==X.sub.end() && it2==Y.sub.end(),
461+
"Unequal lengths will return before this");
460462
}
461463

462464
if(X.named_sub.size()<Y.named_sub.size())
@@ -484,7 +486,8 @@ bool irept::ordering(const irept &other) const
484486
return false;
485487
}
486488

487-
assert(it1==X.named_sub.end() && it2==Y.named_sub.end());
489+
INVARIANT(it1==X.named_sub.end() && it2==Y.named_sub.end(),
490+
"Unequal lengths will return before this");
488491
}
489492

490493
return false;
@@ -521,7 +524,8 @@ int irept::compare(const irept &i) const
521524
return r;
522525
}
523526

524-
assert(it1==get_sub().end() && it2==i.get_sub().end());
527+
INVARIANT(it1==get_sub().end() && it2==i.get_sub().end(),
528+
"Unequal lengths will return before this");
525529
}
526530

527531
const named_subt::size_type n_size=get_named_sub().size(),
@@ -549,8 +553,9 @@ int irept::compare(const irept &i) const
549553
return r;
550554
}
551555

552-
assert(it1==get_named_sub().end() &&
553-
it2==i.get_named_sub().end());
556+
INVARIANT(it1==get_named_sub().end() &&
557+
it2==i.get_named_sub().end(),
558+
"Unequal lengths will return before this");
554559
}
555560

556561
// equal

0 commit comments

Comments
 (0)