Skip to content

Commit a653749

Browse files
authored
[clang][Interp] Implement compound assign operators on bitfields (#67306)
1 parent 93229c7 commit a653749

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,8 +1170,13 @@ bool ByteCodeExprGen<Emitter>::VisitCompoundAssignOperator(
11701170
}
11711171

11721172
// And store the result in LHS.
1173-
if (DiscardResult)
1173+
if (DiscardResult) {
1174+
if (LHS->refersToBitField())
1175+
return this->emitStoreBitFieldPop(*ResultT, E);
11741176
return this->emitStorePop(*ResultT, E);
1177+
}
1178+
if (LHS->refersToBitField())
1179+
return this->emitStoreBitField(*ResultT, E);
11751180
return this->emitStore(*ResultT, E);
11761181
}
11771182

clang/test/AST/Interp/bitfields.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ namespace Basic {
3131
return a.a = 10;
3232
}
3333
static_assert(storeA2() == 2, "");
34-
35-
// TODO: +=, -=, etc. operators.
3634
}
3735

3836
namespace Overflow {
@@ -45,3 +43,39 @@ namespace Overflow {
4543

4644
static_assert(f() == 3, "");
4745
}
46+
47+
namespace Compound {
48+
struct A {
49+
unsigned int a : 2;
50+
constexpr A() : a(0) {}
51+
constexpr A(int a) : a(a) {}
52+
};
53+
54+
constexpr unsigned add() {
55+
A a;
56+
a.a += 10;
57+
return a.a;
58+
}
59+
static_assert(add() == 2, "");
60+
61+
constexpr unsigned sub() {
62+
A a;
63+
a.a -= 10;
64+
return a.a;
65+
}
66+
static_assert(sub() == 2, "");
67+
68+
constexpr unsigned mul() {
69+
A a(1);
70+
a.a *= 5;
71+
return a.a;
72+
}
73+
static_assert(mul() == 1, "");
74+
75+
constexpr unsigned div() {
76+
A a(2);
77+
a.a /= 2;
78+
return a.a;
79+
}
80+
static_assert(div() == 1, "");
81+
}

0 commit comments

Comments
 (0)