Skip to content

Commit 837da38

Browse files
authored
Merge pull request #5611 from tautschnig/enum-dump-c
C front-end: printing enum type declarations with bitvector awareness
2 parents 7256c23 + ecd5aff commit 837da38

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <assert.h>
2+
3+
enum hex
4+
{
5+
V1 = 0x1,
6+
V10 = 0xA,
7+
V11 = 11
8+
};
9+
10+
int main()
11+
{
12+
enum hex h = V10;
13+
assert(0xca == (unsigned char)(char)0b11001010);
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CORE
2+
main.c
3+
--dump-c
4+
enum hex \{ .* \};
5+
h=/\*enum\*/V10;
6+
^EXIT=0$
7+
^SIGNAL=0$
8+
--
9+
^warning: ignoring
10+
--
11+
This test must pass running CBMC on the output generated using dump-c to
12+
demonstrate that the enum output wouldn't generate A and B (just those
13+
characters, no quotes or 0x prefix) instead of 10 and 11, respectively.

src/ansi-c/expr2c.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -378,21 +378,27 @@ std::string expr2ct::convert_rec(
378378

379379
if(!to_c_enum_type(src).is_incomplete())
380380
{
381+
const c_enum_typet &c_enum_type = to_c_enum_type(src);
382+
const bool is_signed = c_enum_type.subtype().id() == ID_signedbv;
383+
const auto width = to_bitvector_type(c_enum_type.subtype()).get_width();
384+
381385
result += '{';
382386

383387
// add members
384-
const c_enum_typet::memberst &members = to_c_enum_type(src).members();
388+
const c_enum_typet::memberst &members = c_enum_type.members();
385389

386390
for(c_enum_typet::memberst::const_iterator it = members.begin();
387391
it != members.end();
388392
it++)
389393
{
394+
mp_integer int_value = bvrep2integer(it->get_value(), width, is_signed);
395+
390396
if(it != members.begin())
391397
result += ',';
392398
result += ' ';
393399
result += id2string(it->get_base_name());
394400
result += '=';
395-
result += id2string(it->get_value());
401+
result += integer2string(int_value);
396402
}
397403

398404
result += " }";
@@ -1703,24 +1709,21 @@ std::string expr2ct::convert_constant(
17031709
if(c_enum_type.id()!=ID_c_enum)
17041710
return convert_norep(src, precedence);
17051711

1706-
const bool is_signed = c_enum_type.subtype().id() == ID_signedbv;
1707-
const auto width = to_bitvector_type(c_enum_type.subtype()).get_width();
1708-
1709-
mp_integer int_value = bvrep2integer(value, width, is_signed);
1710-
mp_integer i=0;
1711-
1712-
irep_idt int_value_string=integer2string(int_value);
1713-
17141712
const c_enum_typet::memberst &members=
17151713
to_c_enum_type(c_enum_type).members();
17161714

17171715
for(const auto &member : members)
17181716
{
1719-
if(member.get_value() == int_value_string)
1717+
if(member.get_value() == value)
17201718
return "/*enum*/" + id2string(member.get_base_name());
17211719
}
17221720

17231721
// failed...
1722+
const bool is_signed = c_enum_type.subtype().id() == ID_signedbv;
1723+
const auto width = to_bitvector_type(c_enum_type.subtype()).get_width();
1724+
1725+
mp_integer int_value = bvrep2integer(value, width, is_signed);
1726+
17241727
return "/*enum*/"+integer2string(int_value);
17251728
}
17261729
else if(type.id()==ID_rational)

0 commit comments

Comments
 (0)