]> granicus.if.org Git - clang/commitdiff
Block rewriting bug. Don't take address of captured
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 16 Feb 2011 22:37:10 +0000 (22:37 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 16 Feb 2011 22:37:10 +0000 (22:37 +0000)
byref variables again when passing them to inner blocks.
// rdar://9006279

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@125690 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Rewrite/RewriteObjC.cpp
test/Rewriter/rewrite-captured-nested-bvar.c [new file with mode: 0644]

index 543439aac1d0dd8db193ce1e8d52e9a7adc727ff..1e9b9d303304f79988df00cbda055fb207ad5f21 100644 (file)
@@ -5269,6 +5269,7 @@ FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(llvm::StringRef name) {
 
 Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp,
           const llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs) {
+  const BlockDecl *block = Exp->getBlockDecl();
   Blocks.push_back(Exp);
 
   CollectBlockDeclRefInfo(Exp);
@@ -5412,7 +5413,22 @@ Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp,
       FD = SynthBlockInitFunctionDecl((*I)->getName());
       Exp = new (Context) DeclRefExpr(FD, FD->getType(), VK_LValue,
                                       SourceLocation());
-      Exp = new (Context) UnaryOperator(Exp, UO_AddrOf,
+      bool isNestedCapturedVar = false;
+      if (block)
+        for (BlockDecl::capture_const_iterator ci = block->capture_begin(),
+             ce = block->capture_end(); ci != ce; ++ci) {
+          const VarDecl *variable = ci->getVariable();
+          if (variable == ND && ci->isNested()) {
+            assert (ci->isByRef() && 
+                    "SynthBlockInitExpr - captured block variable is not byref");
+            isNestedCapturedVar = true;
+            break;
+          }
+        }
+      // captured nested byref variable has its address passed. Do not take
+      // its address again.
+      if (!isNestedCapturedVar)
+          Exp = new (Context) UnaryOperator(Exp, UO_AddrOf,
                                      Context->getPointerType(Exp->getType()),
                                      VK_RValue, OK_Ordinary, SourceLocation());
       Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp);
diff --git a/test/Rewriter/rewrite-captured-nested-bvar.c b/test/Rewriter/rewrite-captured-nested-bvar.c
new file mode 100644 (file)
index 0000000..a48de4b
--- /dev/null
@@ -0,0 +1,35 @@
+// RUN: %clang_cc1 -x c -fblocks -fms-extensions -rewrite-objc %s -o %t-rw.cpp
+// RUN: FileCheck --input-file=%t-rw.cpp %s
+// rdar://9006279
+
+void q(void (^p)(void)) {
+    p();
+}
+
+void f() {
+    __block char BYREF_VAR_CHECK = 'a';
+    __block char d = 'd';
+    q(^{
+        q(^{
+            __block char e = 'e';
+            char l = 'l';
+            BYREF_VAR_CHECK = 'b';
+            d = 'd';
+            q(^{
+                 e = '1';
+                 BYREF_VAR_CHECK = '2';
+                 d = '3';
+               }
+             );
+        });
+    });
+}
+
+int main() {
+    f();
+    return 0;
+}
+
+// CHECK 2: (__Block_byref_BYREF_VAR_CHECK_0 *)BYREF_VAR_CHECK
+// CHECK: (__Block_byref_BYREF_VAR_CHECK_0 *)&BYREF_VAR_CHECK
+// CHECK: (struct __Block_byref_BYREF_VAR_CHECK_0 *)&BYREF_VAR_CHECK, (struct __Block_byref_d_1 *)&d, 570425344));