1 //===--- TransEmptyStatements.cpp - Transformations to ARC mode -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // removeEmptyStatementsAndDealloc:
12 // Removes empty statements that are leftovers from previous transformations.
17 // removeRetainReleaseDealloc will leave an empty ";" that removeEmptyStatements
20 //===----------------------------------------------------------------------===//
22 #include "Transforms.h"
23 #include "Internals.h"
24 #include "clang/AST/ASTContext.h"
25 #include "clang/AST/StmtVisitor.h"
26 #include "clang/Basic/SourceManager.h"
28 using namespace clang;
29 using namespace arcmt;
30 using namespace trans;
32 static bool isEmptyARCMTMacroStatement(NullStmt *S,
33 std::vector<SourceLocation> &MacroLocs,
35 if (!S->hasLeadingEmptyMacro())
38 SourceLocation SemiLoc = S->getSemiLoc();
39 if (SemiLoc.isInvalid() || SemiLoc.isMacroID())
42 if (MacroLocs.empty())
45 SourceManager &SM = Ctx.getSourceManager();
46 std::vector<SourceLocation>::iterator
47 I = std::upper_bound(MacroLocs.begin(), MacroLocs.end(), SemiLoc,
48 BeforeThanCompare<SourceLocation>(SM));
51 AfterMacroLoc = I->getLocWithOffset(getARCMTMacroName().size());
52 assert(AfterMacroLoc.isFileID());
54 if (AfterMacroLoc == SemiLoc)
58 if (!SM.isInSameSLocAddrSpace(AfterMacroLoc, SemiLoc, &RelOffs))
63 // We make the reasonable assumption that a semicolon after 100 characters
64 // means that it is not the next token after our macro. If this assumption
65 // fails it is not critical, we will just fail to clear out, e.g., an empty
67 if (RelOffs - getARCMTMacroName().size() > 100)
70 SourceLocation AfterMacroSemiLoc = findSemiAfterLocation(AfterMacroLoc, Ctx);
71 return AfterMacroSemiLoc == SemiLoc;
76 /// \brief Returns true if the statement became empty due to previous
78 class EmptyChecker : public StmtVisitor<EmptyChecker, bool> {
80 std::vector<SourceLocation> &MacroLocs;
83 EmptyChecker(ASTContext &ctx, std::vector<SourceLocation> ¯oLocs)
84 : Ctx(ctx), MacroLocs(macroLocs) { }
86 bool VisitNullStmt(NullStmt *S) {
87 return isEmptyARCMTMacroStatement(S, MacroLocs, Ctx);
89 bool VisitCompoundStmt(CompoundStmt *S) {
91 return false; // was already empty, not because of transformations.
92 for (CompoundStmt::body_iterator
93 I = S->body_begin(), E = S->body_end(); I != E; ++I)
98 bool VisitIfStmt(IfStmt *S) {
99 if (S->getConditionVariable())
101 Expr *condE = S->getCond();
104 if (hasSideEffects(condE, Ctx))
106 if (!S->getThen() || !Visit(S->getThen()))
108 if (S->getElse() && !Visit(S->getElse()))
112 bool VisitWhileStmt(WhileStmt *S) {
113 if (S->getConditionVariable())
115 Expr *condE = S->getCond();
118 if (hasSideEffects(condE, Ctx))
122 return Visit(S->getBody());
124 bool VisitDoStmt(DoStmt *S) {
125 Expr *condE = S->getCond();
128 if (hasSideEffects(condE, Ctx))
132 return Visit(S->getBody());
134 bool VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
135 Expr *Exp = S->getCollection();
138 if (hasSideEffects(Exp, Ctx))
142 return Visit(S->getBody());
144 bool VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) {
145 if (!S->getSubStmt())
147 return Visit(S->getSubStmt());
151 class EmptyStatementsRemover :
152 public RecursiveASTVisitor<EmptyStatementsRemover> {
156 EmptyStatementsRemover(MigrationPass &pass) : Pass(pass) { }
158 bool TraverseStmtExpr(StmtExpr *E) {
159 CompoundStmt *S = E->getSubStmt();
160 for (CompoundStmt::body_iterator
161 I = S->body_begin(), E = S->body_end(); I != E; ++I) {
169 bool VisitCompoundStmt(CompoundStmt *S) {
170 for (CompoundStmt::body_iterator
171 I = S->body_begin(), E = S->body_end(); I != E; ++I)
176 ASTContext &getContext() { return Pass.Ctx; }
179 void check(Stmt *S) {
181 if (EmptyChecker(Pass.Ctx, Pass.ARCMTMacroLocs).Visit(S)) {
182 Transaction Trans(Pass.TA);
183 Pass.TA.removeStmt(S);
188 } // anonymous namespace
190 static bool isBodyEmpty(CompoundStmt *body, ASTContext &Ctx,
191 std::vector<SourceLocation> &MacroLocs) {
192 for (CompoundStmt::body_iterator
193 I = body->body_begin(), E = body->body_end(); I != E; ++I)
194 if (!EmptyChecker(Ctx, MacroLocs).Visit(*I))
200 static void cleanupDeallocOrFinalize(MigrationPass &pass) {
201 ASTContext &Ctx = pass.Ctx;
202 TransformActions &TA = pass.TA;
203 DeclContext *DC = Ctx.getTranslationUnitDecl();
204 Selector FinalizeSel =
205 Ctx.Selectors.getNullarySelector(&pass.Ctx.Idents.get("finalize"));
207 typedef DeclContext::specific_decl_iterator<ObjCImplementationDecl>
209 for (impl_iterator I = impl_iterator(DC->decls_begin()),
210 E = impl_iterator(DC->decls_end()); I != E; ++I) {
211 ObjCMethodDecl *DeallocM = 0;
212 ObjCMethodDecl *FinalizeM = 0;
213 for (ObjCImplementationDecl::instmeth_iterator
214 MI = I->instmeth_begin(),
215 ME = I->instmeth_end(); MI != ME; ++MI) {
216 ObjCMethodDecl *MD = *MI;
220 if (MD->getMethodFamily() == OMF_dealloc) {
222 } else if (MD->isInstanceMethod() && MD->getSelector() == FinalizeSel) {
228 if (isBodyEmpty(DeallocM->getCompoundBody(), Ctx, pass.ARCMTMacroLocs)) {
229 Transaction Trans(TA);
230 TA.remove(DeallocM->getSourceRange());
234 Transaction Trans(TA);
235 TA.remove(FinalizeM->getSourceRange());
238 } else if (FinalizeM) {
239 if (isBodyEmpty(FinalizeM->getCompoundBody(), Ctx, pass.ARCMTMacroLocs)) {
240 Transaction Trans(TA);
241 TA.remove(FinalizeM->getSourceRange());
243 Transaction Trans(TA);
244 TA.replaceText(FinalizeM->getSelectorStartLoc(), "finalize", "dealloc");
250 void trans::removeEmptyStatementsAndDeallocFinalize(MigrationPass &pass) {
251 EmptyStatementsRemover(pass).TraverseDecl(pass.Ctx.getTranslationUnitDecl());
253 cleanupDeallocOrFinalize(pass);
255 for (unsigned i = 0, e = pass.ARCMTMacroLocs.size(); i != e; ++i) {
256 Transaction Trans(pass.TA);
257 pass.TA.remove(pass.ARCMTMacroLocs[i]);