Skip to content

Commit 6cc2ed2

Browse files
authored
Merge pull request #563 from diffblue/cassert-cleanout1
replace `assert(...)`
2 parents bd5891b + 8e2942e commit 6cc2ed2

File tree

8 files changed

+66
-65
lines changed

8 files changed

+66
-65
lines changed

src/ebmc/output_verilog.cpp

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@ Author: Daniel Kroening, kroening@kroening.com
66
77
\*******************************************************************/
88

9-
#include <cstdlib>
10-
#include <iostream>
9+
#include <verilog/expr2verilog_class.h>
1110

1211
#include <util/ebmc_util.h>
12+
#include <util/mathematical_expr.h>
1313
#include <util/simplify_expr.h>
1414

15+
#include <verilog/expr2verilog.h>
1516
#include <verilog/verilog_language.h>
16-
#include <verilog/verilog_typecheck.h>
1717
#include <verilog/verilog_synthesis.h>
18-
#include <verilog/expr2verilog_class.h>
19-
#include <verilog/expr2verilog.h>
18+
#include <verilog/verilog_typecheck.h>
2019

2120
#include "output_verilog.h"
2221

22+
#include <cstdlib>
23+
#include <iostream>
24+
2325
/*******************************************************************\
2426
2527
Function: output_verilog_baset::width
@@ -41,8 +43,8 @@ std::size_t output_verilog_baset::width(const typet &type)
4143
return to_bitvector_type(type).get_width();
4244

4345
std::cerr << type.id() << '\n';
44-
assert(false);
45-
46+
PRECONDITION(false);
47+
4648
return 0; // not reached
4749
}
4850

@@ -165,8 +167,10 @@ void output_verilog_netlistt::assign_symbol(
165167
rhs.id()==ID_xor ||
166168
rhs.id()==ID_xnor)
167169
{
168-
assert(rhs.type().id()==ID_bool);
169-
assert(lhs.type().id()==ID_bool);
170+
DATA_INVARIANT(
171+
rhs.type().id() == ID_bool, "boolean equivalence rhs must be boolean");
172+
DATA_INVARIANT(
173+
lhs.type().id() == ID_bool, "boolean equivalence lhs must be boolean");
170174

171175
std::string tmp;
172176

@@ -182,8 +186,10 @@ void output_verilog_netlistt::assign_symbol(
182186
}
183187
else if(rhs.id()==ID_not)
184188
{
185-
assert(rhs.type().id()==ID_bool);
186-
assert(lhs.type().id()==ID_bool);
189+
DATA_INVARIANT(
190+
rhs.type().id() == ID_bool, "boolean equivalence rhs must be boolean");
191+
DATA_INVARIANT(
192+
lhs.type().id() == ID_bool, "boolean equivalence lhs must be boolean");
187193

188194
std::string tmp = make_symbol_expr(to_not_expr(rhs).op(), "");
189195

@@ -201,7 +207,8 @@ void output_verilog_netlistt::assign_symbol(
201207
{
202208
std::string tmp;
203209

204-
assert(rhs.operands().size()!=0);
210+
DATA_INVARIANT(
211+
rhs.operands().size() != 0, "multi-ary operator must have operand");
205212

206213
if(rhs.operands().size()==2)
207214
tmp = make_symbol_expr(to_multi_ary_expr(rhs).op0(), "") + ", " +
@@ -420,7 +427,7 @@ std::string output_verilog_netlistt::symbol_string(const exprt &expr)
420427

421428
std::size_t offset = atoi(src.type().get("#offset").c_str());
422429

423-
assert(i>=offset);
430+
DATA_INVARIANT(i >= offset, "extractbit index must be in range");
424431

425432
return symbol_string(src) + '[' + integer2string(i - offset) + ']';
426433
}
@@ -440,10 +447,10 @@ std::string output_verilog_netlistt::symbol_string(const exprt &expr)
440447
auto to = from + width(expr.type());
441448
std::size_t offset = atoi(src.type().get("#offset").c_str());
442449

443-
assert(from>=offset);
444-
assert(to>=offset);
445-
446-
assert(to>=from);
450+
DATA_INVARIANT(from >= offset, "extractbits index must be in range");
451+
DATA_INVARIANT(to >= offset, "extractbits index must be in range");
452+
453+
DATA_INVARIANT(to >= from, "extractbits index must be in range");
447454

448455
return symbol_string(src) + '[' + integer2string(to - offset) + ':' +
449456
integer2string(from - offset) + ']';
@@ -804,7 +811,7 @@ Function: output_verilog_baset::module_instantiation
804811

805812
void output_verilog_baset::module_instantiation(const exprt &expr)
806813
{
807-
assert(expr.type().id()==ID_bool);
814+
PRECONDITION(expr.type().id() == ID_bool);
808815

809816
std::list<std::string> argument_strings;
810817

@@ -852,8 +859,8 @@ Function: output_verilog_baset::invariant
852859

853860
void output_verilog_baset::invariant(const exprt &expr)
854861
{
855-
assert(expr.type().id()==ID_bool);
856-
862+
PRECONDITION(expr.type().id() == ID_bool);
863+
857864
if(expr.id()==ID_and)
858865
{
859866
forall_operands(it, expr)
@@ -894,10 +901,9 @@ Function: output_verilog_baset::invariants
894901

895902
void output_verilog_baset::invariants(const symbolt &symbol)
896903
{
897-
assert(symbol.value.id()==ID_trans &&
898-
symbol.value.operands().size()==3);
904+
PRECONDITION(symbol.value.id() == ID_trans);
899905

900-
invariant(to_ternary_expr(symbol.value).op0());
906+
invariant(to_trans_expr(symbol.value).invar());
901907
}
902908

903909
/*******************************************************************\
@@ -914,8 +920,8 @@ Function: output_verilog_baset::next_state
914920

915921
void output_verilog_baset::next_state(const exprt &expr)
916922
{
917-
assert(expr.type().id()==ID_bool);
918-
923+
PRECONDITION(expr.type().id() == ID_bool);
924+
919925
if(expr.id()==ID_and)
920926
{
921927
forall_operands(it, expr)
@@ -925,7 +931,8 @@ void output_verilog_baset::next_state(const exprt &expr)
925931
else if(expr.is_true())
926932
return;
927933

928-
assert(expr.id()==ID_equal);
934+
DATA_INVARIANT(
935+
expr.id() == ID_equal, "next-state constraints must be equality");
929936

930937
auto &equal_expr = to_equal_expr(expr);
931938
assign_symbol(equal_expr.lhs(), equal_expr.rhs());
@@ -945,10 +952,9 @@ Function: output_verilog_baset::next_state
945952

946953
void output_verilog_baset::next_state(const symbolt &symbol)
947954
{
948-
assert(symbol.value.id()==ID_trans &&
949-
symbol.value.operands().size()==3);
955+
PRECONDITION(symbol.value.id() == ID_trans);
950956

951-
next_state(symbol.value.operands()[2]);
957+
next_state(to_trans_expr(symbol.value).trans());
952958
}
953959

954960
/*******************************************************************\

src/trans-netlist/aig_prop.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ Author: Daniel Kroening, kroening@kroening.com
99
#ifndef CPROVER_TRANS_NETLIST_AIG_PROP_H
1010
#define CPROVER_TRANS_NETLIST_AIG_PROP_H
1111

12-
#include <cassert>
12+
#include <util/invariant.h>
13+
#include <util/threeval.h>
1314

1415
#include <solvers/prop/prop.h>
15-
#include <util/threeval.h>
1616

1717
#include "aig.h"
1818

@@ -28,7 +28,10 @@ class aig_prop_baset : public propt {
2828
literalt lor(literalt a, literalt b) override;
2929
literalt land(const bvt &bv) override;
3030
literalt lor(const bvt &bv) override;
31-
void lcnf(const bvt &clause) override { assert(false); }
31+
void lcnf(const bvt &clause) override
32+
{
33+
PRECONDITION(false);
34+
}
3235
literalt lxor(literalt a, literalt b) override;
3336
literalt lxor(const bvt &bv) override;
3437
literalt lnand(literalt a, literalt b) override;
@@ -38,7 +41,10 @@ class aig_prop_baset : public propt {
3841
literalt lselect(literalt a, literalt b, literalt c) override; // a?b:c
3942
void set_equal(literalt a, literalt b) override;
4043

41-
void l_set_to(literalt a, bool value) override { assert(false); }
44+
void l_set_to(literalt a, bool value) override
45+
{
46+
PRECONDITION(false);
47+
}
4248

4349
literalt new_variable() override { return dest.new_node(); }
4450

@@ -49,12 +55,12 @@ class aig_prop_baset : public propt {
4955
}
5056

5157
tvt l_get(literalt a) const override {
52-
assert(0);
58+
PRECONDITION(false);
5359
return tvt::unknown();
5460
}
5561

5662
resultt prop_solve() {
57-
assert(0);
63+
PRECONDITION(false);
5864
return resultt::P_ERROR;
5965
}
6066

src/trans-netlist/trans_to_netlist.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ void convert_trans_to_netlistt::convert_lhs_rec(
532532
std::size_t from, std::size_t to,
533533
propt &prop)
534534
{
535-
assert(from<=to);
535+
PRECONDITION(from <= to);
536536

537537
if(expr.id()==ID_symbol)
538538
{
@@ -629,11 +629,11 @@ literalt convert_trans_to_netlistt::convert_rhs(
629629
instantiate_convert(
630630
prop, dest.var_map, rhs_entry.expr, ns,
631631
get_message_handler(), rhs_entry.bv);
632-
633-
assert(rhs_entry.bv.size()==rhs_entry.width);
632+
633+
DATA_INVARIANT(rhs_entry.bv.size() == rhs_entry.width, "bit-width match");
634634
}
635635

636-
assert(rhs.bit_number<rhs_entry.bv.size());
636+
DATA_INVARIANT(rhs.bit_number < rhs_entry.bv.size(), "bit index in range");
637637
return rhs_entry.bv[rhs.bit_number];
638638
}
639639

@@ -665,12 +665,12 @@ void convert_trans_to_netlistt::add_equality(const equal_exprt &src)
665665
constraint_list.push_back(src);
666666
return;
667667
}
668-
669-
assert(rhs_entry.width!=0);
668+
669+
DATA_INVARIANT(rhs_entry.width != 0, "no empty entries");
670670

671671
std::size_t lhs_width=boolbv_width(lhs.type());
672672

673-
assert(lhs_width==rhs_entry.width);
673+
DATA_INVARIANT(lhs_width == rhs_entry.width, "bit-width match");
674674

675675
add_equality_rec(src, lhs, 0, lhs_width-1, rhs_entry);
676676
}
@@ -693,8 +693,8 @@ void convert_trans_to_netlistt::add_equality_rec(
693693
std::size_t lhs_from, std::size_t lhs_to,
694694
rhs_entryt &rhs_entry)
695695
{
696-
assert(lhs_from<=lhs_to);
697-
696+
PRECONDITION(lhs_from <= lhs_to);
697+
698698
if(lhs.id()==ID_next_symbol ||
699699
lhs.id()==ID_symbol)
700700
{
@@ -730,11 +730,11 @@ void convert_trans_to_netlistt::add_equality_rec(
730730
}
731731
else if(lhs.id()==ID_extractbit)
732732
{
733-
assert(lhs_to==lhs_from);
733+
PRECONDITION(lhs_to == lhs_from);
734734

735735
mp_integer i;
736736
if(to_integer_non_constant(to_extractbit_expr(lhs).index(), i))
737-
assert(false);
737+
PRECONDITION(false);
738738

739739
lhs_from = lhs_from + i.to_ulong();
740740
add_equality_rec(

src/trans-word-level/counterexample_word_level.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ Author: Daniel Kroening, kroening@kroening.com
66
77
\*******************************************************************/
88

