blob: 0fb7141544aa170fcce9a49d1e4c28a3a26e8f55 [file] [log] [blame]
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +00001//===--- TransEmptyStatements.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// removeEmptyStatementsAndDealloc:
11//
12// Removes empty statements that are leftovers from previous transformations.
13// e.g for
14//
15// [x retain];
16//
17// removeRetainReleaseDealloc will leave an empty ";" that removeEmptyStatements
18// will remove.
19//
20//===----------------------------------------------------------------------===//
21
22#include "Transforms.h"
23#include "Internals.h"
24#include "clang/AST/StmtVisitor.h"
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +000025#include "clang/Basic/SourceManager.h"
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +000026
27using namespace clang;
28using namespace arcmt;
29using namespace trans;
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +000030
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +000031static bool isEmptyARCMTMacroStatement(NullStmt *S,
32 std::vector<SourceLocation> &MacroLocs,
33 ASTContext &Ctx) {
34 if (!S->hasLeadingEmptyMacro())
35 return false;
36
37 SourceLocation SemiLoc = S->getSemiLoc();
38 if (SemiLoc.isInvalid() || SemiLoc.isMacroID())
39 return false;
40
41 if (MacroLocs.empty())
42 return false;
43
44 SourceManager &SM = Ctx.getSourceManager();
45 std::vector<SourceLocation>::iterator
46 I = std::upper_bound(MacroLocs.begin(), MacroLocs.end(), SemiLoc,
47 SourceManager::LocBeforeThanCompare(SM));
48 --I;
49 SourceLocation
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000050 AfterMacroLoc = I->getLocWithOffset(getARCMTMacroName().size());
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +000051 assert(AfterMacroLoc.isFileID());
52
53 if (AfterMacroLoc == SemiLoc)
54 return true;
55
56 int RelOffs = 0;
57 if (!SM.isInSameSLocAddrSpace(AfterMacroLoc, SemiLoc, &RelOffs))
58 return false;
59 if (RelOffs < 0)
60 return false;
61
62 // We make the reasonable assumption that a semicolon after 100 characters
63 // means that it is not the next token after our macro. If this assumption
64 // fails it is not critical, we will just fail to clear out, e.g., an empty
65 // 'if'.
66 if (RelOffs - getARCMTMacroName().size() > 100)
67 return false;
68
69 SourceLocation AfterMacroSemiLoc = findSemiAfterLocation(AfterMacroLoc, Ctx);
70 return AfterMacroSemiLoc == SemiLoc;
71}
72
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +000073namespace {
74
75/// \brief Returns true if the statement became empty due to previous
76/// transformations.
77class EmptyChecker : public StmtVisitor<EmptyChecker, bool> {
78 ASTContext &Ctx;
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +000079 std::vector<SourceLocation> &MacroLocs;
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +000080
81public:
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +000082 EmptyChecker(ASTContext &ctx, std::vector<SourceLocation> &macroLocs)
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +000083 : Ctx(ctx), MacroLocs(macroLocs) { }
84
85 bool VisitNullStmt(NullStmt *S) {
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +000086 return isEmptyARCMTMacroStatement(S, MacroLocs, Ctx);
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +000087 }
88 bool VisitCompoundStmt(CompoundStmt *S) {
89 if (S->body_empty())
90 return false; // was already empty, not because of transformations.
91 for (CompoundStmt::body_iterator
92 I = S->body_begin(), E = S->body_end(); I != E; ++I)
93 if (!Visit(*I))
94 return false;
95 return true;
96 }
97 bool VisitIfStmt(IfStmt *S) {
98 if (S->getConditionVariable())
99 return false;
100 Expr *condE = S->getCond();
101 if (!condE)
102 return false;
103 if (hasSideEffects(condE, Ctx))
104 return false;
105 if (!S->getThen() || !Visit(S->getThen()))
106 return false;
107 if (S->getElse() && !Visit(S->getElse()))
108 return false;
109 return true;
110 }
111 bool VisitWhileStmt(WhileStmt *S) {
112 if (S->getConditionVariable())
113 return false;
114 Expr *condE = S->getCond();
115 if (!condE)
116 return false;
117 if (hasSideEffects(condE, Ctx))
118 return false;
119 if (!S->getBody())
120 return false;
121 return Visit(S->getBody());
122 }
123 bool VisitDoStmt(DoStmt *S) {
124 Expr *condE = S->getCond();
125 if (!condE)
126 return false;
127 if (hasSideEffects(condE, Ctx))
128 return false;
129 if (!S->getBody())
130 return false;
131 return Visit(S->getBody());
132 }
133 bool VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
134 Expr *Exp = S->getCollection();
135 if (!Exp)
136 return false;
137 if (hasSideEffects(Exp, Ctx))
138 return false;
139 if (!S->getBody())
140 return false;
141 return Visit(S->getBody());
142 }
143 bool VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) {
144 if (!S->getSubStmt())
145 return false;
146 return Visit(S->getSubStmt());
147 }
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000148};
149
150class EmptyStatementsRemover :
151 public RecursiveASTVisitor<EmptyStatementsRemover> {
152 MigrationPass &Pass;
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000153
154public:
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +0000155 EmptyStatementsRemover(MigrationPass &pass) : Pass(pass) { }
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000156
157 bool TraverseStmtExpr(StmtExpr *E) {
158 CompoundStmt *S = E->getSubStmt();
159 for (CompoundStmt::body_iterator
160 I = S->body_begin(), E = S->body_end(); I != E; ++I) {
161 if (I != E - 1)
162 check(*I);
163 TraverseStmt(*I);
164 }
165 return true;
166 }
167
168 bool VisitCompoundStmt(CompoundStmt *S) {
169 for (CompoundStmt::body_iterator
170 I = S->body_begin(), E = S->body_end(); I != E; ++I)
171 check(*I);
172 return true;
173 }
174
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000175 ASTContext &getContext() { return Pass.Ctx; }
176
177private:
178 void check(Stmt *S) {
179 if (!S) return;
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +0000180 if (EmptyChecker(Pass.Ctx, Pass.ARCMTMacroLocs).Visit(S)) {
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000181 Transaction Trans(Pass.TA);
182 Pass.TA.removeStmt(S);
183 }
184 }
185};
186
187} // anonymous namespace
188
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +0000189static bool isBodyEmpty(CompoundStmt *body, ASTContext &Ctx,
190 std::vector<SourceLocation> &MacroLocs) {
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000191 for (CompoundStmt::body_iterator
192 I = body->body_begin(), E = body->body_end(); I != E; ++I)
193 if (!EmptyChecker(Ctx, MacroLocs).Visit(*I))
194 return false;
195
196 return true;
197}
198
Argyrios Kyrtzidise7ef8552011-11-04 15:58:22 +0000199static void cleanupDeallocOrFinalize(MigrationPass &pass) {
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000200 ASTContext &Ctx = pass.Ctx;
201 TransformActions &TA = pass.TA;
202 DeclContext *DC = Ctx.getTranslationUnitDecl();
Argyrios Kyrtzidise7ef8552011-11-04 15:58:22 +0000203 Selector FinalizeSel =
204 Ctx.Selectors.getNullarySelector(&pass.Ctx.Idents.get("finalize"));
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000205
206 typedef DeclContext::specific_decl_iterator<ObjCImplementationDecl>
207 impl_iterator;
208 for (impl_iterator I = impl_iterator(DC->decls_begin()),
209 E = impl_iterator(DC->decls_end()); I != E; ++I) {
Argyrios Kyrtzidise7ef8552011-11-04 15:58:22 +0000210 ObjCMethodDecl *DeallocM = 0;
211 ObjCMethodDecl *FinalizeM = 0;
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000212 for (ObjCImplementationDecl::instmeth_iterator
213 MI = (*I)->instmeth_begin(),
214 ME = (*I)->instmeth_end(); MI != ME; ++MI) {
215 ObjCMethodDecl *MD = *MI;
Argyrios Kyrtzidise7ef8552011-11-04 15:58:22 +0000216 if (!MD->hasBody())
217 continue;
218
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000219 if (MD->getMethodFamily() == OMF_dealloc) {
Argyrios Kyrtzidise7ef8552011-11-04 15:58:22 +0000220 DeallocM = MD;
221 } else if (MD->isInstanceMethod() && MD->getSelector() == FinalizeSel) {
222 FinalizeM = MD;
223 }
224 }
225
226 if (DeallocM) {
227 if (isBodyEmpty(DeallocM->getCompoundBody(), Ctx, pass.ARCMTMacroLocs)) {
228 Transaction Trans(TA);
229 TA.remove(DeallocM->getSourceRange());
230 }
231
232 if (FinalizeM) {
233 Transaction Trans(TA);
234 TA.remove(FinalizeM->getSourceRange());
235 }
236
237 } else if (FinalizeM) {
238 if (isBodyEmpty(FinalizeM->getCompoundBody(), Ctx, pass.ARCMTMacroLocs)) {
239 Transaction Trans(TA);
240 TA.remove(FinalizeM->getSourceRange());
241 } else {
242 Transaction Trans(TA);
243 TA.replaceText(FinalizeM->getSelectorStartLoc(), "finalize", "dealloc");
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000244 }
245 }
246 }
247}
248
Argyrios Kyrtzidise7ef8552011-11-04 15:58:22 +0000249void trans::removeEmptyStatementsAndDeallocFinalize(MigrationPass &pass) {
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +0000250 EmptyStatementsRemover(pass).TraverseDecl(pass.Ctx.getTranslationUnitDecl());
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000251
Argyrios Kyrtzidise7ef8552011-11-04 15:58:22 +0000252 cleanupDeallocOrFinalize(pass);
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000253
254 for (unsigned i = 0, e = pass.ARCMTMacroLocs.size(); i != e; ++i) {
255 Transaction Trans(pass.TA);
256 pass.TA.remove(pass.ARCMTMacroLocs[i]);
257 }
258}