Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Sema/ScopeInfo.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- ScopeInfo.cpp - Information about a semantic context -------------===//
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
// This file implements FunctionScopeInfo and its subclasses, which contain
10
// information about a single function, block, lambda, or method body.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/Sema/ScopeInfo.h"
15
#include "clang/AST/Decl.h"
16
#include "clang/AST/DeclCXX.h"
17
#include "clang/AST/DeclObjC.h"
18
#include "clang/AST/Expr.h"
19
#include "clang/AST/ExprCXX.h"
20
#include "clang/AST/ExprObjC.h"
21
22
using namespace clang;
23
using namespace sema;
24
25
3.47M
void FunctionScopeInfo::Clear() {
26
3.47M
  HasBranchProtectedScope = false;
27
3.47M
  HasBranchIntoScope = false;
28
3.47M
  HasIndirectGoto = false;
29
3.47M
  HasDroppedStmt = false;
30
3.47M
  HasOMPDeclareReductionCombiner = false;
31
3.47M
  HasFallthroughStmt = false;
32
3.47M
  HasPotentialAvailabilityViolations = false;
33
3.47M
  ObjCShouldCallSuper = false;
34
3.47M
  ObjCIsDesignatedInit = false;
35
3.47M
  ObjCWarnForNoDesignatedInitChain = false;
36
3.47M
  ObjCIsSecondaryInit = false;
37
3.47M
  ObjCWarnForNoInitDelegation = false;
38
3.47M
  FirstReturnLoc = SourceLocation();
39
3.47M
  FirstCXXTryLoc = SourceLocation();
40
3.47M
  FirstSEHTryLoc = SourceLocation();
41
3.47M
42
3.47M
  // Coroutine state
43
3.47M
  FirstCoroutineStmtLoc = SourceLocation();
44
3.47M
  CoroutinePromise = nullptr;
45
3.47M
  CoroutineParameterMoves.clear();
46
3.47M
  NeedsCoroutineSuspends = true;
47
3.47M
  CoroutineSuspends.first = nullptr;
48
3.47M
  CoroutineSuspends.second = nullptr;
49
3.47M
50
3.47M
  SwitchStack.clear();
51
3.47M
  Returns.clear();
52
3.47M
  ErrorTrap.reset();
53
3.47M
  PossiblyUnreachableDiags.clear();
54
3.47M
  WeakObjectUses.clear();
55
3.47M
  ModifiedNonNullParams.clear();
56
3.47M
  Blocks.clear();
57
3.47M
  ByrefBlockVars.clear();
58
3.47M
}
59
60
272
static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
61
272
  if (PropE->isExplicitProperty())
62
242
    return PropE->getExplicitProperty();
63
30
64
30
  return PropE->getImplicitPropertyGetter();
65
30
}
66
67
FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
68
282
FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
69
282
  E = E->IgnoreParenCasts();
70
282
71
282
  const NamedDecl *D = nullptr;
72
282
  bool IsExact = false;
73
282
74
282
  switch (E->getStmtClass()) {
75
282
  case Stmt::DeclRefExprClass:
76
210
    D = cast<DeclRefExpr>(E)->getDecl();
77
210
    IsExact = isa<VarDecl>(D);
78
210
    break;
79
282
  case Stmt::MemberExprClass: {
80
12
    const MemberExpr *ME = cast<MemberExpr>(E);
81
12
    D = ME->getMemberDecl();
82
12
    IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
83
12
    break;
84
282
  }
85
282
  case Stmt::ObjCIvarRefExprClass: {
86
16
    const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
87
16
    D = IE->getDecl();
88
16
    IsExact = IE->getBase()->isObjCSelfExpr();
89
16
    break;
90
282
  }
91
282
  case Stmt::PseudoObjectExprClass: {
92
44
    const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
93
44
    const ObjCPropertyRefExpr *BaseProp =
94
44
      dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
95
44
    if (BaseProp) {
96
44
      D = getBestPropertyDecl(BaseProp);
97
44
98
44
      if (BaseProp->isObjectReceiver()) {
99
32
        const Expr *DoubleBase = BaseProp->getBase();
100
32
        if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
101
32
          DoubleBase = OVE->getSourceExpr();
102
32
103
32
        IsExact = DoubleBase->isObjCSelfExpr();
104
32
      }
105
44
    }
106
44
    break;
107
282
  }
108
282
  default:
109
0
    break;
110
282
  }
111
282
112
282
  return BaseInfoTy(D, IsExact);
113
282
}
114
115
FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
116
                                          const ObjCPropertyRefExpr *PropE)
117
228
    : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
118
228
119
228
  if (PropE->isObjectReceiver()) {
120
222
    const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
121
222
    const Expr *E = OVE->getSourceExpr();
122
222
    Base = getBaseInfo(E);
123
222
  } else 
