Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 1 | //===--- ScopeInfo.cpp - Information about a semantic context -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements FunctionScopeInfo and its subclasses, which contain |
| 11 | // information about a single function, block, lambda, or method body. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Sema/ScopeInfo.h" |
| 16 | #include "clang/AST/Decl.h" |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/ExprCXX.h" |
| 21 | #include "clang/AST/ExprObjC.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | using namespace sema; |
| 25 | |
| 26 | void FunctionScopeInfo::Clear() { |
| 27 | HasBranchProtectedScope = false; |
| 28 | HasBranchIntoScope = false; |
| 29 | HasIndirectGoto = false; |
Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 30 | HasDroppedStmt = false; |
| 31 | ObjCShouldCallSuper = false; |
| 32 | ObjCIsDesignatedInit = false; |
| 33 | ObjCWarnForNoDesignatedInitChain = false; |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 34 | ObjCIsSecondaryInit = false; |
| 35 | ObjCWarnForNoInitDelegation = false; |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 36 | |
| 37 | SwitchStack.clear(); |
| 38 | Returns.clear(); |
| 39 | ErrorTrap.reset(); |
| 40 | PossiblyUnreachableDiags.clear(); |
| 41 | WeakObjectUses.clear(); |
Fariborz Jahanian | ef202d9 | 2014-11-18 21:57:54 +0000 | [diff] [blame] | 42 | ModifiedNonNullParams.clear(); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) { |
| 46 | if (PropE->isExplicitProperty()) |
| 47 | return PropE->getExplicitProperty(); |
| 48 | |
| 49 | return PropE->getImplicitPropertyGetter(); |
| 50 | } |
| 51 | |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 52 | FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy |
| 53 | FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) { |
| 54 | E = E->IgnoreParenCasts(); |
| 55 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 56 | const NamedDecl *D = nullptr; |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 57 | bool IsExact = false; |
| 58 | |
| 59 | switch (E->getStmtClass()) { |
| 60 | case Stmt::DeclRefExprClass: |
| 61 | D = cast<DeclRefExpr>(E)->getDecl(); |
| 62 | IsExact = isa<VarDecl>(D); |
| 63 | break; |
| 64 | case Stmt::MemberExprClass: { |
| 65 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 66 | D = ME->getMemberDecl(); |
| 67 | IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()); |
| 68 | break; |
| 69 | } |
| 70 | case Stmt::ObjCIvarRefExprClass: { |
| 71 | const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E); |
| 72 | D = IE->getDecl(); |
Anna Zaks | 97c7ce3 | 2012-10-01 20:34:04 +0000 | [diff] [blame] | 73 | IsExact = IE->getBase()->isObjCSelfExpr(); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 74 | break; |
| 75 | } |
| 76 | case Stmt::PseudoObjectExprClass: { |
| 77 | const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E); |
| 78 | const ObjCPropertyRefExpr *BaseProp = |
| 79 | dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm()); |
| 80 | if (BaseProp) { |
| 81 | D = getBestPropertyDecl(BaseProp); |
| 82 | |
| 83 | const Expr *DoubleBase = BaseProp->getBase(); |
| 84 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase)) |
| 85 | DoubleBase = OVE->getSourceExpr(); |
| 86 | |
Anna Zaks | 97c7ce3 | 2012-10-01 20:34:04 +0000 | [diff] [blame] | 87 | IsExact = DoubleBase->isObjCSelfExpr(); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 88 | } |
| 89 | break; |
| 90 | } |
| 91 | default: |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | return BaseInfoTy(D, IsExact); |
| 96 | } |
| 97 | |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 98 | bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const { |
Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 99 | RecordDecl *RD = nullptr; |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 100 | if (auto *LSI = dyn_cast<LambdaScopeInfo>(this)) |
Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 101 | RD = LSI->Lambda; |
| 102 | else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this)) |
| 103 | RD = CRSI->TheRecordDecl; |
| 104 | |
| 105 | if (RD) |
| 106 | for (auto *FD : RD->fields()) { |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 107 | if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT) |
| 108 | return true; |
| 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 113 | FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( |
| 114 | const ObjCPropertyRefExpr *PropE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 115 | : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) { |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 116 | |
| 117 | if (PropE->isObjectReceiver()) { |
| 118 | const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase()); |
| 119 | const Expr *E = OVE->getSourceExpr(); |
| 120 | Base = getBaseInfo(E); |
| 121 | } else if (PropE->isClassReceiver()) { |
| 122 | Base.setPointer(PropE->getClassReceiver()); |
| 123 | } else { |
| 124 | assert(PropE->isSuperReceiver()); |
| 125 | } |
| 126 | } |
| 127 | |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 128 | FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE, |
| 129 | const ObjCPropertyDecl *Prop) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 130 | : Base(nullptr, true), Property(Prop) { |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 131 | if (BaseE) |
| 132 | Base = getBaseInfo(BaseE); |
| 133 | // else, this is a message accessing a property on super. |
| 134 | } |
| 135 | |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 136 | FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( |
| 137 | const DeclRefExpr *DRE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 138 | : Base(nullptr, true), Property(DRE->getDecl()) { |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 139 | assert(isa<VarDecl>(Property)); |
| 140 | } |
| 141 | |
| 142 | FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( |
| 143 | const ObjCIvarRefExpr *IvarE) |
| 144 | : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) { |
| 145 | } |
| 146 | |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 147 | void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg, |
| 148 | const ObjCPropertyDecl *Prop) { |
| 149 | assert(Msg && Prop); |
| 150 | WeakUseVector &Uses = |
| 151 | WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)]; |
| 152 | Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0)); |
| 153 | } |
| 154 | |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 155 | void FunctionScopeInfo::markSafeWeakUse(const Expr *E) { |
Jordan Rose | e723a27 | 2012-10-10 16:43:06 +0000 | [diff] [blame] | 156 | E = E->IgnoreParenCasts(); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 157 | |
| 158 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { |
| 159 | markSafeWeakUse(POE->getSyntacticForm()); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) { |
| 164 | markSafeWeakUse(Cond->getTrueExpr()); |
| 165 | markSafeWeakUse(Cond->getFalseExpr()); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | if (const BinaryConditionalOperator *Cond = |
| 170 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 171 | markSafeWeakUse(Cond->getCommon()); |
| 172 | markSafeWeakUse(Cond->getFalseExpr()); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | // Has this weak object been seen before? |
| 177 | FunctionScopeInfo::WeakObjectUseMap::iterator Uses; |
Fariborz Jahanian | 9277ff4 | 2014-06-17 23:35:13 +0000 | [diff] [blame] | 178 | if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) { |
Fariborz Jahanian | ac1c512 | 2014-11-21 21:12:11 +0000 | [diff] [blame^] | 179 | if (!RefExpr->isObjectReceiver()) |
| 180 | return; |
Fariborz Jahanian | 9277ff4 | 2014-06-17 23:35:13 +0000 | [diff] [blame] | 181 | if (isa<OpaqueValueExpr>(RefExpr->getBase())) |
| 182 | Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr)); |
| 183 | else { |
| 184 | markSafeWeakUse(RefExpr->getBase()); |
| 185 | return; |
| 186 | } |
| 187 | } |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 188 | else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E)) |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 189 | Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE)); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 190 | else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 191 | Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE)); |
Jordan Rose | b1e3e5f | 2012-10-11 17:02:00 +0000 | [diff] [blame] | 192 | else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) { |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 193 | Uses = WeakObjectUses.end(); |
| 194 | if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) { |
| 195 | if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) { |
| 196 | Uses = |
| 197 | WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(), |
| 198 | Prop)); |
| 199 | } |
| 200 | } |
| 201 | } |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 202 | else |
| 203 | return; |
| 204 | |
| 205 | if (Uses == WeakObjectUses.end()) |
| 206 | return; |
| 207 | |
| 208 | // Has there been a read from the object using this Expr? |
| 209 | FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse = |
| 210 | std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true)); |
| 211 | if (ThisUse == Uses->second.rend()) |
| 212 | return; |
| 213 | |
| 214 | ThisUse->markSafe(); |
| 215 | } |
| 216 | |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 217 | void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD, |
| 218 | Expr *&E) const { |
Faisal Vali | 9366a48 | 2013-11-07 16:57:56 +0000 | [diff] [blame] | 219 | assert(Idx < getNumPotentialVariableCaptures() && |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 220 | "Index of potential capture must be within 0 to less than the " |
| 221 | "number of captures!"); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 222 | E = PotentiallyCapturingExprs[Idx]; |
| 223 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 224 | VD = dyn_cast<VarDecl>(DRE->getFoundDecl()); |
| 225 | else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 226 | VD = dyn_cast<VarDecl>(ME->getMemberDecl()); |
| 227 | else |
| 228 | llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for " |
| 229 | "potential captures"); |
| 230 | assert(VD); |
| 231 | } |
| 232 | |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 233 | FunctionScopeInfo::~FunctionScopeInfo() { } |
| 234 | BlockScopeInfo::~BlockScopeInfo() { } |
| 235 | LambdaScopeInfo::~LambdaScopeInfo() { } |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 236 | CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { } |