Skip to content

Commit 1e40c6c

Browse files
authored
Merge pull request #2950 from danpoe/refactor/error-handling-solvers-prop
Error handling cleanup in solvers/prop
2 parents 6b7b29a + b98ed58 commit 1e40c6c

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

src/solvers/prop/bdd_expr.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ Author: Michael Tautschnig, [email protected]
1111

1212
#include "bdd_expr.h"
1313

14-
#include <util/std_expr.h>
1514
#include <util/expr_util.h>
1615
#include <util/format_expr.h>
16+
#include <util/invariant.h>
17+
#include <util/std_expr.h>
1718

1819
#include <sstream>
1920

2021
mini_bddt bdd_exprt::from_expr_rec(const exprt &expr)
2122
{
22-
assert(expr.type().id()==ID_bool);
23+
PRECONDITION(expr.type().id() == ID_bool);
2324

2425
if(expr.is_constant())
2526
return expr.is_false() ? bdd_mgr.False() : bdd_mgr.True();
@@ -29,7 +30,9 @@ mini_bddt bdd_exprt::from_expr_rec(const exprt &expr)
2930
expr.id()==ID_or ||
3031
expr.id()==ID_xor)
3132
{
32-
assert(expr.operands().size()>=2);
33+
DATA_INVARIANT(
34+
expr.operands().size() >= 2,
35+
"logical and, or, and xor expressions have at least two operands");
3336
exprt bin_expr=make_binary(expr);
3437

3538
mini_bddt op0=from_expr_rec(bin_expr.op0());
@@ -103,7 +106,7 @@ exprt bdd_exprt::as_expr(const mini_bddt &r) const
103106
}
104107

105108
node_mapt::const_iterator entry=node_map.find(r.var());
106-
assert(entry!=node_map.end());
109+
CHECK_RETURN(entry != node_map.end());
107110
const exprt &n_expr=entry->second;
108111

109112
if(r.low().is_false())

src/solvers/prop/literal_expr.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ class literal_exprt:public predicate_exprt
4848
*/
4949
inline const literal_exprt &to_literal_expr(const exprt &expr)
5050
{
51-
assert(expr.id()==ID_literal && !expr.has_operands());
51+
PRECONDITION(expr.id() == ID_literal);
52+
DATA_INVARIANT(
53+
!expr.has_operands(), "literal expression should not have operands");
5254
return static_cast<const literal_exprt &>(expr);
5355
}
5456

@@ -57,7 +59,9 @@ inline const literal_exprt &to_literal_expr(const exprt &expr)
5759
*/
5860
inline literal_exprt &to_literal_expr(exprt &expr)
5961
{
60-
assert(expr.id()==ID_literal && !expr.has_operands());
62+
PRECONDITION(expr.id() == ID_literal);
63+
DATA_INVARIANT(
64+
!expr.has_operands(), "literal expression should not have operands");
6165
return static_cast<literal_exprt &>(expr);
6266
}
6367

src/solvers/prop/minimize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ literalt prop_minimizet::constraint()
9393
void prop_minimizet::operator()()
9494
{
9595
// we need to use assumptions
96-
assert(prop_conv.has_set_assumptions());
96+
PRECONDITION(prop_conv.has_set_assumptions());
9797

9898
_iterations=0;
9999
_number_satisfied=0;

src/solvers/prop/prop_conv.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void prop_convt::set_frozen(const bvt &bv)
3535

3636
bool prop_conv_solvert::literal(const symbol_exprt &expr, literalt &dest) const
3737
{
38-
assert(expr.type().id()==ID_bool);
38+
PRECONDITION(expr.type().id() == ID_bool);
3939

4040
const irep_idt &identifier = expr.get_identifier();
4141

@@ -197,10 +197,13 @@ literalt prop_conv_solvert::convert_bool(const exprt &expr)
197197
{
198198
if(expr.is_true())
199199
return const_literal(true);
200-
else if(expr.is_false())
201-
return const_literal(false);
202200
else
203-
throw "unknown boolean constant: "+expr.pretty();
201+
{
202+
INVARIANT(
203+
expr.is_false(),
204+
"constant expression of type bool should be either true or false");
205+
return const_literal(false);
206+
}
204207
}
205208
else if(expr.id()==ID_symbol)
206209
{
@@ -216,22 +219,23 @@ literalt prop_conv_solvert::convert_bool(const exprt &expr)
216219
}
217220
else if(expr.id()==ID_implies)
218221
{
219-
if(op.size()!=2)
220-
throw "implication takes two operands";
221-
222-
return prop.limplies(convert(op[0]), convert(op[1]));
222+
const implies_exprt &implies_expr = to_implies_expr(expr);
223+
return prop.limplies(
224+
convert(implies_expr.op0()), convert(implies_expr.op1()));
223225
}
224226
else if(expr.id()==ID_if)
225227
{
226-
if(op.size()!=3)
227-
throw "if takes three operands";
228-
229-
return prop.lselect(convert(op[0]), convert(op[1]), convert(op[2]));
228+
const if_exprt &if_expr = to_if_expr(expr);
229+
return prop.lselect(
230+
convert(if_expr.cond()),
231+
convert(if_expr.true_case()),
232+
convert(if_expr.false_case()));
230233
}
231234
else if(expr.id()==ID_constraint_select_one)
232235
{
233-
if(op.size()<2)
234-
throw "constraint_select_one takes at least two operands";
236+
DATA_INVARIANT(
237+
op.size() >= 2,
238+
"constraint_select_one should have at least two operands");
235239

236240
std::vector<literalt> op_bv;
237241
op_bv.resize(op.size());
@@ -427,9 +431,10 @@ void prop_conv_solvert::set_to(const exprt &expr, bool value)
427431
// set_to_false
428432
if(expr.id()==ID_implies) // !(a=>b) == (a && !b)
429433
{
430-
assert(expr.operands().size()==2);
431-
set_to_true(expr.op0());
432-
set_to_false(expr.op1());
434+
const implies_exprt &implies_expr = to_implies_expr(expr);
435+
436+
set_to_true(implies_expr.op0());
437+
set_to_false(implies_expr.op1());
433438
return;
434439
}
435440
else if(expr.id()==ID_or) // !(a || b) == (!a && !b)

0 commit comments

Comments
 (0)