summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAkira Hatanaka <ahatanaka@apple.com>2019-12-11 22:51:18 -0800
committerAkira Hatanaka <ahatanaka@apple.com>2019-12-13 13:10:07 -0800
commita0a670614a36f1686c5086033bef85800128cf66 (patch)
tree2cb48ba8b13971558a1c3aef8fba2e802f51943a
parent5623bd52acd34db2e9cfc11d1510407610a14db0 (diff)
downloadbcm5719-llvm-a0a670614a36f1686c5086033bef85800128cf66.tar.gz
bcm5719-llvm-a0a670614a36f1686c5086033bef85800128cf66.zip
Call objc_retainBlock before passing a block as a variadic argument
Copy the block to the heap before passing it to the callee in case the block escapes in the callee. rdar://problem/55683462 Differential Revision: https://reviews.llvm.org/D71431
-rw-r--r--clang/docs/AutomaticReferenceCounting.rst6
-rw-r--r--clang/lib/Sema/SemaExpr.cpp3
-rw-r--r--clang/test/CodeGenObjC/arc-blocks.m10
3 files changed, 19 insertions, 0 deletions
diff --git a/clang/docs/AutomaticReferenceCounting.rst b/clang/docs/AutomaticReferenceCounting.rst
index 9e4456085b6..9d61f58ed4d 100644
--- a/clang/docs/AutomaticReferenceCounting.rst
+++ b/clang/docs/AutomaticReferenceCounting.rst
@@ -1863,6 +1863,12 @@ call for retaining a value of block-pointer type, it has the effect of a
``Block_copy``. The optimizer may remove such copies when it sees that the
result is used only as an argument to a call.
+When a block pointer type is converted to a non-block pointer type (such as
+``id``), ``Block_copy`` is called. This is necessary because a block allocated
+on the stack won't get copied to the heap when the non-block pointer escapes.
+A block pointer is implicitly converted to ``id`` when it is passed to a
+function as a variadic argument.
+
.. _arc.misc.exceptions:
Exceptions
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 620ec30b128..36eef4f425e 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -5247,6 +5247,9 @@ bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, FunctionDecl *FDecl,
for (Expr *A : Args.slice(ArgIx)) {
ExprResult Arg = DefaultVariadicArgumentPromotion(A, CallType, FDecl);
Invalid |= Arg.isInvalid();
+ // Copy blocks to the heap.
+ if (A->getType()->isBlockPointerType())
+ maybeExtendBlockObject(Arg);
AllArgs.push_back(Arg.get());
}
}
diff --git a/clang/test/CodeGenObjC/arc-blocks.m b/clang/test/CodeGenObjC/arc-blocks.m
index cabe552ba85..85c22a1b956 100644
--- a/clang/test/CodeGenObjC/arc-blocks.m
+++ b/clang/test/CodeGenObjC/arc-blocks.m
@@ -730,5 +730,15 @@ void test20(const id x) {
test20_callee(^{ (void)x; });
}
+// CHECK-LABEL: define void @test21(
+// CHECK: %[[V6:.*]] = call i8* @llvm.objc.retainBlock(
+// CHECK: %[[V7:.*]] = bitcast i8* %[[V6]] to void ()*
+// CHECK: call void (i32, ...) @test21_callee(i32 1, void ()* %[[V7]]),
+
+void test21_callee(int n, ...);
+void test21(id x) {
+ test21_callee(1, ^{ (void)x; });
+}
+
// CHECK: attributes [[NUW]] = { nounwind }
// CHECK-UNOPT: attributes [[NUW]] = { nounwind }
OpenPOWER on IntegriCloud