ScopeInfo.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #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. using namespace clang;
  22. using namespace sema;
  23. void FunctionScopeInfo::Clear() {
  24. HasBranchProtectedScope = false;
  25. HasBranchIntoScope = false;
  26. HasIndirectGoto = false;
  27. HasDroppedStmt = false;
  28. ObjCShouldCallSuper = false;
  29. ObjCIsDesignatedInit = false;
  30. ObjCWarnForNoDesignatedInitChain = false;
  31. ObjCIsSecondaryInit = false;
  32. ObjCWarnForNoInitDelegation = false;
  33. FirstCXXTryLoc = SourceLocation();
  34. FirstSEHTryLoc = SourceLocation();
  35. SwitchStack.clear();
  36. Returns.clear();
  37. ErrorTrap.reset();
  38. PossiblyUnreachableDiags.clear();
  39. WeakObjectUses.clear();
  40. ModifiedNonNullParams.clear();
  41. }
  42. static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
  43. if (PropE->isExplicitProperty())
  44. return PropE->getExplicitProperty();
  45. return PropE->getImplicitPropertyGetter();
  46. }
  47. FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
  48. FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
  49. E = E->IgnoreParenCasts();
  50. const NamedDecl *D = nullptr;
  51. bool IsExact = false;
  52. switch (E->getStmtClass()) {
  53. case Stmt::DeclRefExprClass:
  54. D = cast<DeclRefExpr>(E)->getDecl();
  55. IsExact = isa<VarDecl>(D);
  56. break;
  57. case Stmt::MemberExprClass: {
  58. const MemberExpr *ME = cast<MemberExpr>(E);
  59. D = ME->getMemberDecl();
  60. IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
  61. break;
  62. }
  63. case Stmt::ObjCIvarRefExprClass: {
  64. const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
  65. D = IE->getDecl();
  66. IsExact = IE->getBase()->isObjCSelfExpr();
  67. break;
  68. }
  69. case Stmt::PseudoObjectExprClass: {
  70. const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
  71. const ObjCPropertyRefExpr *BaseProp =
  72. dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
  73. if (BaseProp) {
  74. D = getBestPropertyDecl(BaseProp);
  75. const Expr *DoubleBase = BaseProp->getBase();
  76. if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
  77. DoubleBase = OVE->getSourceExpr();
  78. IsExact = DoubleBase->isObjCSelfExpr();
  79. }
  80. break;
  81. }
  82. default:
  83. break;
  84. }
  85. return BaseInfoTy(D, IsExact);
  86. }
  87. bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
  88. RecordDecl *RD = nullptr;
  89. if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
  90. RD = LSI->Lambda;
  91. else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
  92. RD = CRSI->TheRecordDecl;
  93. if (RD)
  94. for (auto *FD : RD->fields()) {
  95. if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
  96. return true;
  97. }
  98. return false;
  99. }
  100. FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
  101. const ObjCPropertyRefExpr *PropE)
  102. : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
  103. if (PropE->isObjectReceiver()) {
  104. const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
  105. const Expr *E = OVE->getSourceExpr();
  106. Base = getBaseInfo(E);
  107. } else if (PropE->isClassReceiver()) {
  108. Base.setPointer(PropE->getClassReceiver());
  109. } else {
  110. assert(PropE->isSuperReceiver());
  111. }
  112. }
  113. FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
  114. const ObjCPropertyDecl *Prop)
  115. : Base(nullptr, true), Property(Prop) {
  116. if (BaseE)
  117. Base = getBaseInfo(BaseE);
  118. // else, this is a message accessing a property on super.
  119. }
  120. FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
  121. const DeclRefExpr *DRE)
  122. : Base(nullptr, true), Property(DRE->getDecl()) {
  123. assert(isa<VarDecl>(Property));
  124. }
  125. FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
  126. const ObjCIvarRefExpr *IvarE)
  127. : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
  128. }
  129. void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
  130. const ObjCPropertyDecl *Prop) {
  131. assert(Msg && Prop);
  132. WeakUseVector &Uses =
  133. WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
  134. Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
  135. }
  136. void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
  137. E = E->IgnoreParenCasts();
  138. if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
  139. markSafeWeakUse(POE->getSyntacticForm());
  140. return;
  141. }
  142. if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
  143. markSafeWeakUse(Cond->getTrueExpr());
  144. markSafeWeakUse(Cond->getFalseExpr());
  145. return;
  146. }
  147. if (const BinaryConditionalOperator *Cond =
  148. dyn_cast<BinaryConditionalOperator>(E)) {
  149. markSafeWeakUse(Cond->getCommon());
  150. markSafeWeakUse(Cond->getFalseExpr());
  151. return;
  152. }
  153. // Has this weak object been seen before?
  154. FunctionScopeInfo::WeakObjectUseMap::iterator Uses;
  155. if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
  156. if (!RefExpr->isObjectReceiver())
  157. return;
  158. if (isa<OpaqueValueExpr>(RefExpr->getBase()))
  159. Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
  160. else {
  161. markSafeWeakUse(RefExpr->getBase());
  162. return;
  163. }
  164. }
  165. else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
  166. Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
  167. else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
  168. Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
  169. else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
  170. Uses = WeakObjectUses.end();
  171. if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
  172. if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
  173. Uses =
  174. WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
  175. Prop));
  176. }
  177. }
  178. }
  179. else
  180. return;
  181. if (Uses == WeakObjectUses.end())
  182. return;
  183. // Has there been a read from the object using this Expr?
  184. FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
  185. std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true));
  186. if (ThisUse == Uses->second.rend())
  187. return;
  188. ThisUse->markSafe();
  189. }
  190. void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD,
  191. Expr *&E) const {
  192. assert(Idx < getNumPotentialVariableCaptures() &&
  193. "Index of potential capture must be within 0 to less than the "
  194. "number of captures!");
  195. E = PotentiallyCapturingExprs[Idx];
  196. if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
  197. VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
  198. else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
  199. VD = dyn_cast<VarDecl>(ME->getMemberDecl());
  200. else
  201. llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
  202. "potential captures");
  203. assert(VD);
  204. }
  205. FunctionScopeInfo::~FunctionScopeInfo() { }
  206. BlockScopeInfo::~BlockScopeInfo() { }
  207. LambdaScopeInfo::~LambdaScopeInfo() { }
  208. CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }