From 81c84a9755c511b8f4c2cf9abe45fc47e705309a Mon Sep 17 00:00:00 2001 From: George Karpenkov Date: Mon, 30 Jul 2018 22:18:21 +0000 Subject: [PATCH] [analyzer] Bugfix for autorelease + main run loop leak checker Do not warn when the other message-send-expression is correctly wrapped in a different autorelease pool. Differential Revision: https://reviews.llvm.org/D49921 llvm-svn: 338314 --- .../Checkers/RunLoopAutoreleaseLeakChecker.cpp | 13 +++++++++---- .../test/Analysis/Checkers/RunLoopAutoreleaseLeakChecker.m | 11 +++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/RunLoopAutoreleaseLeakChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/RunLoopAutoreleaseLeakChecker.cpp index 5c57854..55516a3 100644 --- a/clang/lib/StaticAnalyzer/Checkers/RunLoopAutoreleaseLeakChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/RunLoopAutoreleaseLeakChecker.cpp @@ -45,6 +45,7 @@ const char * RunLoopBind = "NSRunLoopM"; const char * RunLoopRunBind = "RunLoopRunM"; const char * OtherMsgBind = "OtherMessageSentM"; const char * AutoreleasePoolBind = "AutoreleasePoolM"; +const char * OtherStmtAutoreleasePoolBind = "OtherAutoreleasePoolM"; class RunLoopAutoreleaseLeakChecker : public Checker { @@ -111,17 +112,20 @@ static void emitDiagnostics(BoundNodes &Match, const auto *AP = Match.getNodeAs(AutoreleasePoolBind); + const auto *OAP = + Match.getNodeAs(OtherStmtAutoreleasePoolBind); bool HasAutoreleasePool = (AP != nullptr); const auto *RL = Match.getNodeAs(RunLoopBind); const auto *RLR = Match.getNodeAs(RunLoopRunBind); assert(RLR && "Run loop launch not found"); - assert(ME != RLR); - if (HasAutoreleasePool && seenBefore(AP, RLR, ME)) + + // Launch of run loop occurs before the message-sent expression is seen. + if (seenBefore(DeclBody, RLR, ME)) return; - if (!HasAutoreleasePool && seenBefore(DeclBody, RLR, ME)) + if (HasAutoreleasePool && (OAP != AP)) return; PathDiagnosticLocation Location = PathDiagnosticLocation::createBegin( @@ -170,7 +174,8 @@ static void checkTempObjectsInSamePool(const Decl *D, AnalysisManager &AM, BugReporter &BR, const RunLoopAutoreleaseLeakChecker *Chkr) { StatementMatcher RunLoopRunM = getRunLoopRunM(); - StatementMatcher OtherMessageSentM = getOtherMessageSentM(); + StatementMatcher OtherMessageSentM = getOtherMessageSentM( + hasAncestor(autoreleasePoolStmt().bind(OtherStmtAutoreleasePoolBind))); StatementMatcher RunLoopInAutorelease = autoreleasePoolStmt( diff --git a/clang/test/Analysis/Checkers/RunLoopAutoreleaseLeakChecker.m b/clang/test/Analysis/Checkers/RunLoopAutoreleaseLeakChecker.m index 32fc220..b00d71b 100644 --- a/clang/test/Analysis/Checkers/RunLoopAutoreleaseLeakChecker.m +++ b/clang/test/Analysis/Checkers/RunLoopAutoreleaseLeakChecker.m @@ -29,6 +29,17 @@ void runloop_init_before() { // Warning: object created before the loop. } } +void runloop_init_before_separate_pool() { // No warning: separate autorelease pool. + @autoreleasepool { + NSObject *object; + @autoreleasepool { + object = [[NSObject alloc] init]; // no-warning + } + (void) object; + [[NSRunLoop mainRunLoop] run]; + } +} + void xpcmain_init_before() { // Warning: object created before the loop. @autoreleasepool { NSObject *object = [[NSObject alloc] init]; // expected-warning{{Temporary objects allocated in the autorelease pool followed by the launch of xpc_main may never get released; consider moving them to a separate autorelease pool}} -- 2.7.4