Author: hans Date: Wed Feb 15 14:51:59 2017 New Revision: 295234 URL: http://llvm.org/viewvc/llvm-project?rev=295234&view=rev Log: Merging r295150: ------------------------------------------------------------------------ r295150 | ahatanak | 2017-02-14 21:15:28 -0800 (Tue, 14 Feb 2017) | 13 lines
[Sema] Disallow returning a __block variable via a move. r274291 made changes to prefer calling a move constructor to calling a copy constructor when returning from a function. This caused programs to crash when a __block variable in the heap was moved out and used later. This commit fixes the bug by disallowing moving out of __block variables implicitly. rdar://problem/28181080 Differential Revision: https://reviews.llvm.org/D29908 ------------------------------------------------------------------------ Modified: cfe/branches/release_40/ (props changed) cfe/branches/release_40/lib/Sema/SemaStmt.cpp cfe/branches/release_40/test/SemaObjCXX/blocks.mm Propchange: cfe/branches/release_40/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Wed Feb 15 14:51:59 2017 @@ -1,4 +1,4 @@ /cfe/branches/type-system-rewrite:134693-134817 -/cfe/trunk:291850,291853,291865,291871,291877,291879,291881,291907,291955,291963-291964,292032,292052,292183,292194,292247,292265,292497,292555,292558-292559,292561,292590,292800,292847,292874,292991,293043,293134,293360,293369,293596,293678,293787,294008,294800,294855,295149 +/cfe/trunk:291850,291853,291865,291871,291877,291879,291881,291907,291955,291963-291964,292032,292052,292183,292194,292247,292265,292497,292555,292558-292559,292561,292590,292800,292847,292874,292991,293043,293134,293360,293369,293596,293678,293787,294008,294800,294855,295149-295150 /cfe/trunk/test:170344 /cfe/trunk/test/SemaTemplate:126920 Modified: cfe/branches/release_40/lib/Sema/SemaStmt.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_40/lib/Sema/SemaStmt.cpp?rev=295234&r1=295233&r2=295234&view=diff ============================================================================== --- cfe/branches/release_40/lib/Sema/SemaStmt.cpp (original) +++ cfe/branches/release_40/lib/Sema/SemaStmt.cpp Wed Feb 15 14:51:59 2017 @@ -2743,15 +2743,17 @@ bool Sema::isCopyElisionCandidate(QualTy // ...automatic... if (!VD->hasLocalStorage()) return false; + // Return false if VD is a __block variable. We don't want to implicitly move + // out of a __block variable during a return because we cannot assume the + // variable will no longer be used. + if (VD->hasAttr<BlocksAttr>()) return false; + if (AllowParamOrMoveConstructible) return true; // ...non-volatile... if (VD->getType().isVolatileQualified()) return false; - // __block variables can't be allocated in a way that permits NRVO. - if (VD->hasAttr<BlocksAttr>()) return false; - // Variables with higher required alignment than their type's ABI // alignment cannot use NRVO. if (!VD->getType()->isDependentType() && VD->hasAttr<AlignedAttr>() && Modified: cfe/branches/release_40/test/SemaObjCXX/blocks.mm URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_40/test/SemaObjCXX/blocks.mm?rev=295234&r1=295233&r2=295234&view=diff ============================================================================== --- cfe/branches/release_40/test/SemaObjCXX/blocks.mm (original) +++ cfe/branches/release_40/test/SemaObjCXX/blocks.mm Wed Feb 15 14:51:59 2017 @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -fsyntax-only -verify -fblocks -Wno-objc-root-class %s +// RUN: %clang_cc1 -fsyntax-only -verify -fblocks -Wno-objc-root-class -std=c++11 %s @protocol NSObject; void bar(id(^)(void)); @@ -144,3 +144,17 @@ namespace DependentReturn { template void f<X>(X); } + +namespace MoveBlockVariable { +struct B0 { +}; + +struct B1 { // expected-note 2 {{candidate constructor (the implicit}} + B1(B0&&); // expected-note {{candidate constructor not viable}} +}; + +B1 test_move() { + __block B0 b; + return b; // expected-error {{no viable conversion from returned value of type 'MoveBlockVariable::B0' to function return type 'MoveBlockVariable::B1'}} +} +} _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits