TransEmptyStatementsAndDealloc.cpp 7.0 KB

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