TransEmptyStatementsAndDealloc.cpp 7.1 KB

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