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