Skip to content

Commit 6149323

Browse files
Use diagnostics in boolbvt
instead of throw 0.
1 parent aba0fc9 commit 6149323

File tree

4 files changed

+51
-96
lines changed

4 files changed

+51
-96
lines changed

src/solvers/flattening/boolbv.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
191191
else if(expr.id()==ID_member)
192192
return convert_member(to_member_expr(expr));
193193
else if(expr.id()==ID_with)
194-
return convert_with(expr);
194+
return convert_with(to_with_expr(expr));
195195
else if(expr.id()==ID_update)
196196
return convert_update(expr);
197197
else if(expr.id()==ID_width)

src/solvers/flattening/boolbv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class boolbvt:public arrayst
159159
virtual bvt convert_floatbv_op(const exprt &expr);
160160
virtual bvt convert_floatbv_typecast(const floatbv_typecast_exprt &expr);
161161
virtual bvt convert_member(const member_exprt &expr);
162-
virtual bvt convert_with(const exprt &expr);
162+
virtual bvt convert_with(const with_exprt &expr);
163163
virtual bvt convert_update(const exprt &expr);
164164
virtual bvt convert_case(const exprt &expr);
165165
virtual bvt convert_cond(const cond_exprt &);

src/solvers/flattening/boolbv_constant.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@ bvt boolbvt::convert_constant(const constant_exprt &expr)
3131
{
3232
const bvt &tmp=convert_bv(*it);
3333

34-
if(tmp.size()!=op_width)
35-
{
36-
log.error().source_location = expr.find_source_location();
37-
log.error() << "convert_constant: unexpected operand width"
38-
<< messaget::eom;
39-
throw 0;
40-
}
34+
DATA_INVARIANT_WITH_DIAGNOSTICS(
35+
tmp.size() == op_width,
36+
"convert_constant: unexpected operand width",
37+
irep_pretty_diagnosticst{expr});
4138

4239
for(std::size_t j=0; j<op_width; j++)
4340
bv[offset+j]=tmp[j];
@@ -100,13 +97,10 @@ bvt boolbvt::convert_constant(const constant_exprt &expr)
10097
{
10198
const std::string &binary=id2string(expr.get_value());
10299

103-
if(binary.size()*2!=width)
104-
{
105-
log.error().source_location = expr.find_source_location();
106-
log.error() << "wrong value length in constant: " << expr.pretty()
107-
<< messaget::eom;
108-
throw 0;
109-
}
100+
DATA_INVARIANT_WITH_DIAGNOSTICS(
101+
binary.size() * 2 == width,
102+
"wrong value length in constant",
103+
irep_pretty_diagnosticst{expr});
110104

111105
for(std::size_t i=0; i<binary.size(); i++)
112106
{
@@ -136,10 +130,10 @@ bvt boolbvt::convert_constant(const constant_exprt &expr)
136130
break;
137131

138132
default:
139-
log.error().source_location = expr.find_source_location();
140-
log.error() << "unknown character in Verilog constant:" << expr.pretty()
141-
<< messaget::eom;
142-
throw 0;
133+
DATA_INVARIANT_WITH_DIAGNOSTICS(
134+
false,
135+
"unknown character in Verilog constant",
136+
irep_pretty_diagnosticst{expr});
143137
}
144138
}
145139

src/solvers/flattening/boolbv_with.cpp

Lines changed: 37 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,9 @@ Author: Daniel Kroening, [email protected]
1515

1616
#include "bv_endianness_map.h"
1717

18-
bvt boolbvt::convert_with(const exprt &expr)
18+
bvt boolbvt::convert_with(const with_exprt &expr)
1919
{
20-
if(expr.operands().size()<3)
21-
{
22-
log.error().source_location = expr.find_source_location();
23-
log.error() << "with takes at least three operands" << messaget::eom;
24-
throw 0;
25-
}
26-
27-
if((expr.operands().size()%2)!=1)
28-
{
29-
log.error().source_location = expr.find_source_location();
30-
log.error() << "with takes an odd number of operands" << messaget::eom;
31-
throw 0;
32-
}
33-
34-
bvt bv=convert_bv(expr.op0());
20+
bvt bv = convert_bv(expr.old());
3521

3622
std::size_t width=boolbv_width(expr.type());
3723

@@ -44,12 +30,10 @@ bvt boolbvt::convert_with(const exprt &expr)
4430
return conversion_failed(expr);
4531
}
4632

47-
if(bv.size()!=width)
48-
{
49-
log.error().source_location = expr.find_source_location();
50-
log.error() << "unexpected operand 0 width" << messaget::eom;
51-
throw 0;
52-
}
33+
DATA_INVARIANT_WITH_DIAGNOSTICS(
34+
bv.size() == width,
35+
"unexpected operand 0 width",
36+
irep_pretty_diagnosticst{expr});
5337

5438
bvt prev_bv;
5539
prev_bv.resize(width);
@@ -60,12 +44,7 @@ bvt boolbvt::convert_with(const exprt &expr)
6044
{
6145
bv.swap(prev_bv);
6246

63-
convert_with(
64-
expr.op0().type(),
65-
ops[op_no],
66-
ops[op_no+1],
67-
prev_bv,
68-
bv);
47+
convert_with(expr.old().type(), ops[op_no], ops[op_no + 1], prev_bv, bv);
6948
}
7049

7150
return bv;
@@ -100,9 +79,8 @@ void boolbvt::convert_with(
10079
return convert_with(
10180
ns.follow_tag(to_union_tag_type(type)), op1, op2, prev_bv, next_bv);
10281

103-
log.error().source_location = type.source_location();
104-
log.error() << "unexpected with type: " << type.id() << messaget::eom;
105-
throw 0;
82+
DATA_INVARIANT_WITH_DIAGNOSTICS(
83+
false, "unexpected with type", irep_pretty_diagnosticst{type});
10684
}
10785

10886
void boolbvt::convert_with_array(
@@ -112,36 +90,27 @@ void boolbvt::convert_with_array(
11290
const bvt &prev_bv,
11391
bvt &next_bv)
11492
{
115-
if(is_unbounded_array(type))
116-
{
117-
// can't do this
118-
log.error().source_location = type.source_location();
119-
log.error() << "convert_with_array called for unbounded array"
120-
<< messaget::eom;
121-
throw 0;
122-
}
93+
// can't do this
94+
DATA_INVARIANT_WITH_DIAGNOSTICS(
95+
!is_unbounded_array(type),
96+
"convert_with_array called for unbounded array",
97+
irep_pretty_diagnosticst{type});
12398

12499
const exprt &array_size=type.size();
125100

126101
const auto size = numeric_cast<mp_integer>(array_size);
127102

128-
if(!size.has_value())
129-
{
130-
log.error().source_location = type.source_location();
131-
log.error() << "convert_with_array expects constant array size"
132-
<< messaget::eom;
133-
throw 0;
134-
}
103+
DATA_INVARIANT_WITH_DIAGNOSTICS(
104+
size.has_value(),
105+
"convert_with_array expects constant array size",
106+
irep_pretty_diagnosticst{type});
135107

136108
const bvt &op2_bv=convert_bv(op2);
137109

138-
if(*size * op2_bv.size() != prev_bv.size())
139-
{
140-
log.error().source_location = type.source_location();
141-
log.error() << "convert_with_array: unexpected operand 2 width"
142-
<< messaget::eom;
143-
throw 0;
144-
}
110+
DATA_INVARIANT_WITH_DIAGNOSTICS(
111+
*size * op2_bv.size() == prev_bv.size(),
112+
"convert_with_array: unexpected operand 2 width",
113+
irep_pretty_diagnosticst{type});
145114

146115
// Is the index a constant?
147116
if(const auto op1_value = numeric_cast<mp_integer>(op1))
@@ -232,22 +201,17 @@ void boolbvt::convert_with_struct(
232201

233202
if(c.get_name() == component_name)
234203
{
235-
if(subtype != op2.type())
236-
{
237-
log.error().source_location = type.source_location();
238-
log.error() << "with/struct: component `" << component_name
239-
<< "' type does not match: " << subtype.pretty() << " vs. "
240-
<< op2.type().pretty() << messaget::eom;
241-
throw 0;
242-
}
243-
244-
if(sub_width!=op2_bv.size())
245-
{
246-
log.error().source_location = type.source_location();
247-
log.error() << "convert_with_struct: unexpected operand op2 width"
248-
<< messaget::eom;
249-
throw 0;
250-
}
204+
DATA_INVARIANT_WITH_DIAGNOSTICS(
205+
subtype == op2.type(),
206+
"with/struct: component `" + id2string(component_name) +
207+
"' type does not match",
208+
irep_pretty_diagnosticst{subtype},
209+
irep_pretty_diagnosticst{op2.type()});
210+
211+
DATA_INVARIANT_WITH_DIAGNOSTICS(
212+
sub_width == op2_bv.size(),
213+
"convert_with_struct: unexpected operand op2 width",
214+
irep_pretty_diagnosticst{type});
251215

252216
for(std::size_t i=0; i<sub_width; i++)
253217
next_bv[offset+i]=op2_bv[i];
@@ -269,13 +233,10 @@ void boolbvt::convert_with_union(
269233

270234
const bvt &op2_bv=convert_bv(op2);
271235

272-
if(next_bv.size()<op2_bv.size())
273-
{
274-
log.error().source_location = type.source_location();
275-
log.error() << "convert_with_union: unexpected operand op2 width"
276-
<< messaget::eom;
277-
throw 0;
278-
}
236+
DATA_INVARIANT_WITH_DIAGNOSTICS(
237+
next_bv.size() >= op2_bv.size(),
238+
"convert_with_union: unexpected operand op2 width",
239+
irep_pretty_diagnosticst{type});
279240

280241
if(config.ansi_c.endianness==configt::ansi_ct::endiannesst::IS_LITTLE_ENDIAN)
281242
{

0 commit comments

Comments
 (0)