blob: f81133f3aad3702c86ffed412493640fc9d180ed [file] [log] [blame]
Benjamin Kramerd81108f2012-11-14 15:08:31 +00001//===--- TransRetainReleaseDealloc.cpp - Transformations to ARC mode ------===//
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +00002//
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"
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000022#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000023#include "clang/AST/ParentMap.h"
Ted Kremenekf7639e12012-03-06 20:06:33 +000024#include "clang/Basic/SourceManager.h"
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000025#include "clang/Lex/Lexer.h"
26#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000027#include "llvm/ADT/StringSwitch.h"
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000028
29using namespace clang;
30using namespace arcmt;
31using namespace trans;
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000032
33namespace {
34
35class RetainReleaseDeallocRemover :
36 public RecursiveASTVisitor<RetainReleaseDeallocRemover> {
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000037 Stmt *Body;
38 MigrationPass &Pass;
39
40 ExprSet Removables;
Ahmed Charlesb8984322014-03-07 20:03:18 +000041 std::unique_ptr<ParentMap> StmtMap;
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000042
Argyrios Kyrtzidisd2b91122011-11-04 15:58:22 +000043 Selector DelegateSel, FinalizeSel;
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +000044
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000045public:
Argyrios Kyrtzidis0b2bd862011-06-23 21:21:33 +000046 RetainReleaseDeallocRemover(MigrationPass &pass)
Craig Topper8ae12032014-05-07 06:21:57 +000047 : Body(nullptr), Pass(pass) {
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +000048 DelegateSel =
49 Pass.Ctx.Selectors.getNullarySelector(&Pass.Ctx.Idents.get("delegate"));
Argyrios Kyrtzidisd2b91122011-11-04 15:58:22 +000050 FinalizeSel =
51 Pass.Ctx.Selectors.getNullarySelector(&Pass.Ctx.Idents.get("finalize"));
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +000052 }
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000053
Argyrios Kyrtzidisb03cc792012-06-07 00:44:06 +000054 void transformBody(Stmt *body, Decl *ParentD) {
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000055 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:
Argyrios Kyrtzidisd2b91122011-11-04 15:58:22 +000064 if (E->isInstanceMessage() && E->getSelector() == FinalizeSel)
65 break;
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000066 return true;
Argyrios Kyrtzidis84b528f2011-07-14 21:26:49 +000067 case OMF_autorelease:
Argyrios Kyrtzidis937bcb22012-05-21 17:48:31 +000068 if (isRemovable(E)) {
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +000069 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
Eric Christopherc9e2a682014-05-20 17:10:39 +000073 // and emit an error to force the user to restructure their code.
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +000074 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 }
Argyrios Kyrtzidis937bcb22012-05-21 17:48:31 +000079 }
80 // Pass through.
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000081 case OMF_retain:
82 case OMF_release:
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000083 if (E->getReceiverKind() == ObjCMessageExpr::Instance)
84 if (Expr *rec = E->getInstanceReceiver()) {
85 rec = rec->IgnoreParenImpCasts();
Argyrios Kyrtzidis08903e42011-07-14 22:46:12 +000086 if (rec->getType().getObjCLifetime() == Qualifiers::OCL_ExplicitNone &&
87 (E->getMethodFamily() != OMF_retain || isRemovable(E))) {
Argyrios Kyrtzidis84b528f2011-07-14 21:26:49 +000088 std::string err = "it is not safe to remove '";
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000089 err += E->getSelector().getAsString() + "' message on "
90 "an __unsafe_unretained type";
91 Pass.TA.reportError(err, rec->getLocStart());
92 return true;
93 }
Argyrios Kyrtzidisf2a27f42011-07-14 23:32:04 +000094
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 }
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +0000103
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 }
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000111 }
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);
Argyrios Kyrtzidisbcf2bdc2013-05-01 00:24:09 +0000121 clearDiagnostics(E->getSelectorLoc(0));
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000122 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);
Argyrios Kyrtzidisbcf2bdc2013-05-01 00:24:09 +0000135 clearDiagnostics(E->getSelectorLoc(0));
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000136
Ted Kremenekf7639e12012-03-06 20:06:33 +0000137 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)) {
Argyrios Kyrtzidisf59daff2011-07-15 22:04:00 +0000144 // Change the -release to "receiver = nil" in a finally to avoid a leak
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000145 // when an exception is thrown.
Ted Kremenekf7639e12012-03-06 20:06:33 +0000146 Pass.TA.replace(RecContainer->getSourceRange(), RecRange);
Argyrios Kyrtzidis93907472011-07-27 05:28:18 +0000147 std::string str = " = ";
Richard Smith20e883e2015-04-29 23:20:19 +0000148 str += getNilString(Pass);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000149 Pass.TA.insertAfterToken(RecRange.getEnd(), str);
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000150 return true;
151 }
152
Alexander Kornienkoad988852015-11-06 01:26:37 +0000153 if (hasSideEffects(rec, Pass.Ctx) || !tryRemoving(RecContainer))
154 Pass.TA.replace(RecContainer->getSourceRange(), RecRange);
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000155
156 return true;
157 }
158
159private:
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000160 /// \brief Checks for idioms where an unused -autorelease is common.
161 ///
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000162 /// Returns true for this idiom which is common in property
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000163 /// setters:
164 ///
165 /// [backingValue autorelease];
166 /// backingValue = [newValue retain]; // in general a +1 assign
167 ///
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000168 /// For these as well:
169 ///
170 /// [[var retain] autorelease];
171 /// return var;
172 ///
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000173 bool isCommonUnusedAutorelease(ObjCMessageExpr *E) {
Alexander Kornienkoad988852015-11-06 01:26:37 +0000174 return isPlusOneAssignBeforeOrAfterAutorelease(E) ||
175 isReturnedAfterAutorelease(E);
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000176 }
177
178 bool isReturnedAfterAutorelease(ObjCMessageExpr *E) {
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000179 Expr *Rec = E->getInstanceReceiver();
180 if (!Rec)
181 return false;
182
183 Decl *RefD = getReferencedDecl(Rec);
184 if (!RefD)
185 return false;
186
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000187 Stmt *nextStmt = getNextStmt(E);
188 if (!nextStmt)
189 return false;
190
191 // Check for "return <variable>;".
192
193 if (ReturnStmt *RetS = dyn_cast<ReturnStmt>(nextStmt))
194 return RefD == getReferencedDecl(RetS->getRetValue());
195
196 return false;
197 }
198
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000199 bool isPlusOneAssignBeforeOrAfterAutorelease(ObjCMessageExpr *E) {
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000200 Expr *Rec = E->getInstanceReceiver();
201 if (!Rec)
202 return false;
203
204 Decl *RefD = getReferencedDecl(Rec);
205 if (!RefD)
206 return false;
207
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000208 Stmt *prevStmt, *nextStmt;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000209 std::tie(prevStmt, nextStmt) = getPreviousAndNextStmt(E);
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000210
211 return isPlusOneAssignToVar(prevStmt, RefD) ||
212 isPlusOneAssignToVar(nextStmt, RefD);
213 }
214
215 bool isPlusOneAssignToVar(Stmt *S, Decl *RefD) {
216 if (!S)
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000217 return false;
218
219 // Check for "RefD = [+1 retained object];".
220
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000221 if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(S)) {
Alexander Kornienkoad988852015-11-06 01:26:37 +0000222 return (RefD == getReferencedDecl(Bop->getLHS())) && isPlusOneAssign(Bop);
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000223 }
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000224
225 if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
226 if (DS->isSingleDecl() && DS->getSingleDecl() == RefD) {
227 if (VarDecl *VD = dyn_cast<VarDecl>(RefD))
228 return isPlusOne(VD->getInit());
229 }
230 return false;
231 }
232
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000233 return false;
234 }
235
236 Stmt *getNextStmt(Expr *E) {
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000237 return getPreviousAndNextStmt(E).second;
238 }
239
240 std::pair<Stmt *, Stmt *> getPreviousAndNextStmt(Expr *E) {
Craig Topper8ae12032014-05-07 06:21:57 +0000241 Stmt *prevStmt = nullptr, *nextStmt = nullptr;
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000242 if (!E)
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000243 return std::make_pair(prevStmt, nextStmt);
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000244
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000245 Stmt *OuterS = E, *InnerS;
246 do {
247 InnerS = OuterS;
248 OuterS = StmtMap->getParent(InnerS);
249 }
250 while (OuterS && (isa<ParenExpr>(OuterS) ||
251 isa<CastExpr>(OuterS) ||
252 isa<ExprWithCleanups>(OuterS)));
253
254 if (!OuterS)
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000255 return std::make_pair(prevStmt, nextStmt);
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000256
257 Stmt::child_iterator currChildS = OuterS->child_begin();
258 Stmt::child_iterator childE = OuterS->child_end();
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000259 Stmt::child_iterator prevChildS = childE;
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000260 for (; currChildS != childE; ++currChildS) {
261 if (*currChildS == InnerS)
262 break;
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000263 prevChildS = currChildS;
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000264 }
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000265
266 if (prevChildS != childE) {
267 prevStmt = *prevChildS;
268 if (prevStmt)
269 prevStmt = prevStmt->IgnoreImplicit();
270 }
271
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000272 if (currChildS == childE)
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000273 return std::make_pair(prevStmt, nextStmt);
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000274 ++currChildS;
275 if (currChildS == childE)
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000276 return std::make_pair(prevStmt, nextStmt);
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000277
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000278 nextStmt = *currChildS;
279 if (nextStmt)
280 nextStmt = nextStmt->IgnoreImplicit();
281
282 return std::make_pair(prevStmt, nextStmt);
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000283 }
284
285 Decl *getReferencedDecl(Expr *E) {
286 if (!E)
Craig Topper8ae12032014-05-07 06:21:57 +0000287 return nullptr;
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000288
289 E = E->IgnoreParenCasts();
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000290 if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) {
291 switch (ME->getMethodFamily()) {
292 case OMF_copy:
293 case OMF_autorelease:
294 case OMF_release:
295 case OMF_retain:
296 return getReferencedDecl(ME->getInstanceReceiver());
297 default:
Craig Topper8ae12032014-05-07 06:21:57 +0000298 return nullptr;
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000299 }
300 }
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000301 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
302 return DRE->getDecl();
303 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
304 return ME->getMemberDecl();
305 if (ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(E))
306 return IRE->getDecl();
307
Craig Topper8ae12032014-05-07 06:21:57 +0000308 return nullptr;
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000309 }
310
Ted Kremenekf7639e12012-03-06 20:06:33 +0000311 /// \brief Check if the retain/release is due to a GCD/XPC macro that are
312 /// defined as:
313 ///
314 /// #define dispatch_retain(object) ({ dispatch_object_t _o = (object); _dispatch_object_validate(_o); (void)[_o retain]; })
315 /// #define dispatch_release(object) ({ dispatch_object_t _o = (object); _dispatch_object_validate(_o); [_o release]; })
316 /// #define xpc_retain(object) ({ xpc_object_t _o = (object); _xpc_object_validate(_o); [_o retain]; })
317 /// #define xpc_release(object) ({ xpc_object_t _o = (object); _xpc_object_validate(_o); [_o release]; })
318 ///
319 /// and return the top container which is the StmtExpr and the macro argument
320 /// expression.
321 void checkForGCDOrXPC(ObjCMessageExpr *Msg, Expr *&RecContainer,
322 Expr *&Rec, SourceRange &RecRange) {
323 SourceLocation Loc = Msg->getExprLoc();
324 if (!Loc.isMacroID())
325 return;
326 SourceManager &SM = Pass.Ctx.getSourceManager();
327 StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000328 Pass.Ctx.getLangOpts());
Ted Kremenekf7639e12012-03-06 20:06:33 +0000329 bool isGCDOrXPC = llvm::StringSwitch<bool>(MacroName)
330 .Case("dispatch_retain", true)
331 .Case("dispatch_release", true)
332 .Case("xpc_retain", true)
333 .Case("xpc_release", true)
334 .Default(false);
335 if (!isGCDOrXPC)
336 return;
337
Craig Topper8ae12032014-05-07 06:21:57 +0000338 StmtExpr *StmtE = nullptr;
Ted Kremenekf7639e12012-03-06 20:06:33 +0000339 Stmt *S = Msg;
340 while (S) {
341 if (StmtExpr *SE = dyn_cast<StmtExpr>(S)) {
342 StmtE = SE;
343 break;
344 }
345 S = StmtMap->getParent(S);
346 }
347
348 if (!StmtE)
349 return;
350
351 Stmt::child_range StmtExprChild = StmtE->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +0000352 if (StmtExprChild.begin() == StmtExprChild.end())
Ted Kremenekf7639e12012-03-06 20:06:33 +0000353 return;
Benjamin Kramer5733e352015-07-18 17:09:36 +0000354 auto *CompS = dyn_cast_or_null<CompoundStmt>(*StmtExprChild.begin());
Ted Kremenekf7639e12012-03-06 20:06:33 +0000355 if (!CompS)
356 return;
357
358 Stmt::child_range CompStmtChild = CompS->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +0000359 if (CompStmtChild.begin() == CompStmtChild.end())
Ted Kremenekf7639e12012-03-06 20:06:33 +0000360 return;
Benjamin Kramer5733e352015-07-18 17:09:36 +0000361 auto *DeclS = dyn_cast_or_null<DeclStmt>(*CompStmtChild.begin());
Ted Kremenekf7639e12012-03-06 20:06:33 +0000362 if (!DeclS)
363 return;
364 if (!DeclS->isSingleDecl())
365 return;
366 VarDecl *VD = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl());
367 if (!VD)
368 return;
369 Expr *Init = VD->getInit();
370 if (!Init)
371 return;
372
373 RecContainer = StmtE;
374 Rec = Init->IgnoreParenImpCasts();
375 if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Rec))
376 Rec = EWC->getSubExpr()->IgnoreParenImpCasts();
377 RecRange = Rec->getSourceRange();
378 if (SM.isMacroArgExpansion(RecRange.getBegin()))
379 RecRange.setBegin(SM.getImmediateSpellingLoc(RecRange.getBegin()));
380 if (SM.isMacroArgExpansion(RecRange.getEnd()))
381 RecRange.setEnd(SM.getImmediateSpellingLoc(RecRange.getEnd()));
382 }
383
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000384 void clearDiagnostics(SourceLocation loc) const {
385 Pass.TA.clearDiagnostic(diag::err_arc_illegal_explicit_message,
386 diag::err_unavailable,
387 diag::err_unavailable_message,
388 loc);
389 }
390
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +0000391 bool isDelegateMessage(Expr *E) const {
392 if (!E) return false;
393
394 E = E->IgnoreParenCasts();
John McCallfe96e0b2011-11-06 09:01:30 +0000395
396 // Also look through property-getter sugar.
397 if (PseudoObjectExpr *pseudoOp = dyn_cast<PseudoObjectExpr>(E))
398 E = pseudoOp->getResultExpr()->IgnoreImplicit();
399
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +0000400 if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E))
401 return (ME->isInstanceMessage() && ME->getSelector() == DelegateSel);
402
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +0000403 return false;
404 }
405
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000406 bool isInAtFinally(Expr *E) const {
407 assert(E);
408 Stmt *S = E;
409 while (S) {
410 if (isa<ObjCAtFinallyStmt>(S))
411 return true;
412 S = StmtMap->getParent(S);
413 }
414
415 return false;
416 }
417
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000418 bool isRemovable(Expr *E) const {
419 return Removables.count(E);
420 }
421
422 bool tryRemoving(Expr *E) const {
423 if (isRemovable(E)) {
424 Pass.TA.removeStmt(E);
425 return true;
426 }
427
John McCall4db5c3c2011-07-07 06:58:02 +0000428 Stmt *parent = StmtMap->getParent(E);
429
430 if (ImplicitCastExpr *castE = dyn_cast_or_null<ImplicitCastExpr>(parent))
431 return tryRemoving(castE);
432
433 if (ParenExpr *parenE = dyn_cast_or_null<ParenExpr>(parent))
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000434 return tryRemoving(parenE);
435
436 if (BinaryOperator *
John McCall4db5c3c2011-07-07 06:58:02 +0000437 bopE = dyn_cast_or_null<BinaryOperator>(parent)) {
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000438 if (bopE->getOpcode() == BO_Comma && bopE->getLHS() == E &&
439 isRemovable(bopE)) {
440 Pass.TA.replace(bopE->getSourceRange(), bopE->getRHS()->getSourceRange());
441 return true;
442 }
443 }
444
445 return false;
446 }
447
448};
449
450} // anonymous namespace
451
Argyrios Kyrtzidisd2b91122011-11-04 15:58:22 +0000452void trans::removeRetainReleaseDeallocFinalize(MigrationPass &pass) {
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000453 BodyTransform<RetainReleaseDeallocRemover> trans(pass);
454 trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
455}