blob: 389f3655aa520a021030ed15816d14572b044ec8 [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.
Galina Kistanova83b8a6c2017-06-03 06:23:19 +000081 LLVM_FALLTHROUGH;
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000082 case OMF_retain:
83 case OMF_release:
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000084 if (E->getReceiverKind() == ObjCMessageExpr::Instance)
85 if (Expr *rec = E->getInstanceReceiver()) {
86 rec = rec->IgnoreParenImpCasts();
Argyrios Kyrtzidis08903e42011-07-14 22:46:12 +000087 if (rec->getType().getObjCLifetime() == Qualifiers::OCL_ExplicitNone &&
88 (E->getMethodFamily() != OMF_retain || isRemovable(E))) {
Argyrios Kyrtzidis84b528f2011-07-14 21:26:49 +000089 std::string err = "it is not safe to remove '";
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +000090 err += E->getSelector().getAsString() + "' message on "
91 "an __unsafe_unretained type";
92 Pass.TA.reportError(err, rec->getLocStart());
93 return true;
94 }
Argyrios Kyrtzidisf2a27f42011-07-14 23:32:04 +000095
96 if (isGlobalVar(rec) &&
97 (E->getMethodFamily() != OMF_retain || isRemovable(E))) {
98 std::string err = "it is not safe to remove '";
99 err += E->getSelector().getAsString() + "' message on "
100 "a global variable";
101 Pass.TA.reportError(err, rec->getLocStart());
102 return true;
103 }
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +0000104
105 if (E->getMethodFamily() == OMF_release && isDelegateMessage(rec)) {
106 Pass.TA.reportError("it is not safe to remove 'retain' "
107 "message on the result of a 'delegate' message; "
108 "the object that was passed to 'setDelegate:' may not be "
109 "properly retained", rec->getLocStart());
110 return true;
111 }
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000112 }
113 case OMF_dealloc:
114 break;
115 }
116
117 switch (E->getReceiverKind()) {
118 default:
119 return true;
120 case ObjCMessageExpr::SuperInstance: {
121 Transaction Trans(Pass.TA);
Argyrios Kyrtzidisbcf2bdc2013-05-01 00:24:09 +0000122 clearDiagnostics(E->getSelectorLoc(0));
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000123 if (tryRemoving(E))
124 return true;
125 Pass.TA.replace(E->getSourceRange(), "self");
126 return true;
127 }
128 case ObjCMessageExpr::Instance:
129 break;
130 }
131
132 Expr *rec = E->getInstanceReceiver();
133 if (!rec) return true;
134
135 Transaction Trans(Pass.TA);
Argyrios Kyrtzidisbcf2bdc2013-05-01 00:24:09 +0000136 clearDiagnostics(E->getSelectorLoc(0));
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000137
Ted Kremenekf7639e12012-03-06 20:06:33 +0000138 ObjCMessageExpr *Msg = E;
139 Expr *RecContainer = Msg;
140 SourceRange RecRange = rec->getSourceRange();
141 checkForGCDOrXPC(Msg, RecContainer, rec, RecRange);
142
143 if (Msg->getMethodFamily() == OMF_release &&
144 isRemovable(RecContainer) && isInAtFinally(RecContainer)) {
Argyrios Kyrtzidisf59daff2011-07-15 22:04:00 +0000145 // Change the -release to "receiver = nil" in a finally to avoid a leak
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000146 // when an exception is thrown.
Ted Kremenekf7639e12012-03-06 20:06:33 +0000147 Pass.TA.replace(RecContainer->getSourceRange(), RecRange);
Argyrios Kyrtzidis93907472011-07-27 05:28:18 +0000148 std::string str = " = ";
Richard Smith20e883e2015-04-29 23:20:19 +0000149 str += getNilString(Pass);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000150 Pass.TA.insertAfterToken(RecRange.getEnd(), str);
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000151 return true;
152 }
153
Alexander Kornienkoad988852015-11-06 01:26:37 +0000154 if (hasSideEffects(rec, Pass.Ctx) || !tryRemoving(RecContainer))
155 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 ///
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000163 /// Returns true for this idiom which is common in property
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000164 /// setters:
165 ///
166 /// [backingValue autorelease];
167 /// backingValue = [newValue retain]; // in general a +1 assign
168 ///
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000169 /// For these as well:
170 ///
171 /// [[var retain] autorelease];
172 /// return var;
173 ///
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000174 bool isCommonUnusedAutorelease(ObjCMessageExpr *E) {
Alexander Kornienkoad988852015-11-06 01:26:37 +0000175 return isPlusOneAssignBeforeOrAfterAutorelease(E) ||
176 isReturnedAfterAutorelease(E);
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000177 }
178
179 bool isReturnedAfterAutorelease(ObjCMessageExpr *E) {
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000180 Expr *Rec = E->getInstanceReceiver();
181 if (!Rec)
182 return false;
183
184 Decl *RefD = getReferencedDecl(Rec);
185 if (!RefD)
186 return false;
187
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000188 Stmt *nextStmt = getNextStmt(E);
189 if (!nextStmt)
190 return false;
191
192 // Check for "return <variable>;".
193
194 if (ReturnStmt *RetS = dyn_cast<ReturnStmt>(nextStmt))
195 return RefD == getReferencedDecl(RetS->getRetValue());
196
197 return false;
198 }
199
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000200 bool isPlusOneAssignBeforeOrAfterAutorelease(ObjCMessageExpr *E) {
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000201 Expr *Rec = E->getInstanceReceiver();
202 if (!Rec)
203 return false;
204
205 Decl *RefD = getReferencedDecl(Rec);
206 if (!RefD)
207 return false;
208
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000209 Stmt *prevStmt, *nextStmt;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000210 std::tie(prevStmt, nextStmt) = getPreviousAndNextStmt(E);
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000211
212 return isPlusOneAssignToVar(prevStmt, RefD) ||
213 isPlusOneAssignToVar(nextStmt, RefD);
214 }
215
216 bool isPlusOneAssignToVar(Stmt *S, Decl *RefD) {
217 if (!S)
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000218 return false;
219
220 // Check for "RefD = [+1 retained object];".
221
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000222 if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(S)) {
Alexander Kornienkoad988852015-11-06 01:26:37 +0000223 return (RefD == getReferencedDecl(Bop->getLHS())) && isPlusOneAssign(Bop);
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000224 }
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000225
226 if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
227 if (DS->isSingleDecl() && DS->getSingleDecl() == RefD) {
228 if (VarDecl *VD = dyn_cast<VarDecl>(RefD))
229 return isPlusOne(VD->getInit());
230 }
231 return false;
232 }
233
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000234 return false;
235 }
236
237 Stmt *getNextStmt(Expr *E) {
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000238 return getPreviousAndNextStmt(E).second;
239 }
240
241 std::pair<Stmt *, Stmt *> getPreviousAndNextStmt(Expr *E) {
Craig Topper8ae12032014-05-07 06:21:57 +0000242 Stmt *prevStmt = nullptr, *nextStmt = nullptr;
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000243 if (!E)
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000244 return std::make_pair(prevStmt, nextStmt);
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000245
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000246 Stmt *OuterS = E, *InnerS;
247 do {
248 InnerS = OuterS;
249 OuterS = StmtMap->getParent(InnerS);
250 }
251 while (OuterS && (isa<ParenExpr>(OuterS) ||
252 isa<CastExpr>(OuterS) ||
253 isa<ExprWithCleanups>(OuterS)));
254
255 if (!OuterS)
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000256 return std::make_pair(prevStmt, nextStmt);
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000257
258 Stmt::child_iterator currChildS = OuterS->child_begin();
259 Stmt::child_iterator childE = OuterS->child_end();
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000260 Stmt::child_iterator prevChildS = childE;
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000261 for (; currChildS != childE; ++currChildS) {
262 if (*currChildS == InnerS)
263 break;
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000264 prevChildS = currChildS;
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000265 }
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000266
267 if (prevChildS != childE) {
268 prevStmt = *prevChildS;
269 if (prevStmt)
270 prevStmt = prevStmt->IgnoreImplicit();
271 }
272
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000273 if (currChildS == childE)
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000274 return std::make_pair(prevStmt, nextStmt);
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000275 ++currChildS;
276 if (currChildS == childE)
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000277 return std::make_pair(prevStmt, nextStmt);
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000278
Argyrios Kyrtzidisd12c3322013-01-04 18:30:11 +0000279 nextStmt = *currChildS;
280 if (nextStmt)
281 nextStmt = nextStmt->IgnoreImplicit();
282
283 return std::make_pair(prevStmt, nextStmt);
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000284 }
285
286 Decl *getReferencedDecl(Expr *E) {
287 if (!E)
Craig Topper8ae12032014-05-07 06:21:57 +0000288 return nullptr;
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000289
290 E = E->IgnoreParenCasts();
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000291 if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) {
292 switch (ME->getMethodFamily()) {
293 case OMF_copy:
294 case OMF_autorelease:
295 case OMF_release:
296 case OMF_retain:
297 return getReferencedDecl(ME->getInstanceReceiver());
298 default:
Craig Topper8ae12032014-05-07 06:21:57 +0000299 return nullptr;
Argyrios Kyrtzidisb7b89b52013-01-04 18:29:59 +0000300 }
301 }
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000302 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
303 return DRE->getDecl();
304 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
305 return ME->getMemberDecl();
306 if (ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(E))
307 return IRE->getDecl();
308
Craig Topper8ae12032014-05-07 06:21:57 +0000309 return nullptr;
Argyrios Kyrtzidis0b21d822012-05-23 21:50:04 +0000310 }
311
Ted Kremenekf7639e12012-03-06 20:06:33 +0000312 /// \brief Check if the retain/release is due to a GCD/XPC macro that are
313 /// defined as:
314 ///
315 /// #define dispatch_retain(object) ({ dispatch_object_t _o = (object); _dispatch_object_validate(_o); (void)[_o retain]; })
316 /// #define dispatch_release(object) ({ dispatch_object_t _o = (object); _dispatch_object_validate(_o); [_o release]; })
317 /// #define xpc_retain(object) ({ xpc_object_t _o = (object); _xpc_object_validate(_o); [_o retain]; })
318 /// #define xpc_release(object) ({ xpc_object_t _o = (object); _xpc_object_validate(_o); [_o release]; })
319 ///
320 /// and return the top container which is the StmtExpr and the macro argument
321 /// expression.
322 void checkForGCDOrXPC(ObjCMessageExpr *Msg, Expr *&RecContainer,
323 Expr *&Rec, SourceRange &RecRange) {
324 SourceLocation Loc = Msg->getExprLoc();
325 if (!Loc.isMacroID())
326 return;
327 SourceManager &SM = Pass.Ctx.getSourceManager();
328 StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM,
David Blaikiebbafb8a2012-03-11 07:00:24 +0000329 Pass.Ctx.getLangOpts());
Ted Kremenekf7639e12012-03-06 20:06:33 +0000330 bool isGCDOrXPC = llvm::StringSwitch<bool>(MacroName)
331 .Case("dispatch_retain", true)
332 .Case("dispatch_release", true)
333 .Case("xpc_retain", true)
334 .Case("xpc_release", true)
335 .Default(false);
336 if (!isGCDOrXPC)
337 return;
338
Craig Topper8ae12032014-05-07 06:21:57 +0000339 StmtExpr *StmtE = nullptr;
Ted Kremenekf7639e12012-03-06 20:06:33 +0000340 Stmt *S = Msg;
341 while (S) {
342 if (StmtExpr *SE = dyn_cast<StmtExpr>(S)) {
343 StmtE = SE;
344 break;
345 }
346 S = StmtMap->getParent(S);
347 }
348
349 if (!StmtE)
350 return;
351
352 Stmt::child_range StmtExprChild = StmtE->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +0000353 if (StmtExprChild.begin() == StmtExprChild.end())
Ted Kremenekf7639e12012-03-06 20:06:33 +0000354 return;
Benjamin Kramer5733e352015-07-18 17:09:36 +0000355 auto *CompS = dyn_cast_or_null<CompoundStmt>(*StmtExprChild.begin());
Ted Kremenekf7639e12012-03-06 20:06:33 +0000356 if (!CompS)
357 return;
358
359 Stmt::child_range CompStmtChild = CompS->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +0000360 if (CompStmtChild.begin() == CompStmtChild.end())
Ted Kremenekf7639e12012-03-06 20:06:33 +0000361 return;
Benjamin Kramer5733e352015-07-18 17:09:36 +0000362 auto *DeclS = dyn_cast_or_null<DeclStmt>(*CompStmtChild.begin());
Ted Kremenekf7639e12012-03-06 20:06:33 +0000363 if (!DeclS)
364 return;
365 if (!DeclS->isSingleDecl())
366 return;
367 VarDecl *VD = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl());
368 if (!VD)
369 return;
370 Expr *Init = VD->getInit();
371 if (!Init)
372 return;
373
374 RecContainer = StmtE;
375 Rec = Init->IgnoreParenImpCasts();
376 if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Rec))
377 Rec = EWC->getSubExpr()->IgnoreParenImpCasts();
378 RecRange = Rec->getSourceRange();
379 if (SM.isMacroArgExpansion(RecRange.getBegin()))
380 RecRange.setBegin(SM.getImmediateSpellingLoc(RecRange.getBegin()));
381 if (SM.isMacroArgExpansion(RecRange.getEnd()))
382 RecRange.setEnd(SM.getImmediateSpellingLoc(RecRange.getEnd()));
383 }
384
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000385 void clearDiagnostics(SourceLocation loc) const {
386 Pass.TA.clearDiagnostic(diag::err_arc_illegal_explicit_message,
387 diag::err_unavailable,
388 diag::err_unavailable_message,
389 loc);
390 }
391
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +0000392 bool isDelegateMessage(Expr *E) const {
393 if (!E) return false;
394
395 E = E->IgnoreParenCasts();
John McCallfe96e0b2011-11-06 09:01:30 +0000396
397 // Also look through property-getter sugar.
398 if (PseudoObjectExpr *pseudoOp = dyn_cast<PseudoObjectExpr>(E))
399 E = pseudoOp->getResultExpr()->IgnoreImplicit();
400
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +0000401 if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E))
402 return (ME->isInstanceMessage() && ME->getSelector() == DelegateSel);
403
Argyrios Kyrtzidisa6fe4bf2011-07-15 23:48:56 +0000404 return false;
405 }
406
Argyrios Kyrtzidis6a8a14d2011-07-15 21:11:23 +0000407 bool isInAtFinally(Expr *E) const {
408 assert(E);
409 Stmt *S = E;
410 while (S) {
411 if (isa<ObjCAtFinallyStmt>(S))
412 return true;
413 S = StmtMap->getParent(S);
414 }
415
416 return false;
417 }
418
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000419 bool isRemovable(Expr *E) const {
420 return Removables.count(E);
421 }
422
423 bool tryRemoving(Expr *E) const {
424 if (isRemovable(E)) {
425 Pass.TA.removeStmt(E);
426 return true;
427 }
428
John McCall4db5c3c2011-07-07 06:58:02 +0000429 Stmt *parent = StmtMap->getParent(E);
430
431 if (ImplicitCastExpr *castE = dyn_cast_or_null<ImplicitCastExpr>(parent))
432 return tryRemoving(castE);
433
434 if (ParenExpr *parenE = dyn_cast_or_null<ParenExpr>(parent))
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000435 return tryRemoving(parenE);
436
437 if (BinaryOperator *
John McCall4db5c3c2011-07-07 06:58:02 +0000438 bopE = dyn_cast_or_null<BinaryOperator>(parent)) {
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000439 if (bopE->getOpcode() == BO_Comma && bopE->getLHS() == E &&
440 isRemovable(bopE)) {
441 Pass.TA.replace(bopE->getSourceRange(), bopE->getRHS()->getSourceRange());
442 return true;
443 }
444 }
445
446 return false;
447 }
448
449};
450
451} // anonymous namespace
452
Argyrios Kyrtzidisd2b91122011-11-04 15:58:22 +0000453void trans::removeRetainReleaseDeallocFinalize(MigrationPass &pass) {
Argyrios Kyrtzidise5b475c2011-06-21 20:20:39 +0000454 BodyTransform<RetainReleaseDeallocRemover> trans(pass);
455 trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
456}