Skip to content

Commit 8f28c2f

Browse files
author
Sonny Martin
committed
Invariant cleanup
1 parent b0a0002 commit 8f28c2f

File tree

6 files changed

+40
-35
lines changed

6 files changed

+40
-35
lines changed

src/util/mp_arith.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Author: Daniel Kroening, kroening@kroening.com
88

99
#include "mp_arith.h"
1010

11-
#include <cassert>
1211
#include <cctype>
1312
#include <cstdlib>
1413
#include <limits>
@@ -257,9 +256,9 @@ mp_integer arith_left_shift(
257256
std::size_t true_size)
258257
{
259258
PRECONDITION(a.is_long() && b.is_ulong());
259+
PRECONDITION(b <= true_size || a == 0);
260+
260261
ullong_t shift=b.to_ulong();
261-
if(shift>true_size && a!=mp_integer(0))
262-
throw "shift value out of range";
263262

264263
llong_t result=a.to_long()<<shift;
265264
llong_t mask=
@@ -280,8 +279,7 @@ mp_integer arith_right_shift(
280279
PRECONDITION(a.is_long() && b.is_ulong());
281280
llong_t number=a.to_long();
282281
ullong_t shift=b.to_ulong();
283-
if(shift>true_size)
284-
throw "shift value out of range";
282+
PRECONDITION(shift <= true_size);
285283

286284
const llong_t sign = (1LL << (true_size - 1)) & number;
287285
const llong_t pad = (sign == 0) ? 0 : ~((1LL << (true_size - shift)) - 1);
@@ -298,9 +296,9 @@ mp_integer logic_left_shift(
298296
std::size_t true_size)
299297
{
300298
PRECONDITION(a.is_long() && b.is_ulong());
299+
PRECONDITION(b <= true_size || a == 0);
300+
301301
ullong_t shift=b.to_ulong();
302-
if(shift>true_size && a!=mp_integer(0))
303-
throw "shift value out of range";
304302
llong_t result=a.to_long()<<shift;
305303
if(true_size<(sizeof(llong_t)*8))
306304
{
@@ -324,10 +322,9 @@ mp_integer logic_right_shift(
324322
std::size_t true_size)
325323
{
326324
PRECONDITION(a.is_long() && b.is_ulong());
327-
ullong_t shift=b.to_ulong();
328-
if(shift>true_size)
329-
throw "shift value out of range";
325+
PRECONDITION(b <= true_size);
330326

327+
ullong_t shift = b.to_ulong();
331328
ullong_t result=((ullong_t)a.to_long()) >> shift;
332329
return result;
333330
}
@@ -341,10 +338,10 @@ mp_integer rotate_right(
341338
std::size_t true_size)
342339
{
343340
PRECONDITION(a.is_ulong() && b.is_ulong());
341+
PRECONDITION(b <= true_size);
342+
344343
ullong_t number=a.to_ulong();
345344
ullong_t shift=b.to_ulong();
346-
if(shift>true_size)
347-
throw "shift value out of range";
348345

349346
ullong_t revShift=true_size-shift;
350347
const ullong_t filter = 1ULL << (true_size - 1);
@@ -361,10 +358,10 @@ mp_integer rotate_left(
361358
std::size_t true_size)
362359
{
363360
PRECONDITION(a.is_ulong() && b.is_ulong());
361+
PRECONDITION(b <= true_size);
362+
364363
ullong_t number=a.to_ulong();
365364
ullong_t shift=b.to_ulong();
366-
if(shift>true_size)
367-
throw "shift value out of range";
368365

369366
ullong_t revShift=true_size-shift;
370367
const ullong_t filter = 1ULL << (true_size - 1);

src/util/namespace.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ Author: Daniel Kroening, kroening@kroening.com
1313

1414
#include <algorithm>
1515

16-
#include <cassert>
17-
1816
#include "prefix.h"
1917
#include "std_expr.h"
2018
#include "std_types.h"
@@ -80,24 +78,27 @@ const typet &namespace_baset::follow(const typet &src) const
8078
const typet &namespace_baset::follow_tag(const union_tag_typet &src) const
8179
{
8280
const symbolt &symbol=lookup(src.get_identifier());
83-
assert(symbol.is_type);
84-
assert(symbol.type.id()==ID_union || symbol.type.id()==ID_incomplete_union);
81+
CHECK_RETURN(symbol.is_type);
82+
CHECK_RETURN(
83+
symbol.type.id() == ID_union || symbol.type.id() == ID_incomplete_union);
8584
return symbol.type;
8685
}
8786

8887
const typet &namespace_baset::follow_tag(const struct_tag_typet &src) const
8988
{
9089
const symbolt &symbol=lookup(src.get_identifier());
91-
assert(symbol.is_type);
92-
assert(symbol.type.id()==ID_struct || symbol.type.id()==ID_incomplete_struct);
90+
CHECK_RETURN(symbol.is_type);
91+
CHECK_RETURN(
92+
symbol.type.id() == ID_struct || symbol.type.id() == ID_incomplete_struct);
9393
return symbol.type;
9494
}
9595

9696
const typet &namespace_baset::follow_tag(const c_enum_tag_typet &src) const
9797
{
9898
const symbolt &symbol=lookup(src.get_identifier());
99-
assert(symbol.is_type);
100-
assert(symbol.type.id()==ID_c_enum || symbol.type.id()==ID_incomplete_c_enum);
99+
CHECK_RETURN(symbol.is_type);
100+
CHECK_RETURN(
101+
symbol.type.id() == ID_c_enum || symbol.type.id() == ID_incomplete_c_enum);
101102
return symbol.type;
102103
}
103104

src/util/namespace.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Author: Daniel Kroening, kroening@kroening.com
1010
#ifndef CPROVER_UTIL_NAMESPACE_H
1111
#define CPROVER_UTIL_NAMESPACE_H
1212

13+
#include "invariant.h"
1314
#include "irep.h"
1415

1516
class symbol_tablet;
@@ -33,8 +34,12 @@ class namespace_baset
3334
const symbolt &lookup(const irep_idt &name) const
3435
{
3536
const symbolt *symbol;
36-
if(lookup(name, symbol))
37-
throw "identifier "+id2string(name)+" not found";
37+
bool not_found = lookup(name, symbol);
38+
INVARIANT(
39+
!not_found,
40+
"we are assuming that a name exists in the namespace "
41+
"when this function is called - identifier " +
42+
id2string(name) + " was not found");
3843
return *symbol;
3944
}
4045

src/util/pointer_predicates.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ exprt object_lower_bound(
226226
exprt p_offset=pointer_offset(pointer);
227227

228228
exprt zero=from_integer(0, p_offset.type());
229-
assert(zero.is_not_nil());
229+
CHECK_RETURN(zero.is_not_nil());
230230

231231
if(offset.is_not_nil())
232232
{

src/util/rational.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Author: Daniel Kroening, kroening@kroening.com
1111

1212
#include "rational.h"
1313

14+
#include "invariant.h"
15+
1416
#include <algorithm>
1517
#include <ostream>
1618

@@ -48,7 +50,7 @@ rationalt &rationalt::operator*=(const rationalt &n)
4850

4951
rationalt &rationalt::operator/=(const rationalt &n)
5052
{
51-
assert(!n.numerator.is_zero());
53+
PRECONDITION(!n.numerator.is_zero());
5254
numerator*=n.denominator;
5355
denominator*=n.numerator;
5456
normalize();
@@ -90,7 +92,7 @@ void rationalt::same_denominator(rationalt &n)
9092

9193
void rationalt::invert()
9294
{
93-
assert(numerator!=0);
95+
PRECONDITION(numerator != 0);
9496
std::swap(numerator, denominator);
9597
}
9698

src/util/reference_counting.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Author: Daniel Kroening, kroening@kroening.com
1212
#ifndef CPROVER_UTIL_REFERENCE_COUNTING_H
1313
#define CPROVER_UTIL_REFERENCE_COUNTING_H
1414

15-
#include <cassert>
15+
#include "invariant.h"
1616

1717
template<typename T>
1818
// NOLINTNEXTLINE(readability/identifiers)
@@ -33,7 +33,7 @@ class reference_counting
3333
{
3434
if(d!=nullptr)
3535
{
36-
assert(d->ref_count!=0);
36+
PRECONDITION(d->ref_count != 0);
3737
d->ref_count++;
3838
#ifdef REFERENCE_COUNTING_DEBUG
3939
std::cout << "COPY " << d << " " << d->ref_count << '\n';
@@ -96,9 +96,9 @@ class reference_counting
9696

9797
void copy_from(const reference_counting &other)
9898
{
99-
assert(&other!=this); // check if we assign to ourselves
99+
PRECONDITION(&other != this); // check if we assign to ourselves
100100

101-
#ifdef REFERENCE_COUNTING_DEBUG
101+
#ifdef REFERENCE_COUNTING_DEBUG
102102
std::cout << "COPY " << (&other) << "\n";
103103
#endif
104104

@@ -121,9 +121,9 @@ void reference_counting<T>::remove_ref(dt *old_d)
121121
if(old_d==nullptr)
122122
return;
123123

124-
assert(old_d->ref_count!=0);
124+
PRECONDITION(old_d->ref_count != 0);
125125

126-
#ifdef REFERENCE_COUNTING_DEBUG
126+
#ifdef REFERENCE_COUNTING_DEBUG
127127
std::cout << "R: " << old_d << " " << old_d->ref_count << '\n';
128128
#endif
129129

@@ -172,9 +172,9 @@ void reference_counting<T>::detach()
172172
remove_ref(old_d);
173173
}
174174

175-
assert(d->ref_count==1);
175+
POSTCONDITION(d->ref_count == 1);
176176

177-
#ifdef REFERENCE_COUNTING_DEBUG
177+
#ifdef REFERENCE_COUNTING_DEBUG
178178
std::cout << "DETACH2: " << d << '\n'
179179
#endif
180180
}

0 commit comments

Comments
 (0)