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