From: Argyrios Kyrtzidis Date: Fri, 4 Jan 2013 18:29:59 +0000 (+0000) Subject: [arcmt] Don't error if an autoreleased variable is returned after the -autorelease. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=af1c08f216cf1ed541d9f502f6c8944d5fd4320c;p=clang [arcmt] Don't error if an autoreleased variable is returned after the -autorelease. rdar://12952025 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171482 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/ARCMigrate/TransRetainReleaseDealloc.cpp b/lib/ARCMigrate/TransRetainReleaseDealloc.cpp index aa7e25b39a..97d9bffda2 100644 --- a/lib/ARCMigrate/TransRetainReleaseDealloc.cpp +++ b/lib/ARCMigrate/TransRetainReleaseDealloc.cpp @@ -162,13 +162,26 @@ public: private: /// \brief Checks for idioms where an unused -autorelease is common. /// - /// Currently only returns true for this idiom which is common in property + /// Returns true for this idiom which is common in property /// setters: /// /// [backingValue autorelease]; /// backingValue = [newValue retain]; // in general a +1 assign /// + /// For these as well: + /// + /// [[var retain] autorelease]; + /// return var; + /// bool isCommonUnusedAutorelease(ObjCMessageExpr *E) { + if (isPlusOneAssignAfterAutorelease(E)) + return true; + if (isReturnedAfterAutorelease(E)) + return true; + return false; + } + + bool isReturnedAfterAutorelease(ObjCMessageExpr *E) { Expr *Rec = E->getInstanceReceiver(); if (!Rec) return false; @@ -177,6 +190,46 @@ private: if (!RefD) return false; + Stmt *nextStmt = getNextStmt(E); + if (!nextStmt) + return false; + + // Check for "return ;". + + if (ReturnStmt *RetS = dyn_cast(nextStmt)) + return RefD == getReferencedDecl(RetS->getRetValue()); + + return false; + } + + bool isPlusOneAssignAfterAutorelease(ObjCMessageExpr *E) { + Expr *Rec = E->getInstanceReceiver(); + if (!Rec) + return false; + + Decl *RefD = getReferencedDecl(Rec); + if (!RefD) + return false; + + Stmt *nextStmt = getNextStmt(E); + if (!nextStmt) + return false; + + // Check for "RefD = [+1 retained object];". + + if (BinaryOperator *Bop = dyn_cast(nextStmt)) { + if (RefD != getReferencedDecl(Bop->getLHS())) + return false; + if (isPlusOneAssign(Bop)) + return true; + } + return false; + } + + Stmt *getNextStmt(Expr *E) { + if (!E) + return 0; + Stmt *OuterS = E, *InnerS; do { InnerS = OuterS; @@ -187,9 +240,7 @@ private: isa(OuterS))); if (!OuterS) - return false; - - // Find next statement after the -autorelease. + return 0; Stmt::child_iterator currChildS = OuterS->child_begin(); Stmt::child_iterator childE = OuterS->child_end(); @@ -198,25 +249,15 @@ private: break; } if (currChildS == childE) - return false; + return 0; ++currChildS; if (currChildS == childE) - return false; + return 0; Stmt *nextStmt = *currChildS; if (!nextStmt) - return false; - nextStmt = nextStmt->IgnoreImplicit(); - - // Check for "RefD = [+1 retained object];". - - if (BinaryOperator *Bop = dyn_cast(nextStmt)) { - if (RefD != getReferencedDecl(Bop->getLHS())) - return false; - if (isPlusOneAssign(Bop)) - return true; - } - return false; + return 0; + return nextStmt->IgnoreImplicit(); } Decl *getReferencedDecl(Expr *E) { @@ -224,6 +265,17 @@ private: return 0; E = E->IgnoreParenCasts(); + if (ObjCMessageExpr *ME = dyn_cast(E)) { + switch (ME->getMethodFamily()) { + case OMF_copy: + case OMF_autorelease: + case OMF_release: + case OMF_retain: + return getReferencedDecl(ME->getInstanceReceiver()); + default: + return 0; + } + } if (DeclRefExpr *DRE = dyn_cast(E)) return DRE->getDecl(); if (MemberExpr *ME = dyn_cast(E)) diff --git a/test/ARCMT/autoreleases.m b/test/ARCMT/autoreleases.m index a131bc5339..d4913943b1 100644 --- a/test/ARCMT/autoreleases.m +++ b/test/ARCMT/autoreleases.m @@ -64,3 +64,8 @@ void test(A *prevVal, A *newVal) { [prevVal autorelease]; prevVal = [newVal retain]; } + +id test2(A* val) { + [[val retain] autorelease]; + return val; +} diff --git a/test/ARCMT/autoreleases.m.result b/test/ARCMT/autoreleases.m.result index 6593fc9199..76ce8cfb51 100644 --- a/test/ARCMT/autoreleases.m.result +++ b/test/ARCMT/autoreleases.m.result @@ -60,3 +60,7 @@ int main (int argc, const char * argv[]) { void test(A *prevVal, A *newVal) { prevVal = newVal; } + +id test2(A* val) { + return val; +}