TransZeroOutPropsInDealloc.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. ObjCPropertyAttribute::Kind AttrKind = PD->getPropertyAttributes();
  101. if (AttrKind & (ObjCPropertyAttribute::kind_retain |
  102. ObjCPropertyAttribute::kind_copy |
  103. ObjCPropertyAttribute::kind_strong))
  104. SynthesizedProperties[PD] = PID;
  105. }
  106. }
  107. }
  108. // Now, remove all zeroing of ivars etc.
  109. base::TraverseObjCMethodDecl(D);
  110. // clear out for next method.
  111. SynthesizedProperties.clear();
  112. SelfD = nullptr;
  113. Removables.clear();
  114. return true;
  115. }
  116. bool TraverseFunctionDecl(FunctionDecl *D) { return true; }
  117. bool TraverseBlockDecl(BlockDecl *block) { return true; }
  118. bool TraverseBlockExpr(BlockExpr *block) { return true; }
  119. private:
  120. bool isRemovable(Expr *E) const {
  121. return Removables.count(E);
  122. }
  123. bool isZeroingPropIvar(Expr *E) {
  124. E = E->IgnoreParens();
  125. if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
  126. return isZeroingPropIvar(BO);
  127. if (PseudoObjectExpr *PO = dyn_cast<PseudoObjectExpr>(E))
  128. return isZeroingPropIvar(PO);
  129. return false;
  130. }
  131. bool isZeroingPropIvar(BinaryOperator *BOE) {
  132. if (BOE->getOpcode() == BO_Comma)
  133. return isZeroingPropIvar(BOE->getLHS()) &&
  134. isZeroingPropIvar(BOE->getRHS());
  135. if (BOE->getOpcode() != BO_Assign)
  136. return false;
  137. Expr *LHS = BOE->getLHS();
  138. if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(LHS)) {
  139. ObjCIvarDecl *IVDecl = IV->getDecl();
  140. if (!IVDecl->getType()->isObjCObjectPointerType())
  141. return false;
  142. bool IvarBacksPropertySynthesis = false;
  143. for (llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*>::iterator
  144. P = SynthesizedProperties.begin(),
  145. E = SynthesizedProperties.end(); P != E; ++P) {
  146. ObjCPropertyImplDecl *PropImpDecl = P->second;
  147. if (PropImpDecl && PropImpDecl->getPropertyIvarDecl() == IVDecl) {
  148. IvarBacksPropertySynthesis = true;
  149. break;
  150. }
  151. }
  152. if (!IvarBacksPropertySynthesis)
  153. return false;
  154. }
  155. else
  156. return false;
  157. return isZero(BOE->getRHS());
  158. }
  159. bool isZeroingPropIvar(PseudoObjectExpr *PO) {
  160. BinaryOperator *BO = dyn_cast<BinaryOperator>(PO->getSyntacticForm());
  161. if (!BO) return false;
  162. if (BO->getOpcode() != BO_Assign) return false;
  163. ObjCPropertyRefExpr *PropRefExp =
  164. dyn_cast<ObjCPropertyRefExpr>(BO->getLHS()->IgnoreParens());
  165. if (!PropRefExp) return false;
  166. // TODO: Using implicit property decl.
  167. if (PropRefExp->isImplicitProperty())
  168. return false;
  169. if (ObjCPropertyDecl *PDecl = PropRefExp->getExplicitProperty()) {
  170. if (!SynthesizedProperties.count(PDecl))
  171. return false;
  172. }
  173. return isZero(cast<OpaqueValueExpr>(BO->getRHS())->getSourceExpr());
  174. }
  175. bool isZero(Expr *E) {
  176. if (E->isNullPointerConstant(Pass.Ctx, Expr::NPC_ValueDependentIsNull))
  177. return true;
  178. return isZeroingPropIvar(E);
  179. }
  180. };
  181. } // anonymous namespace
  182. void trans::removeZeroOutPropsInDeallocFinalize(MigrationPass &pass) {
  183. ZeroOutInDeallocRemover trans(pass);
  184. trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
  185. }