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 (auto *I : S->body())
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 (auto *I : S->body())
174 ASTContext &getContext() { return Pass.Ctx; }
177 void check(Stmt *S) {
179 if (EmptyChecker(Pass.Ctx, Pass.ARCMTMacroLocs).Visit(S)) {
180 Transaction Trans(Pass.TA);
181 Pass.TA.removeStmt(S);
186 } // anonymous namespace
188 static bool isBodyEmpty(CompoundStmt *body, ASTContext &Ctx,
189 std::vector<SourceLocation> &MacroLocs) {
190 for (auto *I : body->body())
191 if (!EmptyChecker(Ctx, MacroLocs).Visit(I))
197 static void cleanupDeallocOrFinalize(MigrationPass &pass) {
198 ASTContext &Ctx = pass.Ctx;
199 TransformActions &TA = pass.TA;
200 DeclContext *DC = Ctx.getTranslationUnitDecl();
201 Selector FinalizeSel =
202 Ctx.Selectors.getNullarySelector(&pass.Ctx.Idents.get("finalize"));
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 ObjCMethodDecl *DeallocM = nullptr;
209 ObjCMethodDecl *FinalizeM = nullptr;
210 for (auto *MD : I->instance_methods()) {
214 if (MD->getMethodFamily() == OMF_dealloc) {
216 } else if (MD->isInstanceMethod() && MD->getSelector() == FinalizeSel) {
222 if (isBodyEmpty(DeallocM->getCompoundBody(), Ctx, pass.ARCMTMacroLocs)) {
223 Transaction Trans(TA);
224 TA.remove(DeallocM->getSourceRange());
228 Transaction Trans(TA);
229 TA.remove(FinalizeM->getSourceRange());
232 } else if (FinalizeM) {
233 if (isBodyEmpty(FinalizeM->getCompoundBody(), Ctx, pass.ARCMTMacroLocs)) {
234 Transaction Trans(TA);
235 TA.remove(FinalizeM->getSourceRange());
237 Transaction Trans(TA);
238 TA.replaceText(FinalizeM->getSelectorStartLoc(), "finalize", "dealloc");
244 void trans::removeEmptyStatementsAndDeallocFinalize(MigrationPass &pass) {
245 EmptyStatementsRemover(pass).TraverseDecl(pass.Ctx.getTranslationUnitDecl());
247 cleanupDeallocOrFinalize(pass);
249 for (unsigned i = 0, e = pass.ARCMTMacroLocs.size(); i != e; ++i) {
250 Transaction Trans(pass.TA);
251 pass.TA.remove(pass.ARCMTMacroLocs[i]);