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