clang 20.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;
44
45 // Coroutine state
47 CoroutinePromise = nullptr;
50 CoroutineSuspends.first = nullptr;
51 CoroutineSuspends.second = nullptr;
52
53 SwitchStack.clear();
54 Returns.clear();
55 ErrorTrap.reset();
57 WeakObjectUses.clear();
59 Blocks.clear();
60 ByrefBlockVars.clear();
61 AddrLabels.clear();
62}
63
65 if (PropE->isExplicitProperty())
66 return PropE->getExplicitProperty();
67
68 return PropE->getImplicitPropertyGetter();
69}
70
71FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
72FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
73 E = E->IgnoreParenCasts();
74
75 const NamedDecl *D = nullptr;
76 bool IsExact = false;
77
78 switch (E->getStmtClass()) {
79 case Stmt::DeclRefExprClass:
80 D = cast<DeclRefExpr>(E)->getDecl();
81 IsExact = isa<VarDecl>(D);
82 break;
83 case Stmt::MemberExprClass: {
84 const MemberExpr *ME = cast<MemberExpr>(E);
85 D = ME->getMemberDecl();
86 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
87 break;
88 }
89 case Stmt::ObjCIvarRefExprClass: {
90 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
91 D = IE->getDecl();
92 IsExact = IE->getBase()->isObjCSelfExpr();
93 break;
94 }
95 case Stmt::PseudoObjectExprClass: {
96 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
97 const ObjCPropertyRefExpr *BaseProp =
98 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
99 if (BaseProp) {
100 D = getBestPropertyDecl(BaseProp);
101
102 if (BaseProp->isObjectReceiver()) {
103 const Expr *DoubleBase = BaseProp->getBase();
104 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
105 DoubleBase = OVE->getSourceExpr();
106
107 IsExact = DoubleBase->isObjCSelfExpr();
108 }
109 }
110 break;
111 }
112 default:
113 break;
114 }
115
116 return BaseInfoTy(D, IsExact);
117}
118
119FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
120 const ObjCPropertyRefExpr *PropE)
121 : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
122
123 if (PropE->isObjectReceiver()) {
124 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
125 const Expr *E = OVE->getSourceExpr();
126 Base = getBaseInfo(E);
127 } else if (PropE->isClassReceiver()) {
128 Base.setPointer(PropE->getClassReceiver());
129 } else {
130 assert(PropE->isSuperReceiver());
131 }
132}
133
134FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
135 const ObjCPropertyDecl *Prop)
136 : Base(nullptr, true), Property(Prop) {
137 if (BaseE)
138 Base = getBaseInfo(BaseE);
139 // else, this is a message accessing a property on super.
140}
141
142FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
143 const DeclRefExpr *DRE)
144 : Base(nullptr, true), Property(DRE->getDecl()) {
145 assert(isa<VarDecl>(Property));
146}
147
148FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
149 const ObjCIvarRefExpr *IvarE)
150 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
151}
152
154 const ObjCPropertyDecl *Prop) {
155 assert(Msg && Prop);
156 WeakUseVector &Uses =
157 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
158 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
159}
160
162 E = E->IgnoreParenCasts();
163
164 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
166 return;
167 }
168
169 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
170 markSafeWeakUse(Cond->getTrueExpr());
171 markSafeWeakUse(Cond->getFalseExpr());
172 return;
173 }
174
175 if (const BinaryConditionalOperator *Cond =
176 dyn_cast<BinaryConditionalOperator>(E)) {
177 markSafeWeakUse(Cond->getCommon());
178 markSafeWeakUse(Cond->getFalseExpr());
179 return;
180 }
181
182 // Has this weak object been seen before?
183 FunctionScopeInfo::WeakObjectUseMap::iterator Uses = WeakObjectUses.end();
184 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
185 if (!RefExpr->isObjectReceiver())
186 return;
187 if (isa<OpaqueValueExpr>(RefExpr->getBase()))
188 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
189 else {
190 markSafeWeakUse(RefExpr->getBase());
191 return;
192 }
193 }
194 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
195 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
196 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
197 if (isa<VarDecl>(DRE->getDecl()))
198 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
199 } else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
200 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
201 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
202 Uses =
203 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
204 Prop));
205 }
206 }
207 }
208 else
209 return;
210
211 if (Uses == WeakObjectUses.end())
212 return;
213
214 // Has there been a read from the object using this Expr?
215 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
216 llvm::find(llvm::reverse(Uses->second), WeakUseTy(E, true));
217 if (ThisUse == Uses->second.rend())
218 return;
219
220 ThisUse->markSafe();
221}
222
224 // Note that a nested capture of an init-capture is not itself an
225 // init-capture.
226 return !isNested() && isVariableCapture() && getVariable()->isInitCapture();
227}
228
230 for (auto &Cap : Captures)
231 if (Cap.isVLATypeCapture() && Cap.getCapturedVLAType() == VAT)
232 return true;
233 return false;
234}
235
237 llvm::function_ref<void(ValueDecl *, Expr *)> Callback) const {
238 for (Expr *E : PotentiallyCapturingExprs) {
239 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
240 Callback(cast<ValueDecl>(DRE->getFoundDecl()), E);
241 } else if (auto *ME = dyn_cast<MemberExpr>(E)) {
242 Callback(cast<ValueDecl>(ME->getMemberDecl()), E);
243 } else if (auto *FP = dyn_cast<FunctionParmPackExpr>(E)) {
244 for (ValueDecl *VD : *FP)
245 Callback(VD, E);
246 } else {
247 llvm_unreachable("unexpected expression in potential captures list");
248 }
249 }
250}
251
253 if (ExplicitObjectParameter)
254 return ExplicitObjectParameter->getType()
255 .getNonReferenceType()
256 .isConstQualified();
257 return !Mutable;
258}
259
const Decl * D
Expr * E
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:64
BinaryConditionalOperator - The GNU extension to the conditional operator which allows the middle ope...
Definition: Expr.h:4265
ConditionalOperator - The ?: ternary operator.
Definition: Expr.h:4203
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1265
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:3075
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition: Expr.cpp:3070
bool isObjCSelfExpr() const
Check if this expression is the ObjC 'self' implicit parameter.
Definition: Expr.cpp:4084
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3187
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition: Expr.h:3270
Expr * getBase() const
Definition: Expr.h:3264
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:730
static ObjCPropertyDecl * findPropertyDecl(const DeclContext *DC, const IdentifierInfo *propertyID, ObjCPropertyQueryKind queryKind)
Lookup a property by name in the specified DeclContext.
Definition: DeclObjC.cpp:179
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:1173
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Definition: Expr.h:1223
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:6487
Expr * getSyntacticForm()
Return the syntactic form of this expression, i.e.
Definition: Expr.h:6529
Encodes a location in the source.
StmtClass getStmtClass() const
Definition: Stmt.h:1358
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:667
Represents a C array with a specified size that is not an integer-constant-expression.
Definition: Type.h:3769
bool isInitCapture() const
Determine whether this capture is an init-capture.
Definition: ScopeInfo.cpp:223
bool isVLATypeCaptured(const VariableArrayType *VAT) const
Determine whether the given variable-array type has been captured.
Definition: ScopeInfo.cpp:229
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 FirstVLALoc
First use of a VLA within the current function.
Definition: ScopeInfo.h:196
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:161
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:252
void visitPotentialCaptures(llvm::function_ref< void(ValueDecl *, Expr *)> Callback) const
Definition: ScopeInfo.cpp:236
The JSON file list parser is used to communicate input to InstallAPI.
@ Property
The type of a property.
#define true
Definition: stdbool.h:25