clang  3.9.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;
32  ObjCShouldCallSuper = false;
33  ObjCIsDesignatedInit = false;
35  ObjCIsSecondaryInit = false;
40 
41  SwitchStack.clear();
42  Returns.clear();
43  CoroutinePromise = nullptr;
44  CoroutineStmts.clear();
45  ErrorTrap.reset();
47  WeakObjectUses.clear();
48  ModifiedNonNullParams.clear();
49 }
50 
51 static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
52  if (PropE->isExplicitProperty())
53  return PropE->getExplicitProperty();
54 
55  return PropE->getImplicitPropertyGetter();
56 }
57 
58 FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
59 FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
60  E = E->IgnoreParenCasts();
61 
62  const NamedDecl *D = nullptr;
63  bool IsExact = false;
64 
65  switch (E->getStmtClass()) {
66  case Stmt::DeclRefExprClass:
67  D = cast<DeclRefExpr>(E)->getDecl();
68  IsExact = isa<VarDecl>(D);
69  break;
70  case Stmt::MemberExprClass: {
71  const MemberExpr *ME = cast<MemberExpr>(E);
72  D = ME->getMemberDecl();
73  IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
74  break;
75  }
76  case Stmt::ObjCIvarRefExprClass: {
77  const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
78  D = IE->getDecl();
79  IsExact = IE->getBase()->isObjCSelfExpr();
80  break;
81  }
82  case Stmt::PseudoObjectExprClass: {
83  const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
84  const ObjCPropertyRefExpr *BaseProp =
85  dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
86  if (BaseProp) {
87  D = getBestPropertyDecl(BaseProp);
88 
89  if (BaseProp->isObjectReceiver()) {
90  const Expr *DoubleBase = BaseProp->getBase();
91  if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
92  DoubleBase = OVE->getSourceExpr();
93 
94  IsExact = DoubleBase->isObjCSelfExpr();
95  }
96  }
97  break;
98  }
99  default:
100  break;
101  }
102 
103  return BaseInfoTy(D, IsExact);
104 }
105 
107  RecordDecl *RD = nullptr;
108  if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
109  RD = LSI->Lambda;
110  else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
111  RD = CRSI->TheRecordDecl;
112 
113  if (RD)
114  for (auto *FD : RD->fields()) {
115  if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
116  return true;
117  }
118  return false;
119 }
120 
121 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
122  const ObjCPropertyRefExpr *PropE)
123  : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
124 
125  if (PropE->isObjectReceiver()) {
126  const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
127  const Expr *E = OVE->getSourceExpr();
128  Base = getBaseInfo(E);
129  } else if (PropE->isClassReceiver()) {
130  Base.setPointer(PropE->getClassReceiver());
131  } else {
132  assert(PropE->isSuperReceiver());
133  }
134 }
135 
136 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
137  const ObjCPropertyDecl *Prop)
138  : Base(nullptr, true), Property(Prop) {
139  if (BaseE)
140  Base = getBaseInfo(BaseE);
141  // else, this is a message accessing a property on super.
142 }
143 
144 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
145  const DeclRefExpr *DRE)
146  : Base(nullptr, true), Property(DRE->getDecl()) {
147  assert(isa<VarDecl>(Property));
148 }
149 
150 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
151  const ObjCIvarRefExpr *IvarE)
152  : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
153 }
154 
156  const ObjCPropertyDecl *Prop) {
157  assert(Msg && Prop);
158  WeakUseVector &Uses =
159  WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
160  Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
161 }
162 
164  E = E->IgnoreParenCasts();
165 
166  if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
168  return;
169  }
170 
171  if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
172  markSafeWeakUse(Cond->getTrueExpr());
173  markSafeWeakUse(Cond->getFalseExpr());
174  return;
175  }
176 
177  if (const BinaryConditionalOperator *Cond =
178  dyn_cast<BinaryConditionalOperator>(E)) {
179  markSafeWeakUse(Cond->getCommon());
180  markSafeWeakUse(Cond->getFalseExpr());
181  return;
182  }
183 
184  // Has this weak object been seen before?
186  if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
187  if (!RefExpr->isObjectReceiver())
188  return;
189  if (isa<OpaqueValueExpr>(RefExpr->getBase()))
190  Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
191  else {
192  markSafeWeakUse(RefExpr->getBase());
193  return;
194  }
195  }
196  else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
197  Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
198  else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
199  Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
200  else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
201  Uses = WeakObjectUses.end();
202  if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
203  if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
204  Uses =
205  WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
206  Prop));
207  }
208  }
209  }
210  else
211  return;
212 
213  if (Uses == WeakObjectUses.end())
214  return;
215 
216  // Has there been a read from the object using this Expr?
217  FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
218  llvm::find(llvm::reverse(Uses->second), WeakUseTy(E, true));
219  if (ThisUse == Uses->second.rend())
220  return;
221 
222  ThisUse->markSafe();
223 }
224 
226  Expr *&E) const {
227  assert(Idx < getNumPotentialVariableCaptures() &&
228  "Index of potential capture must be within 0 to less than the "
229  "number of captures!");
230  E = PotentiallyCapturingExprs[Idx];
231  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
232  VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
233  else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
234  VD = dyn_cast<VarDecl>(ME->getMemberDecl());
235  else
236  llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
237  "potential captures");
238  assert(VD);
239 }
240 
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:2411
const Expr * getBase() const
Definition: ExprObjC.h:509
Expr * getSyntacticForm()
Return the syntactic form of this expression, i.e.
Definition: Expr.h:4723
static const NamedDecl * getBestPropertyDecl(const ObjCPropertyRefExpr *PropE)
Definition: ScopeInfo.cpp:51
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:768
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:148
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:113
static ObjCPropertyDecl * findPropertyDecl(const DeclContext *DC, const IdentifierInfo *propertyID, ObjCPropertyQueryKind queryKind)
Lookup a property by name in the specified DeclContext.
Definition: DeclObjC.cpp:154
Defines the clang::Expr interface and subclasses for C++ expressions.
bool HasDroppedStmt
Whether a statement was dropped because it was invalid.
Definition: ScopeInfo.h:106
RecordDecl - Represents a struct/union/class.
Definition: Decl.h:3253
ObjCInterfaceDecl * getClassReceiver() const
Definition: ExprObjC.h:696
SourceLocation FirstSEHTryLoc
First SEH '__try' statement in the current function.
Definition: ScopeInfo.h:141
DiagnosticErrorTrap ErrorTrap
Used to determine if errors occurred in this function or block.
Definition: ScopeInfo.h:144
void getPotentialVariableCapture(unsigned Idx, VarDecl *&VD, Expr *&E) const
Definition: ScopeInfo.cpp:225
bool isSuperReceiver() const
Definition: ExprObjC.h:700
field_range fields() const
Definition: Decl.h:3382
Expr * IgnoreParenCasts() LLVM_READONLY
IgnoreParenCasts - Ignore parentheses and casts.
Definition: Expr.cpp:2326
const Expr * getBase() const
Definition: ExprObjC.h:682
SourceLocation FirstCXXTryLoc
First C++ 'try' statement in the current function.
Definition: ScopeInfo.h:138
ObjCIvarDecl * getDecl()
Definition: ExprObjC.h:505
ConditionalOperator - The ?: ternary operator.
Definition: Expr.h:3170
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:153
void recordUseOfWeak(const ExprT *E, bool IsRead=true)
Record that a weak object was accessed.
Definition: ScopeInfo.h:870
bool isObjCSelfExpr() const
Check if this expression is the ObjC 'self' implicit parameter.
Definition: Expr.cpp:3271
Expr - This represents one expression.
Definition: Expr.h:105
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:97
bool isVLATypeCaptured(const VariableArrayType *VAT) const
Determine whether the given variable-array type has been captured.
Definition: ScopeInfo.cpp:106
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:174
VarDecl * CoroutinePromise
The promise object for this coroutine, if any.
Definition: ScopeInfo.h:156
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class...
Definition: Expr.h:848
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:4679
Encodes a location in the source.
const TemplateArgument * iterator
Definition: Type.h:4233
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Definition: Expr.h:902
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:161
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:124
void reset()
Set to initial state of "no errors occurred".
Definition: Diagnostic.h:851
Represents one property declaration in an Objective-C interface.
Definition: DeclObjC.h:699
bool ObjCIsDesignatedInit
True when this is a method marked as a designated initializer.
Definition: ScopeInfo.h:120
bool ObjCShouldCallSuper
A flag that is set when parsing a method that must call super's implementation, such as -dealloc...
Definition: ScopeInfo.h:117
bool isObjectReceiver() const
Definition: ExprObjC.h:699
bool HasOMPDeclareReductionCombiner
True if current scope is for OpenMP declare reduction combiner.
Definition: ScopeInfo.h:109
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:129
bool isClassReceiver() const
Definition: ExprObjC.h:701
bool HasIndirectGoto
Whether this function contains any indirect gotos.
Definition: ScopeInfo.h:103
Represents a simple identification of a weak object.
Definition: ScopeInfo.h:199
bool ObjCWarnForNoInitDelegation
This starts true for a secondary initializer method and will be set to false if there is an invocatio...
Definition: ScopeInfo.h:132
detail::InMemoryDirectory::const_iterator E
Expr * IgnoreParenImpCasts() LLVM_READONLY
IgnoreParenImpCasts - Ignore parentheses and implicit casts.
Definition: Expr.cpp:2413
SourceLocation FirstReturnLoc
First 'return' statement in the current function.
Definition: ScopeInfo.h:135
void markSafeWeakUse(const Expr *E)
Record that a given expression is a "safe" access of a weak object (e.g.
Definition: ScopeInfo.cpp:163
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:2405
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:2315
SmallVector< PossiblyUnreachableDiag, 4 > PossiblyUnreachableDiags
A list of PartialDiagnostics created but delayed within the current function scope.
Definition: ScopeInfo.h:170
BinaryConditionalOperator - The GNU extension to the conditional operator which allows the middle ope...
Definition: Expr.h:3240
Represents a single use of a weak object.
Definition: ScopeInfo.h:281
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:932
#define true
Definition: stdbool.h:32
NamedDecl - This represents a decl with a name.
Definition: Decl.h:213
Represents a C array with a specified size that is not an integer-constant-expression.
Definition: Type.h:2607
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:100