Skip to content

Commit 361ed3d

Browse files
Lancernlanza
authored andcommitted
[CIR][CIRGen] Lvalues and comma expression (#376)
Currently, codegen of lvalue comma expression would crash: ```cpp int &foo1(); int &foo2(); void c1() { int &x = (foo1(), foo2()); // CRASH } ``` This simple patch fixes this issue.
1 parent d0446c0 commit 361ed3d

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,8 @@ LValue CIRGenFunction::buildDeclRefLValue(const DeclRefExpr *E) {
868868
LValue CIRGenFunction::buildBinaryOperatorLValue(const BinaryOperator *E) {
869869
// Comma expressions just emit their LHS then their RHS as an l-value.
870870
if (E->getOpcode() == BO_Comma) {
871-
assert(0 && "not implemented");
871+
buildIgnoredExpr(E->getLHS());
872+
return buildLValue(E->getRHS());
872873
}
873874

874875
if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI)

clang/test/CIR/CodeGen/comma.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,16 @@ int c0() {
1515
// CHECK: %[[#]] = cir.binop(add, %[[#LOADED_B]], %[[#]]) : !s32i
1616
// CHECK: %[[#LOADED_A:]] = cir.load %[[#A]] : cir.ptr <!s32i>, !s32i
1717
// CHECK: cir.store %[[#LOADED_A]], %[[#RET]] : !s32i, cir.ptr <!s32i>
18+
19+
int &foo1();
20+
int &foo2();
21+
22+
void c1() {
23+
int &x = (foo1(), foo2());
24+
}
25+
26+
// CHECK: cir.func @_Z2c1v()
27+
// CHECK: %0 = cir.alloca !cir.ptr<!s32i>, cir.ptr <!cir.ptr<!s32i>>
28+
// CHECK: %1 = cir.call @_Z4foo1v() : () -> !cir.ptr<!s32i>
29+
// CHECK: %2 = cir.call @_Z4foo2v() : () -> !cir.ptr<!s32i>
30+
// CHECK: cir.store %2, %0 : !cir.ptr<!s32i>, cir.ptr <!cir.ptr<!s32i>>

0 commit comments

Comments
 (0)