clang  3.7.0
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 "llvm/ADT/DenseMap.h"
19 
20 using namespace clang;
21 
22 typedef llvm::DenseMap<Stmt*, Stmt*> MapTy;
23 
27 };
28 
29 static void BuildParentMap(MapTy& M, Stmt* S,
31 
32  switch (S->getStmtClass()) {
33  case Stmt::PseudoObjectExprClass: {
34  assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
35  PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S);
36 
37  // If we are rebuilding the map, clear out any existing state.
38  if (M[POE->getSyntacticForm()])
39  for (Stmt *SubStmt : S->children())
40  M[SubStmt] = nullptr;
41 
42  M[POE->getSyntacticForm()] = S;
44 
46  E = POE->semantics_end();
47  I != E; ++I) {
48  M[*I] = S;
49  BuildParentMap(M, *I, OV_Opaque);
50  }
51  break;
52  }
53  case Stmt::BinaryConditionalOperatorClass: {
54  assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
55  BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S);
56 
57  M[BCO->getCommon()] = S;
59 
60  M[BCO->getCond()] = S;
61  BuildParentMap(M, BCO->getCond(), OV_Opaque);
62 
63  M[BCO->getTrueExpr()] = S;
65 
66  M[BCO->getFalseExpr()] = S;
68 
69  break;
70  }
71  case Stmt::OpaqueValueExprClass: {
72  // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs
73  // share a single source expression, but in the AST a single
74  // OpaqueValueExpr is shared among multiple parent expressions.
75  // The right thing to do is to give the OpaqueValueExpr its syntactic
76  // parent, then not reassign that when traversing the semantic expressions.
77  OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S);
78  if (OVMode == OV_Transparent || !M[OVE->getSourceExpr()]) {
79  M[OVE->getSourceExpr()] = S;
81  }
82  break;
83  }
84  default:
85  for (Stmt *SubStmt : S->children()) {
86  if (SubStmt) {
87  M[SubStmt] = S;
88  BuildParentMap(M, SubStmt, OVMode);
89  }
90  }
91  break;
92  }
93 }
94 
95 ParentMap::ParentMap(Stmt *S) : Impl(nullptr) {
96  if (S) {
97  MapTy *M = new MapTy();
98  BuildParentMap(*M, S);
99  Impl = M;
100  }
101 }
102 
104  delete (MapTy*) Impl;
105 }
106 
108  if (S) {
109  BuildParentMap(*(MapTy*) Impl, S);
110  }
111 }
112 
113 void ParentMap::setParent(const Stmt *S, const Stmt *Parent) {
114  assert(S);
115  assert(Parent);
116  MapTy *M = reinterpret_cast<MapTy *>(Impl);
117  M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent)));
118 }
119 
121  MapTy* M = (MapTy*) Impl;
122  MapTy::iterator I = M->find(S);
123  return I == M->end() ? nullptr : I->second;
124 }
125 
127  do { S = getParent(S); } while (S && isa<ParenExpr>(S));
128  return S;
129 }
130 
132  do {
133  S = getParent(S);
134  }
135  while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
136 
137  return S;
138 }
139 
141  do {
142  S = getParent(S);
143  } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S);
144 
145  return S;
146 }
147 
149  Stmt *Paren = nullptr;
150  while (isa<ParenExpr>(S)) {
151  Paren = S;
152  S = getParent(S);
153  };
154  return Paren;
155 }
156 
158  Stmt *P = getParent(E);
159  Stmt *DirectChild = E;
160 
161  // Ignore parents that don't guarantee consumption.
162  while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P) ||
163  isa<ExprWithCleanups>(P))) {
164  DirectChild = P;
165  P = getParent(P);
166  }
167 
168  if (!P)
169  return false;
170 
171  switch (P->getStmtClass()) {
172  default:
173  return isa<Expr>(P);
174  case Stmt::DeclStmtClass:
175  return true;
176  case Stmt::BinaryOperatorClass: {
177  BinaryOperator *BE = cast<BinaryOperator>(P);
178  // If it is a comma, only the right side is consumed.
179  // If it isn't a comma, both sides are consumed.
180  return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
181  }
182  case Stmt::ForStmtClass:
183  return DirectChild == cast<ForStmt>(P)->getCond();
184  case Stmt::WhileStmtClass:
185  return DirectChild == cast<WhileStmt>(P)->getCond();
186  case Stmt::DoStmtClass:
187  return DirectChild == cast<DoStmt>(P)->getCond();
188  case Stmt::IfStmtClass:
189  return DirectChild == cast<IfStmt>(P)->getCond();
190  case Stmt::IndirectGotoStmtClass:
191  return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
192  case Stmt::SwitchStmtClass:
193  return DirectChild == cast<SwitchStmt>(P)->getCond();
194  case Stmt::ReturnStmtClass:
195  return true;
196  }
197 }
198 
Expr * getSyntacticForm()
Definition: Expr.h:4756
Expr *const * semantics_iterator
Definition: Expr.h:4778
llvm::DenseMap< Stmt *, Stmt * > MapTy
Definition: ParentMap.cpp:22
Expr * getCond() const
getCond - Return the condition expression; this is defined in terms of the opaque value...
Definition: Expr.h:3305
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. All children of S incl...
Definition: ParentMap.cpp:107
void setParent(const Stmt *S, const Stmt *Parent)
Definition: ParentMap.cpp:113
semantics_iterator semantics_end()
Definition: Expr.h:4786
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:2918
AnnotatingParser & P
static void BuildParentMap(MapTy &M, Stmt *S, OpaqueValueMode OVMode=OV_Transparent)
Definition: ParentMap.cpp:29
bool isConsumedExpr(Expr *E) const
Definition: ParentMap.cpp:157
Stmt * getParent(Stmt *) const
Definition: ParentMap.cpp:120
Expr * getSourceExpr() const
Definition: Expr.h:869
Stmt * getParentIgnoreParens(Stmt *) const
Definition: ParentMap.cpp:126
Stmt * getOuterParenParent(Stmt *) const
Definition: ParentMap.cpp:148
OpaqueValueMode
Definition: ParentMap.cpp:24
Expr * getCommon() const
getCommon - Return the common expression, written to the left of the condition. The opaque value will...
Definition: Expr.h:3298
ParentMap(Stmt *ASTRoot)
Definition: ParentMap.cpp:95
semantics_iterator semantics_begin()
Definition: Expr.h:4780
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression which will be evaluated if the condnition evaluates to false;...
Definition: Expr.h:3317
Opcode getOpcode() const
Definition: Expr.h:2961
Stmt * getParentIgnoreParenImpCasts(Stmt *) const
Definition: ParentMap.cpp:140
Stmt * getParentIgnoreParenCasts(Stmt *) const
Definition: ParentMap.cpp:131
Expr * getRHS() const
Definition: Expr.h:2966
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression which will be evaluated if the condition evaluates to true; th...
Definition: Expr.h:3310