[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
This commit is contained in:
Raul Tambre 2020-04-08 12:05:49 -07:00 committed by Richard Smith
parent 0605f5fbe7
commit 878d96011a
2 changed files with 16 additions and 0 deletions

View File

@ -4331,6 +4331,16 @@ EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) {
// If the true case is live, we need to track its region.
if (CondExprBool)
incrementProfileCounter(expr);
// If a throw expression we emit it and return an undefined lvalue
// because it can't be used.
if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(live->IgnoreParens())) {
EmitCXXThrowExpr(ThrowExpr);
llvm::Type *Ty =
llvm::PointerType::getUnqual(ConvertType(dead->getType()));
return MakeAddrLValue(
Address(llvm::UndefValue::get(Ty), CharUnits::One()),
dead->getType());
}
return EmitLValue(live);
}
}

View File

@ -79,6 +79,12 @@ namespace DR1560 {
// CHECK-NOT: call {{.*}}@_ZN6DR15601AD1Ev
// CHECK: call {{.*}} @__cxa_atexit({{.*}} @_ZN6DR15601AD1Ev {{.*}} @_ZGRN6DR15601rE
// CHECK-NOT: call {{.*}}@_ZN6DR15601AD1Ev
// PR28184
void conditional_throw() {
int a;
(true ? throw 0 : a) = 0; // CHECK: call void @__cxa_throw({{.*}})
}
}
// CHECK-LABEL: define void @_Z5test7b(