TransZeroOutPropsInDealloc.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //===--- TransZeroOutPropsInDealloc.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. // removeZeroOutPropsInDealloc:
  10. //
  11. // Removes zero'ing out "strong" @synthesized properties in a -dealloc method.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "Transforms.h"
  15. #include "Internals.h"
  16. #include "clang/AST/ASTContext.h"
  17. using namespace clang;
  18. using namespace arcmt;
  19. using namespace trans;
  20. namespace {
  21. class ZeroOutInDeallocRemover :
  22. public RecursiveASTVisitor<ZeroOutInDeallocRemover> {
  23. typedef RecursiveASTVisitor<ZeroOutInDeallocRemover> base;
  24. MigrationPass &Pass;
  25. llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*> SynthesizedProperties;
  26. ImplicitParamDecl *SelfD;
  27. ExprSet Removables;
  28. Selector FinalizeSel;
  29. public:
  30. ZeroOutInDeallocRemover(MigrationPass &pass) : Pass(pass), SelfD(nullptr) {
  31. FinalizeSel =
  32. Pass.Ctx.Selectors.getNullarySelector(&Pass.Ctx.Idents.get("finalize"));
  33. }
  34. bool VisitObjCMessageExpr(ObjCMessageExpr *ME) {
  35. ASTContext &Ctx = Pass.Ctx;
  36. TransformActions &TA = Pass.TA;
  37. if (ME->getReceiverKind() != ObjCMessageExpr::Instance)
  38. return true;
  39. Expr *receiver = ME->getInstanceReceiver();
  40. if (!receiver)
  41. return true;
  42. DeclRefExpr *refE = dyn_cast<DeclRefExpr>(receiver->IgnoreParenCasts());
  43. if (!refE || refE->getDecl() != SelfD)
  44. return true;
  45. bool BackedBySynthesizeSetter = false;
  46. for (llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*>::iterator
  47. P = SynthesizedProperties.begin(),
  48. E = SynthesizedProperties.end(); P != E; ++P) {
  49. ObjCPropertyDecl *PropDecl = P->first;
  50. if (PropDecl->getSetterName() == ME->getSelector()) {
  51. BackedBySynthesizeSetter = true;
  52. break;
  53. }
  54. }
  55. if (!BackedBySynthesizeSetter)
  56. return true;
  57. // Remove the setter message if RHS is null
  58. Transaction Trans(TA);
  59. Expr *RHS = ME->getArg(0);
  60. bool RHSIsNull =
  61. RHS->isNullPointerConstant(Ctx,
  62. Expr::NPC_ValueDependentIsNull);
  63. if (RHSIsNull && isRemovable(ME))
  64. TA.removeStmt(ME);
  65. return true;
  66. }
  67. bool VisitPseudoObjectExpr(PseudoObjectExpr *POE) {
  68. if (isZeroingPropIvar(POE) && isRemovable(POE)) {
  69. Transaction Trans(Pass.TA);
  70. Pass.TA.removeStmt(POE);
  71. }
  72. return true;
  73. }
  74. bool VisitBinaryOperator(BinaryOperator *BOE) {
  75. if (isZeroingPropIvar(BOE) && isRemovable(BOE)) {
  76. Transaction Trans(Pass.TA);
  77. Pass.TA.removeStmt(BOE);
  78. }
  79. return true;
  80. }
  81. bool TraverseObjCMethodDecl(ObjCMethodDecl *D) {
  82. if (D->getMethodFamily() != OMF_dealloc &&
  83. !(D->isInstanceMethod() && D->getSelector() == FinalizeSel))
  84. return true;
  85. if (!D->hasBody())
  86. return true;
  87. ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(D->getDeclContext());
  88. if (!IMD)
  89. return true;
  90. SelfD = D->getSelfDecl();
  91. collectRemovables(D->getBody(), Removables);
  92. // For a 'dealloc' method use, find all property implementations in
  93. // this class implementation.
  94. for (auto *PID : IMD->property_impls()) {
  95. if (PID->getPropertyImplementation() ==
  96. ObjCPropertyImplDecl::Synthesize) {
  97. ObjCPropertyDecl *PD = PID->getPropertyDecl();
  98. ObjCMethodDecl *setterM = PD->getSetterMethodDecl();
  99. if (!(setterM && setterM->isDefined())) {
  100. ObjCPropertyDecl::PropertyAttributeKind AttrKind =
  101. PD->getPropertyAttributes();
  102. if (AttrKind &
  103. (ObjCPropertyDecl::OBJC_PR_retain |
  104. ObjCPropertyDecl::OBJC_PR_copy |
  105. ObjCPropertyDecl::OBJC_PR_strong))
  106. SynthesizedProperties[PD] = PID;
  107. }
  108. }
  109. }
  110. // Now, remove all zeroing of ivars etc.
  111. base::TraverseObjCMethodDecl(D);
  112. // clear out for next method.
  113. SynthesizedProperties.clear();
  114. SelfD = nullptr;
  115. Removables.clear();
  116. return true;
  117. }
  118. bool TraverseFunctionDecl(FunctionDecl *D) { return true; }
  119. bool TraverseBlockDecl(BlockDecl *block) { return true; }
  120. bool TraverseBlockExpr(BlockExpr *block) { return true; }
  121. private:
  122. bool isRemovable(Expr *E) const {
  123. return Removables.count(E);
  124. }
  125. bool isZeroingPropIvar(Expr *E) {
  126. E = E->IgnoreParens();
  127. if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
  128. return isZeroingPropIvar(BO);
  129. if (PseudoObjectExpr *PO = dyn_cast<PseudoObjectExpr>(E))
  130. return isZeroingPropIvar(PO);
  131. return false;
  132. }
  133. bool isZeroingPropIvar(BinaryOperator *BOE) {
  134. if (BOE->getOpcode() == BO_Comma)
  135. return isZeroingPropIvar(BOE->getLHS()) &&
  136. isZeroingPropIvar(BOE->getRHS());
  137. if (BOE->getOpcode() != BO_Assign)
  138. return false;
  139. Expr *LHS = BOE->getLHS();
  140. if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(LHS)) {
  141. ObjCIvarDecl *IVDecl = IV->getDecl();
  142. if (!IVDecl->getType()->isObjCObjectPointerType())
  143. return false;
  144. bool IvarBacksPropertySynthesis = false;
  145. for (llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*>::iterator
  146. P = SynthesizedProperties.begin(),
  147. E = SynthesizedProperties.end(); P != E; ++P) {
  148. ObjCPropertyImplDecl *PropImpDecl = P->second;
  149. if (PropImpDecl && PropImpDecl->getPropertyIvarDecl() == IVDecl) {
  150. IvarBacksPropertySynthesis = true;
  151. break;
  152. }
  153. }
  154. if (!IvarBacksPropertySynthesis)
  155. return false;
  156. }
  157. else
  158. return false;
  159. return isZero(BOE->getRHS());
  160. }
  161. bool isZeroingPropIvar(PseudoObjectExpr *PO) {
  162. BinaryOperator *BO = dyn_cast<BinaryOperator>(PO->getSyntacticForm());
  163. if (!BO) return false;
  164. if (BO->getOpcode() != BO_Assign) return false;
  165. ObjCPropertyRefExpr *PropRefExp =
  166. dyn_cast<ObjCPropertyRefExpr>(BO->getLHS()->IgnoreParens());
  167. if (!PropRefExp) return false;
  168. // TODO: Using implicit property decl.
  169. if (PropRefExp->isImplicitProperty())
  170. return false;
  171. if (ObjCPropertyDecl *PDecl = PropRefExp->getExplicitProperty()) {
  172. if (!SynthesizedProperties.count(PDecl))
  173. return false;
  174. }
  175. return isZero(cast<OpaqueValueExpr>(BO->getRHS())->getSourceExpr());
  176. }
  177. bool isZero(Expr *E) {
  178. if (E->isNullPointerConstant(Pass.Ctx, Expr::NPC_ValueDependentIsNull))
  179. return true;
  180. return isZeroingPropIvar(E);
  181. }
  182. };
  183. } // anonymous namespace
  184. void trans::removeZeroOutPropsInDeallocFinalize(MigrationPass &pass) {
  185. ZeroOutInDeallocRemover trans(pass);
  186. trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
  187. }