TransZeroOutPropsInDealloc.cpp 6.7 KB

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