9-
#include <cassert>
109
#include <iostream>
1110

1211
#include <langapi/language_util.h>

src/trans-word-level/instantiate_word_level.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ Author: Daniel Kroening, kroening@kroening.com
1616

1717
#include "property.h"
1818

19-
#include <cassert>
20-
2119
/*******************************************************************\
2220
2321
Function: timeframe_identifier
@@ -166,7 +164,7 @@ wl_instantiatet::instantiate_rec(exprt expr, const mp_integer &t) const
166164

167165
if(sva_cycle_delay_expr.to().id() == ID_infinity)
168166
{
169-
assert(no_timeframes != 0);
167+
DATA_INVARIANT(no_timeframes != 0, "must have timeframe");
170168
to = no_timeframes - 1;
171169
}
172170
else if(to_integer_non_constant(sva_cycle_delay_expr.to(), to))
@@ -283,9 +281,6 @@ wl_instantiatet::instantiate_rec(exprt expr, const mp_integer &t) const
283281
expr.id()==ID_sva_s_until)
284282
{
285283
// non-overlapping until
286-
287-
assert(expr.operands().size()==2);
288-
289284
// we need a lasso to refute these
290285

291286
// we expand: p U q <=> q || (p && X(p U q))
@@ -308,8 +303,6 @@ wl_instantiatet::instantiate_rec(exprt expr, const mp_integer &t) const
308303
expr.id()==ID_sva_s_until_with)
309304
{
310305
// overlapping until
311-
312-
assert(expr.operands().size()==2);
313306

314307
// we rewrite using 'next'
315308
binary_exprt tmp = to_binary_expr(expr);

src/verilog/verilog_typecheck_base.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ Author: Daniel Kroening, kroening@kroening.com
1616
#include "expr2verilog.h"
1717
#include "verilog_types.h"
1818

19-
#include <cassert>
20-
2119
/*******************************************************************\
2220
2321
Function: verilog_module_symbol
@@ -50,8 +48,10 @@ Function: strip_verilog_prefix
5048
irep_idt strip_verilog_prefix(const irep_idt &identifier)
5149
{
5250
std::string prefix="Verilog::";
53-
assert(has_prefix(id2string(identifier), prefix));
54-
assert(identifier.size()>=prefix.size());
51+
DATA_INVARIANT(
52+
has_prefix(id2string(identifier), prefix), "Verilog identifier syntax");
53+
DATA_INVARIANT(
54+
identifier.size() >= prefix.size(), "Verilog identifier syntax");
5555
return identifier.c_str()+prefix.size();
5656
}
5757

src/verilog/verilog_typecheck_expr.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,7 @@ void verilog_typecheck_exprt::implicit_typecast(
15531553
{
15541554
const std::string &value=expr.get_string(ID_value);
15551555
// least significant bit is last
1556-
assert(value.size()!=0);
1556+
DATA_INVARIANT(value.size() != 0, "no empty bitvector");
15571557
expr = make_boolean_expr(value[value.size() - 1] == '1');
15581558
return;
15591559
}
@@ -1860,7 +1860,6 @@ exprt verilog_typecheck_exprt::convert_unary_expr(unary_exprt expr)
18601860
expr.id() == ID_sva_cycle_delay_star || expr.id() == ID_sva_weak ||
18611861
expr.id() == ID_sva_strong)
18621862
{
1863-
assert(expr.operands().size()==1);
18641863
convert_expr(expr.op());
18651864
make_boolean(expr.op());
18661865
expr.type()=bool_typet();
@@ -2163,7 +2162,7 @@ exprt verilog_typecheck_exprt::convert_binary_expr(binary_exprt expr)
21632162
else if(expr.id()==ID_ashr)
21642163
{
21652164
// would only happen when re-typechecking, otherwise see above
2166-
assert(0);
2165+
DATA_INVARIANT(false, "no re-typechecking");
21672166
}
21682167
else if(expr.id()==ID_lshr)
21692168
{
@@ -2452,7 +2451,6 @@ exprt verilog_typecheck_exprt::convert_trinary_expr(ternary_exprt expr)
24522451
else if(expr.id()==ID_sva_cycle_delay) // #[1:2] something
24532452
{
24542453
expr.type()=bool_typet();
2455-
assert(expr.operands().size()==3);
24562454
convert_expr(expr.op0());
24572455
if(expr.op1().is_not_nil()) convert_expr(expr.op1());
24582456
convert_expr(expr.op2());

0 commit comments

Comments
 (0)