diff options
author | Fariborz Jahanian <fjahanian@apple.com> | 2011-02-16 22:37:10 +0000 |
---|---|---|
committer | Fariborz Jahanian <fjahanian@apple.com> | 2011-02-16 22:37:10 +0000 |
commit | 05318d835e8beb16ac6bdd2c0db0ab6a922f35cf (patch) | |
tree | ea4f874df1f47eec3297f9dd28c10fb1ed104282 | |
parent | bac77374729b3bed72f41b66cd80d6e0d38ace3b (diff) |
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
-rw-r--r-- | lib/Rewrite/RewriteObjC.cpp | 18 | ||||
-rw-r--r-- | test/Rewriter/rewrite-captured-nested-bvar.c | 35 |
2 files changed, 52 insertions, 1 deletions
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<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 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)); |