summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAkira Hatanaka <ahatanaka@apple.com>2017-02-15 05:15:28 +0000
committerAkira Hatanaka <ahatanaka@apple.com>2017-02-15 05:15:28 +0000
commit6697eff4b1eac5ae0bd6274f15a4e493b0b1d951 (patch)
tree9d1f32c31bcef3b91dc4e6bb53adce4f7c7ab98c
parent171e4b545df5cedf8f631e6724a93cf90d42502c (diff)
downloadbcm5719-llvm-6697eff4b1eac5ae0bd6274f15a4e493b0b1d951.tar.gz
bcm5719-llvm-6697eff4b1eac5ae0bd6274f15a4e493b0b1d951.zip
[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 llvm-svn: 295150
-rw-r--r--clang/lib/Sema/SemaStmt.cpp8
-rw-r--r--clang/test/SemaObjCXX/blocks.mm16
2 files changed, 20 insertions, 4 deletions
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index a8832e9a1c5..390e1b52c8e 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -2743,15 +2743,17 @@ bool Sema::isCopyElisionCandidate(QualType ReturnType, const VarDecl *VD,
// ...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>() &&
diff --git a/clang/test/SemaObjCXX/blocks.mm b/clang/test/SemaObjCXX/blocks.mm
index 09d614d3728..3f901cc0a84 100644
--- a/clang/test/SemaObjCXX/blocks.mm
+++ b/clang/test/SemaObjCXX/blocks.mm
@@ -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'}}
+}
+}
OpenPOWER on IntegriCloud