]> granicus.if.org Git - clang/commitdiff
When copy-capturing values for a nested capture, use a BlockDeclRefExpr.
authorJohn McCall <rjmccall@apple.com>
Mon, 7 Feb 2011 18:37:40 +0000 (18:37 +0000)
committerJohn McCall <rjmccall@apple.com>
Mon, 7 Feb 2011 18:37:40 +0000 (18:37 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@125021 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGBlocks.cpp
test/CodeGenCXX/blocks.cpp [new file with mode: 0644]

index bdbc9d3b55515ec83a3ec688fb652b48d467db72..1a7abd242aa8efe011e44c3465d929af66c8f38b 100644 (file)
@@ -583,10 +583,18 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const BlockExpr *blockExpr) {
 
     // Otherwise, fake up a POD copy into the block field.
     } else {
-      DeclRefExpr declRef(const_cast<VarDecl*>(variable), type, VK_LValue,
-                          SourceLocation());
+      // We use one of these or the other depending on whether the
+      // reference is nested.
+      DeclRefExpr notNested(const_cast<VarDecl*>(variable), type, VK_LValue,
+                            SourceLocation());
+      BlockDeclRefExpr nested(const_cast<VarDecl*>(variable), type,
+                              VK_LValue, SourceLocation(), /*byref*/ false);
+
+      Expr *declRef = 
+        (ci->isNested() ? static_cast<Expr*>(&nested) : &notNested);
+
       ImplicitCastExpr l2r(ImplicitCastExpr::OnStack, type, CK_LValueToRValue,
-                           &declRef, VK_RValue);
+                           declRef, VK_RValue);
       EmitAnyExprToMem(&l2r, blockField, /*volatile*/ false, /*init*/ true);
     }
 
diff --git a/test/CodeGenCXX/blocks.cpp b/test/CodeGenCXX/blocks.cpp
new file mode 100644 (file)
index 0000000..568f9b1
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 %s -fblocks -triple x86_64-apple-darwin -emit-llvm -o - | FileCheck %s
+
+namespace test0 {
+  // CHECK: define void @_ZN5test04testEi(
+  // CHECK: define internal void @__test_block_invoke_{{.*}}(
+  // CHECK: define internal void @__block_global_{{.*}}(
+  void test(int x) {
+    ^{ ^{ (void) x; }; };
+  }
+}