Coverage Report

Created: 2020-09-19 12:23

/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
62
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
55
52
55
    DeclRefExpr *refE = dyn_cast<DeclRefExpr>(receiver->IgnoreParenCasts());
53
55
    if (!refE || 
refE->getDecl() != SelfD18
)
54
49
      return true;
55
6
56
6
    bool BackedBySynthesizeSetter = false;
57
6
    for (llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*>::iterator
58
6
         P = SynthesizedProperties.begin(),
59
13
         E = SynthesizedProperties.end(); P != E; 
++P7
) {
60
11
      ObjCPropertyDecl *PropDecl = P->first;
61
11
      if (PropDecl->getSetterName() == ME->getSelector()) {
62
4
        BackedBySynthesizeSetter = true;
63
4
        break;
64
4
      }
65
11
    }
66
6
    if (!BackedBySynthesizeSetter)
67
2
      return true;
68
4
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
4
78
4
    return true;
79
4
  }
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
27
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
39
96
39
    return true;
97
39
  }
98
99
1.03k
  bool TraverseObjCMethodDecl(ObjCMethodDecl *D) {
100
1.03k
    if (D->getMethodFamily() != OMF_dealloc &&
101
958
        !(D->isInstanceMethod() && 
D->getSelector() == FinalizeSel805
))
102
894
      return true;
103
139
    if (!D->hasBody())
104
97
      return true;
105
42
106
42
    ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(D->getDeclContext());
107
42
    if (!IMD)
108
0
      return true;
109
42
110
42
    SelfD = D->getSelfDecl();
111
42
    collectRemovables(D->getBody(), Removables);
112
42
113
    // For a 'dealloc' method use, find all property implementations in
114
    // this class implementation.
115
29
    for (auto *PID : IMD->property_impls()) {
116
29
      if (PID->getPropertyImplementation() ==
117
27
          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
42
130
    // Now, remove all zeroing of ivars etc.
131
42
    base::TraverseObjCMethodDecl(D);
132
42
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
0
  }
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
41
163
41
    if (BOE->getOpcode() != BO_Assign)
164
0
      return false;
165
41
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
42
           E = SynthesizedProperties.end(); P != E; 
++P26
) {
175
42
        ObjCPropertyImplDecl *PropImpDecl = P->second;
176
42
        if (PropImpDecl && PropImpDecl->getPropertyIvarDecl() == IVDecl) {
177
16
          IvarBacksPropertySynthesis = true;
178
16
          break;
179
16
        }
180
42
      }
181
16
      if (!IvarBacksPropertySynthesis)
182
0
        return false;
183
25
    }
184
25
    else
185
25
        return false;
186
16
187
16
    return isZero(BOE->getRHS());
188
16
  }
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
35
195
35
    ObjCPropertyRefExpr *PropRefExp =
196
35
      dyn_cast<ObjCPropertyRefExpr>(BO->getLHS()->IgnoreParens());
197
35
    if (!PropRefExp) 
return false0
;
198
35
199
    // TODO: Using implicit property decl.
200
35
    if (PropRefExp->isImplicitProperty())
201
0
      return false;
202
35
203
35
    if (ObjCPropertyDecl *PDecl = PropRefExp->getExplicitProperty()) {
204
35
      if (!SynthesizedProperties.count(PDecl))
205
4
        return false;
206
31
    }
207
31
208
31
    return isZero(cast<OpaqueValueExpr>(BO->getRHS())->getSourceExpr());
209
31
  }
210
211
47
  bool isZero(Expr *E) {
212
47
    if (E->isNullPointerConstant(Pass.Ctx, Expr::NPC_ValueDependentIsNull))
213
41
      return true;
214
6
215
6
    return isZeroingPropIvar(E);
216
6
  }
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
}