diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2013-01-04 18:29:59 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2013-01-04 18:29:59 +0000 |
commit | b7b89b5ebc7c9c74e071607cc2952d6096ac1b03 (patch) | |
tree | cbb83532e6fd478ac02578178758886c0a631d28 /clang/lib/ARCMigrate/TransRetainReleaseDealloc.cpp | |
parent | dde168b8b587d2560f54c403fa0e09214b90e9f7 (diff) | |
download | bcm5719-llvm-b7b89b5ebc7c9c74e071607cc2952d6096ac1b03.tar.gz bcm5719-llvm-b7b89b5ebc7c9c74e071607cc2952d6096ac1b03.zip |
[arcmt] Don't error if an autoreleased variable is returned after the -autorelease.
rdar://12952025
llvm-svn: 171482
Diffstat (limited to 'clang/lib/ARCMigrate/TransRetainReleaseDealloc.cpp')
-rw-r--r-- | clang/lib/ARCMigrate/TransRetainReleaseDealloc.cpp | 88 |
1 files changed, 70 insertions, 18 deletions
diff --git a/clang/lib/ARCMigrate/TransRetainReleaseDealloc.cpp b/clang/lib/ARCMigrate/TransRetainReleaseDealloc.cpp index aa7e25b39ab..97d9bffda27 100644 --- a/clang/lib/ARCMigrate/TransRetainReleaseDealloc.cpp +++ b/clang/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 <variable>;". + + if (ReturnStmt *RetS = dyn_cast<ReturnStmt>(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<BinaryOperator>(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<ExprWithCleanups>(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<BinaryOperator>(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<ObjCMessageExpr>(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<DeclRefExpr>(E)) return DRE->getDecl(); if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |