[cfe-commits] r125690 - in /cfe/trunk: lib/Rewrite/RewriteObjC.cpp test/Rewriter/rewrite-captured-nested-bvar.c

Fariborz Jahanian fjahanian at apple.com
Wed Feb 16 14:37:10 PST 2011


Author: fjahanian
Date: Wed Feb 16 16:37:10 2011
New Revision: 125690

URL: http://llvm.org/viewvc/llvm-project?rev=125690&view=rev
Log:
Block rewriting bug. Don't take address of captured 
byref variables again when passing them to inner blocks. 
// rdar://9006279

Added:
    cfe/trunk/test/Rewriter/rewrite-captured-nested-bvar.c
Modified:
    cfe/trunk/lib/Rewrite/RewriteObjC.cpp

Modified: cfe/trunk/lib/Rewrite/RewriteObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/RewriteObjC.cpp?rev=125690&r1=125689&r2=125690&view=diff
==============================================================================
--- cfe/trunk/lib/Rewrite/RewriteObjC.cpp (original)
+++ cfe/trunk/lib/Rewrite/RewriteObjC.cpp Wed Feb 16 16:37:10 2011
@@ -5269,6 +5269,7 @@
 
 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 @@
       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);

Added: cfe/trunk/test/Rewriter/rewrite-captured-nested-bvar.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Rewriter/rewrite-captured-nested-bvar.c?rev=125690&view=auto
==============================================================================
--- cfe/trunk/test/Rewriter/rewrite-captured-nested-bvar.c (added)
+++ cfe/trunk/test/Rewriter/rewrite-captured-nested-bvar.c Wed Feb 16 16:37:10 2011
@@ -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));





More information about the cfe-commits mailing list