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