]> granicus.if.org Git - clang/commitdiff
When possible, move __block variables to the heap rather than copying them.
authorDouglas Gregor <dgregor@apple.com>
Thu, 7 Mar 2013 22:38:24 +0000 (22:38 +0000)
committerDouglas Gregor <dgregor@apple.com>
Thu, 7 Mar 2013 22:38:24 +0000 (22:38 +0000)
Fixes <rdar://problem/13330126>.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@176663 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/SemaCXX/blocks.cpp

index 82d216ee01cc63152db0f22d7b7a53682bbe1b76..4fa790515dab4f747baa58990a9f794a77fbc2c6 100644 (file)
@@ -7811,10 +7811,10 @@ void Sema::CheckCompleteVariableDeclaration(VarDecl *var) {
     if (type->isStructureOrClassType()) {
       SourceLocation poi = var->getLocation();
       Expr *varRef =new (Context) DeclRefExpr(var, false, type, VK_LValue, poi);
-      ExprResult result =
-        PerformCopyInitialization(
-                        InitializedEntity::InitializeBlock(poi, type, false),
-                                  poi, Owned(varRef));
+      ExprResult result
+        = PerformMoveOrCopyInitialization(
+            InitializedEntity::InitializeBlock(poi, type, false),
+            var, var->getType(), varRef, /*AllowNRVO=*/true);
       if (!result.isInvalid()) {
         result = MaybeCreateExprWithCleanups(result);
         Expr *init = result.takeAs<Expr>();
index 3f81c274d04f82d3bdcb3a42177e4a52bc8d3ff3..a635e998d92fe4ebd0cb53c26416a62bb8aef02e 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s -fblocks
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s -fblocks
 // expected-no-diagnostics
 
 void tovoid(void*);
@@ -69,3 +69,16 @@ namespace radar8382559 {
     return hasProperty = 1;
   }
 }
+
+// Move __block variables to the heap when possible.
+class MoveOnly {
+public:
+  MoveOnly();
+  MoveOnly(const MoveOnly&) = delete;
+  MoveOnly(MoveOnly&&);
+};
+
+void move_block() {
+  __block MoveOnly mo;
+}
+