[cfe-commits] r130372 - in /cfe/trunk: lib/CodeGen/CGClass.cpp lib/Sema/SemaExpr.cpp test/CodeGenCXX/blocks.cpp

John McCall rjmccall at apple.com
Wed Apr 27 19:15:35 PDT 2011


Author: rjmccall
Date: Wed Apr 27 21:15:35 2011
New Revision: 130372

URL: http://llvm.org/viewvc/llvm-project?rev=130372&view=rev
Log:
When block-capturing a variable with a non-trivial destructor,
make sure to mark the destructor.  This normally isn't required,
because the destructor should have been marked as part of the
declaration of the local, but it's necessary when the variable
is a parameter because it's the call sites that are responsible
for those destructors.


Modified:
    cfe/trunk/lib/CodeGen/CGClass.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/CodeGenCXX/blocks.cpp

Modified: cfe/trunk/lib/CodeGen/CGClass.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGClass.cpp?rev=130372&r1=130371&r2=130372&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGClass.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGClass.cpp Wed Apr 27 21:15:35 2011
@@ -1305,6 +1305,7 @@
   if (ClassDecl->hasTrivialDestructor()) return;
 
   const CXXDestructorDecl *D = ClassDecl->getDestructor();
+  assert(D && D->isUsed() && "destructor not marked as used!");
   PushDestructorCleanup(D, Addr);
 }
 

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=130372&r1=130371&r2=130372&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Apr 27 21:15:35 2011
@@ -1124,8 +1124,18 @@
 
   // Build a copy expression.
   Expr *copyExpr = 0;
-  if (!byRef && S.getLangOptions().CPlusPlus &&
-      !type->isDependentType() && type->isStructureOrClassType()) {
+  const RecordType *rtype;
+  if (!byRef && S.getLangOptions().CPlusPlus && !type->isDependentType() &&
+      (rtype = type->getAs<RecordType>())) {
+
+    // The capture logic needs the destructor, so make sure we mark it.
+    // Usually this is unnecessary because most local variables have
+    // their destructors marked at declaration time, but parameters are
+    // an exception because it's technically only the call site that
+    // actually requires the destructor.
+    if (isa<ParmVarDecl>(var))
+      S.FinalizeVarWithDestructor(var, rtype);
+
     // According to the blocks spec, the capture of a variable from
     // the stack requires a const copy constructor.  This is not true
     // of the copy/move done to move a __block variable to the heap.

Modified: cfe/trunk/test/CodeGenCXX/blocks.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/blocks.cpp?rev=130372&r1=130371&r2=130372&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/blocks.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/blocks.cpp Wed Apr 27 21:15:35 2011
@@ -87,3 +87,20 @@
   // CHECK: define internal void @__Block_byref_object_dispose
   // CHECK: call void @_ZN5test21BD1Ev(
 }
+
+// rdar://problem/9334739
+// Make sure we mark destructors for parameters captured in blocks.
+namespace test3 {
+  struct A {
+    A(const A&);
+    ~A();
+  };
+
+  struct B : A {
+  };
+
+  void test(B b) {
+    extern void consume(void(^)());
+    consume(^{ (void) b; });
+  }
+}





More information about the cfe-commits mailing list