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 bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const { 116 RecordDecl *RD = nullptr; 117 if (auto *LSI = dyn_cast<LambdaScopeInfo>(this)) 118 RD = LSI->Lambda; 119 else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this)) 120 RD = CRSI->TheRecordDecl; 121 122 if (RD) 123 for (auto *FD : RD->fields()) { 124 if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT) 125 return true; 126 } 127 return false; 128 } 129 130 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( 131 const ObjCPropertyRefExpr *PropE) 132 : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) { 133 134 if (PropE->isObjectReceiver()) { 135 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase()); 136 const Expr *E = OVE->getSourceExpr(); 137 Base = getBaseInfo(E); 138 } else if (PropE->isClassReceiver()) { 139 Base.setPointer(PropE->getClassReceiver()); 140 } else { 141 assert(PropE->isSuperReceiver()); 142 } 143 } 144 145 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE, 146 const ObjCPropertyDecl *Prop) 147 : Base(nullptr, true), Property(Prop) { 148 if (BaseE) 149 Base = getBaseInfo(BaseE); 150 // else, this is a message accessing a property on super. 151 } 152 153 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( 154 const DeclRefExpr *DRE) 155 : Base(nullptr, true), Property(DRE->getDecl()) { 156 assert(isa<VarDecl>(Property)); 157 } 158 159 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( 160 const ObjCIvarRefExpr *IvarE) 161 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) { 162 } 163 164 void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg, 165 const ObjCPropertyDecl *Prop) { 166 assert(Msg && Prop); 167 WeakUseVector &Uses = 168 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)]; 169 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0)); 170 } 171 172 void FunctionScopeInfo::markSafeWeakUse(const Expr *E) { 173 E = E->IgnoreParenCasts(); 174 175 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { 176 markSafeWeakUse(POE->getSyntacticForm()); 177 return; 178 } 179 180 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) { 181 markSafeWeakUse(Cond->getTrueExpr()); 182 markSafeWeakUse(Cond->getFalseExpr()); 183 return; 184 } 185 186 if (const BinaryConditionalOperator *Cond = 187 dyn_cast<BinaryConditionalOperator>(E)) { 188 markSafeWeakUse(Cond->getCommon()); 189 markSafeWeakUse(Cond->getFalseExpr()); 190 return; 191 } 192 193 // Has this weak object been seen before? 194 FunctionScopeInfo::WeakObjectUseMap::iterator Uses = WeakObjectUses.end(); 195 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) { 196 if (!RefExpr->isObjectReceiver()) 197 return; 198 if (isa<OpaqueValueExpr>(RefExpr->getBase())) 199 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr)); 200 else { 201 markSafeWeakUse(RefExpr->getBase()); 202 return; 203 } 204 } 205 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E)) 206 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE)); 207 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { 208 if (isa<VarDecl>(DRE->getDecl())) 209 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE)); 210 } else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) { 211 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) { 212 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) { 213 Uses = 214 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(), 215 Prop)); 216 } 217 } 218 } 219 else 220 return; 221 222 if (Uses == WeakObjectUses.end()) 223 return; 224 225 // Has there been a read from the object using this Expr? 226 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse = 227 llvm::find(llvm::reverse(Uses->second), WeakUseTy(E, true)); 228 if (ThisUse == Uses->second.rend()) 229 return; 230 231 ThisUse->markSafe(); 232 } 233 234 void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD, 235 Expr *&E) const { 236 assert(Idx < getNumPotentialVariableCaptures() && 237 "Index of potential capture must be within 0 to less than the " 238 "number of captures!"); 239 E = PotentiallyCapturingExprs[Idx]; 240 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 241 VD = dyn_cast<VarDecl>(DRE->getFoundDecl()); 242 else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) 243 VD = dyn_cast<VarDecl>(ME->getMemberDecl()); 244 else 245 llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for " 246 "potential captures"); 247 assert(VD); 248 } 249 250 FunctionScopeInfo::~FunctionScopeInfo() { } 251 BlockScopeInfo::~BlockScopeInfo() { } 252 CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { } 253