Skip to content

Commit af0eb45

Browse files
Replace throws with invariants in boolbv_extractbits
Also removes one throw that guards against violation of extractbits_expr invariants
1 parent ad1b927 commit af0eb45

File tree

1 file changed

+17
-29
lines changed

1 file changed

+17
-29
lines changed

src/solvers/flattening/boolbv_extractbits.cpp

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ bvt boolbvt::convert_extractbits(const extractbits_exprt &expr)
1717
if(bv_width == 0)
1818
return conversion_failed(expr);
1919

20-
if(expr.operands().size()!=3)
21-
{
22-
error().source_location=expr.find_source_location();
23-
error() << "extractbits takes three operands" << eom;
24-
throw 0;
25-
}
26-
2720
auto const &src_bv = convert_bv(expr.src());
2821

2922
auto const maybe_upper_as_int = numeric_cast<mp_integer>(expr.upper());
@@ -37,34 +30,29 @@ bvt boolbvt::convert_extractbits(const extractbits_exprt &expr)
3730
auto upper_as_int = maybe_upper_as_int.value();
3831
auto lower_as_int = maybe_lower_as_int.value();
3932

40-
if(upper_as_int < 0 || upper_as_int >= src_bv.size())
41-
{
42-
error().source_location=expr.find_source_location();
43-
error() << "extractbits: second operand out of range: "
44-
<< expr.pretty() << eom;
45-
}
46-
47-
if(lower_as_int < 0 || lower_as_int >= src_bv.size())
48-
{
49-
error().source_location=expr.find_source_location();
50-
error() << "extractbits: third operand out of range: "
51-
<< expr.pretty() << eom;
52-
throw 0;
53-
}
33+
DATA_INVARIANT_WITH_DIAGNOSTICS(
34+
upper_as_int >= 0 && upper_as_int < src_bv.size(),
35+
"upper end of extracted bits must be within the bitvector",
36+
expr.find_source_location(),
37+
irep_pretty_diagnosticst{expr});
38+
39+
DATA_INVARIANT_WITH_DIAGNOSTICS(
40+
lower_as_int >= 0 && lower_as_int < src_bv.size(),
41+
"lower end of extracted bits must be within the bitvector",
42+
expr.find_source_location(),
43+
irep_pretty_diagnosticst{expr});
5444

5545
if(lower_as_int > upper_as_int)
5646
std::swap(upper_as_int, lower_as_int);
5747

5848
// now lower_as_int <= upper_as_int
5949

60-
if((upper_as_int - lower_as_int + 1) != bv_width)
61-
{
62-
error().source_location=expr.find_source_location();
63-
error() << "extractbits: wrong width (expected "
64-
<< (upper_as_int - lower_as_int + 1) << " but got " << bv_width
65-
<< "): " << expr.pretty() << eom;
66-
throw 0;
67-
}
50+
DATA_INVARIANT_WITH_DIAGNOSTICS(
51+
(upper_as_int - lower_as_int + 1) == bv_width,
52+
"the difference between upper and lower end of the range must have the "
53+
"same width as the resulting bitvector type",
54+
expr.find_source_location(),
55+
irep_pretty_diagnosticst{expr});
6856

6957
const std::size_t offset = integer2unsigned(lower_as_int);
7058

0 commit comments

Comments
 (0)