Skip to content

Commit

Permalink
[ObjC][ARC] Honor noescape attribute for -Warc-retain-cycles
Browse files Browse the repository at this point in the history
rdar://35409566

Differential Revision: https://reviews.llvm.org/D40141

llvm-svn: 318552
  • Loading branch information
hyp committed Nov 17, 2017
1 parent 0f90672 commit 42a97a9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
10 changes: 8 additions & 2 deletions clang/lib/Sema/SemaChecking.cpp
Expand Up @@ -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<NoEscapeAttr>())
continue;
return diagnoseRetainCycle(*this, capturer, owner);
}
}
}

/// Check a property assign to see if it's likely to cause a retain cycle.
Expand Down
12 changes: 12 additions & 0 deletions clang/test/SemaObjC/warn-retain-cycle.m
Expand Up @@ -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.
}];
}

0 comments on commit 42a97a9

Please sign in to comment.