blob: 6f917b324655f6ccc6f3844a1e8ce3256d7dc6cd [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"
Argyrios Kyrtzidis1aa60ff2011-07-15 22:04:00 +000022#include "clang/Sema/Sema.h"
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000023#include "clang/Sema/SemaDiagnostic.h"
Argyrios Kyrtzidis1aa60ff2011-07-15 22:04:00 +000024#include "clang/Lex/Preprocessor.h"
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000025#include "clang/AST/ParentMap.h"
26
27using namespace clang;
28using namespace arcmt;
29using namespace trans;
30using llvm::StringRef;
31
32namespace {
33
34class RetainReleaseDeallocRemover :
35 public RecursiveASTVisitor<RetainReleaseDeallocRemover> {
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000036 Stmt *Body;
37 MigrationPass &Pass;
38
39 ExprSet Removables;
40 llvm::OwningPtr<ParentMap> StmtMap;
41
42public:
Argyrios Kyrtzidisb1094a02011-06-23 21:21:33 +000043 RetainReleaseDeallocRemover(MigrationPass &pass)
44 : Body(0), Pass(pass) { }
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000045
46 void transformBody(Stmt *body) {
47 Body = body;
48 collectRemovables(body, Removables);
49 StmtMap.reset(new ParentMap(body));
50 TraverseStmt(body);
51 }
52
53 bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
54 switch (E->getMethodFamily()) {
55 default:
56 return true;
Argyrios Kyrtzidise0e40762011-07-14 21:26:49 +000057 case OMF_autorelease:
58 if (isRemovable(E)) {
59 // An unused autorelease is badness. If we remove it the receiver
60 // will likely die immediately while previously it was kept alive
61 // by the autorelease pool. This is bad practice in general, leave it
62 // and emit an error to force the user to restructure his code.
63 std::string err = "it is not safe to remove an unused '";
64 err += E->getSelector().getAsString() + "'; its receiver may be "
65 "destroyed immediately";
66 Pass.TA.reportError(err, E->getLocStart(), E->getSourceRange());
67 return true;
68 }
69 // Pass through.
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000070 case OMF_retain:
71 case OMF_release:
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000072 if (E->getReceiverKind() == ObjCMessageExpr::Instance)
73 if (Expr *rec = E->getInstanceReceiver()) {
74 rec = rec->IgnoreParenImpCasts();
Argyrios Kyrtzidis2cd53662011-07-14 22:46:12 +000075 if (rec->getType().getObjCLifetime() == Qualifiers::OCL_ExplicitNone &&
76 (E->getMethodFamily() != OMF_retain || isRemovable(E))) {
Argyrios Kyrtzidise0e40762011-07-14 21:26:49 +000077 std::string err = "it is not safe to remove '";
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000078 err += E->getSelector().getAsString() + "' message on "
79 "an __unsafe_unretained type";
80 Pass.TA.reportError(err, rec->getLocStart());
81 return true;
82 }
Argyrios Kyrtzidis2c18ca02011-07-14 23:32:04 +000083
84 if (isGlobalVar(rec) &&
85 (E->getMethodFamily() != OMF_retain || isRemovable(E))) {
86 std::string err = "it is not safe to remove '";
87 err += E->getSelector().getAsString() + "' message on "
88 "a global variable";
89 Pass.TA.reportError(err, rec->getLocStart());
90 return true;
91 }
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000092 }
93 case OMF_dealloc:
94 break;
95 }
96
97 switch (E->getReceiverKind()) {
98 default:
99 return true;
100 case ObjCMessageExpr::SuperInstance: {
101 Transaction Trans(Pass.TA);
Argyrios Kyrtzidisf55a8692011-07-15 21:11:23 +0000102 clearDiagnostics(E->getSuperLoc());
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000103 if (tryRemoving(E))
104 return true;
105 Pass.TA.replace(E->getSourceRange(), "self");
106 return true;
107 }
108 case ObjCMessageExpr::Instance:
109 break;
110 }
111
112 Expr *rec = E->getInstanceReceiver();
113 if (!rec) return true;
114
115 Transaction Trans(Pass.TA);
Argyrios Kyrtzidisf55a8692011-07-15 21:11:23 +0000116 clearDiagnostics(rec->getExprLoc());
117
118 if (E->getMethodFamily() == OMF_release &&
119 isRemovable(E) && isInAtFinally(E)) {
Argyrios Kyrtzidis1aa60ff2011-07-15 22:04:00 +0000120 // Change the -release to "receiver = nil" in a finally to avoid a leak
Argyrios Kyrtzidisf55a8692011-07-15 21:11:23 +0000121 // when an exception is thrown.
122 Pass.TA.replace(E->getSourceRange(), rec->getSourceRange());
Argyrios Kyrtzidis1aa60ff2011-07-15 22:04:00 +0000123 if (Pass.SemaRef.getPreprocessor()
124 .getIdentifierInfo("nil")->hasMacroDefinition())
125 Pass.TA.insertAfterToken(rec->getLocEnd(), " = nil");
126 else
127 Pass.TA.insertAfterToken(rec->getLocEnd(), " = 0");
Argyrios Kyrtzidisf55a8692011-07-15 21:11:23 +0000128 return true;
129 }
130
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000131 if (!hasSideEffects(E, Pass.Ctx)) {
132 if (tryRemoving(E))
133 return true;
134 }
135 Pass.TA.replace(E->getSourceRange(), rec->getSourceRange());
136
137 return true;
138 }
139
140private:
Argyrios Kyrtzidisf55a8692011-07-15 21:11:23 +0000141 void clearDiagnostics(SourceLocation loc) const {
142 Pass.TA.clearDiagnostic(diag::err_arc_illegal_explicit_message,
143 diag::err_unavailable,
144 diag::err_unavailable_message,
145 loc);
146 }
147
148 bool isInAtFinally(Expr *E) const {
149 assert(E);
150 Stmt *S = E;
151 while (S) {
152 if (isa<ObjCAtFinallyStmt>(S))
153 return true;
154 S = StmtMap->getParent(S);
155 }
156
157 return false;
158 }
159
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000160 bool isRemovable(Expr *E) const {
161 return Removables.count(E);
162 }
163
164 bool tryRemoving(Expr *E) const {
165 if (isRemovable(E)) {
166 Pass.TA.removeStmt(E);
167 return true;
168 }
169
John McCall7e5e5f42011-07-07 06:58:02 +0000170 Stmt *parent = StmtMap->getParent(E);
171
172 if (ImplicitCastExpr *castE = dyn_cast_or_null<ImplicitCastExpr>(parent))
173 return tryRemoving(castE);
174
175 if (ParenExpr *parenE = dyn_cast_or_null<ParenExpr>(parent))
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000176 return tryRemoving(parenE);
177
178 if (BinaryOperator *
John McCall7e5e5f42011-07-07 06:58:02 +0000179 bopE = dyn_cast_or_null<BinaryOperator>(parent)) {
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000180 if (bopE->getOpcode() == BO_Comma && bopE->getLHS() == E &&
181 isRemovable(bopE)) {
182 Pass.TA.replace(bopE->getSourceRange(), bopE->getRHS()->getSourceRange());
183 return true;
184 }
185 }
186
187 return false;
188 }
189
190};
191
192} // anonymous namespace
193
194void trans::removeRetainReleaseDealloc(MigrationPass &pass) {
195 BodyTransform<RetainReleaseDeallocRemover> trans(pass);
196 trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
197}