Skip to content

Commit 628f776

Browse files
Merge pull request #4226 from peterschrammel/fix-c-enum-output
Fix C enum output
2 parents c011ed5 + 7fa0f97 commit 628f776

File tree

5 files changed

+53
-7
lines changed

5 files changed

+53
-7
lines changed

regression/cbmc/enum-trace1/main.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <assert.h>
2+
3+
enum ENUM
4+
{
5+
E1,
6+
E2,
7+
E3
8+
};
9+
10+
typedef enum ENUMT
11+
{
12+
T1,
13+
T2,
14+
T3
15+
} enum_t;
16+
17+
void test(enum ENUM e, enum_t t)
18+
{
19+
enum ENUM ee = e;
20+
enum_t tt = t;
21+
22+
assert(ee != tt);
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--json-ui --function test --trace
4+
activate-multi-line-match
5+
EXIT=10
6+
SIGNAL=0
7+
VERIFICATION FAILED
8+
\{\n\s*"hidden": false,\n\s*"inputID": "e",\n\s*"internal": true,\n\s*"mode": "C",\n\s*"sourceLocation": \{(\n.*)*\},\n\s*"stepType": "input",\n\s*"thread": 0,\n\s*"values": \[\n\s*\{\n\s*"binary": "000000000000000000000000000000(0|1){2}",\n\s*"data": ".*E(1|2|3)",\n\s*"name": "integer",\n\s*"type": "enum",\n\s*"width": 32\n\s*\}\n\s*\]\n\s*\},
9+
\{\n\s*"hidden": false,\n\s*"inputID": "t",\n\s*"internal": true,\n\s*"mode": "C",\n\s*"sourceLocation": \{(\n.*)*\},\n\s*"stepType": "input",\n\s*"thread": 0,\n\s*"values": \[\n\s*\{\n\s*"binary": "000000000000000000000000000000(0|1){2}",\n\s*"data": ".*T(1|2|3)",\n\s*"name": "integer",\n\s*"type": "enum",\n\s*"width": 32\n\s*\}\n\s*\]\n\s*\},
10+
--
11+
^warning: ignoring
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--xml-ui --function test --trace
4+
activate-multi-line-match
5+
EXIT=10
6+
SIGNAL=0
7+
VERIFICATION FAILED
8+
<input hidden="false" step_nr="\d+" thread="0">\n\s*<input_id>e</input_id>\n\s*<value>.*E(1|2|3)</value>\n\s*<value_expression>\n\s*<integer binary="000000000000000000000000000000(0|1){2}" c_type="enum" width="32">(0|1|2)</integer>\n\s*</value_expression>
9+
<input hidden="false" step_nr="\d+" thread="0">\n\s*<input_id>t</input_id>\n\s*<value>.*T(1|2|3)</value>\n\s*<value_expression>\n\s*<integer binary="000000000000000000000000000000(0|1){2}" c_type="enum" width="32">(0|1|2)</integer>\n\s*</value_expression>
10+
--
11+
^warning: ignoring

src/goto-programs/json_expr.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,11 @@ json_objectt json(const typet &type, const namespacet &ns, const irep_idt &mode)
206206

207207
static std::string binary(const constant_exprt &src)
208208
{
209-
const auto width = to_bitvector_type(src.type()).get_width();
209+
std::size_t width;
210+
if(src.type().id() == ID_c_enum)
211+
width = to_bitvector_type(to_c_enum_type(src.type()).subtype()).get_width();
212+
else
213+
width = to_bitvector_type(src.type()).get_width();
210214
const auto int_val = bvrep2integer(src.get_value(), width, false);
211215
return integer2binary(int_val, width);
212216
}

src/goto-programs/xml_expr.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,13 @@ xmlt xml(const exprt &expr, const namespacet &ns)
179179
const auto width =
180180
to_bitvector_type(to_c_enum_type(type).subtype()).get_width();
181181

182+
const auto integer_value = bvrep2integer(value, width, false);
182183
result.name = "integer";
183-
result.set_attribute(
184-
"binary",
185-
integer2binary(numeric_cast_v<mp_integer>(constant_expr), width));
184+
result.set_attribute("binary", integer2binary(integer_value, width));
186185
result.set_attribute("width", width);
187186
result.set_attribute("c_type", "enum");
188187

189-
mp_integer i;
190-
if(!to_integer(constant_expr, i))
191-
result.data = integer2string(i);
188+
result.data = integer2string(integer_value);
192189
}
193190
else if(type.id() == ID_c_enum_tag)
194191
{

0 commit comments

Comments
 (0)