Coverage Report

Created: 2021-08-24 07:12

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