Skip to content

Commit 375e6db

Browse files
committed
Remove (meant to be) dead code
The front-ends handle type conversion when doing arithmetic over enum types. The case of an enum tag is not expected here. That, however, was only the case for some operators. Made the C front-end properly handle all assignment operators.
1 parent 6eb9142 commit 375e6db

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

regression/cbmc/enum7/main.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
enum E
44
{
55
A = 1,
6-
B = 2
6+
B = 16
77
};
88

99
int main()
1010
{
11-
enum E array[2] = {A, B};
12-
unsigned *p = array;
13-
assert(*p == 1);
11+
enum E e = A;
12+
e <<= 4;
13+
assert(e == B);
14+
e >>= 4;
15+
assert(e == A);
16+
e |= B;
17+
e ^= A;
18+
assert(e == B);
19+
e -= 15;
20+
assert(e == A);
1421
return 0;
1522
}

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,6 +3350,7 @@ void c_typecheck_baset::typecheck_side_effect_assignment(
33503350
else if(statement==ID_assign_shl ||
33513351
statement==ID_assign_shr)
33523352
{
3353+
implicit_typecast_arithmetic(op0);
33533354
implicit_typecast_arithmetic(op1);
33543355

33553356
if(is_number(op1.type()))
@@ -3364,12 +3365,6 @@ void c_typecheck_baset::typecheck_side_effect_assignment(
33643365

33653366
typet underlying_type=op0.type();
33663367

3367-
if(underlying_type.id()==ID_c_enum_tag)
3368-
{
3369-
const auto &c_enum_type = to_c_enum_tag_type(underlying_type);
3370-
underlying_type=c_enum_type.subtype();
3371-
}
3372-
33733368
if(underlying_type.id()==ID_unsignedbv ||
33743369
underlying_type.id()==ID_c_bool)
33753370
{
@@ -3405,7 +3400,7 @@ void c_typecheck_baset::typecheck_side_effect_assignment(
34053400
o_type0.id()==ID_signedbv ||
34063401
o_type0.id()==ID_c_bit_field)
34073402
{
3408-
implicit_typecast(op1, o_type0);
3403+
implicit_typecast_arithmetic(op0, op1);
34093404
return;
34103405
}
34113406
else if(o_type0.id()==ID_vector &&

src/goto-programs/goto_convert_side_effect.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,10 @@ void goto_convertt::remove_assignment(
105105
tmp.op0().make_typecast(expr.op1().type());
106106
rhs=typecast_exprt(is_not_zero(tmp, ns), expr.op0().type());
107107
}
108-
else if(op0_type.id() == ID_c_enum_tag)
109-
{
110-
// We convert c_enums to their underlying type, do the
111-
// operation, and then convert back
112-
const auto &enum_type = ns.follow_tag(to_c_enum_tag_type(op0_type));
113-
auto underlying_type = enum_type.subtype();
114-
auto op0 = typecast_exprt(expr.op0(), underlying_type);
115-
auto op1 = typecast_exprt(expr.op1(), underlying_type);
116-
binary_exprt tmp(op0, new_id, op1, underlying_type);
117-
rhs = typecast_exprt(tmp, expr.op0().type());
118-
}
119108
else
120109
{
110+
PRECONDITION(
111+
op0_type.id() != ID_c_enum_tag && op0_type.id() != ID_c_enum);
121112
rhs.id(new_id);
122113
rhs.copy_to_operands(expr.op0(), expr.op1());
123114
rhs.type()=expr.op0().type();

0 commit comments

Comments
 (0)