[ObjC] Copy a block to the heap if it is passed as a variadic argument
authorAkira Hatanaka <ahatanaka@apple.com>
Wed, 24 Jun 2020 18:46:38 +0000 (11:46 -0700)
committerAkira Hatanaka <ahatanaka@apple.com>
Wed, 24 Jun 2020 18:46:38 +0000 (11:46 -0700)
Call maybeExtendBlockObject in DefaultVariadicArgumentPromotion so that
the block is copied to the heap when it is passed as a variadic argument
to any calls, not only to C function calls.

rdar://problem/64201532

clang/lib/Sema/SemaExpr.cpp
clang/test/CodeGenObjC/arc-blocks.m

index ec595ae..bc02487 100644 (file)
@@ -964,6 +964,11 @@ ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT,
   ExprResult ExprRes = DefaultArgumentPromotion(E);
   if (ExprRes.isInvalid())
     return ExprError();
+
+  // Copy blocks to the heap.
+  if (ExprRes.get()->getType()->isBlockPointerType())
+    maybeExtendBlockObject(ExprRes);
+
   E = ExprRes.get();
 
   // Diagnostics regarding non-POD argument types are
@@ -5941,9 +5946,6 @@ 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());
       }
     }
index 3851773..0694155 100644 (file)
@@ -747,5 +747,18 @@ id test22(int c, id x) {
   return c ? test22_0() : ({ id (^b)(void) = ^{ return x; }; test22_1(); b(); });
 }
 
+@interface Test23
+-(void)m:(int)i, ...;
+@end
+
+// CHECK-COMMON-LABEL: define void @test23(
+// CHECK-COMMON: %[[V9:.*]] = call i8* @llvm.objc.retainBlock(
+// CHECK-COMMON: %[[V10:.*]] = bitcast i8* %[[V9]] to void ()*
+// CHECK-COMMON: call void (i8*, i8*, i32, ...) bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to void (i8*, i8*, i32, ...)*)(i8* %{{.*}}, i8* %{{.*}}, i32 123, void ()* %[[V10]])
+
+void test23(id x, Test23 *t) {
+  [t m:123, ^{ (void)x; }];
+}
+
 // CHECK: attributes [[NUW]] = { nounwind }
 // CHECK-UNOPT: attributes [[NUW]] = { nounwind }