Skip to content

Commit fe40b19

Browse files
Use invariant instead of printing error message to cerr
1 parent aaf9477 commit fe40b19

File tree

5 files changed

+37
-66
lines changed

5 files changed

+37
-66
lines changed

src/solvers/flattening/boolbv_add_sub.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ Author: Daniel Kroening, [email protected]
88

99
#include "boolbv.h"
1010

11-
#include <iostream>
12-
11+
#include <util/invariant.h>
1312
#include <util/std_types.h>
1413

1514
#include "../floatbv/float_utils.h"
@@ -38,12 +37,8 @@ bvt boolbvt::convert_add_sub(const exprt &expr)
3837
throw "operator "+expr.id_string()+" takes at least one operand";
3938

4039
const exprt &op0=expr.op0();
41-
42-
if(op0.type()!=type)
43-
{
44-
std::cerr << expr.pretty() << '\n';
45-
throw "add/sub with mixed types";
46-
}
40+
DATA_INVARIANT(
41+
op0.type() == type, "add/sub with mixed types:\n" + expr.pretty());
4742

4843
bvt bv=convert_bv(op0);
4944

@@ -69,11 +64,8 @@ bvt boolbvt::convert_add_sub(const exprt &expr)
6964
it=operands.begin()+1;
7065
it!=operands.end(); it++)
7166
{
72-
if(it->type()!=type)
73-
{
74-
std::cerr << expr.pretty() << '\n';
75-
throw "add/sub with mixed types";
76-
}
67+
DATA_INVARIANT(
68+
it->type() == type, "add/sub with mixed types:\n" + expr.pretty());
7769

7870
const bvt &op=convert_bv(*it);
7971

src/solvers/flattening/boolbv_case.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Author: Daniel Kroening, [email protected]
88

99
#include "boolbv.h"
1010

11-
#include <iostream>
11+
#include <util/invariant.h>
1212

1313
bvt boolbvt::convert_case(const exprt &expr)
1414
{
@@ -49,15 +49,11 @@ bvt boolbvt::convert_case(const exprt &expr)
4949
break;
5050

5151
case COMPARE:
52-
if(compare_bv.size()!=op.size())
53-
{
54-
std::cerr << "compare operand: " << compare_bv.size()
55-
<< "\noperand: " << op.size() << '\n'
56-
<< it->pretty()
57-
<< '\n';
58-
59-
throw "size of compare operand does not match";
60-
}
52+
DATA_INVARIANT(
53+
compare_bv.size() == op.size(),
54+
std::string("size of compare operand does not match:\n") +
55+
"compare operand: " + std::to_string(compare_bv.size()) +
56+
"\noperand: " + std::to_string(op.size()) + '\n' + it->pretty());
6157

6258
compare_literal=bv_utils.equal(compare_bv, op);
6359
compare_literal=prop.land(!previous_compare, compare_literal);
@@ -68,15 +64,11 @@ bvt boolbvt::convert_case(const exprt &expr)
6864
break;
6965

7066
case VALUE:
71-
if(bv.size()!=op.size())
72-
{
73-
std::cerr << "result size: " << bv.size()
74-
<< "\noperand: " << op.size() << '\n'
75-
<< it->pretty()
76-
<< '\n';
77-
78-
throw "size of value operand does not match";
79-
}
67+
DATA_INVARIANT(
68+
bv.size() == op.size(),
69+
std::string("size of value operand does not match:\n") +
70+
"result size: " + std::to_string(bv.size()) +
71+
"\noperand: " + std::to_string(op.size()) + '\n' + it->pretty());
8072

8173
{
8274
literalt value_literal=bv_utils.equal(bv, op);

src/solvers/flattening/boolbv_cond.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Author: Daniel Kroening, [email protected]
88

99
#include "boolbv.h"
1010

11-
#include <iostream>
11+
#include <util/invariant.h>
1212

1313
bvt boolbvt::convert_cond(const exprt &expr)
1414
{
@@ -51,15 +51,11 @@ bvt boolbvt::convert_cond(const exprt &expr)
5151
{
5252
const bvt &op=convert_bv(*it);
5353

54-
if(bv.size()!=op.size())
55-
{
56-
std::cerr << "result size: " << bv.size()
57-
<< "\noperand: " << op.size() << '\n'
58-
<< it->pretty()
59-
<< '\n';
60-
61-
throw "size of value operand does not match";
62-
}
54+
DATA_INVARIANT(
55+
bv.size() == op.size(),
56+
std::string("size of value operand does not match:\n") +
57+
"result size: " + std::to_string(bv.size()) +
58+
"\noperand: " + std::to_string(op.size()) + '\n' + it->pretty());
6359

6460
literalt value_literal=bv_utils.equal(bv, op);
6561

src/solvers/flattening/boolbv_equality.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ Author: Daniel Kroening, [email protected]
88

99
#include "boolbv.h"
1010

11-
#include <iostream>
12-
1311
#include <util/std_expr.h>
1412
#include <util/base_type.h>
1513
#include <util/invariant.h>
@@ -44,14 +42,12 @@ literalt boolbvt::convert_equality(const equal_exprt &expr)
4442
const bvt &bv0=convert_bv(expr.lhs());
4543
const bvt &bv1=convert_bv(expr.rhs());
4644

47-
if(bv0.size()!=bv1.size())
48-
{
49-
std::cerr << "lhs: " << expr.lhs().pretty() << '\n';
50-
std::cerr << "lhs size: " << bv0.size() << '\n';
51-
std::cerr << "rhs: " << expr.rhs().pretty() << '\n';
52-
std::cerr << "rhs size: " << bv1.size() << '\n';
53-
throw "unexpected size mismatch on equality";
54-
}
45+
DATA_INVARIANT(
46+
bv0.size() == bv1.size(),
47+
std::string("unexpected size mismatch on equality:\n") + "lhs: " +
48+
expr.lhs().pretty() + '\n' + "lhs size: " + std::to_string(bv0.size()) +
49+
'\n' + "rhs: " + expr.rhs().pretty() + '\n' +
50+
"rhs size: " + std::to_string(bv1.size()));
5551

5652
if(bv0.empty())
5753
{
@@ -80,14 +76,12 @@ literalt boolbvt::convert_verilog_case_equality(
8076
const bvt &bv0=convert_bv(expr.lhs());
8177
const bvt &bv1=convert_bv(expr.rhs());
8278

83-
if(bv0.size()!=bv1.size())
84-
{
85-
std::cerr << "lhs: " << expr.lhs().pretty() << '\n';
86-
std::cerr << "lhs size: " << bv0.size() << '\n';
87-
std::cerr << "rhs: " << expr.rhs().pretty() << '\n';
88-
std::cerr << "rhs size: " << bv1.size() << '\n';
89-
throw "unexpected size mismatch on verilog_case_equality";
90-
}
79+
DATA_INVARIANT(
80+
bv0.size() == bv1.size(),
81+
std::string("unexpected size mismatch on verilog_case_equality:\n") +
82+
"lhs: " + expr.lhs().pretty() + '\n' + "lhs size: " +
83+
std::to_string(bv0.size()) + '\n' + "rhs: " + expr.rhs().pretty() + '\n' +
84+
"rhs size: " + std::to_string(bv1.size()));
9185

9286
if(expr.id()==ID_verilog_case_inequality)
9387
return !bv_utils.equal(bv0, bv1);

src/solvers/flattening/boolbv_floatbv_op.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,9 @@ bvt boolbvt::convert_floatbv_op(const exprt &expr)
8888
bvt bv2=convert_bv(op2);
8989

9090
const typet &type=ns.follow(expr.type());
91-
92-
if(op0.type()!=type || op1.type()!=type)
93-
{
94-
std::cerr << expr.pretty() << '\n';
95-
throw "float op with mixed types";
96-
}
91+
DATA_INVARIANT(
92+
op0.type() == type && op1.type() == type,
93+
"float op with mixed types:\n" + expr.pretty());
9794

9895
float_utilst float_utils(prop);
9996

0 commit comments

Comments
 (0)