blob: df3cd5858edbc3ebe8fd90c6c6592dd91717dd10 [file] [log] [blame]
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +00001//===--- TransRetainReleaseDealloc.cpp - Tranformations 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/Sema/SemaDiagnostic.h"
23#include "clang/AST/ParentMap.h"
Ted Kremenekf7639e12012-03-06 20:06:33 +000024#include "clang/Lex/Lexer.h"
25#include "clang/Basic/SourceManager.h"
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000026
27using namespace clang;
28using namespace arcmt;
29using namespace trans;
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000030
31namespace {
32
33class RetainReleaseDeallocRemover :
34 public RecursiveASTVisitor<RetainReleaseDeallocRemover> {
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000035 Stmt *Body;
36 MigrationPass &Pass;
37
38 ExprSet Removables;
Dylan Noblesmithe2778992012-02-05 02:12:40 +000039 OwningPtr<ParentMap> StmtMap;
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000040
Argyrios Kyrtzidisd2b91122011-11-04 15:58:22 +000041 Selector DelegateSel, FinalizeSel;
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +000042
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000043public:
Argyrios Kyrtzidis0b2bd862011-06-23 21:21:33 +000044 RetainReleaseDeallocRemover(MigrationPass &pass)
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +000045 : Body(0), Pass(pass) {
46 DelegateSel =
47 Pass.Ctx.Selectors.getNullarySelector(&Pass.Ctx.Idents.get("delegate"));
Argyrios Kyrtzidisd2b91122011-11-04 15:58:22 +000048 FinalizeSel =
49 Pass.Ctx.Selectors.getNullarySelector(&Pass.Ctx.Idents.get("finalize"));
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +000050 }
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000051
52 void transformBody(Stmt *body) {
53 Body = body;
54 collectRemovables(body, Removables);
55 StmtMap.reset(new ParentMap(body));
56 TraverseStmt(body);
57 }
58
59 bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
60 switch (E->getMethodFamily()) {
61 default:
Argyrios Kyrtzidisd2b91122011-11-04 15:58:22 +000062 if (E->isInstanceMessage() && E->getSelector() == FinalizeSel)
63 break;
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000064 return true;
Argyrios Kyrtzidis84b528f2011-07-14 21:26:49 +000065 case OMF_autorelease:
Argyrios Kyrtzidis937bcb22012-05-21 17:48:31 +000066 if (isRemovable(E)) {
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +000067 if (!isCommonUnusedAutorelease(E)) {
68 // An unused autorelease is badness. If we remove it the receiver
69 // will likely die immediately while previously it was kept alive
70 // by the autorelease pool. This is bad practice in general, leave it
71 // and emit an error to force the user to restructure his code.
72 Pass.TA.reportError("it is not safe to remove an unused 'autorelease' "
73 "message; its receiver may be destroyed immediately",
74 E->getLocStart(), E->getSourceRange());
75 return true;
76 }
Argyrios Kyrtzidis937bcb22012-05-21 17:48:31 +000077 }
78 // Pass through.
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000079 case OMF_retain:
80 case OMF_release:
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000081 if (E->getReceiverKind() == ObjCMessageExpr::Instance)
82 if (Expr *rec = E->getInstanceReceiver()) {
83 rec = rec->IgnoreParenImpCasts();
Argyrios Kyrtzidis08903e42011-07-14 22:46:12 +000084 if (rec->getType().getObjCLifetime() == Qualifiers::OCL_ExplicitNone &&
85 (E->getMethodFamily() != OMF_retain || isRemovable(E))) {
Argyrios Kyrtzidis84b528f2011-07-14 21:26:49 +000086 std::string err = "it is not safe to remove '";
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000087 err += E->getSelector().getAsString() + "' message on "
88 "an __unsafe_unretained type";
89 Pass.TA.reportError(err, rec->getLocStart());
90 return true;
91 }
Argyrios Kyrtzidisf2a27f42011-07-14 23:32:04 +000092
93 if (isGlobalVar(rec) &&
94 (E->getMethodFamily() != OMF_retain || isRemovable(E))) {
95 std::string err = "it is not safe to remove '";
96 err += E->getSelector().getAsString() + "' message on "
97 "a global variable";
98 Pass.TA.reportError(err, rec->getLocStart());
99 return true;
100 }
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +0000101
102 if (E->getMethodFamily() == OMF_release && isDelegateMessage(rec)) {
103 Pass.TA.reportError("it is not safe to remove 'retain' "
104 "message on the result of a 'delegate' message; "
105 "the object that was passed to 'setDelegate:' may not be "
106 "properly retained", rec->getLocStart());
107 return true;
108 }
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000109 }
110 case OMF_dealloc:
111 break;
112 }
113
114 switch (E->getReceiverKind()) {
115 default:
116 return true;
117 case ObjCMessageExpr::SuperInstance: {
118 Transaction Trans(Pass.TA);
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000119 clearDiagnostics(E->getSuperLoc());
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000120 if (tryRemoving(E))
121 return true;
122 Pass.TA.replace(E->getSourceRange(), "self");
123 return true;
124 }
125 case ObjCMessageExpr::Instance:
126 break;
127 }
128
129 Expr *rec = E->getInstanceReceiver();
130 if (!rec) return true;
131
132 Transaction Trans(Pass.TA);
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000133 clearDiagnostics(rec->getExprLoc());
134
Ted Kremenekf7639e12012-03-06 20:06:33 +0000135 ObjCMessageExpr *Msg = E;
136 Expr *RecContainer = Msg;
137 SourceRange RecRange = rec->getSourceRange();
138 checkForGCDOrXPC(Msg, RecContainer, rec, RecRange);
139
140 if (Msg->getMethodFamily() == OMF_release &&
141 isRemovable(RecContainer) && isInAtFinally(RecContainer)) {
Argyrios Kyrtzidisf59daff2011-07-15 22:04:00 +0000142 // Change the -release to "receiver = nil" in a finally to avoid a leak
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000143 // when an exception is thrown.
Ted Kremenekf7639e12012-03-06 20:06:33 +0000144 Pass.TA.replace(RecContainer->getSourceRange(), RecRange);
Argyrios Kyrtzidis93907472011-07-27 05:28:18 +0000145 std::string str = " = ";
146 str += getNilString(Pass.Ctx);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000147 Pass.TA.insertAfterToken(RecRange.getEnd(), str);
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000148 return true;
149 }
150
Ted Kremenekf7639e12012-03-06 20:06:33 +0000151 if (!hasSideEffects(rec, Pass.Ctx)) {
152 if (tryRemoving(RecContainer))
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000153 return true;
154 }
Ted Kremenekf7639e12012-03-06 20:06:33 +0000155 Pass.TA.replace(RecContainer->getSourceRange(), RecRange);
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000156
157 return true;
158 }
159
160private:
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000161 /// \brief Checks for idioms where an unused -autorelease is common.
162 ///
163 /// Currently only returns true for this idiom which is common in property
164 /// setters:
165 ///
166 /// [backingValue autorelease];
167 /// backingValue = [newValue retain]; // in general a +1 assign
168 ///
169 bool isCommonUnusedAutorelease(ObjCMessageExpr *E) {
170 Expr *Rec = E->getInstanceReceiver();
171 if (!Rec)
172 return false;
173
174 Decl *RefD = getReferencedDecl(Rec);
175 if (!RefD)
176 return false;
177
178 Stmt *OuterS = E, *InnerS;
179 do {
180 InnerS = OuterS;
181 OuterS = StmtMap->getParent(InnerS);
182 }
183 while (OuterS && (isa<ParenExpr>(OuterS) ||
184 isa<CastExpr>(OuterS) ||
185 isa<ExprWithCleanups>(OuterS)));
186
187 if (!OuterS)
188 return false;
189
190 // Find next statement after the -autorelease.
191
192 Stmt::child_iterator currChildS = OuterS->child_begin();
193 Stmt::child_iterator childE = OuterS->child_end();
194 for (; currChildS != childE; ++currChildS) {
195 if (*currChildS == InnerS)
196 break;
197 }
198 if (currChildS == childE)
199 return false;
200 ++currChildS;
201 if (currChildS == childE)
202 return false;
203
204 Stmt *nextStmt = *currChildS;
205 if (!nextStmt)
206 return false;
207 nextStmt = nextStmt->IgnoreImplicit();
208
209 // Check for "RefD = [+1 retained object];".
210
211 if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(nextStmt)) {
212 if (RefD != getReferencedDecl(Bop->getLHS()))
213 return false;
214 if (isPlusOneAssign(Bop))
215 return true;
216 }
217 return false;
218 }
219
220 Decl *getReferencedDecl(Expr *E) {
221 if (!E)
222 return 0;
223
224 E = E->IgnoreParenCasts();
225 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
226 return DRE->getDecl();
227 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
228 return ME->getMemberDecl();
229 if (ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(E))
230 return IRE->getDecl();
231
232 return 0;
233 }
234
Ted Kremenekf7639e12012-03-06 20:06:33 +0000235 /// \brief Check if the retain/release is due to a GCD/XPC macro that are
236 /// defined as:
237 ///
238 /// #define dispatch_retain(object) ({ dispatch_object_t _o = (object); _dispatch_object_validate(_o); (void)[_o retain]; })
239 /// #define dispatch_release(object) ({ dispatch_object_t _o = (object); _dispatch_object_validate(_o); [_o release]; })
240 /// #define xpc_retain(object) ({ xpc_object_t _o = (object); _xpc_object_validate(_o); [_o retain]; })
241 /// #define xpc_release(object) ({ xpc_object_t _o = (object); _xpc_object_validate(_o); [_o release]; })
242 ///
243 /// and return the top container which is the StmtExpr and the macro argument
244 /// expression.
245 void checkForGCDOrXPC(ObjCMessageExpr *Msg, Expr *&RecContainer,
246 Expr *&Rec, SourceRange &RecRange) {
247 SourceLocation Loc = Msg->getExprLoc();
248 if (!Loc.isMacroID())
249 return;
250 SourceManager &SM = Pass.Ctx.getSourceManager();
251 StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000252 Pass.Ctx.getLangOpts());
Ted Kremenekf7639e12012-03-06 20:06:33 +0000253 bool isGCDOrXPC = llvm::StringSwitch<bool>(MacroName)
254 .Case("dispatch_retain", true)
255 .Case("dispatch_release", true)
256 .Case("xpc_retain", true)
257 .Case("xpc_release", true)
258 .Default(false);
259 if (!isGCDOrXPC)
260 return;
261
262 StmtExpr *StmtE = 0;
263 Stmt *S = Msg;
264 while (S) {
265 if (StmtExpr *SE = dyn_cast<StmtExpr>(S)) {
266 StmtE = SE;
267 break;
268 }
269 S = StmtMap->getParent(S);
270 }
271
272 if (!StmtE)
273 return;
274
275 Stmt::child_range StmtExprChild = StmtE->children();
276 if (!StmtExprChild)
277 return;
278 CompoundStmt *CompS = dyn_cast_or_null<CompoundStmt>(*StmtExprChild);
279 if (!CompS)
280 return;
281
282 Stmt::child_range CompStmtChild = CompS->children();
283 if (!CompStmtChild)
284 return;
285 DeclStmt *DeclS = dyn_cast_or_null<DeclStmt>(*CompStmtChild);
286 if (!DeclS)
287 return;
288 if (!DeclS->isSingleDecl())
289 return;
290 VarDecl *VD = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl());
291 if (!VD)
292 return;
293 Expr *Init = VD->getInit();
294 if (!Init)
295 return;
296
297 RecContainer = StmtE;
298 Rec = Init->IgnoreParenImpCasts();
299 if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Rec))
300 Rec = EWC->getSubExpr()->IgnoreParenImpCasts();
301 RecRange = Rec->getSourceRange();
302 if (SM.isMacroArgExpansion(RecRange.getBegin()))
303 RecRange.setBegin(SM.getImmediateSpellingLoc(RecRange.getBegin()));
304 if (SM.isMacroArgExpansion(RecRange.getEnd()))
305 RecRange.setEnd(SM.getImmediateSpellingLoc(RecRange.getEnd()));
306 }
307
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000308 void clearDiagnostics(SourceLocation loc) const {
309 Pass.TA.clearDiagnostic(diag::err_arc_illegal_explicit_message,
310 diag::err_unavailable,
311 diag::err_unavailable_message,
312 loc);
313 }
314
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +0000315 bool isDelegateMessage(Expr *E) const {
316 if (!E) return false;
317
318 E = E->IgnoreParenCasts();
John McCallfe96e0b2011-11-06 09:01:30 +0000319
320 // Also look through property-getter sugar.
321 if (PseudoObjectExpr *pseudoOp = dyn_cast<PseudoObjectExpr>(E))
322 E = pseudoOp->getResultExpr()->IgnoreImplicit();
323
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +0000324 if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E))
325 return (ME->isInstanceMessage() && ME->getSelector() == DelegateSel);
326
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +0000327 return false;
328 }
329
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000330 bool isInAtFinally(Expr *E) const {
331 assert(E);
332 Stmt *S = E;
333 while (S) {
334 if (isa<ObjCAtFinallyStmt>(S))
335 return true;
336 S = StmtMap->getParent(S);
337 }
338
339 return false;
340 }
341
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000342 bool isRemovable(Expr *E) const {
343 return Removables.count(E);
344 }
345
346 bool tryRemoving(Expr *E) const {
347 if (isRemovable(E)) {
348 Pass.TA.removeStmt(E);
349 return true;
350 }
351
John McCall4db5c3c2011-07-07 06:58:02 +0000352 Stmt *parent = StmtMap->getParent(E);
353
354 if (ImplicitCastExpr *castE = dyn_cast_or_null<ImplicitCastExpr>(parent))
355 return tryRemoving(castE);
356
357 if (ParenExpr *parenE = dyn_cast_or_null<ParenExpr>(parent))
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000358 return tryRemoving(parenE);
359
360 if (BinaryOperator *
John McCall4db5c3c2011-07-07 06:58:02 +0000361 bopE = dyn_cast_or_null<BinaryOperator>(parent)) {
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000362 if (bopE->getOpcode() == BO_Comma && bopE->getLHS() == E &&
363 isRemovable(bopE)) {
364 Pass.TA.replace(bopE->getSourceRange(), bopE->getRHS()->getSourceRange());
365 return true;
366 }
367 }
368
369 return false;
370 }
371
372};
373
374} // anonymous namespace
375
Argyrios Kyrtzidisd2b91122011-11-04 15:58:22 +0000376void trans::removeRetainReleaseDeallocFinalize(MigrationPass &pass) {
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000377 BodyTransform<RetainReleaseDeallocRemover> trans(pass);
378 trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
379}