Skip to content

Commit

Permalink
[CIR][CIRGen] Lvalues and comma expression (#376)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Lancern authored Jan 4, 2024
1 parent c5e1756 commit 3adad6b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,8 @@ LValue CIRGenFunction::buildDeclRefLValue(const DeclRefExpr *E) {
LValue CIRGenFunction::buildBinaryOperatorLValue(const BinaryOperator *E) {
// Comma expressions just emit their LHS then their RHS as an l-value.
if (E->getOpcode() == BO_Comma) {
assert(0 && "not implemented");
buildIgnoredExpr(E->getLHS());
return buildLValue(E->getRHS());
}

if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI)
Expand Down
13 changes: 13 additions & 0 deletions clang/test/CIR/CodeGen/comma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@ int c0() {
// CHECK: %[[#]] = cir.binop(add, %[[#LOADED_B]], %[[#]]) : !s32i
// CHECK: %[[#LOADED_A:]] = cir.load %[[#A]] : cir.ptr <!s32i>, !s32i
// CHECK: cir.store %[[#LOADED_A]], %[[#RET]] : !s32i, cir.ptr <!s32i>

int &foo1();
int &foo2();

void c1() {
int &x = (foo1(), foo2());
}

// CHECK: cir.func @_Z2c1v()
// CHECK: %0 = cir.alloca !cir.ptr<!s32i>, cir.ptr <!cir.ptr<!s32i>>
// CHECK: %1 = cir.call @_Z4foo1v() : () -> !cir.ptr<!s32i>
// CHECK: %2 = cir.call @_Z4foo2v() : () -> !cir.ptr<!s32i>
// CHECK: cir.store %2, %0 : !cir.ptr<!s32i>, cir.ptr <!cir.ptr<!s32i>>

0 comments on commit 3adad6b

Please sign in to comment.