blob: 11a6553341442c65af572917a053fb4b254f1456 [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)) {
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());
74 return true;
75 }
76 // Pass through.
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000077 case OMF_retain:
78 case OMF_release:
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000079 if (E->getReceiverKind() == ObjCMessageExpr::Instance)
80 if (Expr *rec = E->getInstanceReceiver()) {
81 rec = rec->IgnoreParenImpCasts();
Argyrios Kyrtzidis08903e42011-07-14 22:46:12 +000082 if (rec->getType().getObjCLifetime() == Qualifiers::OCL_ExplicitNone &&
83 (E->getMethodFamily() != OMF_retain || isRemovable(E))) {
Argyrios Kyrtzidis84b528f2011-07-14 21:26:49 +000084 std::string err = "it is not safe to remove '";
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000085 err += E->getSelector().getAsString() + "' message on "
86 "an __unsafe_unretained type";
87 Pass.TA.reportError(err, rec->getLocStart());
88 return true;
89 }
Argyrios Kyrtzidisf2a27f42011-07-14 23:32:04 +000090
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 "
95 "a global variable";
96 Pass.TA.reportError(err, rec->getLocStart());
97 return true;
98 }
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +000099
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());
105 return true;
106 }
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000107 }
108 case OMF_dealloc:
109 break;
110 }
111
112 switch (E->getReceiverKind()) {
113 default:
114 return true;
115 case ObjCMessageExpr::SuperInstance: {
116 Transaction Trans(Pass.TA);
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000117 clearDiagnostics(E->getSuperLoc());
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000118 if (tryRemoving(E))
119 return true;
120 Pass.TA.replace(E->getSourceRange(), "self");
121 return true;
122 }
123 case ObjCMessageExpr::Instance:
124 break;
125 }
126
127 Expr *rec = E->getInstanceReceiver();
128 if (!rec) return true;
129
130 Transaction Trans(Pass.TA);
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000131 clearDiagnostics(rec->getExprLoc());
132
Ted Kremenekf7639e12012-03-06 20:06:33 +0000133 ObjCMessageExpr *Msg = E;
134 Expr *RecContainer = Msg;
135 SourceRange RecRange = rec->getSourceRange();
136 checkForGCDOrXPC(Msg, RecContainer, rec, RecRange);
137
138 if (Msg->getMethodFamily() == OMF_release &&
139 isRemovable(RecContainer) && isInAtFinally(RecContainer)) {
Argyrios Kyrtzidisf59daff2011-07-15 22:04:00 +0000140 // Change the -release to "receiver = nil" in a finally to avoid a leak
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000141 // when an exception is thrown.
Ted Kremenekf7639e12012-03-06 20:06:33 +0000142 Pass.TA.replace(RecContainer->getSourceRange(), RecRange);
Argyrios Kyrtzidis93907472011-07-27 05:28:18 +0000143 std::string str = " = ";
144 str += getNilString(Pass.Ctx);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000145 Pass.TA.insertAfterToken(RecRange.getEnd(), str);
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000146 return true;
147 }
148
Ted Kremenekf7639e12012-03-06 20:06:33 +0000149 if (!hasSideEffects(rec, Pass.Ctx)) {
150 if (tryRemoving(RecContainer))
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000151 return true;
152 }
Ted Kremenekf7639e12012-03-06 20:06:33 +0000153 Pass.TA.replace(RecContainer->getSourceRange(), RecRange);
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000154
155 return true;
156 }
157
158private:
Ted Kremenekf7639e12012-03-06 20:06:33 +0000159 /// \brief Check if the retain/release is due to a GCD/XPC macro that are
160 /// defined as:
161 ///
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]; })
166 ///
167 /// and return the top container which is the StmtExpr and the macro argument
168 /// expression.
169 void checkForGCDOrXPC(ObjCMessageExpr *Msg, Expr *&RecContainer,
170 Expr *&Rec, SourceRange &RecRange) {
171 SourceLocation Loc = Msg->getExprLoc();
172 if (!Loc.isMacroID())
173 return;
174 SourceManager &SM = Pass.Ctx.getSourceManager();
175 StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000176 Pass.Ctx.getLangOpts());
Ted Kremenekf7639e12012-03-06 20:06:33 +0000177 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)
182 .Default(false);
183 if (!isGCDOrXPC)
184 return;
185
186 StmtExpr *StmtE = 0;
187 Stmt *S = Msg;
188 while (S) {
189 if (StmtExpr *SE = dyn_cast<StmtExpr>(S)) {
190 StmtE = SE;
191 break;
192 }
193 S = StmtMap->getParent(S);
194 }
195
196 if (!StmtE)
197 return;
198
199 Stmt::child_range StmtExprChild = StmtE->children();
200 if (!StmtExprChild)
201 return;
202 CompoundStmt *CompS = dyn_cast_or_null<CompoundStmt>(*StmtExprChild);
203 if (!CompS)
204 return;
205
206 Stmt::child_range CompStmtChild = CompS->children();
207 if (!CompStmtChild)
208 return;
209 DeclStmt *DeclS = dyn_cast_or_null<DeclStmt>(*CompStmtChild);
210 if (!DeclS)
211 return;
212 if (!DeclS->isSingleDecl())
213 return;
214 VarDecl *VD = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl());
215 if (!VD)
216 return;
217 Expr *Init = VD->getInit();
218 if (!Init)
219 return;
220
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()));
230 }
231
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000232 void clearDiagnostics(SourceLocation loc) const {
233 Pass.TA.clearDiagnostic(diag::err_arc_illegal_explicit_message,
234 diag::err_unavailable,
235 diag::err_unavailable_message,
236 loc);
237 }
238
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +0000239 bool isDelegateMessage(Expr *E) const {
240 if (!E) return false;
241
242 E = E->IgnoreParenCasts();
John McCallfe96e0b2011-11-06 09:01:30 +0000243
244 // Also look through property-getter sugar.
245 if (PseudoObjectExpr *pseudoOp = dyn_cast<PseudoObjectExpr>(E))
246 E = pseudoOp->getResultExpr()->IgnoreImplicit();
247
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +0000248 if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E))
249 return (ME->isInstanceMessage() && ME->getSelector() == DelegateSel);
250
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +0000251 return false;
252 }
253
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000254 bool isInAtFinally(Expr *E) const {
255 assert(E);
256 Stmt *S = E;
257 while (S) {
258 if (isa<ObjCAtFinallyStmt>(S))
259 return true;
260 S = StmtMap->getParent(S);
261 }
262
263 return false;
264 }
265
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000266 bool isRemovable(Expr *E) const {
267 return Removables.count(E);
268 }
269
270 bool tryRemoving(Expr *E) const {
271 if (isRemovable(E)) {
272 Pass.TA.removeStmt(E);
273 return true;
274 }
275
John McCall4db5c3c2011-07-07 06:58:02 +0000276 Stmt *parent = StmtMap->getParent(E);
277
278 if (ImplicitCastExpr *castE = dyn_cast_or_null<ImplicitCastExpr>(parent))
279 return tryRemoving(castE);
280
281 if (ParenExpr *parenE = dyn_cast_or_null<ParenExpr>(parent))
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000282 return tryRemoving(parenE);
283
284 if (BinaryOperator *
John McCall4db5c3c2011-07-07 06:58:02 +0000285 bopE = dyn_cast_or_null<BinaryOperator>(parent)) {
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000286 if (bopE->getOpcode() == BO_Comma && bopE->getLHS() == E &&
287 isRemovable(bopE)) {
288 Pass.TA.replace(bopE->getSourceRange(), bopE->getRHS()->getSourceRange());
289 return true;
290 }
291 }
292
293 return false;
294 }
295
296};
297
298} // anonymous namespace
299
Argyrios Kyrtzidisd2b91122011-11-04 15:58:22 +0000300void trans::removeRetainReleaseDeallocFinalize(MigrationPass &pass) {
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000301 BodyTransform<RetainReleaseDeallocRemover> trans(pass);
302 trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
303}