1 //===--- TransEmptyStatements.cpp - Tranformations 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/StmtVisitor.h"
25 #include "clang/Basic/SourceManager.h"
27 using namespace clang;
28 using namespace arcmt;
29 using namespace trans;
31 static bool isEmptyARCMTMacroStatement(NullStmt *S,
32 std::vector<SourceLocation> &MacroLocs,
34 if (!S->hasLeadingEmptyMacro())
37 SourceLocation SemiLoc = S->getSemiLoc();
38 if (SemiLoc.isInvalid() || SemiLoc.isMacroID())
41 if (MacroLocs.empty())
44 SourceManager &SM = Ctx.getSourceManager();
45 std::vector<SourceLocation>::iterator
46 I = std::upper_bound(MacroLocs.begin(), MacroLocs.end(), SemiLoc,
47 SourceManager::LocBeforeThanCompare(SM));
50 AfterMacroLoc = I->getLocWithOffset(getARCMTMacroName().size());
51 assert(AfterMacroLoc.isFileID());
53 if (AfterMacroLoc == SemiLoc)
57 if (!SM.isInSameSLocAddrSpace(AfterMacroLoc, SemiLoc, &RelOffs))
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
66 if (RelOffs - getARCMTMacroName().size() > 100)
69 SourceLocation AfterMacroSemiLoc = findSemiAfterLocation(AfterMacroLoc, Ctx);
70 return AfterMacroSemiLoc == SemiLoc;
75 /// \brief Returns true if the statement became empty due to previous
77 class EmptyChecker : public StmtVisitor<EmptyChecker, bool> {
79 std::vector<SourceLocation> &MacroLocs;
82 EmptyChecker(ASTContext &ctx, std::vector<SourceLocation> ¯oLocs)
83 : Ctx(ctx), MacroLocs(macroLocs) { }
85 bool VisitNullStmt(NullStmt *S) {
86 return isEmptyARCMTMacroStatement(S, MacroLocs, Ctx);
88 bool VisitCompoundStmt(CompoundStmt *S) {
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)
97 bool VisitIfStmt(IfStmt *S) {
98 if (S->getConditionVariable())
100 Expr *condE = S->getCond();
103 if (hasSideEffects(condE, Ctx))
105 if (!S->getThen() || !Visit(S->getThen()))
107 if (S->getElse() && !Visit(S->getElse()))
111 bool VisitWhileStmt(WhileStmt *S) {
112 if (S->getConditionVariable())
114 Expr *condE = S->getCond();
117 if (hasSideEffects(condE, Ctx))
121 return Visit(S->getBody());
123 bool VisitDoStmt(DoStmt *S) {
124 Expr *condE = S->getCond();
127 if (hasSideEffects(condE, Ctx))
131 return Visit(S->getBody());
133 bool VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
134 Expr *Exp = S->getCollection();
137 if (hasSideEffects(Exp, Ctx))
141 return Visit(S->getBody());
143 bool VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) {
144 if (!S->getSubStmt())
146 return Visit(S->getSubStmt());
150 class EmptyStatementsRemover :
151 public RecursiveASTVisitor<EmptyStatementsRemover> {
155 EmptyStatementsRemover(MigrationPass &pass) : Pass(pass) { }
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) {
168 bool VisitCompoundStmt(CompoundStmt *S) {
169 for (CompoundStmt::body_iterator
170 I = S->body_begin(), E = S->body_end(); I != E; ++I)
175 ASTContext &getContext() { return Pass.Ctx; }
178 void check(Stmt *S) {
180 if (EmptyChecker(Pass.Ctx, Pass.ARCMTMacroLocs).Visit(S)) {
181 Transaction Trans(Pass.TA);
182 Pass.TA.removeStmt(S);
187 } // anonymous namespace
189 static bool isBodyEmpty(CompoundStmt *body, ASTContext &Ctx,
190 std::vector<SourceLocation> &MacroLocs) {
191 for (CompoundStmt::body_iterator
192 I = body->body_begin(), E = body->body_end(); I != E; ++I)
193 if (!EmptyChecker(Ctx, MacroLocs).Visit(*I))
199 static void removeDeallocMethod(MigrationPass &pass) {
200 ASTContext &Ctx = pass.Ctx;
201 TransformActions &TA = pass.TA;
202 DeclContext *DC = Ctx.getTranslationUnitDecl();
204 typedef DeclContext::specific_decl_iterator<ObjCImplementationDecl>
206 for (impl_iterator I = impl_iterator(DC->decls_begin()),
207 E = impl_iterator(DC->decls_end()); I != E; ++I) {
208 for (ObjCImplementationDecl::instmeth_iterator
209 MI = (*I)->instmeth_begin(),
210 ME = (*I)->instmeth_end(); MI != ME; ++MI) {
211 ObjCMethodDecl *MD = *MI;
212 if (MD->getMethodFamily() == OMF_dealloc) {
214 isBodyEmpty(MD->getCompoundBody(), Ctx, pass.ARCMTMacroLocs)) {
215 Transaction Trans(TA);
216 TA.remove(MD->getSourceRange());
224 void trans::removeEmptyStatementsAndDealloc(MigrationPass &pass) {
225 EmptyStatementsRemover(pass).TraverseDecl(pass.Ctx.getTranslationUnitDecl());
227 removeDeallocMethod(pass);
229 for (unsigned i = 0, e = pass.ARCMTMacroLocs.size(); i != e; ++i) {
230 Transaction Trans(pass.TA);
231 pass.TA.remove(pass.ARCMTMacroLocs[i]);