Skip to content

Commit 878d960

Browse files
tambryzygoloid
authored andcommitted
[clang][CodeGen] Handle throw expression in conditional operator constant folding
Summary: We're smart and do constant folding when emitting conditional operators. Thus we emit the live value as a lvalue. This doesn't work if the live value is a throw expression. Handle this by emitting the throw and returning the dead value as the lvalue. Fixes PR28184. Reviewers: rsmith Reviewed By: rsmith Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D77502
1 parent 0605f5f commit 878d960

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4331,6 +4331,16 @@ EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) {
43314331
// If the true case is live, we need to track its region.
43324332
if (CondExprBool)
43334333
incrementProfileCounter(expr);
4334+
// If a throw expression we emit it and return an undefined lvalue
4335+
// because it can't be used.
4336+
if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(live->IgnoreParens())) {
4337+
EmitCXXThrowExpr(ThrowExpr);
4338+
llvm::Type *Ty =
4339+
llvm::PointerType::getUnqual(ConvertType(dead->getType()));
4340+
return MakeAddrLValue(
4341+
Address(llvm::UndefValue::get(Ty), CharUnits::One()),
4342+
dead->getType());
4343+
}
43344344
return EmitLValue(live);
43354345
}
43364346
}

clang/test/CodeGenCXX/throw-expressions.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ namespace DR1560 {
7979
// CHECK-NOT: call {{.*}}@_ZN6DR15601AD1Ev
8080
// CHECK: call {{.*}} @__cxa_atexit({{.*}} @_ZN6DR15601AD1Ev {{.*}} @_ZGRN6DR15601rE
8181
// CHECK-NOT: call {{.*}}@_ZN6DR15601AD1Ev
82+
83+
// PR28184
84+
void conditional_throw() {
85+
int a;
86+
(true ? throw 0 : a) = 0; // CHECK: call void @__cxa_throw({{.*}})
87+
}
8288
}
8389

8490
// CHECK-LABEL: define void @_Z5test7b(

0 commit comments

Comments
 (0)