Skip to content

Commit 72aafbe

Browse files
committed
Fix the parsing of enums and enum constants
1 parent 5d2e8b9 commit 72aafbe

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/ansi-c/c_typecheck_base.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ class c_typecheck_baset:
228228
typet enum_constant_type(
229229
const mp_integer &min, const mp_integer &max) const;
230230

231-
typet enum_underlying_type(
232-
const mp_integer &min, const mp_integer &max,
231+
bitvector_typet enum_underlying_type(
232+
const mp_integer &min,
233+
const mp_integer &max,
233234
bool is_packed) const;
234235

235236
// this cleans expressions in array types

src/ansi-c/c_typecheck_type.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ typet c_typecheck_baset::enum_constant_type(
10781078
}
10791079
}
10801080

1081-
typet c_typecheck_baset::enum_underlying_type(
1081+
bitvector_typet c_typecheck_baset::enum_underlying_type(
10821082
const mp_integer &min_value,
10831083
const mp_integer &max_value,
10841084
bool is_packed) const
@@ -1216,6 +1216,8 @@ void c_typecheck_baset::typecheck_c_enum_type(typet &type)
12161216
c_enum_typet::c_enum_membert member;
12171217
member.set_identifier(identifier);
12181218
member.set_base_name(base_name);
1219+
// Note: The value will be correctly set to a bv type when we know
1220+
// the width of the bitvector
12191221
member.set_value(integer2string(value));
12201222
enum_members.push_back(member);
12211223

@@ -1229,6 +1231,20 @@ void c_typecheck_baset::typecheck_c_enum_type(typet &type)
12291231

12301232
bool is_packed=type.get_bool(ID_C_packed);
12311233

1234+
// We use a subtype to store the underlying type.
1235+
bitvector_typet underlying_type =
1236+
enum_underlying_type(min_value, max_value, is_packed);
1237+
1238+
// Get the width to make the values have a bitvector type
1239+
std::size_t width = underlying_type.get_width();
1240+
for(auto &member : enum_members)
1241+
{
1242+
// Note: This is inefficient as it first turns integers to strings
1243+
// and then turns them back to bvrep
1244+
auto value = string2integer(id2string(member.get_value()));
1245+
member.set_value(integer2bvrep(value, width));
1246+
}
1247+
12321248
// tag?
12331249
if(type.find(ID_tag).is_nil())
12341250
{
@@ -1269,10 +1285,6 @@ void c_typecheck_baset::typecheck_c_enum_type(typet &type)
12691285
for(const auto &member : enum_members)
12701286
body.push_back(member);
12711287

1272-
// We use a subtype to store the underlying type.
1273-
typet underlying_type=
1274-
enum_underlying_type(min_value, max_value, is_packed);
1275-
12761288
enum_tag_symbol.type.subtype()=underlying_type;
12771289

12781290
// is it in the symbol table already?

0 commit comments

Comments
 (0)