From: Fariborz Jahanian Date: Wed, 28 Jul 2010 23:27:30 +0000 (+0000) Subject: Initialize block's imported variable(s) in X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=20432ef377e8ebcf2933d3f568eaf72766b06116;p=clang Initialize block's imported variable(s) in block's synthesized constructor initalizer list. Fixes radar 8240371. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@109698 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Rewrite/RewriteObjC.cpp b/lib/Rewrite/RewriteObjC.cpp index 7c88d49c7b..9c52aa5f52 100644 --- a/lib/Rewrite/RewriteObjC.cpp +++ b/lib/Rewrite/RewriteObjC.cpp @@ -4309,37 +4309,48 @@ std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, S += FieldName + "; // by ref\n"; } // Finish writing the constructor. - Constructor += ", int flags=0) {\n"; - if (GlobalVarDecl) - Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; - else - Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; - Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; - - Constructor += " Desc = desc;\n"; - + Constructor += ", int flags=0)"; // Initialize all "by copy" arguments. + bool firsTime = true; for (llvm::SmallVector::iterator I = BlockByCopyDecls.begin(), E = BlockByCopyDecls.end(); I != E; ++I) { std::string Name = (*I)->getNameAsString(); - Constructor += " "; - if (isTopLevelBlockPointerType((*I)->getType())) - Constructor += Name + " = (struct __block_impl *)_"; - else - Constructor += Name + " = _"; - Constructor += Name + ";\n"; + if (firsTime) { + Constructor += " : "; + firsTime = false; + } + else + Constructor += ", "; + if (isTopLevelBlockPointerType((*I)->getType())) + Constructor += Name + "((struct __block_impl *)_" + Name + ")"; + else + Constructor += Name + "(_" + Name + ")"; } // Initialize all "by ref" arguments. for (llvm::SmallVector::iterator I = BlockByRefDecls.begin(), E = BlockByRefDecls.end(); I != E; ++I) { std::string Name = (*I)->getNameAsString(); - Constructor += " "; + if (firsTime) { + Constructor += " : "; + firsTime = false; + } + else + Constructor += ", "; if (isTopLevelBlockPointerType((*I)->getType())) - Constructor += Name + " = (struct __block_impl *)_"; + Constructor += Name + "((struct __block_impl *)_" + + Name + "->__forwarding)"; else - Constructor += Name + " = _"; - Constructor += Name + "->__forwarding;\n"; + Constructor += Name + "(_" + Name + "->__forwarding)"; } + + Constructor += " {\n"; + if (GlobalVarDecl) + Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; + else + Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; + Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; + + Constructor += " Desc = desc;\n"; } else { // Finish writing the constructor. Constructor += ", int flags=0) {\n"; diff --git a/test/Rewriter/rewrite-block-consts.mm b/test/Rewriter/rewrite-block-consts.mm new file mode 100644 index 0000000000..c74873f657 --- /dev/null +++ b/test/Rewriter/rewrite-block-consts.mm @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -x objective-c++ -Wno-return-type -fblocks -fms-extensions -rewrite-objc %s -o %t-rw.cpp +// RUN: %clang_cc1 -fsyntax-only -fblocks -Wno-address-of-temporary -D"SEL=void*" -D"__declspec(X)=" %t-rw.cpp +// rdar:// 8243071 + +void x(int y) {} +void f() { + const int bar = 3; + int baz = 4; + __block int bab = 4; + __block const int bas = 5; + void (^b)() = ^{ + x(bar); + x(baz); + x(bab); + x(bas); + b(); + }; + b(); +}