From: Fariborz Jahanian Date: Wed, 16 Feb 2011 22:37:10 +0000 (+0000) Subject: Block rewriting bug. Don't take address of captured X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=05318d835e8beb16ac6bdd2c0db0ab6a922f35cf;p=clang Block rewriting bug. Don't take address of captured 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 --- diff --git a/lib/Rewrite/RewriteObjC.cpp b/lib/Rewrite/RewriteObjC.cpp index 543439aac1..1e9b9d3033 100644 --- a/lib/Rewrite/RewriteObjC.cpp +++ b/lib/Rewrite/RewriteObjC.cpp @@ -5269,6 +5269,7 @@ FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(llvm::StringRef name) { Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, const llvm::SmallVector &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 index 0000000000..a48de4bbea --- /dev/null +++ b/test/Rewriter/rewrite-captured-nested-bvar.c @@ -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));