clang  19.0.0git
TransZeroOutPropsInDealloc.cpp
Go to the documentation of this file.
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> {
28 
29  MigrationPass &Pass;
30 
31  llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*> SynthesizedProperties;
32  ImplicitParamDecl *SelfD;
33  ExprSet Removables;
34  Selector FinalizeSel;
35 
36 public:
37  ZeroOutInDeallocRemover(MigrationPass &pass) : Pass(pass), SelfD(nullptr) {
38  FinalizeSel =
39  Pass.Ctx.Selectors.getNullarySelector(&Pass.Ctx.Idents.get("finalize"));
40  }
41 
42  bool VisitObjCMessageExpr(ObjCMessageExpr *ME) {
43  ASTContext &Ctx = Pass.Ctx;
44  TransformActions &TA = Pass.TA;
45 
47  return true;
48  Expr *receiver = ME->getInstanceReceiver();
49  if (!receiver)
50  return true;
51 
52  DeclRefExpr *refE = dyn_cast<DeclRefExpr>(receiver->IgnoreParenCasts());
53  if (!refE || refE->getDecl() != SelfD)
54  return true;
55 
56  bool BackedBySynthesizeSetter = false;
57  for (llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*>::iterator
58  P = SynthesizedProperties.begin(),
59  E = SynthesizedProperties.end(); P != E; ++P) {
60  ObjCPropertyDecl *PropDecl = P->first;
61  if (PropDecl->getSetterName() == ME->getSelector()) {
62  BackedBySynthesizeSetter = true;
63  break;
64  }
65  }
66  if (!BackedBySynthesizeSetter)
67  return true;
68 
69  // Remove the setter message if RHS is null
70  Transaction Trans(TA);
71  Expr *RHS = ME->getArg(0);
72  bool RHSIsNull =
73  RHS->isNullPointerConstant(Ctx,
75  if (RHSIsNull && isRemovable(ME))
76  TA.removeStmt(ME);
77 
78  return true;
79  }
80 
81  bool VisitPseudoObjectExpr(PseudoObjectExpr *POE) {
82  if (isZeroingPropIvar(POE) && isRemovable(POE)) {
83  Transaction Trans(Pass.TA);
84  Pass.TA.removeStmt(POE);
85  }
86 
87  return true;
88  }
89 
90  bool VisitBinaryOperator(BinaryOperator *BOE) {
91  if (isZeroingPropIvar(BOE) && isRemovable(BOE)) {
92  Transaction Trans(Pass.TA);
93  Pass.TA.removeStmt(BOE);
94  }
95 
96  return true;
97  }
98 
99  bool TraverseObjCMethodDecl(ObjCMethodDecl *D) {
100  if (D->getMethodFamily() != OMF_dealloc &&
101  !(D->isInstanceMethod() && D->getSelector() == FinalizeSel))
102  return true;
103  if (!D->hasBody())
104  return true;
105 
106  ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(D->getDeclContext());
107  if (!IMD)
108  return true;
109 
110  SelfD = D->getSelfDecl();
111  collectRemovables(D->getBody(), Removables);
112 
113  // For a 'dealloc' method use, find all property implementations in
114  // this class implementation.
115  for (auto *PID : IMD->property_impls()) {
116  if (PID->getPropertyImplementation() ==
118  ObjCPropertyDecl *PD = PID->getPropertyDecl();
119  ObjCMethodDecl *setterM = PD->getSetterMethodDecl();
120  if (!(setterM && setterM->isDefined())) {
122  if (AttrKind & (ObjCPropertyAttribute::kind_retain |
125  SynthesizedProperties[PD] = PID;
126  }
127  }
128  }
129 
130  // Now, remove all zeroing of ivars etc.
131  base::TraverseObjCMethodDecl(D);
132 
133  // clear out for next method.
134  SynthesizedProperties.clear();
135  SelfD = nullptr;
136  Removables.clear();
137  return true;
138  }
139 
140  bool TraverseFunctionDecl(FunctionDecl *D) { return true; }
141  bool TraverseBlockDecl(BlockDecl *block) { return true; }
142  bool TraverseBlockExpr(BlockExpr *block) { return true; }
143 
144 private:
145  bool isRemovable(Expr *E) const {
146  return Removables.count(E);
147  }
148 
149  bool isZeroingPropIvar(Expr *E) {
150  E = E->IgnoreParens();
151  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
152  return isZeroingPropIvar(BO);
153  if (PseudoObjectExpr *PO = dyn_cast<PseudoObjectExpr>(E))
154  return isZeroingPropIvar(PO);
155  return false;
156  }
157 
158  bool isZeroingPropIvar(BinaryOperator *BOE) {
159  if (BOE->getOpcode() == BO_Comma)
160  return isZeroingPropIvar(BOE->getLHS()) &&
161  isZeroingPropIvar(BOE->getRHS());
162 
163  if (BOE->getOpcode() != BO_Assign)
164  return false;
165 
166  Expr *LHS = BOE->getLHS();
167  if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(LHS)) {
168  ObjCIvarDecl *IVDecl = IV->getDecl();
169  if (!IVDecl->getType()->isObjCObjectPointerType())
170  return false;
171  bool IvarBacksPropertySynthesis = false;
172  for (llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*>::iterator
173  P = SynthesizedProperties.begin(),
174  E = SynthesizedProperties.end(); P != E; ++P) {
175  ObjCPropertyImplDecl *PropImpDecl = P->second;
176  if (PropImpDecl && PropImpDecl->getPropertyIvarDecl() == IVDecl) {
177  IvarBacksPropertySynthesis = true;
178  break;
179  }
180  }
181  if (!IvarBacksPropertySynthesis)
182  return false;
183  }
184  else
185  return false;
186 
187  return isZero(BOE->getRHS());
188  }
189 
190  bool isZeroingPropIvar(PseudoObjectExpr *PO) {
191  BinaryOperator *BO = dyn_cast<BinaryOperator>(PO->getSyntacticForm());
192  if (!BO) return false;
193  if (BO->getOpcode() != BO_Assign) return false;
194 
195  ObjCPropertyRefExpr *PropRefExp =
196  dyn_cast<ObjCPropertyRefExpr>(BO->getLHS()->IgnoreParens());
197  if (!PropRefExp) return false;
198 
199  // TODO: Using implicit property decl.
200  if (PropRefExp->isImplicitProperty())
201  return false;
202 
203  if (ObjCPropertyDecl *PDecl = PropRefExp->getExplicitProperty()) {
204  if (!SynthesizedProperties.count(PDecl))
205  return false;
206  }
207 
208  return isZero(cast<OpaqueValueExpr>(BO->getRHS())->getSourceExpr());
209  }
210 
211  bool isZero(Expr *E) {
213  return true;
214 
215  return isZeroingPropIvar(E);
216  }
217 };
218 
219 } // anonymous namespace
220 
222  ZeroOutInDeallocRemover trans(pass);
223  trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
224 }
Defines the clang::ASTContext interface.
StringRef P
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:185
IdentifierTable & Idents
Definition: ASTContext.h:647
SelectorTable & Selectors
Definition: ASTContext.h:648
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1076
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3892
Opcode getOpcode() const
Definition: Expr.h:3936
Expr * getRHS() const
Definition: Expr.h:3943
Expr * getLHS() const
Definition: Expr.h:3941
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4497
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:6214
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1260
ValueDecl * getDecl()
Definition: Expr.h:1328
DeclContext * getDeclContext()
Definition: DeclBase.h:454
This represents one expression.
Definition: Expr.h:110
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
Definition: Expr.cpp:3116
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition: Expr.cpp:3107
@ NPC_ValueDependentIsNull
Specifies that a value-dependent expression of integral or dependent type should be considered a null...
Definition: Expr.h:821
NullPointerConstantKind isNullPointerConstant(ASTContext &Ctx, NullPointerConstantValueDependence NPC) const
isNullPointerConstant - C99 6.3.2.3p3 - Test if this reduces down to a Null pointer constant.
Definition: Expr.cpp:3980
Represents a function declaration or definition.
Definition: Decl.h:1972
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
propimpl_range property_impls() const
Definition: DeclObjC.h:2510
ObjCIvarDecl - Represents an ObjC instance variable.
Definition: DeclObjC.h:1950
ObjCIvarRefExpr - A reference to an ObjC instance variable.
Definition: ExprObjC.h:549
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:945
Selector getSelector() const
Definition: ExprObjC.cpp:293
Expr * getInstanceReceiver()
Returns the object expression (receiver) for an instance message, or null for a message that is not a...
Definition: ExprObjC.h:1260
@ Instance
The receiver is an object instance.
Definition: ExprObjC.h:953
ReceiverKind getReceiverKind() const
Determine the kind of receiver that this message is being sent to.
Definition: ExprObjC.h:1234
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition: ExprObjC.h:1395
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:140
bool hasBody() const override
Determine whether this method has a body.
Definition: DeclObjC.h:523
Stmt * getBody() const override
Retrieve the body of this method, if it has one.
Definition: DeclObjC.cpp:909
ImplicitParamDecl * getSelfDecl() const
Definition: DeclObjC.h:418
Selector getSelector() const
Definition: DeclObjC.h:327
bool isInstanceMethod() const
Definition: DeclObjC.h:426
bool isDefined() const
Definition: DeclObjC.h:452
ObjCMethodFamily getMethodFamily() const
Determines the family of this method.
Definition: DeclObjC.cpp:1053
Represents one property declaration in an Objective-C interface.
Definition: DeclObjC.h:730
Selector getSetterName() const
Definition: DeclObjC.h:892
ObjCPropertyAttribute::Kind getPropertyAttributes() const
Definition: DeclObjC.h:814
ObjCMethodDecl * getSetterMethodDecl() const
Definition: DeclObjC.h:903
ObjCPropertyImplDecl - Represents implementation declaration of a property in a class or category imp...
Definition: DeclObjC.h:2802
ObjCIvarDecl * getPropertyIvarDecl() const
Definition: DeclObjC.h:2876
ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC property.
Definition: ExprObjC.h:617
ObjCPropertyDecl * getExplicitProperty() const
Definition: ExprObjC.h:706
bool isImplicitProperty() const
Definition: ExprObjC.h:703
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:6346
Expr * getSyntacticForm()
Return the syntactic form of this expression, i.e.
Definition: Expr.h:6388
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
Selector getNullarySelector(const IdentifierInfo *ID)
Smart pointer class that efficiently represents Objective-C method names.
bool isObjCObjectPointerType() const
Definition: Type.h:7760
QualType getType() const
Definition: Decl.h:718
TransformActions & TA
Definition: Internals.h:152
void removeZeroOutPropsInDeallocFinalize(MigrationPass &pass)
void collectRemovables(Stmt *S, ExprSet &exprs)
Definition: Transforms.cpp:308
The JSON file list parser is used to communicate input to InstallAPI.