1 //===--- TransZeroOutPropsInDealloc.cpp - Tranformations to ARC mode ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // removeZeroOutPropsInDealloc:
12 // Removes zero'ing out "strong" @synthesized properties in a -dealloc method.
14 //===----------------------------------------------------------------------===//
16 #include "Transforms.h"
17 #include "Internals.h"
19 using namespace clang;
20 using namespace arcmt;
21 using namespace trans;
25 class ZeroOutInDeallocRemover :
26 public RecursiveASTVisitor<ZeroOutInDeallocRemover> {
27 typedef RecursiveASTVisitor<ZeroOutInDeallocRemover> base;
31 llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*> SynthesizedProperties;
32 ImplicitParamDecl *SelfD;
36 ZeroOutInDeallocRemover(MigrationPass &pass) : Pass(pass), SelfD(0) { }
38 bool VisitObjCMessageExpr(ObjCMessageExpr *ME) {
39 ASTContext &Ctx = Pass.Ctx;
40 TransformActions &TA = Pass.TA;
42 if (ME->getReceiverKind() != ObjCMessageExpr::Instance)
44 Expr *receiver = ME->getInstanceReceiver();
48 DeclRefExpr *refE = dyn_cast<DeclRefExpr>(receiver->IgnoreParenCasts());
49 if (!refE || refE->getDecl() != SelfD)
52 bool BackedBySynthesizeSetter = false;
53 for (llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*>::iterator
54 P = SynthesizedProperties.begin(),
55 E = SynthesizedProperties.end(); P != E; ++P) {
56 ObjCPropertyDecl *PropDecl = P->first;
57 if (PropDecl->getSetterName() == ME->getSelector()) {
58 BackedBySynthesizeSetter = true;
62 if (!BackedBySynthesizeSetter)
65 // Remove the setter message if RHS is null
66 Transaction Trans(TA);
67 Expr *RHS = ME->getArg(0);
69 RHS->isNullPointerConstant(Ctx,
70 Expr::NPC_ValueDependentIsNull);
71 if (RHSIsNull && isRemovable(ME))
77 bool VisitBinaryOperator(BinaryOperator *BOE) {
78 if (isZeroingPropIvar(BOE) && isRemovable(BOE)) {
79 Transaction Trans(Pass.TA);
80 Pass.TA.removeStmt(BOE);
86 bool TraverseObjCMethodDecl(ObjCMethodDecl *D) {
87 if (D->getMethodFamily() != OMF_dealloc)
92 ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(D->getDeclContext());
96 SelfD = D->getSelfDecl();
97 collectRemovables(D->getBody(), Removables);
99 // For a 'dealloc' method use, find all property implementations in
100 // this class implementation.
101 for (ObjCImplDecl::propimpl_iterator
102 I = IMD->propimpl_begin(), EI = IMD->propimpl_end(); I != EI; ++I) {
103 ObjCPropertyImplDecl *PID = *I;
104 if (PID->getPropertyImplementation() ==
105 ObjCPropertyImplDecl::Synthesize) {
106 ObjCPropertyDecl *PD = PID->getPropertyDecl();
107 ObjCMethodDecl *setterM = PD->getSetterMethodDecl();
108 if (!(setterM && setterM->isDefined())) {
109 ObjCPropertyDecl::PropertyAttributeKind AttrKind =
110 PD->getPropertyAttributes();
112 (ObjCPropertyDecl::OBJC_PR_retain |
113 ObjCPropertyDecl::OBJC_PR_copy |
114 ObjCPropertyDecl::OBJC_PR_strong))
115 SynthesizedProperties[PD] = PID;
120 // Now, remove all zeroing of ivars etc.
121 base::TraverseObjCMethodDecl(D);
123 // clear out for next method.
124 SynthesizedProperties.clear();
130 bool TraverseFunctionDecl(FunctionDecl *D) { return true; }
131 bool TraverseBlockDecl(BlockDecl *block) { return true; }
132 bool TraverseBlockExpr(BlockExpr *block) { return true; }
135 bool isRemovable(Expr *E) const {
136 return Removables.count(E);
139 bool isZeroingPropIvar(Expr *E) {
140 BinaryOperator *BOE = dyn_cast_or_null<BinaryOperator>(E);
141 if (!BOE) return false;
143 if (BOE->getOpcode() == BO_Comma)
144 return isZeroingPropIvar(BOE->getLHS()) &&
145 isZeroingPropIvar(BOE->getRHS());
147 if (BOE->getOpcode() != BO_Assign)
150 ASTContext &Ctx = Pass.Ctx;
152 Expr *LHS = BOE->getLHS();
153 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(LHS)) {
154 ObjCIvarDecl *IVDecl = IV->getDecl();
155 if (!IVDecl->getType()->isObjCObjectPointerType())
157 bool IvarBacksPropertySynthesis = false;
158 for (llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*>::iterator
159 P = SynthesizedProperties.begin(),
160 E = SynthesizedProperties.end(); P != E; ++P) {
161 ObjCPropertyImplDecl *PropImpDecl = P->second;
162 if (PropImpDecl && PropImpDecl->getPropertyIvarDecl() == IVDecl) {
163 IvarBacksPropertySynthesis = true;
167 if (!IvarBacksPropertySynthesis)
170 else if (ObjCPropertyRefExpr *PropRefExp = dyn_cast<ObjCPropertyRefExpr>(LHS)) {
171 // TODO: Using implicit property decl.
172 if (PropRefExp->isImplicitProperty())
174 if (ObjCPropertyDecl *PDecl = PropRefExp->getExplicitProperty()) {
175 if (!SynthesizedProperties.count(PDecl))
182 Expr *RHS = BOE->getRHS();
183 bool RHSIsNull = RHS->isNullPointerConstant(Ctx,
184 Expr::NPC_ValueDependentIsNull);
188 return isZeroingPropIvar(RHS);
192 } // anonymous namespace
194 void trans::removeZeroOutPropsInDealloc(MigrationPass &pass) {
195 ZeroOutInDeallocRemover trans(pass);
196 trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());