clang  8.0.0svn
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  HasFallthroughStmt = false;
34  ObjCShouldCallSuper = false;
35  ObjCIsDesignatedInit = false;
37  ObjCIsSecondaryInit = false;
42 
43  // Coroutine state
45  CoroutinePromise = nullptr;
48  CoroutineSuspends.first = nullptr;
49  CoroutineSuspends.second = nullptr;
50 
51  SwitchStack.clear();
52  Returns.clear();
53  ErrorTrap.reset();
55  WeakObjectUses.clear();
56  ModifiedNonNullParams.clear();
57 }
58 
59 static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
60  if (PropE->isExplicitProperty())
61  return PropE->getExplicitProperty();
62 
63  return PropE->getImplicitPropertyGetter();
64 }
65 
66 FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
67 FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
68  E = E->IgnoreParenCasts();
69 
70  const NamedDecl *D = nullptr;
71  bool IsExact = false;
72 
73  switch (E->getStmtClass()) {
74  case Stmt::DeclRefExprClass:
75  D = cast<DeclRefExpr>(E)->getDecl();
76  IsExact = isa<VarDecl>(D);
77  break;
78  case Stmt::MemberExprClass: {
79  const MemberExpr *ME = cast<MemberExpr>(E);
80  D = ME->getMemberDecl();
81  IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
82  break;
83  }
84  case Stmt::ObjCIvarRefExprClass: {
85  const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
86  D = IE->getDecl();
87  IsExact = IE->getBase()->isObjCSelfExpr();
88  break;
89  }
90  case Stmt::PseudoObjectExprClass: {
91  const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
92  const ObjCPropertyRefExpr *BaseProp =
93  dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
94  if (BaseProp) {
95  D = getBestPropertyDecl(BaseProp);
96 
97  if (BaseProp->isObjectReceiver()) {
98  const Expr *DoubleBase = BaseProp->getBase();
99  if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
100  DoubleBase = OVE->getSourceExpr();
101 
102  IsExact = DoubleBase->isObjCSelfExpr();
103  }
104  }
105  break;
106  }
107  default:
108  break;
109  }
110 
111  return BaseInfoTy(D, IsExact);
112 }
113 
115  RecordDecl *RD = nullptr;
116  if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
117  RD = LSI->Lambda;
118  else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
119  RD = CRSI->TheRecordDecl;
120 
121  if (RD)
122  for (auto *FD : RD->fields()) {
123  if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
124  return true;
125  }
126  return false;
127 }
128 
129 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
130  const ObjCPropertyRefExpr *PropE)
131  : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
132 
133  if (PropE->isObjectReceiver()) {
134  const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
135  const Expr *E = OVE->getSourceExpr();
136  Base = getBaseInfo(E);
137  } else if (PropE->isClassReceiver()) {
138  Base.setPointer(PropE->getClassReceiver());
139  } else {
140  assert(PropE->isSuperReceiver());
141  }
142 }
143 
144 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
145  const ObjCPropertyDecl *Prop)
146  : Base(nullptr, true), Property(Prop) {
147  if (BaseE)
148  Base = getBaseInfo(BaseE);
149  // else, this is a message accessing a property on super.
150 }
151 
152 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
153  const DeclRefExpr *DRE)
154  : Base(nullptr, true), Property(DRE->getDecl()) {
155  assert(isa<VarDecl>(Property));
156 }
157 
158 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
159  const ObjCIvarRefExpr *IvarE)
160  : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
161 }
162 
164  const ObjCPropertyDecl *Prop) {
165  assert(Msg && Prop);
166  WeakUseVector &Uses =
167  WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
168  Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
169 }
170 
172  E = E->IgnoreParenCasts();
173 
174  if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
175  markSafeWeakUse(POE->getSyntacticForm());
176  return;
177  }
178 
179  if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
180  markSafeWeakUse(Cond->getTrueExpr());
181  markSafeWeakUse(Cond->getFalseExpr());
182  return;
183  }
184 
185  if (const BinaryConditionalOperator *Cond =
186  dyn_cast<BinaryConditionalOperator>(E)) {
187  markSafeWeakUse(Cond->getCommon());
188  markSafeWeakUse(Cond->getFalseExpr());
189  return;
190  }
191 
192  // Has this weak object been seen before?
193  FunctionScopeInfo::WeakObjectUseMap::iterator Uses = WeakObjectUses.end();
194  if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
195  if (!RefExpr->isObjectReceiver())
196  return;
197  if (isa<OpaqueValueExpr>(RefExpr->getBase()))
198  Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
199  else {
200  markSafeWeakUse(RefExpr->getBase());
201  return;
202  }
203  }
204  else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
205  Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
206  else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
207  if (isa<VarDecl>(DRE->getDecl()))
208  Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
209  } else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
210  if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
211  if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
212  Uses =
213  WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
214  Prop));
215  }
216  }
217  }
218  else
219  return;
220 
221  if (Uses == WeakObjectUses.end())
222  return;
223 
224  // Has there been a read from the object using this Expr?
225  FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
226  llvm::find(llvm::reverse(Uses->second), WeakUseTy(E, true));
227  if (ThisUse == Uses->second.rend())
228  return;
229 
230  ThisUse->markSafe();
231 }
232 
234  Expr *&E) const {
235  assert(Idx < getNumPotentialVariableCaptures() &&
236  "Index of potential capture must be within 0 to less than the "
237  "number of captures!");
238  E = PotentiallyCapturingExprs[Idx];
239  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
240  VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
241  else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
242  VD = dyn_cast<VarDecl>(ME->getMemberDecl());
243  else
244  llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
245  "potential captures");
246  assert(VD);
247 }
248 
ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC property.
Definition: ExprObjC.h:577
Expr * getSyntacticForm()
Return the syntactic form of this expression, i.e.
Definition: Expr.h:5140
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition: Expr.h:2563
void getPotentialVariableCapture(unsigned Idx, VarDecl *&VD, Expr *&E) const
Definition: ScopeInfo.cpp:233
bool HasFallthroughStmt
Whether there is a fallthrough statement in this function.
Definition: ScopeInfo.h:126
bool isSuperReceiver() const
Definition: ExprObjC.h:739
bool isVLATypeCaptured(const VariableArrayType *VAT) const
Determine whether the given variable-array type has been captured.
Definition: ScopeInfo.cpp:114
Expr * getBase() const
Definition: Expr.h:2557
static const NamedDecl * getBestPropertyDecl(const ObjCPropertyRefExpr *PropE)
Definition: ScopeInfo.cpp:59
std::pair< Stmt *, Stmt * > CoroutineSuspends
The initial and final coroutine suspend points.
Definition: ScopeInfo.h:199
Represents a variable declaration or definition.
Definition: Decl.h:812
ObjCInterfaceDecl * getClassReceiver() const
Definition: ExprObjC.h:734
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:139
static ObjCPropertyDecl * findPropertyDecl(const DeclContext *DC, const IdentifierInfo *propertyID, ObjCPropertyQueryKind queryKind)
Lookup a property by name in the specified DeclContext.
Definition: DeclObjC.cpp:177
bool NeedsCoroutineSuspends
True only when this function has not already built, or attempted to build, the initial and final coro...
Definition: ScopeInfo.h:156
Defines the clang::Expr interface and subclasses for C++ expressions.
bool HasDroppedStmt
Whether a statement was dropped because it was invalid.
Definition: ScopeInfo.h:120
ObjCPropertyDecl * getExplicitProperty() const
Definition: ExprObjC.h:670
llvm::SmallMapVector< ParmVarDecl *, Stmt *, 4 > CoroutineParameterMoves
A mapping between the coroutine function parameters that were moved to the coroutine frame...
Definition: ScopeInfo.h:196
Represents a struct/union/class.
Definition: Decl.h:3570
bool isExplicitProperty() const
Definition: ExprObjC.h:668
field_range fields() const
Definition: Decl.h:3761
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Definition: Expr.h:933
SourceLocation FirstSEHTryLoc
First SEH &#39;__try&#39; statement in the current function.
Definition: ScopeInfo.h:173
DiagnosticErrorTrap ErrorTrap
Used to determine if errors occurred in this function or block.
Definition: ScopeInfo.h:176
bool isClassReceiver() const
Definition: ExprObjC.h:740
Expr * IgnoreParenCasts() LLVM_READONLY
IgnoreParenCasts - Ignore parentheses and casts.
Definition: Expr.cpp:2544
bool isObjCSelfExpr() const
Check if this expression is the ObjC &#39;self&#39; implicit parameter.
Definition: Expr.cpp:3507
SourceLocation FirstCXXTryLoc
First C++ &#39;try&#39; statement in the current function.
Definition: ScopeInfo.h:170
ObjCIvarDecl * getDecl()
Definition: ExprObjC.h:543
ConditionalOperator - The ?: ternary operator.
Definition: Expr.h:3392
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:189
void recordUseOfWeak(const ExprT *E, bool IsRead=true)
Record that a weak object was accessed.
Definition: ScopeInfo.h:984
This represents one expression.
Definition: Expr.h:105
SmallVector< SwitchInfo, 8 > SwitchStack
SwitchStack - This is the current set of active switch statements in the block.
Definition: ScopeInfo.h:184
bool HasBranchProtectedScope
Whether this function contains a VLA, @try, try, C++ initializer, or anything else that can&#39;t be jump...
Definition: ScopeInfo.h:111
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:904
ObjCMethodDecl * getImplicitPropertyGetter() const
Definition: ExprObjC.h:675
VarDecl * CoroutinePromise
The promise object for this coroutine, if any.
Definition: ScopeInfo.h:192
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class...
Definition: Expr.h:874
unsigned getNumArgs() const
Return the number of actual arguments in this message, not counting the receiver. ...
Definition: ExprObjC.h:1329
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:5096
bool HasPotentialAvailabilityViolations
Whether we make reference to a declaration that could be unavailable.
Definition: ScopeInfo.h:130
Encodes a location in the source.
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:143
void reset()
Set to initial state of "no errors occurred".
Definition: Diagnostic.h:1020
SourceLocation FirstCoroutineStmtLoc
First coroutine statement in the current function.
Definition: ScopeInfo.h:164
Represents one property declaration in an Objective-C interface.
Definition: DeclObjC.h:721
bool ObjCIsDesignatedInit
True when this is a method marked as a designated initializer.
Definition: ScopeInfo.h:138
bool ObjCShouldCallSuper
A flag that is set when parsing a method that must call super&#39;s implementation, such as -dealloc...
Definition: ScopeInfo.h:135
bool HasOMPDeclareReductionCombiner
True if current scope is for OpenMP declare reduction combiner.
Definition: ScopeInfo.h:123
Expr * getInstanceReceiver()
Returns the object expression (receiver) for an instance message, or null for a message that is not a...
Definition: ExprObjC.h:1207
Dataflow Directional Tag Classes.
bool ObjCIsSecondaryInit
True when this is an initializer method not marked as a designated initializer within a class that ha...
Definition: ScopeInfo.h:148
bool HasIndirectGoto
Whether this function contains any indirect gotos.
Definition: ScopeInfo.h:117
StmtClass getStmtClass() const
Definition: Stmt.h:391
Represents a simple identification of a weak object.
Definition: ScopeInfo.h:237
bool ObjCWarnForNoInitDelegation
This starts true for a secondary initializer method and will be set to false if there is an invocatio...
Definition: ScopeInfo.h:152
Expr * IgnoreParenImpCasts() LLVM_READONLY
IgnoreParenImpCasts - Ignore parentheses and implicit casts.
Definition: Expr.cpp:2631
SourceLocation FirstReturnLoc
First &#39;return&#39; statement in the current function.
Definition: ScopeInfo.h:167
void markSafeWeakUse(const Expr *E)
Record that a given expression is a "safe" access of a weak object (e.g.
Definition: ScopeInfo.cpp:171
const Expr * getBase() const
Definition: ExprObjC.h:547
ObjCIvarRefExpr - A reference to an ObjC instance variable.
Definition: ExprObjC.h:513
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:2467
SmallVector< PossiblyUnreachableDiag, 4 > PossiblyUnreachableDiags
A list of PartialDiagnostics created but delayed within the current function scope.
Definition: ScopeInfo.h:208
BinaryConditionalOperator - The GNU extension to the conditional operator which allows the middle ope...
Definition: Expr.h:3465
llvm::SmallPtrSet< const ParmVarDecl *, 8 > ModifiedNonNullParams
A list of parameters which have the nonnull attribute and are modified in the function.
Definition: ScopeInfo.h:212
Represents a single use of a weak object.
Definition: ScopeInfo.h:321
bool isObjectReceiver() const
Definition: ExprObjC.h:738
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:971
#define true
Definition: stdbool.h:32
const Expr * getBase() const
Definition: ExprObjC.h:719
This represents a decl that may have a name.
Definition: Decl.h:248
Represents a C array with a specified size that is not an integer-constant-expression.
Definition: Type.h:2945
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:114