1 //===--- TransRetainReleaseDealloc.cpp - Transformations to ARC mode ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // removeRetainReleaseDealloc:
11 //
12 // Removes retain/release/autorelease/dealloc messages.
13 //
14 //  return [[foo retain] autorelease];
15 // ---->
16 //  return foo;
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #include "Transforms.h"
21 #include "Internals.h"
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/ParentMap.h"
24 #include "clang/Basic/SourceManager.h"
25 #include "clang/Lex/Lexer.h"
26 #include "clang/Sema/SemaDiagnostic.h"
27 #include "llvm/ADT/StringSwitch.h"
28 
29 using namespace clang;
30 using namespace arcmt;
31 using namespace trans;
32 
33 namespace {
34 
35 class RetainReleaseDeallocRemover :
36                        public RecursiveASTVisitor<RetainReleaseDeallocRemover> {
37   Stmt *Body;
38   MigrationPass &Pass;
39 
40   ExprSet Removables;
41   OwningPtr<ParentMap> StmtMap;
42 
43   Selector DelegateSel, FinalizeSel;
44 
45 public:
46   RetainReleaseDeallocRemover(MigrationPass &pass)
47     : Body(0), Pass(pass) {
48     DelegateSel =
49         Pass.Ctx.Selectors.getNullarySelector(&Pass.Ctx.Idents.get("delegate"));
50     FinalizeSel =
51         Pass.Ctx.Selectors.getNullarySelector(&Pass.Ctx.Idents.get("finalize"));
52   }
53 
54   void transformBody(Stmt *body, Decl *ParentD) {
55     Body = body;
56     collectRemovables(body, Removables);
57     StmtMap.reset(new ParentMap(body));
58     TraverseStmt(body);
59   }
60 
61   bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
62     switch (E->getMethodFamily()) {
63     default:
64       if (E->isInstanceMessage() && E->getSelector() == FinalizeSel)
65         break;
66       return true;
67     case OMF_autorelease:
68       if (isRemovable(E)) {
69         if (!isCommonUnusedAutorelease(E)) {
70           // An unused autorelease is badness. If we remove it the receiver
71           // will likely die immediately while previously it was kept alive
72           // by the autorelease pool. This is bad practice in general, leave it
73           // and emit an error to force the user to restructure his code.
74           Pass.TA.reportError("it is not safe to remove an unused 'autorelease' "
75               "message; its receiver may be destroyed immediately",
76               E->getLocStart(), E->getSourceRange());
77           return true;
78         }
79       }
80       // Pass through.
81     case OMF_retain:
82     case OMF_release:
83       if (E->getReceiverKind() == ObjCMessageExpr::Instance)
84         if (Expr *rec = E->getInstanceReceiver()) {
85           rec = rec->IgnoreParenImpCasts();
86           if (rec->getType().getObjCLifetime() == Qualifiers::OCL_ExplicitNone &&
87               (E->getMethodFamily() != OMF_retain || isRemovable(E))) {
88             std::string err = "it is not safe to remove '";
89             err += E->getSelector().getAsString() + "' message on "
90                 "an __unsafe_unretained type";
91             Pass.TA.reportError(err, rec->getLocStart());
92             return true;
93           }
94 
95           if (isGlobalVar(rec) &&
96               (E->getMethodFamily() != OMF_retain || isRemovable(E))) {
97             std::string err = "it is not safe to remove '";
98             err += E->getSelector().getAsString() + "' message on "
99                 "a global variable";
100             Pass.TA.reportError(err, rec->getLocStart());
101             return true;
102           }
103 
104           if (E->getMethodFamily() == OMF_release && isDelegateMessage(rec)) {
105             Pass.TA.reportError("it is not safe to remove 'retain' "
106                 "message on the result of a 'delegate' message; "
107                 "the object that was passed to 'setDelegate:' may not be "
108                 "properly retained", rec->getLocStart());
109             return true;
110           }
111         }
112     case OMF_dealloc:
113       break;
114     }
115 
116     switch (E->getReceiverKind()) {
117     default:
118       return true;
119     case ObjCMessageExpr::SuperInstance: {
120       Transaction Trans(Pass.TA);
121       clearDiagnostics(E->getSuperLoc());
122       if (tryRemoving(E))
123         return true;
124       Pass.TA.replace(E->getSourceRange(), "self");
125       return true;
126     }
127     case ObjCMessageExpr::Instance:
128       break;
129     }
130 
131     Expr *rec = E->getInstanceReceiver();
132     if (!rec) return true;
133 
134     Transaction Trans(Pass.TA);
135     clearDiagnostics(rec->getExprLoc());
136 
137     ObjCMessageExpr *Msg = E;
138     Expr *RecContainer = Msg;
139     SourceRange RecRange = rec->getSourceRange();
140     checkForGCDOrXPC(Msg, RecContainer, rec, RecRange);
141 
142     if (Msg->getMethodFamily() == OMF_release &&
143         isRemovable(RecContainer) && isInAtFinally(RecContainer)) {
144       // Change the -release to "receiver = nil" in a finally to avoid a leak
145       // when an exception is thrown.
146       Pass.TA.replace(RecContainer->getSourceRange(), RecRange);
147       std::string str = " = ";
148       str += getNilString(Pass.Ctx);
149       Pass.TA.insertAfterToken(RecRange.getEnd(), str);
150       return true;
151     }
152 
153     if (!hasSideEffects(rec, Pass.Ctx)) {
154       if (tryRemoving(RecContainer))
155         return true;
156     }
157     Pass.TA.replace(RecContainer->getSourceRange(), RecRange);
158 
159     return true;
160   }
161 
162 private:
163   /// \brief Checks for idioms where an unused -autorelease is common.
164   ///
165   /// Currently only returns true for this idiom which is common in property
166   /// setters:
167   ///
168   ///   [backingValue autorelease];
169   ///   backingValue = [newValue retain]; // in general a +1 assign
170   ///
171   bool isCommonUnusedAutorelease(ObjCMessageExpr *E) {
172     Expr *Rec = E->getInstanceReceiver();
173     if (!Rec)
174       return false;
175 
176     Decl *RefD = getReferencedDecl(Rec);
177     if (!RefD)
178       return false;
179 
180     Stmt *OuterS = E, *InnerS;
181     do {
182       InnerS = OuterS;
183       OuterS = StmtMap->getParent(InnerS);
184     }
185     while (OuterS && (isa<ParenExpr>(OuterS) ||
186                       isa<CastExpr>(OuterS) ||
187                       isa<ExprWithCleanups>(OuterS)));
188 
189     if (!OuterS)
190       return false;
191 
192     // Find next statement after the -autorelease.
193 
194     Stmt::child_iterator currChildS = OuterS->child_begin();
195     Stmt::child_iterator childE = OuterS->child_end();
196     for (; currChildS != childE; ++currChildS) {
197       if (*currChildS == InnerS)
198         break;
199     }
200     if (currChildS == childE)
201       return false;
202     ++currChildS;
203     if (currChildS == childE)
204       return false;
205 
206     Stmt *nextStmt = *currChildS;
207     if (!nextStmt)
208       return false;
209     nextStmt = nextStmt->IgnoreImplicit();
210 
211     // Check for "RefD = [+1 retained object];".
212 
213     if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(nextStmt)) {
214       if (RefD != getReferencedDecl(Bop->getLHS()))
215         return false;
216       if (isPlusOneAssign(Bop))
217         return true;
218     }
219     return false;
220   }
221 
222   Decl *getReferencedDecl(Expr *E) {
223     if (!E)
224       return 0;
225 
226     E = E->IgnoreParenCasts();
227     if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
228       return DRE->getDecl();
229     if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
230       return ME->getMemberDecl();
231     if (ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(E))
232       return IRE->getDecl();
233 
234     return 0;
235   }
236 
237   /// \brief Check if the retain/release is due to a GCD/XPC macro that are
238   /// defined as:
239   ///
240   /// #define dispatch_retain(object) ({ dispatch_object_t _o = (object); _dispatch_object_validate(_o); (void)[_o retain]; })
241   /// #define dispatch_release(object) ({ dispatch_object_t _o = (object); _dispatch_object_validate(_o); [_o release]; })
242   /// #define xpc_retain(object) ({ xpc_object_t _o = (object); _xpc_object_validate(_o); [_o retain]; })
243   /// #define xpc_release(object) ({ xpc_object_t _o = (object); _xpc_object_validate(_o); [_o release]; })
244   ///
245   /// and return the top container which is the StmtExpr and the macro argument
246   /// expression.
247   void checkForGCDOrXPC(ObjCMessageExpr *Msg, Expr *&RecContainer,
248                         Expr *&Rec, SourceRange &RecRange) {
249     SourceLocation Loc = Msg->getExprLoc();
250     if (!Loc.isMacroID())
251       return;
252     SourceManager &SM = Pass.Ctx.getSourceManager();
253     StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM,
254                                                      Pass.Ctx.getLangOpts());
255     bool isGCDOrXPC = llvm::StringSwitch<bool>(MacroName)
256         .Case("dispatch_retain", true)
257         .Case("dispatch_release", true)
258         .Case("xpc_retain", true)
259         .Case("xpc_release", true)
260         .Default(false);
261     if (!isGCDOrXPC)
262       return;
263 
264     StmtExpr *StmtE = 0;
265     Stmt *S = Msg;
266     while (S) {
267       if (StmtExpr *SE = dyn_cast<StmtExpr>(S)) {
268         StmtE = SE;
269         break;
270       }
271       S = StmtMap->getParent(S);
272     }
273 
274     if (!StmtE)
275       return;
276 
277     Stmt::child_range StmtExprChild = StmtE->children();
278     if (!StmtExprChild)
279       return;
280     CompoundStmt *CompS = dyn_cast_or_null<CompoundStmt>(*StmtExprChild);
281     if (!CompS)
282       return;
283 
284     Stmt::child_range CompStmtChild = CompS->children();
285     if (!CompStmtChild)
286       return;
287     DeclStmt *DeclS = dyn_cast_or_null<DeclStmt>(*CompStmtChild);
288     if (!DeclS)
289       return;
290     if (!DeclS->isSingleDecl())
291       return;
292     VarDecl *VD = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl());
293     if (!VD)
294       return;
295     Expr *Init = VD->getInit();
296     if (!Init)
297       return;
298 
299     RecContainer = StmtE;
300     Rec = Init->IgnoreParenImpCasts();
301     if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Rec))
302       Rec = EWC->getSubExpr()->IgnoreParenImpCasts();
303     RecRange = Rec->getSourceRange();
304     if (SM.isMacroArgExpansion(RecRange.getBegin()))
305       RecRange.setBegin(SM.getImmediateSpellingLoc(RecRange.getBegin()));
306     if (SM.isMacroArgExpansion(RecRange.getEnd()))
307       RecRange.setEnd(SM.getImmediateSpellingLoc(RecRange.getEnd()));
308   }
309 
310   void clearDiagnostics(SourceLocation loc) const {
311     Pass.TA.clearDiagnostic(diag::err_arc_illegal_explicit_message,
312                             diag::err_unavailable,
313                             diag::err_unavailable_message,
314                             loc);
315   }
316 
317   bool isDelegateMessage(Expr *E) const {
318     if (!E) return false;
319 
320     E = E->IgnoreParenCasts();
321 
322     // Also look through property-getter sugar.
323     if (PseudoObjectExpr *pseudoOp = dyn_cast<PseudoObjectExpr>(E))
324       E = pseudoOp->getResultExpr()->IgnoreImplicit();
325 
326     if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E))
327       return (ME->isInstanceMessage() && ME->getSelector() == DelegateSel);
328 
329     return false;
330   }
331 
332   bool isInAtFinally(Expr *E) const {
333     assert(E);
334     Stmt *S = E;
335     while (S) {
336       if (isa<ObjCAtFinallyStmt>(S))
337         return true;
338       S = StmtMap->getParent(S);
339     }
340 
341     return false;
342   }
343 
344   bool isRemovable(Expr *E) const {
345     return Removables.count(E);
346   }
347 
348   bool tryRemoving(Expr *E) const {
349     if (isRemovable(E)) {
350       Pass.TA.removeStmt(E);
351       return true;
352     }
353 
354     Stmt *parent = StmtMap->getParent(E);
355 
356     if (ImplicitCastExpr *castE = dyn_cast_or_null<ImplicitCastExpr>(parent))
357       return tryRemoving(castE);
358 
359     if (ParenExpr *parenE = dyn_cast_or_null<ParenExpr>(parent))
360       return tryRemoving(parenE);
361 
362     if (BinaryOperator *
363           bopE = dyn_cast_or_null<BinaryOperator>(parent)) {
364       if (bopE->getOpcode() == BO_Comma && bopE->getLHS() == E &&
365           isRemovable(bopE)) {
366         Pass.TA.replace(bopE->getSourceRange(), bopE->getRHS()->getSourceRange());
367         return true;
368       }
369     }
370 
371     return false;
372   }
373 
374 };
375 
376 } // anonymous namespace
377 
378 void trans::removeRetainReleaseDeallocFinalize(MigrationPass &pass) {
379   BodyTransform<RetainReleaseDeallocRemover> trans(pass);
380   trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
381 }
382