diff --git a/src/util/format_expr.cpp b/src/util/format_expr.cpp index bd9b2f70e3c..436fc054046 100644 --- a/src/util/format_expr.cpp +++ b/src/util/format_expr.cpp @@ -186,6 +186,19 @@ static std::ostream &format_rec(std::ostream &os, const constant_exprt &src) type == ID_unsignedbv || type == ID_signedbv || type == ID_c_bool || type == ID_c_bit_field) return os << *numeric_cast(src); + else if(type == ID_bv) + { + // These do not have a numerical interpretation. + // We'll print the 0/1 bit pattern, starting with the bit + // that has the highest index. + auto width = to_bv_type(src.type()).get_width(); + std::string result; + result.reserve(width); + auto &value = src.get_value(); + for(std::size_t i = 0; i < width; i++) + result += get_bvrep_bit(value, width, width - i - 1) ? '1' : '0'; + return os << result; + } else if(type == ID_integer || type == ID_natural || type == ID_range) return os << src.get_value(); else if(type == ID_string) diff --git a/unit/util/format_expr.cpp b/unit/util/format_expr.cpp index 3b3c45c4f27..154627aca84 100644 --- a/unit/util/format_expr.cpp +++ b/unit/util/format_expr.cpp @@ -6,8 +6,10 @@ \*******************************************************************/ -#include +#include +#include #include +#include #include @@ -23,3 +25,10 @@ TEST_CASE( }); REQUIRE(format_to_string(exprt{custom_id}) == "output"); } + +TEST_CASE("Format a bv-typed constant", "[core][util][format_expr]") +{ + auto value = make_bvrep(4, [](std::size_t index) { return index != 2; }); + auto expr = constant_exprt{value, bv_typet{4}}; + REQUIRE(format_to_string(expr) == "1011"); +}