clang  8.0.0svn
ParentMap.cpp
Go to the documentation of this file.
1 //===--- ParentMap.cpp - Mappings from Stmts to their Parents ---*- C++ -*-===//
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 defines the ParentMap class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/AST/ParentMap.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/ExprCXX.h"
18 #include "clang/AST/StmtObjC.h"
19 #include "llvm/ADT/DenseMap.h"
20 
21 using namespace clang;
22 
23 typedef llvm::DenseMap<Stmt*, Stmt*> MapTy;
24 
28 };
29 
30 static void BuildParentMap(MapTy& M, Stmt* S,
32  if (!S)
33  return;
34 
35  switch (S->getStmtClass()) {
36  case Stmt::PseudoObjectExprClass: {
37  assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
38  PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S);
39 
40  // If we are rebuilding the map, clear out any existing state.
41  if (M[POE->getSyntacticForm()])
42  for (Stmt *SubStmt : S->children())
43  M[SubStmt] = nullptr;
44 
45  M[POE->getSyntacticForm()] = S;
47 
49  E = POE->semantics_end();
50  I != E; ++I) {
51  M[*I] = S;
52  BuildParentMap(M, *I, OV_Opaque);
53  }
54  break;
55  }
56  case Stmt::BinaryConditionalOperatorClass: {
57  assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
58  BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S);
59 
60  M[BCO->getCommon()] = S;
62 
63  M[BCO->getCond()] = S;
64  BuildParentMap(M, BCO->getCond(), OV_Opaque);
65 
66  M[BCO->getTrueExpr()] = S;
68 
69  M[BCO->getFalseExpr()] = S;
71 
72  break;
73  }
74  case Stmt::OpaqueValueExprClass: {
75  // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs
76  // share a single source expression, but in the AST a single
77  // OpaqueValueExpr is shared among multiple parent expressions.
78  // The right thing to do is to give the OpaqueValueExpr its syntactic
79  // parent, then not reassign that when traversing the semantic expressions.
80  OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S);
81  if (OVMode == OV_Transparent || !M[OVE->getSourceExpr()]) {
82  M[OVE->getSourceExpr()] = S;
84  }
85  break;
86  }
87  default:
88  for (Stmt *SubStmt : S->children()) {
89  if (SubStmt) {
90  M[SubStmt] = S;
91  BuildParentMap(M, SubStmt, OVMode);
92  }
93  }
94  break;
95  }
96 }
97 
98 ParentMap::ParentMap(Stmt *S) : Impl(nullptr) {
99  if (S) {
100  MapTy *M = new MapTy();
101  BuildParentMap(*M, S);
102  Impl = M;
103  }
104 }
105 
107  delete (MapTy*) Impl;
108 }
109 
111  if (S) {
112  BuildParentMap(*(MapTy*) Impl, S);
113  }
114 }
115 
116 void ParentMap::setParent(const Stmt *S, const Stmt *Parent) {
117  assert(S);
118  assert(Parent);
119  MapTy *M = reinterpret_cast<MapTy *>(Impl);
120  M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent)));
121 }
122 
124  MapTy* M = (MapTy*) Impl;
125  MapTy::iterator I = M->find(S);
126  return I == M->end() ? nullptr : I->second;
127 }
128 
130  do { S = getParent(S); } while (S && isa<ParenExpr>(S));
131  return S;
132 }
133 
135  do {
136  S = getParent(S);
137  }
138  while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
139 
140  return S;
141 }
142 
144  do {
145  S = getParent(S);
146  } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S);
147 
148  return S;
149 }
150 
152  Stmt *Paren = nullptr;
153  while (isa<ParenExpr>(S)) {
154  Paren = S;
155  S = getParent(S);
156  };
157  return Paren;
158 }
159 
161  Stmt *P = getParent(E);
162  Stmt *DirectChild = E;
163 
164  // Ignore parents that don't guarantee consumption.
165  while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P) ||
166  isa<FullExpr>(P))) {
167  DirectChild = P;
168  P = getParent(P);
169  }
170 
171  if (!P)
172  return false;
173 
174  switch (P->getStmtClass()) {
175  default:
176  return isa<Expr>(P);
177  case Stmt::DeclStmtClass:
178  return true;
179  case Stmt::BinaryOperatorClass: {
180  BinaryOperator *BE = cast<BinaryOperator>(P);
181  // If it is a comma, only the right side is consumed.
182  // If it isn't a comma, both sides are consumed.
183  return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
184  }
185  case Stmt::ForStmtClass:
186  return DirectChild == cast<ForStmt>(P)->getCond();
187  case Stmt::WhileStmtClass:
188  return DirectChild == cast<WhileStmt>(P)->getCond();
189  case Stmt::DoStmtClass:
190  return DirectChild == cast<DoStmt>(P)->getCond();
191  case Stmt::IfStmtClass:
192  return DirectChild == cast<IfStmt>(P)->getCond();
193  case Stmt::IndirectGotoStmtClass:
194  return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
195  case Stmt::SwitchStmtClass:
196  return DirectChild == cast<SwitchStmt>(P)->getCond();
197  case Stmt::ObjCForCollectionStmtClass:
198  return DirectChild == cast<ObjCForCollectionStmt>(P)->getCollection();
199  case Stmt::ReturnStmtClass:
200  return true;
201  }
202 }
203 
Expr * getSyntacticForm()
Return the syntactic form of this expression, i.e.
Definition: Expr.h:5262
Expr *const * semantics_iterator
Definition: Expr.h:5284
Stmt - This represents one statement.
Definition: Stmt.h:66
Opcode getOpcode() const
Definition: Expr.h:3254
StringRef P
Stmt * getParent(Stmt *) const
Definition: ParentMap.cpp:123
llvm::DenseMap< Stmt *, Stmt * > MapTy
Definition: ParentMap.cpp:23
bool isConsumedExpr(Expr *E) const
Definition: ParentMap.cpp:160
Defines the Objective-C statement AST node classes.
Defines the clang::Expr interface and subclasses for C++ expressions.
void addStmt(Stmt *S)
Adds and/or updates the parent/child-relations of the complete stmt tree of S.
Definition: ParentMap.cpp:110
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Definition: Expr.h:998
void setParent(const Stmt *S, const Stmt *Parent)
Manually sets the parent of S to Parent.
Definition: ParentMap.cpp:116
child_range children()
Definition: Stmt.cpp:229
semantics_iterator semantics_end()
Definition: Expr.h:5292
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3219
NodeId Parent
Definition: ASTDiff.cpp:192
This represents one expression.
Definition: Expr.h:106
static void BuildParentMap(MapTy &M, Stmt *S, OpaqueValueMode OVMode=OV_Transparent)
Definition: ParentMap.cpp:30
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression which will be evaluated if the condition evaluates to true; th...
Definition: Expr.h:3639
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class...
Definition: Expr.h:939
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:5218
Stmt * getParentIgnoreParens(Stmt *) const
Definition: ParentMap.cpp:129
OpaqueValueMode
Definition: ParentMap.cpp:25
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression which will be evaluated if the condnition evaluates to false;...
Definition: Expr.h:3646
Dataflow Directional Tag Classes.
ParentMap(Stmt *ASTRoot)
Definition: ParentMap.cpp:98
StmtClass getStmtClass() const
Definition: Stmt.h:763
semantics_iterator semantics_begin()
Definition: Expr.h:5286
Stmt * getParentIgnoreParenImpCasts(Stmt *) const
Definition: ParentMap.cpp:143
Expr * getCond() const
getCond - Return the condition expression; this is defined in terms of the opaque value...
Definition: Expr.h:3634
BinaryConditionalOperator - The GNU extension to the conditional operator which allows the middle ope...
Definition: Expr.h:3587
Stmt * getParentIgnoreParenCasts(Stmt *) const
Definition: ParentMap.cpp:134
Expr * getRHS() const
Definition: Expr.h:3261
Expr * getCommon() const
getCommon - Return the common expression, written to the left of the condition.
Definition: Expr.h:3627
Stmt * getOuterParenParent(Stmt *) const
Definition: ParentMap.cpp:151