1 //===--- TransRetainReleaseDealloc.cpp - Tranformations to ARC mode -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // removeRetainReleaseDealloc:
12 // Removes retain/release/autorelease/dealloc messages.
14 // return [[foo retain] autorelease];
18 //===----------------------------------------------------------------------===//
20 #include "Transforms.h"
21 #include "Internals.h"
22 #include "clang/Sema/SemaDiagnostic.h"
23 #include "clang/AST/ParentMap.h"
24 #include "clang/Lex/Lexer.h"
25 #include "clang/Basic/SourceManager.h"
27 using namespace clang;
28 using namespace arcmt;
29 using namespace trans;
33 class RetainReleaseDeallocRemover :
34 public RecursiveASTVisitor<RetainReleaseDeallocRemover> {
39 OwningPtr<ParentMap> StmtMap;
41 Selector DelegateSel, FinalizeSel;
44 RetainReleaseDeallocRemover(MigrationPass &pass)
45 : Body(0), Pass(pass) {
47 Pass.Ctx.Selectors.getNullarySelector(&Pass.Ctx.Idents.get("delegate"));
49 Pass.Ctx.Selectors.getNullarySelector(&Pass.Ctx.Idents.get("finalize"));
52 void transformBody(Stmt *body) {
54 collectRemovables(body, Removables);
55 StmtMap.reset(new ParentMap(body));
59 bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
60 switch (E->getMethodFamily()) {
62 if (E->isInstanceMessage() && E->getSelector() == FinalizeSel)
67 // An unused autorelease is badness. If we remove it the receiver
68 // will likely die immediately while previously it was kept alive
69 // by the autorelease pool. This is bad practice in general, leave it
70 // and emit an error to force the user to restructure his code.
71 Pass.TA.reportError("it is not safe to remove an unused 'autorelease' "
72 "message; its receiver may be destroyed immediately",
73 E->getLocStart(), E->getSourceRange());
79 if (E->getReceiverKind() == ObjCMessageExpr::Instance)
80 if (Expr *rec = E->getInstanceReceiver()) {
81 rec = rec->IgnoreParenImpCasts();
82 if (rec->getType().getObjCLifetime() == Qualifiers::OCL_ExplicitNone &&
83 (E->getMethodFamily() != OMF_retain || isRemovable(E))) {
84 std::string err = "it is not safe to remove '";
85 err += E->getSelector().getAsString() + "' message on "
86 "an __unsafe_unretained type";
87 Pass.TA.reportError(err, rec->getLocStart());
91 if (isGlobalVar(rec) &&
92 (E->getMethodFamily() != OMF_retain || isRemovable(E))) {
93 std::string err = "it is not safe to remove '";
94 err += E->getSelector().getAsString() + "' message on "
96 Pass.TA.reportError(err, rec->getLocStart());
100 if (E->getMethodFamily() == OMF_release && isDelegateMessage(rec)) {
101 Pass.TA.reportError("it is not safe to remove 'retain' "
102 "message on the result of a 'delegate' message; "
103 "the object that was passed to 'setDelegate:' may not be "
104 "properly retained", rec->getLocStart());
112 switch (E->getReceiverKind()) {
115 case ObjCMessageExpr::SuperInstance: {
116 Transaction Trans(Pass.TA);
117 clearDiagnostics(E->getSuperLoc());
120 Pass.TA.replace(E->getSourceRange(), "self");
123 case ObjCMessageExpr::Instance:
127 Expr *rec = E->getInstanceReceiver();
128 if (!rec) return true;
130 Transaction Trans(Pass.TA);
131 clearDiagnostics(rec->getExprLoc());
133 ObjCMessageExpr *Msg = E;
134 Expr *RecContainer = Msg;
135 SourceRange RecRange = rec->getSourceRange();
136 checkForGCDOrXPC(Msg, RecContainer, rec, RecRange);
138 if (Msg->getMethodFamily() == OMF_release &&
139 isRemovable(RecContainer) && isInAtFinally(RecContainer)) {
140 // Change the -release to "receiver = nil" in a finally to avoid a leak
141 // when an exception is thrown.
142 Pass.TA.replace(RecContainer->getSourceRange(), RecRange);
143 std::string str = " = ";
144 str += getNilString(Pass.Ctx);
145 Pass.TA.insertAfterToken(RecRange.getEnd(), str);
149 if (!hasSideEffects(rec, Pass.Ctx)) {
150 if (tryRemoving(RecContainer))
153 Pass.TA.replace(RecContainer->getSourceRange(), RecRange);
159 /// \brief Check if the retain/release is due to a GCD/XPC macro that are
162 /// #define dispatch_retain(object) ({ dispatch_object_t _o = (object); _dispatch_object_validate(_o); (void)[_o retain]; })
163 /// #define dispatch_release(object) ({ dispatch_object_t _o = (object); _dispatch_object_validate(_o); [_o release]; })
164 /// #define xpc_retain(object) ({ xpc_object_t _o = (object); _xpc_object_validate(_o); [_o retain]; })
165 /// #define xpc_release(object) ({ xpc_object_t _o = (object); _xpc_object_validate(_o); [_o release]; })
167 /// and return the top container which is the StmtExpr and the macro argument
169 void checkForGCDOrXPC(ObjCMessageExpr *Msg, Expr *&RecContainer,
170 Expr *&Rec, SourceRange &RecRange) {
171 SourceLocation Loc = Msg->getExprLoc();
172 if (!Loc.isMacroID())
174 SourceManager &SM = Pass.Ctx.getSourceManager();
175 StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM,
176 Pass.Ctx.getLangOpts());
177 bool isGCDOrXPC = llvm::StringSwitch<bool>(MacroName)
178 .Case("dispatch_retain", true)
179 .Case("dispatch_release", true)
180 .Case("xpc_retain", true)
181 .Case("xpc_release", true)
189 if (StmtExpr *SE = dyn_cast<StmtExpr>(S)) {
193 S = StmtMap->getParent(S);
199 Stmt::child_range StmtExprChild = StmtE->children();
202 CompoundStmt *CompS = dyn_cast_or_null<CompoundStmt>(*StmtExprChild);
206 Stmt::child_range CompStmtChild = CompS->children();
209 DeclStmt *DeclS = dyn_cast_or_null<DeclStmt>(*CompStmtChild);
212 if (!DeclS->isSingleDecl())
214 VarDecl *VD = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl());
217 Expr *Init = VD->getInit();
221 RecContainer = StmtE;
222 Rec = Init->IgnoreParenImpCasts();
223 if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Rec))
224 Rec = EWC->getSubExpr()->IgnoreParenImpCasts();
225 RecRange = Rec->getSourceRange();
226 if (SM.isMacroArgExpansion(RecRange.getBegin()))
227 RecRange.setBegin(SM.getImmediateSpellingLoc(RecRange.getBegin()));
228 if (SM.isMacroArgExpansion(RecRange.getEnd()))
229 RecRange.setEnd(SM.getImmediateSpellingLoc(RecRange.getEnd()));
232 void clearDiagnostics(SourceLocation loc) const {
233 Pass.TA.clearDiagnostic(diag::err_arc_illegal_explicit_message,
234 diag::err_unavailable,
235 diag::err_unavailable_message,
239 bool isDelegateMessage(Expr *E) const {
240 if (!E) return false;
242 E = E->IgnoreParenCasts();
244 // Also look through property-getter sugar.
245 if (PseudoObjectExpr *pseudoOp = dyn_cast<PseudoObjectExpr>(E))
246 E = pseudoOp->getResultExpr()->IgnoreImplicit();
248 if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E))
249 return (ME->isInstanceMessage() && ME->getSelector() == DelegateSel);
254 bool isInAtFinally(Expr *E) const {
258 if (isa<ObjCAtFinallyStmt>(S))
260 S = StmtMap->getParent(S);
266 bool isRemovable(Expr *E) const {
267 return Removables.count(E);
270 bool tryRemoving(Expr *E) const {
271 if (isRemovable(E)) {
272 Pass.TA.removeStmt(E);
276 Stmt *parent = StmtMap->getParent(E);
278 if (ImplicitCastExpr *castE = dyn_cast_or_null<ImplicitCastExpr>(parent))
279 return tryRemoving(castE);
281 if (ParenExpr *parenE = dyn_cast_or_null<ParenExpr>(parent))
282 return tryRemoving(parenE);
285 bopE = dyn_cast_or_null<BinaryOperator>(parent)) {
286 if (bopE->getOpcode() == BO_Comma && bopE->getLHS() == E &&
288 Pass.TA.replace(bopE->getSourceRange(), bopE->getRHS()->getSourceRange());
298 } // anonymous namespace
300 void trans::removeRetainReleaseDeallocFinalize(MigrationPass &pass) {
301 BodyTransform<RetainReleaseDeallocRemover> trans(pass);
302 trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());