[clang] 878d960 - [clang][CodeGen] Handle throw expression in conditional operator constant folding

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 8 12:32:29 PDT 2020


Author: Raul Tambre
Date: 2020-04-08T12:32:21-07:00
New Revision: 878d96011acc0314ae7e5f87aca515286abbe4db

URL: https://github.com/llvm/llvm-project/commit/878d96011acc0314ae7e5f87aca515286abbe4db
DIFF: https://github.com/llvm/llvm-project/commit/878d96011acc0314ae7e5f87aca515286abbe4db.diff

LOG: [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

Added: 
    

Modified: 
    clang/lib/CodeGen/CGExpr.cpp
    clang/test/CodeGenCXX/throw-expressions.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index b53b77a819b4..25c3616fb921 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -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);
     }
   }

diff  --git a/clang/test/CodeGenCXX/throw-expressions.cpp b/clang/test/CodeGenCXX/throw-expressions.cpp
index 3fe20388f2fc..3cf7123ab796 100644
--- a/clang/test/CodeGenCXX/throw-expressions.cpp
+++ b/clang/test/CodeGenCXX/throw-expressions.cpp
@@ -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(


        


More information about the cfe-commits mailing list