1f4a2713aSLionel Sambuc //===--- TransEmptyStatements.cpp - Transformations to ARC mode -----------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // removeEmptyStatementsAndDealloc:
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc // Removes empty statements that are leftovers from previous transformations.
13f4a2713aSLionel Sambuc // e.g for
14f4a2713aSLionel Sambuc //
15f4a2713aSLionel Sambuc // [x retain];
16f4a2713aSLionel Sambuc //
17f4a2713aSLionel Sambuc // removeRetainReleaseDealloc will leave an empty ";" that removeEmptyStatements
18f4a2713aSLionel Sambuc // will remove.
19f4a2713aSLionel Sambuc //
20f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
21f4a2713aSLionel Sambuc
22f4a2713aSLionel Sambuc #include "Transforms.h"
23f4a2713aSLionel Sambuc #include "Internals.h"
24f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
25f4a2713aSLionel Sambuc #include "clang/AST/StmtVisitor.h"
26f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
27f4a2713aSLionel Sambuc
28f4a2713aSLionel Sambuc using namespace clang;
29f4a2713aSLionel Sambuc using namespace arcmt;
30f4a2713aSLionel Sambuc using namespace trans;
31f4a2713aSLionel Sambuc
isEmptyARCMTMacroStatement(NullStmt * S,std::vector<SourceLocation> & MacroLocs,ASTContext & Ctx)32f4a2713aSLionel Sambuc static bool isEmptyARCMTMacroStatement(NullStmt *S,
33f4a2713aSLionel Sambuc std::vector<SourceLocation> &MacroLocs,
34f4a2713aSLionel Sambuc ASTContext &Ctx) {
35f4a2713aSLionel Sambuc if (!S->hasLeadingEmptyMacro())
36f4a2713aSLionel Sambuc return false;
37f4a2713aSLionel Sambuc
38f4a2713aSLionel Sambuc SourceLocation SemiLoc = S->getSemiLoc();
39f4a2713aSLionel Sambuc if (SemiLoc.isInvalid() || SemiLoc.isMacroID())
40f4a2713aSLionel Sambuc return false;
41f4a2713aSLionel Sambuc
42f4a2713aSLionel Sambuc if (MacroLocs.empty())
43f4a2713aSLionel Sambuc return false;
44f4a2713aSLionel Sambuc
45f4a2713aSLionel Sambuc SourceManager &SM = Ctx.getSourceManager();
46f4a2713aSLionel Sambuc std::vector<SourceLocation>::iterator
47f4a2713aSLionel Sambuc I = std::upper_bound(MacroLocs.begin(), MacroLocs.end(), SemiLoc,
48f4a2713aSLionel Sambuc BeforeThanCompare<SourceLocation>(SM));
49f4a2713aSLionel Sambuc --I;
50f4a2713aSLionel Sambuc SourceLocation
51f4a2713aSLionel Sambuc AfterMacroLoc = I->getLocWithOffset(getARCMTMacroName().size());
52f4a2713aSLionel Sambuc assert(AfterMacroLoc.isFileID());
53f4a2713aSLionel Sambuc
54f4a2713aSLionel Sambuc if (AfterMacroLoc == SemiLoc)
55f4a2713aSLionel Sambuc return true;
56f4a2713aSLionel Sambuc
57f4a2713aSLionel Sambuc int RelOffs = 0;
58f4a2713aSLionel Sambuc if (!SM.isInSameSLocAddrSpace(AfterMacroLoc, SemiLoc, &RelOffs))
59f4a2713aSLionel Sambuc return false;
60f4a2713aSLionel Sambuc if (RelOffs < 0)
61f4a2713aSLionel Sambuc return false;
62f4a2713aSLionel Sambuc
63f4a2713aSLionel Sambuc // We make the reasonable assumption that a semicolon after 100 characters
64f4a2713aSLionel Sambuc // means that it is not the next token after our macro. If this assumption
65f4a2713aSLionel Sambuc // fails it is not critical, we will just fail to clear out, e.g., an empty
66f4a2713aSLionel Sambuc // 'if'.
67f4a2713aSLionel Sambuc if (RelOffs - getARCMTMacroName().size() > 100)
68f4a2713aSLionel Sambuc return false;
69f4a2713aSLionel Sambuc
70f4a2713aSLionel Sambuc SourceLocation AfterMacroSemiLoc = findSemiAfterLocation(AfterMacroLoc, Ctx);
71f4a2713aSLionel Sambuc return AfterMacroSemiLoc == SemiLoc;
72f4a2713aSLionel Sambuc }
73f4a2713aSLionel Sambuc
74f4a2713aSLionel Sambuc namespace {
75f4a2713aSLionel Sambuc
76f4a2713aSLionel Sambuc /// \brief Returns true if the statement became empty due to previous
77f4a2713aSLionel Sambuc /// transformations.
78f4a2713aSLionel Sambuc class EmptyChecker : public StmtVisitor<EmptyChecker, bool> {
79f4a2713aSLionel Sambuc ASTContext &Ctx;
80f4a2713aSLionel Sambuc std::vector<SourceLocation> &MacroLocs;
81f4a2713aSLionel Sambuc
82f4a2713aSLionel Sambuc public:
EmptyChecker(ASTContext & ctx,std::vector<SourceLocation> & macroLocs)83f4a2713aSLionel Sambuc EmptyChecker(ASTContext &ctx, std::vector<SourceLocation> ¯oLocs)
84f4a2713aSLionel Sambuc : Ctx(ctx), MacroLocs(macroLocs) { }
85f4a2713aSLionel Sambuc
VisitNullStmt(NullStmt * S)86f4a2713aSLionel Sambuc bool VisitNullStmt(NullStmt *S) {
87f4a2713aSLionel Sambuc return isEmptyARCMTMacroStatement(S, MacroLocs, Ctx);
88f4a2713aSLionel Sambuc }
VisitCompoundStmt(CompoundStmt * S)89f4a2713aSLionel Sambuc bool VisitCompoundStmt(CompoundStmt *S) {
90f4a2713aSLionel Sambuc if (S->body_empty())
91f4a2713aSLionel Sambuc return false; // was already empty, not because of transformations.
92*0a6a1f1dSLionel Sambuc for (auto *I : S->body())
93*0a6a1f1dSLionel Sambuc if (!Visit(I))
94f4a2713aSLionel Sambuc return false;
95f4a2713aSLionel Sambuc return true;
96f4a2713aSLionel Sambuc }
VisitIfStmt(IfStmt * S)97f4a2713aSLionel Sambuc bool VisitIfStmt(IfStmt *S) {
98f4a2713aSLionel Sambuc if (S->getConditionVariable())
99f4a2713aSLionel Sambuc return false;
100f4a2713aSLionel Sambuc Expr *condE = S->getCond();
101f4a2713aSLionel Sambuc if (!condE)
102f4a2713aSLionel Sambuc return false;
103f4a2713aSLionel Sambuc if (hasSideEffects(condE, Ctx))
104f4a2713aSLionel Sambuc return false;
105f4a2713aSLionel Sambuc if (!S->getThen() || !Visit(S->getThen()))
106f4a2713aSLionel Sambuc return false;
107f4a2713aSLionel Sambuc if (S->getElse() && !Visit(S->getElse()))
108f4a2713aSLionel Sambuc return false;
109f4a2713aSLionel Sambuc return true;
110f4a2713aSLionel Sambuc }
VisitWhileStmt(WhileStmt * S)111f4a2713aSLionel Sambuc bool VisitWhileStmt(WhileStmt *S) {
112f4a2713aSLionel Sambuc if (S->getConditionVariable())
113f4a2713aSLionel Sambuc return false;
114f4a2713aSLionel Sambuc Expr *condE = S->getCond();
115f4a2713aSLionel Sambuc if (!condE)
116f4a2713aSLionel Sambuc return false;
117f4a2713aSLionel Sambuc if (hasSideEffects(condE, Ctx))
118f4a2713aSLionel Sambuc return false;
119f4a2713aSLionel Sambuc if (!S->getBody())
120f4a2713aSLionel Sambuc return false;
121f4a2713aSLionel Sambuc return Visit(S->getBody());
122f4a2713aSLionel Sambuc }
VisitDoStmt(DoStmt * S)123f4a2713aSLionel Sambuc bool VisitDoStmt(DoStmt *S) {
124f4a2713aSLionel Sambuc Expr *condE = S->getCond();
125f4a2713aSLionel Sambuc if (!condE)
126f4a2713aSLionel Sambuc return false;
127f4a2713aSLionel Sambuc if (hasSideEffects(condE, Ctx))
128f4a2713aSLionel Sambuc return false;
129f4a2713aSLionel Sambuc if (!S->getBody())
130f4a2713aSLionel Sambuc return false;
131f4a2713aSLionel Sambuc return Visit(S->getBody());
132f4a2713aSLionel Sambuc }
VisitObjCForCollectionStmt(ObjCForCollectionStmt * S)133f4a2713aSLionel Sambuc bool VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
134f4a2713aSLionel Sambuc Expr *Exp = S->getCollection();
135f4a2713aSLionel Sambuc if (!Exp)
136f4a2713aSLionel Sambuc return false;
137f4a2713aSLionel Sambuc if (hasSideEffects(Exp, Ctx))
138f4a2713aSLionel Sambuc return false;
139f4a2713aSLionel Sambuc if (!S->getBody())
140f4a2713aSLionel Sambuc return false;
141f4a2713aSLionel Sambuc return Visit(S->getBody());
142f4a2713aSLionel Sambuc }
VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt * S)143f4a2713aSLionel Sambuc bool VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) {
144f4a2713aSLionel Sambuc if (!S->getSubStmt())
145f4a2713aSLionel Sambuc return false;
146f4a2713aSLionel Sambuc return Visit(S->getSubStmt());
147f4a2713aSLionel Sambuc }
148f4a2713aSLionel Sambuc };
149f4a2713aSLionel Sambuc
150f4a2713aSLionel Sambuc class EmptyStatementsRemover :
151f4a2713aSLionel Sambuc public RecursiveASTVisitor<EmptyStatementsRemover> {
152f4a2713aSLionel Sambuc MigrationPass &Pass;
153f4a2713aSLionel Sambuc
154f4a2713aSLionel Sambuc public:
EmptyStatementsRemover(MigrationPass & pass)155f4a2713aSLionel Sambuc EmptyStatementsRemover(MigrationPass &pass) : Pass(pass) { }
156f4a2713aSLionel Sambuc
TraverseStmtExpr(StmtExpr * E)157f4a2713aSLionel Sambuc bool TraverseStmtExpr(StmtExpr *E) {
158f4a2713aSLionel Sambuc CompoundStmt *S = E->getSubStmt();
159f4a2713aSLionel Sambuc for (CompoundStmt::body_iterator
160f4a2713aSLionel Sambuc I = S->body_begin(), E = S->body_end(); I != E; ++I) {
161f4a2713aSLionel Sambuc if (I != E - 1)
162f4a2713aSLionel Sambuc check(*I);
163f4a2713aSLionel Sambuc TraverseStmt(*I);
164f4a2713aSLionel Sambuc }
165f4a2713aSLionel Sambuc return true;
166f4a2713aSLionel Sambuc }
167f4a2713aSLionel Sambuc
VisitCompoundStmt(CompoundStmt * S)168f4a2713aSLionel Sambuc bool VisitCompoundStmt(CompoundStmt *S) {
169*0a6a1f1dSLionel Sambuc for (auto *I : S->body())
170*0a6a1f1dSLionel Sambuc check(I);
171f4a2713aSLionel Sambuc return true;
172f4a2713aSLionel Sambuc }
173f4a2713aSLionel Sambuc
getContext()174f4a2713aSLionel Sambuc ASTContext &getContext() { return Pass.Ctx; }
175f4a2713aSLionel Sambuc
176f4a2713aSLionel Sambuc private:
check(Stmt * S)177f4a2713aSLionel Sambuc void check(Stmt *S) {
178f4a2713aSLionel Sambuc if (!S) return;
179f4a2713aSLionel Sambuc if (EmptyChecker(Pass.Ctx, Pass.ARCMTMacroLocs).Visit(S)) {
180f4a2713aSLionel Sambuc Transaction Trans(Pass.TA);
181f4a2713aSLionel Sambuc Pass.TA.removeStmt(S);
182f4a2713aSLionel Sambuc }
183f4a2713aSLionel Sambuc }
184f4a2713aSLionel Sambuc };
185f4a2713aSLionel Sambuc
186f4a2713aSLionel Sambuc } // anonymous namespace
187f4a2713aSLionel Sambuc
isBodyEmpty(CompoundStmt * body,ASTContext & Ctx,std::vector<SourceLocation> & MacroLocs)188f4a2713aSLionel Sambuc static bool isBodyEmpty(CompoundStmt *body, ASTContext &Ctx,
189f4a2713aSLionel Sambuc std::vector<SourceLocation> &MacroLocs) {
190*0a6a1f1dSLionel Sambuc for (auto *I : body->body())
191*0a6a1f1dSLionel Sambuc if (!EmptyChecker(Ctx, MacroLocs).Visit(I))
192f4a2713aSLionel Sambuc return false;
193f4a2713aSLionel Sambuc
194f4a2713aSLionel Sambuc return true;
195f4a2713aSLionel Sambuc }
196f4a2713aSLionel Sambuc
cleanupDeallocOrFinalize(MigrationPass & pass)197f4a2713aSLionel Sambuc static void cleanupDeallocOrFinalize(MigrationPass &pass) {
198f4a2713aSLionel Sambuc ASTContext &Ctx = pass.Ctx;
199f4a2713aSLionel Sambuc TransformActions &TA = pass.TA;
200f4a2713aSLionel Sambuc DeclContext *DC = Ctx.getTranslationUnitDecl();
201f4a2713aSLionel Sambuc Selector FinalizeSel =
202f4a2713aSLionel Sambuc Ctx.Selectors.getNullarySelector(&pass.Ctx.Idents.get("finalize"));
203f4a2713aSLionel Sambuc
204f4a2713aSLionel Sambuc typedef DeclContext::specific_decl_iterator<ObjCImplementationDecl>
205f4a2713aSLionel Sambuc impl_iterator;
206f4a2713aSLionel Sambuc for (impl_iterator I = impl_iterator(DC->decls_begin()),
207f4a2713aSLionel Sambuc E = impl_iterator(DC->decls_end()); I != E; ++I) {
208*0a6a1f1dSLionel Sambuc ObjCMethodDecl *DeallocM = nullptr;
209*0a6a1f1dSLionel Sambuc ObjCMethodDecl *FinalizeM = nullptr;
210*0a6a1f1dSLionel Sambuc for (auto *MD : I->instance_methods()) {
211f4a2713aSLionel Sambuc if (!MD->hasBody())
212f4a2713aSLionel Sambuc continue;
213f4a2713aSLionel Sambuc
214f4a2713aSLionel Sambuc if (MD->getMethodFamily() == OMF_dealloc) {
215f4a2713aSLionel Sambuc DeallocM = MD;
216f4a2713aSLionel Sambuc } else if (MD->isInstanceMethod() && MD->getSelector() == FinalizeSel) {
217f4a2713aSLionel Sambuc FinalizeM = MD;
218f4a2713aSLionel Sambuc }
219f4a2713aSLionel Sambuc }
220f4a2713aSLionel Sambuc
221f4a2713aSLionel Sambuc if (DeallocM) {
222f4a2713aSLionel Sambuc if (isBodyEmpty(DeallocM->getCompoundBody(), Ctx, pass.ARCMTMacroLocs)) {
223f4a2713aSLionel Sambuc Transaction Trans(TA);
224f4a2713aSLionel Sambuc TA.remove(DeallocM->getSourceRange());
225f4a2713aSLionel Sambuc }
226f4a2713aSLionel Sambuc
227f4a2713aSLionel Sambuc if (FinalizeM) {
228f4a2713aSLionel Sambuc Transaction Trans(TA);
229f4a2713aSLionel Sambuc TA.remove(FinalizeM->getSourceRange());
230f4a2713aSLionel Sambuc }
231f4a2713aSLionel Sambuc
232f4a2713aSLionel Sambuc } else if (FinalizeM) {
233f4a2713aSLionel Sambuc if (isBodyEmpty(FinalizeM->getCompoundBody(), Ctx, pass.ARCMTMacroLocs)) {
234f4a2713aSLionel Sambuc Transaction Trans(TA);
235f4a2713aSLionel Sambuc TA.remove(FinalizeM->getSourceRange());
236f4a2713aSLionel Sambuc } else {
237f4a2713aSLionel Sambuc Transaction Trans(TA);
238f4a2713aSLionel Sambuc TA.replaceText(FinalizeM->getSelectorStartLoc(), "finalize", "dealloc");
239f4a2713aSLionel Sambuc }
240f4a2713aSLionel Sambuc }
241f4a2713aSLionel Sambuc }
242f4a2713aSLionel Sambuc }
243f4a2713aSLionel Sambuc
removeEmptyStatementsAndDeallocFinalize(MigrationPass & pass)244f4a2713aSLionel Sambuc void trans::removeEmptyStatementsAndDeallocFinalize(MigrationPass &pass) {
245f4a2713aSLionel Sambuc EmptyStatementsRemover(pass).TraverseDecl(pass.Ctx.getTranslationUnitDecl());
246f4a2713aSLionel Sambuc
247f4a2713aSLionel Sambuc cleanupDeallocOrFinalize(pass);
248f4a2713aSLionel Sambuc
249f4a2713aSLionel Sambuc for (unsigned i = 0, e = pass.ARCMTMacroLocs.size(); i != e; ++i) {
250f4a2713aSLionel Sambuc Transaction Trans(pass.TA);
251f4a2713aSLionel Sambuc pass.TA.remove(pass.ARCMTMacroLocs[i]);
252f4a2713aSLionel Sambuc }
253f4a2713aSLionel Sambuc }
254