Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
11
         E = SynthesizedProperties.end(); P != E; 
++P5
) {
60
9
      ObjCPropertyDecl *PropDecl = P->first;
61
9
      if (PropDecl->getSetterName() == ME->getSelector()) {
62
4
        BackedBySynthesizeSetter = true;
63
4
        break;
64
4
      }
65
9
    }
66
6
    if (!BackedBySynthesizeSetter)
67
2
      return true;
68
4
69
4
    // 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
1.03k
        
!(958
D->isInstanceMethod()958
&&
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
42
    // For a 'dealloc' method use, find all property implementations in
114
42
    // 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
          ObjCPropertyDecl::PropertyAttributeKind AttrKind =
122
25
            PD->getPropertyAttributes();
123
25
            if (AttrKind &
124
25
                (ObjCPropertyDecl::OBJC_PR_retain |
125
25
                  ObjCPropertyDecl::OBJC_PR_copy   |
126
25
                  ObjCPropertyDecl::OBJC_PR_strong))
127
25
              SynthesizedProperties[PD] = PID;
128
25
        }
129
27
      }
130
29
    }
131
42
132
42
    // Now, remove all zeroing of ivars etc.
133
42
    base::TraverseObjCMethodDecl(D);
134
42
135
42
    // clear out for next method.
136
42
    SynthesizedProperties.clear();
137
42
    SelfD = nullptr;
138
42
    Removables.clear();
139
42
    return true;
140
42
  }
141
142
682
  bool TraverseFunctionDecl(FunctionDecl *D) { return true; }
143
0
  bool TraverseBlockDecl(BlockDecl *block) { return true; }
144
0
  bool TraverseBlockExpr(BlockExpr *block) { return true; }
145
146
private:
147
39
  bool isRemovable(Expr *E) const {
148
39
    return Removables.count(E);
149
39
  }
150
151
18
  bool isZeroingPropIvar(Expr *E) {
152
18
    E = E->IgnoreParens();
153
18
    if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
154
8
      return isZeroingPropIvar(BO);
155
10
    if (PseudoObjectExpr *PO = dyn_cast<PseudoObjectExpr>(E))
156
10
      return isZeroingPropIvar(PO);
157
0
    return false;
158
0
  }
159
160
47
  bool isZeroingPropIvar(BinaryOperator *BOE) {
161
47
    if (BOE->getOpcode() == BO_Comma)
162
6
      return isZeroingPropIvar(BOE->getLHS()) &&
163
6
             isZeroingPropIvar(BOE->getRHS());
164
41
165
41
    if (BOE->getOpcode() != BO_Assign)
166
0
      return false;
167
41
168
41
    Expr *LHS = BOE->getLHS();
169
41
    if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(LHS)) {
170
16
      ObjCIvarDecl *IVDecl = IV->getDecl();
171
16
      if (!IVDecl->getType()->isObjCObjectPointerType())
172
0
        return false;
173
16
      bool IvarBacksPropertySynthesis = false;
174
16
      for (llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*>::iterator
175
16
           P = SynthesizedProperties.begin(),
176
49
           E = SynthesizedProperties.end(); P != E; 
++P33
) {
177
49
        ObjCPropertyImplDecl *PropImpDecl = P->second;
178
49
        if (PropImpDecl && PropImpDecl->getPropertyIvarDecl() == IVDecl) {
179
16
          IvarBacksPropertySynthesis = true;
180
16
          break;
181
16
        }
182
49
      }
183
16
      if (!IvarBacksPropertySynthesis)
184
0
        return false;
185
25
    }
186
25
    else
187
25
        return false;
188
16
189
16
    return isZero(BOE->getRHS());
190
16
  }
191
192
37
  bool isZeroingPropIvar(PseudoObjectExpr *PO) {
193
37
    BinaryOperator *BO = dyn_cast<BinaryOperator>(PO->getSyntacticForm());
194
37
    if (!BO) 
return false2
;
195
35
    if (BO->getOpcode() != BO_Assign) 
return false0
;
196
35
197
35
    ObjCPropertyRefExpr *PropRefExp =
198
35
      dyn_cast<ObjCPropertyRefExpr>(BO->getLHS()->IgnoreParens());
199
35
    if (!PropRefExp) 
return false0
;
200
35
201
35
    // TODO: Using implicit property decl.
202
35
    if (PropRefExp->isImplicitProperty())
203
0
      return false;
204
35
205
35
    if (ObjCPropertyDecl *PDecl = PropRefExp->getExplicitProperty()) {
206
35
      if (!SynthesizedProperties.count(PDecl))
207
4
        return false;
208
31
    }
209
31
210
31
    return isZero(cast<OpaqueValueExpr>(BO->getRHS())->getSourceExpr());
211
31
  }
212
213
47
  bool isZero(Expr *E) {
214
47
    if (E->isNullPointerConstant(Pass.Ctx, Expr::NPC_ValueDependentIsNull))
215
41
      return true;
216
6
217
6
    return isZeroingPropIvar(E);
218
6
  }
219
};
220
221
} // anonymous namespace
222
223
85
void trans::removeZeroOutPropsInDeallocFinalize(MigrationPass &pass) {
224
85
  ZeroOutInDeallocRemover trans(pass);
225
85
  trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
226
85
}