[PATCH] D64656: Ensure placeholder instruction for cleanup is created

Øystein Dale via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 12 13:53:52 PDT 2019


oydale updated this revision to Diff 209584.
oydale added a comment.

Added regression test based on code in bug report
Moved logic to a lambda


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D64656/new/

https://reviews.llvm.org/D64656

Files:
  clang/lib/CodeGen/CGExprAgg.cpp
  clang/test/CodeGenCXX/pr40771-ctad-with-lambda-copy-capture.cpp


Index: clang/test/CodeGenCXX/pr40771-ctad-with-lambda-copy-capture.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGenCXX/pr40771-ctad-with-lambda-copy-capture.cpp
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -emit-obj --std=c++17 -fcxx-exceptions -fexceptions -O0 %s
+// RUN: %clang_cc1 -emit-obj --std=c++17 -fcxx-exceptions -fexceptions -O0 %s
+// CHECK no crash
+
+struct outer
+{
+    struct inner
+    {
+        ~inner() {}
+    };
+
+    outer() : member() {}
+    outer(const outer&) : member() {}
+
+    inner member;
+};
+
+template <class... Ts> struct ctad : Ts... {};
+template <class... Ts> ctad(Ts...) -> ctad<Ts...>;
+
+void crash()
+{
+    outer s;
+    ctad c { [s] (int) {}, [s] (short) {} };
+}
Index: clang/lib/CodeGen/CGExprAgg.cpp
===================================================================
--- clang/lib/CodeGen/CGExprAgg.cpp
+++ clang/lib/CodeGen/CGExprAgg.cpp
@@ -1495,6 +1495,13 @@
   // initializers throws an exception.
   SmallVector<EHScopeStack::stable_iterator, 16> cleanups;
   llvm::Instruction *cleanupDominator = nullptr;
+  auto addCleanup = [&](const EHScopeStack::stable_iterator &cleanup) {
+    cleanups.push_back(cleanup);
+    if (!cleanupDominator) // create placeholder once needed
+      cleanupDominator = CGF.Builder.CreateAlignedLoad(
+          CGF.Int8Ty, llvm::Constant::getNullValue(CGF.Int8PtrTy),
+          CharUnits::One());
+  };
 
   unsigned curInitIndex = 0;
 
@@ -1519,7 +1526,7 @@
       if (QualType::DestructionKind dtorKind =
               Base.getType().isDestructedType()) {
         CGF.pushDestroy(dtorKind, V, Base.getType());
-        cleanups.push_back(CGF.EHStack.stable_begin());
+        addCleanup(CGF.EHStack.stable_begin());
       }
     }
   }
@@ -1596,15 +1603,9 @@
           = field->getType().isDestructedType()) {
       assert(LV.isSimple());
       if (CGF.needsEHCleanup(dtorKind)) {
-        if (!cleanupDominator)
-          cleanupDominator = CGF.Builder.CreateAlignedLoad(
-              CGF.Int8Ty,
-              llvm::Constant::getNullValue(CGF.Int8PtrTy),
-              CharUnits::One()); // placeholder
-
         CGF.pushDestroy(EHCleanup, LV.getAddress(), field->getType(),
                         CGF.getDestroyer(dtorKind), false);
-        cleanups.push_back(CGF.EHStack.stable_begin());
+        addCleanup(CGF.EHStack.stable_begin());
         pushedCleanup = true;
       }
     }
@@ -1620,6 +1621,8 @@
 
   // Deactivate all the partial cleanups in reverse order, which
   // generally means popping them.
+  assert((cleanupDominator || cleanups.empty()) &&
+         "Missing cleanupDominator before deactivating cleanup blocks");
   for (unsigned i = cleanups.size(); i != 0; --i)
     CGF.DeactivateCleanupBlock(cleanups[i-1], cleanupDominator);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64656.209584.patch
Type: text/x-patch
Size: 2820 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190712/aa197a6a/attachment.bin>


More information about the cfe-commits mailing list