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
Clear()25 void FunctionScopeInfo::Clear() {
26 HasBranchProtectedScope = false;
27 HasBranchIntoScope = false;
28 HasIndirectGoto = false;
29 HasDroppedStmt = false;
30 HasOMPDeclareReductionCombiner = false;
31 HasFallthroughStmt = false;
32 UsesFPIntrin = false;
33 HasPotentialAvailabilityViolations = false;
34 ObjCShouldCallSuper = false;
35 ObjCIsDesignatedInit = false;
36 ObjCWarnForNoDesignatedInitChain = false;
37 ObjCIsSecondaryInit = false;
38 ObjCWarnForNoInitDelegation = false;
39 FirstReturnLoc = SourceLocation();
40 FirstCXXOrObjCTryLoc = SourceLocation();
41 FirstSEHTryLoc = SourceLocation();
42
43 // Coroutine state
44 FirstCoroutineStmtLoc = SourceLocation();
45 CoroutinePromise = nullptr;
46 CoroutineParameterMoves.clear();
47 NeedsCoroutineSuspends = true;
48 CoroutineSuspends.first = nullptr;
49 CoroutineSuspends.second = nullptr;
50
51 SwitchStack.clear();
52 Returns.clear();
53 ErrorTrap.reset();
54 PossiblyUnreachableDiags.clear();
55 WeakObjectUses.clear();
56 ModifiedNonNullParams.clear();
57 Blocks.clear();
58 ByrefBlockVars.clear();
59 AddrLabels.clear();
60 }
61
getBestPropertyDecl(const ObjCPropertyRefExpr * PropE)62 static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
63 if (PropE->isExplicitProperty())
64 return PropE->getExplicitProperty();
65
66 return PropE->getImplicitPropertyGetter();
67 }
68
69 FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
getBaseInfo(const Expr * E)70 FunctionScopeInfo::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
WeakObjectProfileTy(const ObjCPropertyRefExpr * PropE)117 FunctionScopeInfo::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
WeakObjectProfileTy(const Expr * BaseE,const ObjCPropertyDecl * Prop)132 FunctionScopeInfo::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
WeakObjectProfileTy(const DeclRefExpr * DRE)140 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
141 const DeclRefExpr *DRE)
142 : Base(nullptr, true), Property(DRE->getDecl()) {
143 assert(isa<VarDecl>(Property));
144 }
145
WeakObjectProfileTy(const ObjCIvarRefExpr * IvarE)146 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
147 const ObjCIvarRefExpr *IvarE)
148 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
149 }
150
recordUseOfWeak(const ObjCMessageExpr * Msg,const ObjCPropertyDecl * Prop)151 void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
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
markSafeWeakUse(const Expr * E)159 void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
160 E = E->IgnoreParenCasts();
161
162 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
163 markSafeWeakUse(POE->getSyntacticForm());
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
isInitCapture() const221 bool Capture::isInitCapture() const {
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
isVLATypeCaptured(const VariableArrayType * VAT) const227 bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
228 for (auto &Cap : Captures)
229 if (Cap.isVLATypeCapture() && Cap.getCapturedVLAType() == VAT)
230 return true;
231 return false;
232 }
233
visitPotentialCaptures(llvm::function_ref<void (ValueDecl *,Expr *)> Callback) const234 void LambdaScopeInfo::visitPotentialCaptures(
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
~FunctionScopeInfo()250 FunctionScopeInfo::~FunctionScopeInfo() { }
~BlockScopeInfo()251 BlockScopeInfo::~BlockScopeInfo() { }
~CapturedRegionScopeInfo()252 CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }
253