]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/tools/clang/lib/ARCMigrate/TransEmptyStatementsAndDealloc.cpp
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / llvm / tools / clang / lib / ARCMigrate / TransEmptyStatementsAndDealloc.cpp
1 //===--- 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"
25 #include "clang/Basic/SourceManager.h"
26
27 using namespace clang;
28 using namespace arcmt;
29 using namespace trans;
30
31 static 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
50       AfterMacroLoc = I->getLocWithOffset(getARCMTMacroName().size());
51   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
73 namespace {
74
75 /// \brief Returns true if the statement became empty due to previous
76 /// transformations.
77 class EmptyChecker : public StmtVisitor<EmptyChecker, bool> {
78   ASTContext &Ctx;
79   std::vector<SourceLocation> &MacroLocs;
80
81 public:
82   EmptyChecker(ASTContext &ctx, std::vector<SourceLocation> &macroLocs)
83     : Ctx(ctx), MacroLocs(macroLocs) { }
84
85   bool VisitNullStmt(NullStmt *S) {
86     return isEmptyARCMTMacroStatement(S, MacroLocs, Ctx);
87   }
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   }
148 };
149
150 class EmptyStatementsRemover :
151                             public RecursiveASTVisitor<EmptyStatementsRemover> {
152   MigrationPass &Pass;
153
154 public:
155   EmptyStatementsRemover(MigrationPass &pass) : Pass(pass) { }
156
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
175   ASTContext &getContext() { return Pass.Ctx; }
176
177 private:
178   void check(Stmt *S) {
179     if (!S) return;
180     if (EmptyChecker(Pass.Ctx, Pass.ARCMTMacroLocs).Visit(S)) {
181       Transaction Trans(Pass.TA);
182       Pass.TA.removeStmt(S);
183     }
184   }
185 };
186
187 } // anonymous namespace
188
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))
194       return false;
195
196   return true;
197 }
198
199 static void removeDeallocMethod(MigrationPass &pass) {
200   ASTContext &Ctx = pass.Ctx;
201   TransformActions &TA = pass.TA;
202   DeclContext *DC = Ctx.getTranslationUnitDecl();
203
204   typedef DeclContext::specific_decl_iterator<ObjCImplementationDecl>
205     impl_iterator;
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) {
213         if (MD->hasBody() &&
214             isBodyEmpty(MD->getCompoundBody(), Ctx, pass.ARCMTMacroLocs)) {
215           Transaction Trans(TA);
216           TA.remove(MD->getSourceRange());
217         }
218         break;
219       }
220     }
221   }
222 }
223
224 void trans::removeEmptyStatementsAndDealloc(MigrationPass &pass) {
225   EmptyStatementsRemover(pass).TraverseDecl(pass.Ctx.getTranslationUnitDecl());
226
227   removeDeallocMethod(pass);
228
229   for (unsigned i = 0, e = pass.ARCMTMacroLocs.size(); i != e; ++i) {
230     Transaction Trans(pass.TA);
231     pass.TA.remove(pass.ARCMTMacroLocs[i]);
232   }
233 }