Skip to content

Commit bf04f20

Browse files
committed
Add optional expected_width parameter to convert_bv
1 parent 20f05af commit bf04f20

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

src/solvers/flattening/boolbv.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ bool boolbvt::literal(
113113
throw "found no literal for expression";
114114
}
115115

116-
const bvt &boolbvt::convert_bv(const exprt &expr)
116+
const bvt &boolbvt::convert_bv(
117+
const exprt &expr,
118+
optionalt<std::size_t> expected_width)
117119
{
118120
// check cache first
119121
std::pair<bv_cachet::iterator, bool> cache_result=
@@ -126,18 +128,29 @@ const bvt &boolbvt::convert_bv(const exprt &expr)
126128
// Iterators into hash_maps supposedly stay stable
127129
// even though we are inserting more elements recursively.
128130

129-
cache_result.first->second=convert_bitvector(expr);
131+
const bvt &bv = convert_bitvector(expr);
132+
CHECK_RETURN(!expected_width || bv.size() == *expected_width);
133+
134+
INVARIANT_WITH_DIAGNOSTICS(
135+
!expected_width || bv.size() == *expected_width,
136+
"bitvector width shall match the indicated expected width",
137+
expr.find_source_location(),
138+
expr.pretty()
139+
);
140+
141+
cache_result.first->second = bv;
130142

131143
// check
132144
forall_literals(it, cache_result.first->second)
133145
{
134146
if(freeze_all && !it->is_constant())
135147
prop.set_frozen(*it);
136-
if(it->var_no()==literalt::unused_var_no())
137-
{
138-
error() << "unused_var_no: " << expr.pretty() << eom;
139-
assert(false);
140-
}
148+
149+
INVARIANT_WITH_DIAGNOSTICS(
150+
it->var_no() != literalt::unused_var_no(),
151+
"variable number must be different from the unused variable number",
152+
expr.find_source_location(),
153+
expr.pretty());
141154
}
142155

143156
return cache_result.first->second;

src/solvers/flattening/boolbv.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Author: Daniel Kroening, [email protected]
1717
#include <util/mp_arith.h>
1818
#include <util/expr.h>
1919
#include <util/byte_operators.h>
20+
#include <util/optional.h>
2021

2122
#include "bv_utils.h"
2223
#include "boolbv_width.h"
@@ -43,7 +44,10 @@ class boolbvt:public arrayst
4344
{
4445
}
4546

46-
virtual const bvt &convert_bv(const exprt &expr); // check cache
47+
virtual const bvt &convert_bv( // check cache
48+
const exprt &expr,
49+
const optionalt<std::size_t> expected_width = nullopt);
50+
4751
virtual bvt convert_bitvector(const exprt &expr); // no cache
4852

4953
// overloading

src/util/optional.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ using optionalt=nonstd::optional<T>; // NOLINT template typedef
3636

3737
typedef nonstd::bad_optional_access bad_optional_accesst;
3838

39+
using nonstd::nullopt;
40+
3941
#endif // CPROVER_UTIL_OPTIONAL_H

0 commit comments

Comments
 (0)