if (6
PropE->isClassReceiver()6
) {
124
6
    Base.setPointer(PropE->getClassReceiver());
125
6
  } else {
126
0
    assert(PropE->isSuperReceiver());
127
0
  }
128
228
}
129
130
FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
131
                                                const ObjCPropertyDecl *Prop)
132
30
    : Base(nullptr, true), Property(Prop) {
133
30
  if (BaseE)
134
30
    Base = getBaseInfo(BaseE);
135
30
  // else, this is a message accessing a property on super.
136
30
}
137
138
FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
139
                                                      const DeclRefExpr *DRE)
140
44
  : Base(nullptr, true), Property(DRE->getDecl()) {
141
44
  assert(isa<VarDecl>(Property));
142
44
}
143
144
FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
145
                                                  const ObjCIvarRefExpr *IvarE)
146
30
  : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
147
30
}
148
149
void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
150
26
                                        const ObjCPropertyDecl *Prop) {
151
26
  assert(Msg && Prop);
152
26
  WeakUseVector &Uses =
153
26
    WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
154
26
  Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
155
26
}
156
157
109
void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
158
109
  E = E->IgnoreParenCasts();
159
109
160
109
  if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
161
26
    markSafeWeakUse(POE->getSyntacticForm());
162
26
    return;
163
26
  }
164
83
165
83
  if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
166
2
    markSafeWeakUse(Cond->getTrueExpr());
167
2
    markSafeWeakUse(Cond->getFalseExpr());
168
2
    return;
169
2
  }
170
81
171
81
  if (const BinaryConditionalOperator *Cond =
172
4
        dyn_cast<BinaryConditionalOperator>(E)) {
173
4
    markSafeWeakUse(Cond->getCommon());
174
4
    markSafeWeakUse(Cond->getFalseExpr());
175
4
    return;
176
4
  }
177
77
178
77
  // Has this weak object been seen before?
179
77
  FunctionScopeInfo::WeakObjectUseMap::iterator Uses = WeakObjectUses.end();
180
77
  if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
181
34
    if (!RefExpr->isObjectReceiver())
182
2
      return;
183
32
    if (isa<OpaqueValueExpr>(RefExpr->getBase()))
184
24
     Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
185
8
    else {
186
8
      markSafeWeakUse(RefExpr->getBase());
187
8
      return;
188
8
    }
189
43
  }
190
43
  else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
191
6
    Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
192
37
  else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
193
20
    if (isa<VarDecl>(DRE->getDecl()))
194
18
      Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
195
20
  } else 
if (const ObjCMessageExpr *17
MsgE17
= dyn_cast<ObjCMessageExpr>(E)) {
196
4
    if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
197
4
      if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
198
4
        Uses =
199
4
          WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
200
4
                                                  Prop));
201
4
      }
202
4
    }
203
4
  }
204
13
  else
205
13
    return;
206
54
207
54
  if (Uses == WeakObjectUses.end())
208
10
    return;
209
44
210
44
  // Has there been a read from the object using this Expr?
211
44
  FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
212
44
      llvm::find(llvm::reverse(Uses->second), WeakUseTy(E, true));
213
44
  if (ThisUse == Uses->second.rend())
214
0
    return;
215
44
216
44
  ThisUse->markSafe();
217
44
}
218
219
224k
bool Capture::isInitCapture() const {
220
224k
  // Note that a nested capture of an init-capture is not itself an
221
224k
  // init-capture.
222
224k
  return !isNested() && 
isVariableCapture()85.3k
&&
getVariable()->isInitCapture()81.6k
;
223
224k
}
224
225
16.5k
bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
226
16.5k
  for (auto &Cap : Captures)
227
49.0k
    if (Cap.isVLATypeCapture() && 
Cap.getCapturedVLAType() == VAT18.9k
)
228
10.9k
      return true;
229
16.5k
  
return false5.59k
;
230
16.5k
}
231
232
void LambdaScopeInfo::visitPotentialCaptures(
233
597
    llvm::function_ref<void(VarDecl *, Expr *)> Callback) const {
234
732
  for (Expr *E : PotentiallyCapturingExprs) {
235
732
    if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
236
724
      Callback(cast<VarDecl>(DRE->getFoundDecl()), E);
237
724
    } else 
if (auto *8
ME8
= dyn_cast<MemberExpr>(E)) {
238
0
      Callback(cast<VarDecl>(ME->getMemberDecl()), E);
239
8
    } else if (auto *FP = dyn_cast<FunctionParmPackExpr>(E)) {
240
8
      for (VarDecl *VD : *FP)
241
22
        Callback(VD, E);
242
8
    } else {
243
0
      llvm_unreachable("unexpected expression in potential captures list");
244
0
    }
245
732
  }
246
597
}
247
248
465k
FunctionScopeInfo::~FunctionScopeInfo() { }
249
2.75k
BlockScopeInfo::~BlockScopeInfo() { }
250
295k
CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }