diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 6b209cdef7c4..e662a5c8b977 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -11652,9 +11652,15 @@ void Sema::checkRetainCycles(ObjCMessageExpr *msg) { } // Check whether the receiver is captured by any of the arguments. - for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) - if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) + const ObjCMethodDecl *MD = msg->getMethodDecl(); + for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) { + if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) { + // noescape blocks should not be retained by the method. + if (MD && MD->parameters()[i]->hasAttr()) + continue; return diagnoseRetainCycle(*this, capturer, owner); + } + } } /// Check a property assign to see if it's likely to cause a retain cycle. diff --git a/clang/test/SemaObjC/warn-retain-cycle.m b/clang/test/SemaObjC/warn-retain-cycle.m index 4398d29e4a8c..f27f1f8e041f 100644 --- a/clang/test/SemaObjC/warn-retain-cycle.m +++ b/clang/test/SemaObjC/warn-retain-cycle.m @@ -198,3 +198,15 @@ __block void(^myBlock)(void) = ^{ }; } + +typedef void (^a_block_t)(void); + +@interface HonorNoEscape +- (void)addStuffUsingBlock:(__attribute__((noescape)) a_block_t)block; +@end + +void testNoEscape(HonorNoEscape *obj) { + [obj addStuffUsingBlock:^{ + (void)obj; // ok. + }]; +}