blob: 8e6c533991ec402b54d5eb9a746a2d801635d2b6 [file] [log] [blame]
Argyrios Kyrtzidis7196d062011-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"
24
25using namespace clang;
26using namespace arcmt;
27using namespace trans;
28using llvm::StringRef;
29
30namespace {
31
32class RetainReleaseDeallocRemover :
33 public RecursiveASTVisitor<RetainReleaseDeallocRemover> {
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000034 Stmt *Body;
35 MigrationPass &Pass;
36
37 ExprSet Removables;
38 llvm::OwningPtr<ParentMap> StmtMap;
39
40public:
Argyrios Kyrtzidisb1094a02011-06-23 21:21:33 +000041 RetainReleaseDeallocRemover(MigrationPass &pass)
42 : Body(0), Pass(pass) { }
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000043
44 void transformBody(Stmt *body) {
45 Body = body;
46 collectRemovables(body, Removables);
47 StmtMap.reset(new ParentMap(body));
48 TraverseStmt(body);
49 }
50
51 bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
52 switch (E->getMethodFamily()) {
53 default:
54 return true;
Argyrios Kyrtzidise0e40762011-07-14 21:26:49 +000055 case OMF_autorelease:
56 if (isRemovable(E)) {
57 // An unused autorelease is badness. If we remove it the receiver
58 // will likely die immediately while previously it was kept alive
59 // by the autorelease pool. This is bad practice in general, leave it
60 // and emit an error to force the user to restructure his code.
61 std::string err = "it is not safe to remove an unused '";
62 err += E->getSelector().getAsString() + "'; its receiver may be "
63 "destroyed immediately";
64 Pass.TA.reportError(err, E->getLocStart(), E->getSourceRange());
65 return true;
66 }
67 // Pass through.
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000068 case OMF_retain:
69 case OMF_release:
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000070 if (E->getReceiverKind() == ObjCMessageExpr::Instance)
71 if (Expr *rec = E->getInstanceReceiver()) {
72 rec = rec->IgnoreParenImpCasts();
Argyrios Kyrtzidis2cd53662011-07-14 22:46:12 +000073 if (rec->getType().getObjCLifetime() == Qualifiers::OCL_ExplicitNone &&
74 (E->getMethodFamily() != OMF_retain || isRemovable(E))) {
Argyrios Kyrtzidise0e40762011-07-14 21:26:49 +000075 std::string err = "it is not safe to remove '";
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000076 err += E->getSelector().getAsString() + "' message on "
77 "an __unsafe_unretained type";
78 Pass.TA.reportError(err, rec->getLocStart());
79 return true;
80 }
Argyrios Kyrtzidis2c18ca02011-07-14 23:32:04 +000081
82 if (isGlobalVar(rec) &&
83 (E->getMethodFamily() != OMF_retain || isRemovable(E))) {
84 std::string err = "it is not safe to remove '";
85 err += E->getSelector().getAsString() + "' message on "
86 "a global variable";
87 Pass.TA.reportError(err, rec->getLocStart());
88 return true;
89 }
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000090 }
91 case OMF_dealloc:
92 break;
93 }
94
95 switch (E->getReceiverKind()) {
96 default:
97 return true;
98 case ObjCMessageExpr::SuperInstance: {
99 Transaction Trans(Pass.TA);
Argyrios Kyrtzidisf55a8692011-07-15 21:11:23 +0000100 clearDiagnostics(E->getSuperLoc());
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000101 if (tryRemoving(E))
102 return true;
103 Pass.TA.replace(E->getSourceRange(), "self");
104 return true;
105 }
106 case ObjCMessageExpr::Instance:
107 break;
108 }
109
110 Expr *rec = E->getInstanceReceiver();
111 if (!rec) return true;
112
113 Transaction Trans(Pass.TA);
Argyrios Kyrtzidisf55a8692011-07-15 21:11:23 +0000114 clearDiagnostics(rec->getExprLoc());
115
116 if (E->getMethodFamily() == OMF_release &&
117 isRemovable(E) && isInAtFinally(E)) {
118 // Change the -release to "receiver = 0" in a finally to avoid a leak
119 // when an exception is thrown.
120 Pass.TA.replace(E->getSourceRange(), rec->getSourceRange());
121 Pass.TA.insertAfterToken(rec->getLocEnd(), " = 0");
122 return true;
123 }
124
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000125 if (!hasSideEffects(E, Pass.Ctx)) {
126 if (tryRemoving(E))
127 return true;
128 }
129 Pass.TA.replace(E->getSourceRange(), rec->getSourceRange());
130
131 return true;
132 }
133
134private:
Argyrios Kyrtzidisf55a8692011-07-15 21:11:23 +0000135 void clearDiagnostics(SourceLocation loc) const {
136 Pass.TA.clearDiagnostic(diag::err_arc_illegal_explicit_message,
137 diag::err_unavailable,
138 diag::err_unavailable_message,
139 loc);
140 }
141
142 bool isInAtFinally(Expr *E) const {
143 assert(E);
144 Stmt *S = E;
145 while (S) {
146 if (isa<ObjCAtFinallyStmt>(S))
147 return true;
148 S = StmtMap->getParent(S);
149 }
150
151 return false;
152 }
153
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000154 bool isRemovable(Expr *E) const {
155 return Removables.count(E);
156 }
157
158 bool tryRemoving(Expr *E) const {
159 if (isRemovable(E)) {
160 Pass.TA.removeStmt(E);
161 return true;
162 }
163
John McCall7e5e5f42011-07-07 06:58:02 +0000164 Stmt *parent = StmtMap->getParent(E);
165
166 if (ImplicitCastExpr *castE = dyn_cast_or_null<ImplicitCastExpr>(parent))
167 return tryRemoving(castE);
168
169 if (ParenExpr *parenE = dyn_cast_or_null<ParenExpr>(parent))
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000170 return tryRemoving(parenE);
171
172 if (BinaryOperator *
John McCall7e5e5f42011-07-07 06:58:02 +0000173 bopE = dyn_cast_or_null<BinaryOperator>(parent)) {
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000174 if (bopE->getOpcode() == BO_Comma && bopE->getLHS() == E &&
175 isRemovable(bopE)) {
176 Pass.TA.replace(bopE->getSourceRange(), bopE->getRHS()->getSourceRange());
177 return true;
178 }
179 }
180
181 return false;
182 }
183
184};
185
186} // anonymous namespace
187
188void trans::removeRetainReleaseDealloc(MigrationPass &pass) {
189 BodyTransform<RetainReleaseDeallocRemover> trans(pass);
190 trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
191}