clang  14.0.0git
ScopeInfo.cpp
Go to the documentation of this file.
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 
27  HasBranchIntoScope = false;
28  HasIndirectGoto = false;
29  HasDroppedStmt = false;
31  HasFallthroughStmt = false;
32  UsesFPIntrin = false;
34  ObjCShouldCallSuper = false;
35  ObjCIsDesignatedInit = false;
37  ObjCIsSecondaryInit = false;
42 
43  // Coroutine state
45  CoroutinePromise = nullptr;
48  CoroutineSuspends.first = nullptr;
49  CoroutineSuspends.second = nullptr;
50 
51  SwitchStack.clear();
52  Returns.clear();
53  ErrorTrap.reset();
55  WeakObjectUses.clear();
56  ModifiedNonNullParams.clear();
57  Blocks.clear();
58  ByrefBlockVars.clear();
59 }
60 
61 static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
62  if (PropE->isExplicitProperty())
63  return PropE->getExplicitProperty();
64 
65  return PropE->getImplicitPropertyGetter();
66 }
67 
68 FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
69 FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
70  E = E->IgnoreParenCasts();
71 
72  const NamedDecl *D = nullptr;
73  bool IsExact = false;
74 
75  switch (E->getStmtClass()) {
76  case Stmt::DeclRefExprClass:
77  D = cast<DeclRefExpr>(E)->getDecl();
78  IsExact = isa<VarDecl>(D);
79  break;
80  case Stmt::MemberExprClass: {
81  const MemberExpr *ME = cast<MemberExpr>(E);
82  D = ME->getMemberDecl();
83  IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
84  break;
85  }
86  case Stmt::ObjCIvarRefExprClass: {
87  const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
88  D = IE->getDecl();
89  IsExact = IE->getBase()->isObjCSelfExpr();
90  break;
91  }
92  case Stmt::PseudoObjectExprClass: {
93  const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
94  const ObjCPropertyRefExpr *BaseProp =
95  dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
96  if (BaseProp) {
97  D = getBestPropertyDecl(BaseProp);
98 
99  if (BaseProp->isObjectReceiver()) {
100  const Expr *DoubleBase = BaseProp->getBase();
101  if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
102  DoubleBase = OVE->getSourceExpr();
103 
104  IsExact = DoubleBase->isObjCSelfExpr();
105  }
106  }
107  break;
108  }
109  default:
110  break;
111  }
112 
113  return BaseInfoTy(D, IsExact);
114 }
115 
116 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
117  const ObjCPropertyRefExpr *PropE)
118  : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
119 
120  if (PropE->isObjectReceiver()) {
121  const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
122  const Expr *E = OVE->getSourceExpr();
123  Base = getBaseInfo(E);
124  } else if (PropE->isClassReceiver()) {
125  Base.setPointer(PropE->getClassReceiver());
126  } else {
127  assert(PropE->isSuperReceiver());
128  }
129 }
130 
131 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
132  const ObjCPropertyDecl *Prop)
133  : Base(nullptr, true), Property(Prop) {
134  if (BaseE)
135  Base = getBaseInfo(BaseE);
136  // else, this is a message accessing a property on super.
137 }
138 
139 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
140  const DeclRefExpr *DRE)
141  : Base(nullptr, true), Property(DRE->getDecl()) {
142  assert(isa<VarDecl>(Property));
143 }
144 
145 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
146  const ObjCIvarRefExpr *IvarE)
147  : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
148 }
149 
151  const ObjCPropertyDecl *Prop) {
152  assert(Msg && Prop);
153  WeakUseVector &Uses =
154  WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
155  Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
156 }
157 
159  E = E->IgnoreParenCasts();
160 
161  if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
163  return;
164  }
165 
166  if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
167  markSafeWeakUse(Cond->getTrueExpr());
168  markSafeWeakUse(Cond->getFalseExpr());
169  return;
170  }
171 
172  if (const BinaryConditionalOperator *Cond =
173  dyn_cast<BinaryConditionalOperator>(E)) {
174  markSafeWeakUse(Cond->getCommon());
175  markSafeWeakUse(Cond->getFalseExpr());
176  return;
177  }
178 
179  // Has this weak object been seen before?
180  FunctionScopeInfo::WeakObjectUseMap::iterator Uses = WeakObjectUses.end();
181  if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
182  if (!RefExpr->isObjectReceiver())
183  return;
184  if (isa<OpaqueValueExpr>(RefExpr->getBase()))
185  Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
186  else {
187  markSafeWeakUse(RefExpr->getBase());
188  return;
189  }
190  }
191  else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
192  Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
193  else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
194  if (isa<VarDecl>(DRE->getDecl()))
195  Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
196  } else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
197  if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
198  if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
199  Uses =
200  WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
201  Prop));
202  }
203  }
204  }
205  else
206  return;
207 
208  if (Uses == WeakObjectUses.end())
209  return;
210 
211  // Has there been a read from the object using this Expr?
212  FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
213  llvm::find(llvm::reverse(Uses->second), WeakUseTy(E, true));
214  if (ThisUse == Uses->second.rend())
215  return;
216 
217  ThisUse->markSafe();
218 }
219 
221  // Note that a nested capture of an init-capture is not itself an
222  // init-capture.
223  return !isNested() && isVariableCapture() && getVariable()->isInitCapture();
224 }
225 
227  for (auto &Cap : Captures)
228  if (Cap.isVLATypeCapture() && Cap.getCapturedVLAType() == VAT)
229  return true;
230  return false;
231 }
232 
234  llvm::function_ref<void(VarDecl *, Expr *)> Callback) const {
235  for (Expr *E : PotentiallyCapturingExprs) {
236  if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
237  Callback(cast<VarDecl>(DRE->getFoundDecl()), E);
238  } else if (auto *ME = dyn_cast<MemberExpr>(E)) {
239  Callback(cast<VarDecl>(ME->getMemberDecl()), E);
240  } else if (auto *FP = dyn_cast<FunctionParmPackExpr>(E)) {
241  for (VarDecl *VD : *FP)
242  Callback(VD, E);
243  } else {
244  llvm_unreachable("unexpected expression in potential captures list");
245  }
246  }
247 }
248 
clang::ObjCPropertyRefExpr::getBase
const Expr * getBase() const
Definition: ExprObjC.h:752
clang::sema::FunctionScopeInfo::NeedsCoroutineSuspends
bool NeedsCoroutineSuspends
True only when this function has not already built, or attempted to build, the initial and final coro...
Definition: ScopeInfo.h:165
clang::sema::FunctionScopeInfo::PossiblyUnreachableDiags
SmallVector< PossiblyUnreachableDiag, 4 > PossiblyUnreachableDiags
A list of PartialDiagnostics created but delayed within the current function scope.
Definition: ScopeInfo.h:226
clang::ObjCPropertyRefExpr::isObjectReceiver
bool isObjectReceiver() const
Definition: ExprObjC.h:771
clang::sema::FunctionScopeInfo::CoroutineSuspends
std::pair< Stmt *, Stmt * > CoroutineSuspends
The initial and final coroutine suspend points.
Definition: ScopeInfo.h:211
clang::DiagnosticErrorTrap::reset
void reset()
Set to initial state of "no errors occurred".
Definition: Diagnostic.h:1092
clang::sema::FunctionScopeInfo::ByrefBlockVars
llvm::TinyPtrVector< VarDecl * > ByrefBlockVars
The set of __block variables that are introduced in this function.
Definition: ScopeInfo.h:221
clang::sema::Capture::isInitCapture
bool isInitCapture() const
Determine whether this capture is an init-capture.
Definition: ScopeInfo.cpp:220
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::sema::FunctionScopeInfo::Clear
void Clear()
Clear out the information in this function scope, making it suitable for reuse.
Definition: ScopeInfo.cpp:25
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:249
clang::sema::FunctionScopeInfo::CoroutineParameterMoves
llvm::SmallMapVector< ParmVarDecl *, Stmt *, 4 > CoroutineParameterMoves
A mapping between the coroutine function parameters that were moved to the coroutine frame,...
Definition: ScopeInfo.h:208
clang::sema::FunctionScopeInfo::HasBranchIntoScope
bool HasBranchIntoScope
Whether this function contains any switches or direct gotos.
Definition: ScopeInfo.h:116
DeclCXX.h
clang::ObjCMessageExpr::getInstanceReceiver
Expr * getInstanceReceiver()
Returns the object expression (receiver) for an instance message, or null for a message that is not a...
Definition: ExprObjC.h:1250
clang::sema::FunctionScopeInfo::FirstSEHTryLoc
SourceLocation FirstSEHTryLoc
First SEH '__try' statement in the current function.
Definition: ScopeInfo.h:183
clang::ObjCPropertyRefExpr::isSuperReceiver
bool isSuperReceiver() const
Definition: ExprObjC.h:772
clang::sema::FunctionScopeInfo::UsesFPIntrin
bool UsesFPIntrin
Whether this function uses constrained floating point intrinsics.
Definition: ScopeInfo.h:135
clang::sema::FunctionScopeInfo::Returns
SmallVector< ReturnStmt *, 4 > Returns
The list of return statements that occur within the function or block, if there is any chance of appl...
Definition: ScopeInfo.h:201
clang::sema::FunctionScopeInfo::recordUseOfWeak
void recordUseOfWeak(const ExprT *E, bool IsRead=true)
Record that a weak object was accessed.
Definition: ScopeInfo.h:1042
getBestPropertyDecl
static const NamedDecl * getBestPropertyDecl(const ObjCPropertyRefExpr *PropE)
Definition: ScopeInfo.cpp:61
clang::OpaqueValueExpr
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
Definition: Expr.h:1129
clang::ObjCPropertyRefExpr::getExplicitProperty
ObjCPropertyDecl * getExplicitProperty() const
Definition: ExprObjC.h:703
Decl.h
clang::ConditionalOperator
ConditionalOperator - The ?: ternary operator.
Definition: Expr.h:4141
DeclObjC.h
clang::ObjCPropertyRefExpr
ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC property.
Definition: ExprObjC.h:614
clang::ObjCIvarRefExpr::getDecl
ObjCIvarDecl * getDecl()
Definition: ExprObjC.h:576
clang::sema::FunctionScopeInfo::Blocks
llvm::SmallPtrSet< const BlockDecl *, 1 > Blocks
The set of blocks that are introduced in this function.
Definition: ScopeInfo.h:218
clang::sema::FunctionScopeInfo::HasDroppedStmt
bool HasDroppedStmt
Whether a statement was dropped because it was invalid.
Definition: ScopeInfo.h:126
clang::sema::FunctionScopeInfo::CoroutinePromise
VarDecl * CoroutinePromise
The promise object for this coroutine, if any.
Definition: ScopeInfo.h:204
clang::sema::FunctionScopeInfo::WeakUseTy
Represents a single use of a weak object.
Definition: ScopeInfo.h:339
clang::sema::FunctionScopeInfo::SwitchStack
SmallVector< SwitchInfo, 8 > SwitchStack
SwitchStack - This is the current set of active switch statements in the block.
Definition: ScopeInfo.h:196
clang::sema::FunctionScopeInfo::HasBranchProtectedScope
bool HasBranchProtectedScope
Whether this function contains a VLA, @try, try, C++ initializer, or anything else that can't be jump...
Definition: ScopeInfo.h:113
clang::PseudoObjectExpr
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:6095
clang::sema::FunctionScopeInfo::HasPotentialAvailabilityViolations
bool HasPotentialAvailabilityViolations
Whether we make reference to a declaration that could be unavailable.
Definition: ScopeInfo.h:139
clang::BinaryConditionalOperator
BinaryConditionalOperator - The GNU extension to the conditional operator which allows the middle ope...
Definition: Expr.h:4203
Expr.h
clang::OpaqueValueExpr::getSourceExpr
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Definition: Expr.h:1179
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:876
clang::sema::FunctionScopeInfo::~FunctionScopeInfo
virtual ~FunctionScopeInfo()
Definition: ScopeInfo.cpp:249
clang::sema::LambdaScopeInfo::visitPotentialCaptures
void visitPotentialCaptures(llvm::function_ref< void(VarDecl *, Expr *)> Callback) const
Definition: ScopeInfo.cpp:233
ExprObjC.h
ExprCXX.h
Base
clang::sema::CapturedRegionScopeInfo::~CapturedRegionScopeInfo
~CapturedRegionScopeInfo() override
Definition: ScopeInfo.cpp:251
clang::sema::FunctionScopeInfo::ObjCShouldCallSuper
bool ObjCShouldCallSuper
A flag that is set when parsing a method that must call super's implementation, such as -dealloc,...
Definition: ScopeInfo.h:144
clang::ObjCMessageExpr
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:940
clang::MemberExpr::getMemberDecl
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition: Expr.h:3245
clang::Expr::IgnoreParenCasts
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
Definition: Expr.cpp:2924
clang::MemberExpr::getBase
Expr * getBase() const
Definition: Expr.h:3239
clang::ObjCPropertyRefExpr::isClassReceiver
bool isClassReceiver() const
Definition: ExprObjC.h:773
clang::sema::FunctionScopeInfo::FirstCoroutineStmtLoc
SourceLocation FirstCoroutineStmtLoc
First coroutine statement in the current function.
Definition: ScopeInfo.h:173
clang::sema::FunctionScopeInfo::ObjCWarnForNoInitDelegation
bool ObjCWarnForNoInitDelegation
This starts true for a secondary initializer method and will be set to false if there is an invocatio...
Definition: ScopeInfo.h:161
clang::PseudoObjectExpr::getSyntacticForm
Expr * getSyntacticForm()
Return the syntactic form of this expression, i.e.
Definition: Expr.h:6139
clang::VariableArrayType
Represents a C array with a specified size that is not an integer-constant-expression.
Definition: Type.h:3033
clang::sema::FunctionScopeInfo::ObjCIsDesignatedInit
bool ObjCIsDesignatedInit
True when this is a method marked as a designated initializer.
Definition: ScopeInfo.h:147
clang::Stmt::getStmtClass
StmtClass getStmtClass() const
Definition: Stmt.h:1163
clang::sema::FunctionScopeInfo::FirstReturnLoc
SourceLocation FirstReturnLoc
First 'return' statement in the current function.
Definition: ScopeInfo.h:176
clang::Expr::IgnoreParenImpCasts
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition: Expr.cpp:2919
clang::ObjCPropertyDecl
Represents one property declaration in an Objective-C interface.
Definition: DeclObjC.h:732
clang::sema::FunctionScopeInfo::ObjCIsSecondaryInit
bool ObjCIsSecondaryInit
True when this is an initializer method not marked as a designated initializer within a class that ha...
Definition: ScopeInfo.h:157
clang::sema::FunctionScopeInfo::ObjCWarnForNoDesignatedInitChain
bool ObjCWarnForNoDesignatedInitChain
This starts true for a method marked as designated initializer and will be set to false if there is a...
Definition: ScopeInfo.h:152
clang::Expr::isObjCSelfExpr
bool isObjCSelfExpr() const
Check if this expression is the ObjC 'self' implicit parameter.
Definition: Expr.cpp:3884
clang::sema::FunctionScopeInfo::markSafeWeakUse
void markSafeWeakUse(const Expr *E)
Record that a given expression is a "safe" access of a weak object (e.g.
Definition: ScopeInfo.cpp:158
clang::ObjCMethodDecl
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:139
ScopeInfo.h
clang::sema::FunctionScopeInfo::WeakObjectProfileTy
Represents a simple identification of a weak object.
Definition: ScopeInfo.h:255
clang::sema::FunctionScopeInfo::HasOMPDeclareReductionCombiner
bool HasOMPDeclareReductionCombiner
True if current scope is for OpenMP declare reduction combiner.
Definition: ScopeInfo.h:129
clang::sema::BlockScopeInfo::~BlockScopeInfo
~BlockScopeInfo() override
Definition: ScopeInfo.cpp:250
clang
Definition: CalledOnceCheck.h:17
clang::ObjCPropertyRefExpr::getImplicitPropertyGetter
ObjCMethodDecl * getImplicitPropertyGetter() const
Definition: ExprObjC.h:708
clang::sema::FunctionScopeInfo::ModifiedNonNullParams
llvm::SmallPtrSet< const ParmVarDecl *, 8 > ModifiedNonNullParams
A list of parameters which have the nonnull attribute and are modified in the function.
Definition: ScopeInfo.h:230
clang::sema::FunctionScopeInfo::FirstCXXOrObjCTryLoc
SourceLocation FirstCXXOrObjCTryLoc
First C++ 'try' or ObjC @try statement in the current function.
Definition: ScopeInfo.h:179
clang::sema::FunctionScopeInfo::HasIndirectGoto
bool HasIndirectGoto
Whether this function contains any indirect gotos.
Definition: ScopeInfo.h:119
clang::ObjCSubstitutionContext::Property
@ Property
The type of a property.
clang::MemberExpr
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3162
clang::sema::FunctionScopeInfo::HasFallthroughStmt
bool HasFallthroughStmt
Whether there is a fallthrough statement in this function.
Definition: ScopeInfo.h:132
clang::ObjCIvarRefExpr::getBase
const Expr * getBase() const
Definition: ExprObjC.h:580
clang::sema::CapturingScopeInfo::isVLATypeCaptured
bool isVLATypeCaptured(const VariableArrayType *VAT) const
Determine whether the given variable-array type has been captured.
Definition: ScopeInfo.cpp:226
clang::ObjCMessageExpr::getNumArgs
unsigned getNumArgs() const
Return the number of actual arguments in this message, not counting the receiver.
Definition: ExprObjC.h:1372
true
#define true
Definition: stdbool.h:16
clang::ObjCPropertyRefExpr::getClassReceiver
ObjCInterfaceDecl * getClassReceiver() const
Definition: ExprObjC.h:767
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::DeclRefExpr
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1217
clang::ObjCPropertyDecl::findPropertyDecl
static ObjCPropertyDecl * findPropertyDecl(const DeclContext *DC, const IdentifierInfo *propertyID, ObjCPropertyQueryKind queryKind)
Lookup a property by name in the specified DeclContext.
Definition: DeclObjC.cpp:178
clang::ObjCIvarRefExpr
ObjCIvarRefExpr - A reference to an ObjC instance variable.
Definition: ExprObjC.h:548
clang::ObjCPropertyRefExpr::isExplicitProperty
bool isExplicitProperty() const
Definition: ExprObjC.h:701