r176431 - Fix the emission of the copy-initialization of a block capture

John McCall rjmccall at apple.com
Sun Mar 3 22:32:36 PST 2013


Author: rjmccall
Date: Mon Mar  4 00:32:36 2013
New Revision: 176431

URL: http://llvm.org/viewvc/llvm-project?rev=176431&view=rev
Log:
Fix the emission of the copy-initialization of a block capture
from a lambda capture when the capture is not trivially-copyable.

rdar://13295759

Modified:
    cfe/trunk/lib/CodeGen/CGBlocks.cpp
    cfe/trunk/test/CodeGenCXX/blocks-cxx11.cpp

Modified: cfe/trunk/lib/CodeGen/CGBlocks.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBlocks.cpp?rev=176431&r1=176430&r2=176431&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGBlocks.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBlocks.cpp Mon Mar  4 00:32:36 2013
@@ -777,8 +777,16 @@ llvm::Value *CodeGenFunction::EmitBlockL
       // special; we'll simply emit it directly.
       src = 0;
     } else {
-      // This is a [[type]]*.
-      src = LocalDeclMap[variable];
+      // Just look it up in the locals map, which will give us back a
+      // [[type]]*.  If that doesn't work, do the more elaborate DRE
+      // emission.
+      src = LocalDeclMap.lookup(variable);
+      if (!src) {
+        DeclRefExpr declRef(const_cast<VarDecl*>(variable),
+                            /*refersToEnclosing*/ ci->isNested(), type,
+                            VK_LValue, SourceLocation());
+        src = EmitDeclRefLValue(&declRef).getAddress();
+      }
     }
 
     // For byrefs, we just write the pointer to the byref struct into

Modified: cfe/trunk/test/CodeGenCXX/blocks-cxx11.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/blocks-cxx11.cpp?rev=176431&r1=176430&r2=176431&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/blocks-cxx11.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/blocks-cxx11.cpp Mon Mar  4 00:32:36 2013
@@ -82,3 +82,33 @@ namespace test_complex_int_ref_mutable {
   }
 }
 
+// rdar://13295759
+namespace test_block_in_lambda {
+  void takeBlock(void (^block)());
+
+  // The captured variable has to be non-POD so that we have a copy expression.
+  struct A {
+    void *p;
+    A(const A &);
+    ~A();
+    void use() const;
+  };
+
+  void test(A a) {
+    auto lambda = [a]() {
+      takeBlock(^{ a.use(); });
+    };
+    lambda(); // make sure we emit the invocation function
+  }
+  // CHECK:    define internal void @"_ZZN20test_block_in_lambda4testENS_1AEENK3$_0clEv"(
+  // CHECK:      [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]], align 8
+  // CHECK:      [[THIS:%.*]] = load [[LAMBDA_T:%.*]]**
+  // CHECK:      [[TO_DESTROY:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
+  // CHECK:      [[T0:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
+  // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[LAMBDA_T]]* [[THIS]], i32 0, i32 0
+  // CHECK-NEXT: call void @_ZN20test_block_in_lambda1AC1ERKS0_({{.*}}* [[T0]], {{.*}}* [[T1]])
+  // CHECK-NEXT: [[T0:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] to void ()*
+  // CHECK-NEXT: call void @_ZN20test_block_in_lambda9takeBlockEU13block_pointerFvvE(void ()* [[T0]])
+  // CHECK-NEXT: call void @_ZN20test_block_in_lambda1AD1Ev({{.*}}* [[TO_DESTROY]])
+  // CHECK-NEXT: ret void
+}





More information about the cfe-commits mailing list