Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/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
2.85M
void FunctionScopeInfo::Clear() {
26
2.85M
  HasBranchProtectedScope = false;
27
2.85M
  HasBranchIntoScope = false;
28
2.85M
  HasIndirectGoto = false;
29
2.85M
  HasDroppedStmt = false;
30
2.85M
  HasOMPDeclareReductionCombiner = false;
31
2.85M
  HasFallthroughStmt = false;
32
2.85M
  HasPotentialAvailabilityViolations = false;
33
2.85M
  ObjCShouldCallSuper = false;
34
2.85M
  ObjCIsDesignatedInit = false;
35
2.85M
  ObjCWarnForNoDesignatedInitChain = false;
36
2.85M
  ObjCIsSecondaryInit = false;
37
2.85M
  ObjCWarnForNoInitDelegation = false;
38
2.85M
  FirstReturnLoc = SourceLocation();
39
2.85M
  FirstCXXTryLoc = SourceLocation();
40
2.85M
  FirstSEHTryLoc = SourceLocation();
41
2.85M
42
2.85M
  // Coroutine state
43
2.85M
  FirstCoroutineStmtLoc = SourceLocation();
44
2.85M
  CoroutinePromise = nullptr;
45
2.85M
  CoroutineParameterMoves.clear();
46
2.85M
  NeedsCoroutineSuspends = true;
47
2.85M
  CoroutineSuspends.first = nullptr;
48
2.85M
  CoroutineSuspends.second = nullptr;
49
2.85M
50
2.85M
  SwitchStack.clear();
51
2.85M
  Returns.clear();
52
2.85M
  ErrorTrap.reset();
53
2.85M
  PossiblyUnreachableDiags.clear();
54
2.85M
  WeakObjectUses.clear();
55
2.85M
  ModifiedNonNullParams.clear();
56
2.85M
  Blocks.clear();
57
2.85M
  ByrefBlockVars.clear();
58
2.85M
}
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
210
  case Stmt::DeclRefExprClass:
76
210
    D = cast<DeclRefExpr>(E)->getDecl();
77
210
    IsExact = isa<VarDecl>(D);
78
210
    break;
79
12
  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
0
  }
85
16
  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
0
  }
91
44
  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
0
  }
108
0
  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
318k
bool Capture::isInitCapture() const {
220
318k
  // Note that a nested capture of an init-capture is not itself an
221
318k
  // init-capture.
222
318k
  return !isNested() && 
isVariableCapture()122k
&&
getVariable()->isInitCapture()117k
;
223
318k
}
224
225
19.6k
bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
226
19.6k
  for (auto &Cap : Captures)
227
60.5k
    if (Cap.isVLATypeCapture() && 
Cap.getCapturedVLAType() == VAT23.1k
)
228
12.9k
      return true;
229
19.6k
  
return false6.69k
;
230
19.6k
}
231
232
void LambdaScopeInfo::visitPotentialCaptures(
233
488
    llvm::function_ref<void(VarDecl *, Expr *)> Callback) const {
234
546
  for (Expr *E : PotentiallyCapturingExprs) {
235
546
    if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
236
538
      Callback(cast<VarDecl>(DRE->getFoundDecl()), E);
237
538
    } 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
546
  }
246
488
}
247
248
594k
FunctionScopeInfo::~FunctionScopeInfo() { }
249
2.80k
BlockScopeInfo::~BlockScopeInfo() { }
250
427k
CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }