clang  3.8.0
ScopeInfo.cpp
Go to the documentation of this file.
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"
17 #include "clang/AST/DeclCXX.h"
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 
28  HasBranchIntoScope = false;
29  HasIndirectGoto = false;
30  HasDroppedStmt = false;
31  ObjCShouldCallSuper = false;
32  ObjCIsDesignatedInit = false;
34  ObjCIsSecondaryInit = false;
39 
40  SwitchStack.clear();
41  Returns.clear();
42  CoroutinePromise = nullptr;
43  CoroutineStmts.clear();
44  ErrorTrap.reset();
46  WeakObjectUses.clear();
47  ModifiedNonNullParams.clear();
48 }
49 
50 static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
51  if (PropE->isExplicitProperty())
52  return PropE->getExplicitProperty();
53 
54  return PropE->getImplicitPropertyGetter();
55 }
56 
57 FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
58 FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
59  E = E->IgnoreParenCasts();
60 
61  const NamedDecl *D = nullptr;
62  bool IsExact = false;
63 
64  switch (E->getStmtClass()) {
65  case Stmt::DeclRefExprClass:
66  D = cast<DeclRefExpr>(E)->getDecl();
67  IsExact = isa<VarDecl>(D);
68  break;
69  case Stmt::MemberExprClass: {
70  const MemberExpr *ME = cast<MemberExpr>(E);
71  D = ME->getMemberDecl();
72  IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
73  break;
74  }
75  case Stmt::ObjCIvarRefExprClass: {
76  const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
77  D = IE->getDecl();
78  IsExact = IE->getBase()->isObjCSelfExpr();
79  break;
80  }
81  case Stmt::PseudoObjectExprClass: {
82  const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
83  const ObjCPropertyRefExpr *BaseProp =
84  dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
85  if (BaseProp) {
86  D = getBestPropertyDecl(BaseProp);
87 
88  const Expr *DoubleBase = BaseProp->getBase();
89  if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
90  DoubleBase = OVE->getSourceExpr();
91 
92  IsExact = DoubleBase->isObjCSelfExpr();
93  }
94  break;
95  }
96  default:
97  break;
98  }
99 
100  return BaseInfoTy(D, IsExact);
101 }
102 
104  RecordDecl *RD = nullptr;
105  if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
106  RD = LSI->Lambda;
107  else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
108  RD = CRSI->TheRecordDecl;
109 
110  if (RD)
111  for (auto *FD : RD->fields()) {
112  if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
113  return true;
114  }
115  return false;
116 }
117 
118 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
119  const ObjCPropertyRefExpr *PropE)
120  : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
121 
122  if (PropE->isObjectReceiver()) {
123  const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
124  const Expr *E = OVE->getSourceExpr();
125  Base = getBaseInfo(E);
126  } else if (PropE->isClassReceiver()) {
127  Base.setPointer(PropE->getClassReceiver());
128  } else {
129  assert(PropE->isSuperReceiver());
130  }
131 }
132 
133 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
134  const ObjCPropertyDecl *Prop)
135  : Base(nullptr, true), Property(Prop) {
136  if (BaseE)
137  Base = getBaseInfo(BaseE);
138  // else, this is a message accessing a property on super.
139 }
140 
141 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
142  const DeclRefExpr *DRE)
143  : Base(nullptr, true), Property(DRE->getDecl()) {
144  assert(isa<VarDecl>(Property));
145 }
146 
147 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
148  const ObjCIvarRefExpr *IvarE)
149  : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
150 }
151 
153  const ObjCPropertyDecl *Prop) {
154  assert(Msg && Prop);
155  WeakUseVector &Uses =
156  WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
157  Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
158 }
159 
161  E = E->IgnoreParenCasts();
162 
163  if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
165  return;
166  }
167 
168  if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
169  markSafeWeakUse(Cond->getTrueExpr());
170  markSafeWeakUse(Cond->getFalseExpr());
171  return;
172  }
173 
174  if (const BinaryConditionalOperator *Cond =
175  dyn_cast<BinaryConditionalOperator>(E)) {
176  markSafeWeakUse(Cond->getCommon());
177  markSafeWeakUse(Cond->getFalseExpr());
178  return;
179  }
180 
181  // Has this weak object been seen before?
183  if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
184  if (!RefExpr->isObjectReceiver())
185  return;
186  if (isa<OpaqueValueExpr>(RefExpr->getBase()))
187  Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
188  else {
189  markSafeWeakUse(RefExpr->getBase());
190  return;
191  }
192  }
193  else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
194  Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
195  else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
196  Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
197  else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
198  Uses = WeakObjectUses.end();
199  if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
200  if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
201  Uses =
202  WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
203  Prop));
204  }
205  }
206  }
207  else
208  return;
209 
210  if (Uses == WeakObjectUses.end())
211  return;
212 
213  // Has there been a read from the object using this Expr?
214  FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
215  std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true));
216  if (ThisUse == Uses->second.rend())
217  return;
218 
219  ThisUse->markSafe();
220 }
221 
223  Expr *&E) const {
224  assert(Idx < getNumPotentialVariableCaptures() &&
225  "Index of potential capture must be within 0 to less than the "
226  "number of captures!");
227  E = PotentiallyCapturingExprs[Idx];
228  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
229  VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
230  else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
231  VD = dyn_cast<VarDecl>(ME->getMemberDecl());
232  else
233  llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
234  "potential captures");
235  assert(VD);
236 }
237 
ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC property.
Definition: ExprObjC.h:539
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition: Expr.h:2393
const Expr * getBase() const
Definition: ExprObjC.h:509
Expr * getSyntacticForm()
Return the syntactic form of this expression, i.e.
Definition: Expr.h:4736
static const NamedDecl * getBestPropertyDecl(const ObjCPropertyRefExpr *PropE)
Definition: ScopeInfo.cpp:50
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:699
bool isExplicitProperty() const
Definition: ExprObjC.h:631
SmallVector< SwitchStmt *, 8 > SwitchStack
SwitchStack - This is the current set of active switch statements in the block.
Definition: ScopeInfo.h:141
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:113
Defines the clang::Expr interface and subclasses for C++ expressions.
bool HasDroppedStmt
Whether a statement was dropped because it was invalid.
Definition: ScopeInfo.h:105
RecordDecl - Represents a struct/union/class.
Definition: Decl.h:3166
ObjCInterfaceDecl * getClassReceiver() const
Definition: ExprObjC.h:696
SourceLocation FirstSEHTryLoc
First SEH '__try' statement in the current function.
Definition: ScopeInfo.h:134
DiagnosticErrorTrap ErrorTrap
Used to determine if errors occurred in this function or block.
Definition: ScopeInfo.h:137
void getPotentialVariableCapture(unsigned Idx, VarDecl *&VD, Expr *&E) const
Definition: ScopeInfo.cpp:222
bool isSuperReceiver() const
Definition: ExprObjC.h:700
field_range fields() const
Definition: Decl.h:3295
Expr * IgnoreParenCasts() LLVM_READONLY
IgnoreParenCasts - Ignore parentheses and casts.
Definition: Expr.cpp:2464
const Expr * getBase() const
Definition: ExprObjC.h:682
SourceLocation FirstCXXTryLoc
First C++ 'try' statement in the current function.
Definition: ScopeInfo.h:131
ObjCIvarDecl * getDecl()
Definition: ExprObjC.h:505
static ObjCPropertyDecl * findPropertyDecl(const DeclContext *DC, const IdentifierInfo *propertyID)
Lookup a property by name in the specified DeclContext.
Definition: DeclObjC.cpp:154
ConditionalOperator - The ?: ternary operator.
Definition: Expr.h:3148
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:146
void recordUseOfWeak(const ExprT *E, bool IsRead=true)
Record that a weak object was accessed.
Definition: ScopeInfo.h:841
bool isObjCSelfExpr() const
Check if this expression is the ObjC 'self' implicit parameter.
Definition: Expr.cpp:3396
Expr - This represents one expression.
Definition: Expr.h:104
ObjCMethodDecl * getImplicitPropertyGetter() const
Definition: ExprObjC.h:638
bool HasBranchProtectedScope
Whether this function contains a VLA, @try, try, C++ initializer, or anything else that can't be jump...
Definition: ScopeInfo.h:96
bool isVLATypeCaptured(const VariableArrayType *VAT) const
Determine whether the given variable-array type has been captured.
Definition: ScopeInfo.cpp:103
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:860
llvm::SmallPtrSet< const ParmVarDecl *, 8 > ModifiedNonNullParams
A list of parameters which have the nonnull attribute and are modified in the function.
Definition: ScopeInfo.h:167
VarDecl * CoroutinePromise
The promise object for this coroutine, if any.
Definition: ScopeInfo.h:149
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class...
Definition: Expr.h:840
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:4692
Encodes a location in the source.
const TemplateArgument * iterator
Definition: Type.h:4070
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Definition: Expr.h:892
SmallVector< Stmt *, 4 > CoroutineStmts
The list of coroutine control flow constructs (co_await, co_yield, co_return) that occur within the f...
Definition: ScopeInfo.h:154
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:117
void reset()
Set to initial state of "no errors occurred".
Definition: Diagnostic.h:844
Represents one property declaration in an Objective-C interface.
Definition: DeclObjC.h:2416
bool ObjCIsDesignatedInit
True when this is a method marked as a designated initializer.
Definition: ScopeInfo.h:113
bool ObjCShouldCallSuper
A flag that is set when parsing a method that must call super's implementation, such as -dealloc...
Definition: ScopeInfo.h:110
bool isObjectReceiver() const
Definition: ExprObjC.h:699
Expr * getInstanceReceiver()
Returns the object expression (receiver) for an instance message, or null for a message that is not a...
Definition: ExprObjC.h:1155
bool ObjCIsSecondaryInit
True when this is an initializer method not marked as a designated initializer within a class that ha...
Definition: ScopeInfo.h:122
bool isClassReceiver() const
Definition: ExprObjC.h:701
bool HasIndirectGoto
Whether this function contains any indirect gotos.
Definition: ScopeInfo.h:102
Represents a simple identification of a weak object.
Definition: ScopeInfo.h:191
bool ObjCWarnForNoInitDelegation
This starts true for a secondary initializer method and will be set to false if there is an invocatio...
Definition: ScopeInfo.h:125
detail::InMemoryDirectory::const_iterator E
Expr * IgnoreParenImpCasts() LLVM_READONLY
IgnoreParenImpCasts - Ignore parentheses and implicit casts.
Definition: Expr.cpp:2551
SourceLocation FirstReturnLoc
First 'return' statement in the current function.
Definition: ScopeInfo.h:128
void markSafeWeakUse(const Expr *E)
Record that a given expression is a "safe" access of a weak object (e.g.
Definition: ScopeInfo.cpp:160
unsigned getNumArgs() const
Return the number of actual arguments in this message, not counting the receiver. ...
Definition: ExprObjC.h:1277
ObjCPropertyDecl * getExplicitProperty() const
Definition: ExprObjC.h:633
ObjCIvarRefExpr - A reference to an ObjC instance variable.
Definition: ExprObjC.h:479
Expr * getBase() const
Definition: Expr.h:2387
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:2297
SmallVector< PossiblyUnreachableDiag, 4 > PossiblyUnreachableDiags
A list of PartialDiagnostics created but delayed within the current function scope.
Definition: ScopeInfo.h:163
BinaryConditionalOperator - The GNU extension to the conditional operator which allows the middle ope...
Definition: Expr.h:3218
Represents a single use of a weak object.
Definition: ScopeInfo.h:273
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:922
#define true
Definition: stdbool.h:32
NamedDecl - This represents a decl with a name.
Definition: Decl.h:145
Represents a C array with a specified size that is not an integer-constant-expression.
Definition: Type.h:2575
void Clear()
Clear out the information in this function scope, making it suitable for reuse.
Definition: ScopeInfo.cpp:26
bool HasBranchIntoScope
Whether this function contains any switches or direct gotos.
Definition: ScopeInfo.h:99