clang  16.0.0git
ParentMap.cpp
Go to the documentation of this file.
1 //===--- ParentMap.cpp - Mappings from Stmts to their Parents ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the ParentMap class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/AST/ParentMap.h"
14 #include "clang/AST/Decl.h"
15 #include "clang/AST/Expr.h"
16 #include "clang/AST/ExprCXX.h"
17 #include "clang/AST/StmtObjC.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  if (!S)
32  return;
33 
34  switch (S->getStmtClass()) {
35  case Stmt::PseudoObjectExprClass: {
36  assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
37  PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S);
38 
39  // If we are rebuilding the map, clear out any existing state.
40  if (M[POE->getSyntacticForm()])
41  for (Stmt *SubStmt : S->children())
42  M[SubStmt] = nullptr;
43 
44  M[POE->getSyntacticForm()] = S;
46 
48  E = POE->semantics_end();
49  I != E; ++I) {
50  M[*I] = S;
51  BuildParentMap(M, *I, OV_Opaque);
52  }
53  break;
54  }
55  case Stmt::BinaryConditionalOperatorClass: {
56  assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
57  BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S);
58 
59  M[BCO->getCommon()] = S;
61 
62  M[BCO->getCond()] = S;
63  BuildParentMap(M, BCO->getCond(), OV_Opaque);
64 
65  M[BCO->getTrueExpr()] = S;
67 
68  M[BCO->getFalseExpr()] = S;
70 
71  break;
72  }
73  case Stmt::OpaqueValueExprClass: {
74  // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs
75  // share a single source expression, but in the AST a single
76  // OpaqueValueExpr is shared among multiple parent expressions.
77  // The right thing to do is to give the OpaqueValueExpr its syntactic
78  // parent, then not reassign that when traversing the semantic expressions.
79  OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S);
80  if (OVMode == OV_Transparent || !M[OVE->getSourceExpr()]) {
81  M[OVE->getSourceExpr()] = S;
83  }
84  break;
85  }
86  case Stmt::CapturedStmtClass:
87  for (Stmt *SubStmt : S->children()) {
88  if (SubStmt) {
89  M[SubStmt] = S;
90  BuildParentMap(M, SubStmt, OVMode);
91  }
92  }
93  if (Stmt *SubStmt = cast<CapturedStmt>(S)->getCapturedStmt()) {
94  M[SubStmt] = S;
95  BuildParentMap(M, SubStmt, OVMode);
96  }
97  break;
98  default:
99  for (Stmt *SubStmt : S->children()) {
100  if (SubStmt) {
101  M[SubStmt] = S;
102  BuildParentMap(M, SubStmt, OVMode);
103  }
104  }
105  break;
106  }
107 }
108 
109 ParentMap::ParentMap(Stmt *S) : Impl(nullptr) {
110  if (S) {
111  MapTy *M = new MapTy();
112  BuildParentMap(*M, S);
113  Impl = M;
114  }
115 }
116 
118  delete (MapTy*) Impl;
119 }
120 
122  if (S) {
123  BuildParentMap(*(MapTy*) Impl, S);
124  }
125 }
126 
127 void ParentMap::setParent(const Stmt *S, const Stmt *Parent) {
128  assert(S);
129  assert(Parent);
130  MapTy *M = reinterpret_cast<MapTy *>(Impl);
131  M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent)));
132 }
133 
135  MapTy* M = (MapTy*) Impl;
136  return M->lookup(S);
137 }
138 
140  do { S = getParent(S); } while (S && isa<ParenExpr>(S));
141  return S;
142 }
143 
145  do {
146  S = getParent(S);
147  }
148  while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
149 
150  return S;
151 }
152 
154  do {
155  S = getParent(S);
156  } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S);
157 
158  return S;
159 }
160 
162  Stmt *Paren = nullptr;
163  while (isa<ParenExpr>(S)) {
164  Paren = S;
165  S = getParent(S);
166  };
167  return Paren;
168 }
169 
171  Stmt *P = getParent(E);
172  Stmt *DirectChild = E;
173 
174  // Ignore parents that don't guarantee consumption.
175  while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P) ||
176  isa<FullExpr>(P))) {
177  DirectChild = P;
178  P = getParent(P);
179  }
180 
181  if (!P)
182  return false;
183 
184  switch (P->getStmtClass()) {
185  default:
186  return isa<Expr>(P);
187  case Stmt::DeclStmtClass:
188  return true;
189  case Stmt::BinaryOperatorClass: {
190  BinaryOperator *BE = cast<BinaryOperator>(P);
191  // If it is a comma, only the right side is consumed.
192  // If it isn't a comma, both sides are consumed.
193  return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
194  }
195  case Stmt::ForStmtClass:
196  return DirectChild == cast<ForStmt>(P)->getCond();
197  case Stmt::WhileStmtClass:
198  return DirectChild == cast<WhileStmt>(P)->getCond();
199  case Stmt::DoStmtClass:
200  return DirectChild == cast<DoStmt>(P)->getCond();
201  case Stmt::IfStmtClass:
202  return DirectChild == cast<IfStmt>(P)->getCond();
203  case Stmt::IndirectGotoStmtClass:
204  return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
205  case Stmt::SwitchStmtClass:
206  return DirectChild == cast<SwitchStmt>(P)->getCond();
207  case Stmt::ObjCForCollectionStmtClass:
208  return DirectChild == cast<ObjCForCollectionStmt>(P)->getCollection();
209  case Stmt::ReturnStmtClass:
210  return true;
211  }
212 }
213 
clang::ParentMap::addStmt
void addStmt(Stmt *S)
Adds and/or updates the parent/child-relations of the complete stmt tree of S.
Definition: ParentMap.cpp:121
OV_Opaque
@ OV_Opaque
Definition: ParentMap.cpp:26
clang::PseudoObjectExpr::semantics_begin
semantics_iterator semantics_begin()
Definition: Expr.h:6164
clang::BinaryConditionalOperator::getCommon
Expr * getCommon() const
getCommon - Return the common expression, written to the left of the condition.
Definition: Expr.h:4238
clang::ParentMap::~ParentMap
~ParentMap()
Definition: ParentMap.cpp:117
clang::BinaryConditionalOperator::getTrueExpr
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression which will be evaluated if the condition evaluates to true; th...
Definition: Expr.h:4250
clang::ParentMap::getParentIgnoreParenCasts
Stmt * getParentIgnoreParenCasts(Stmt *) const
Definition: ParentMap.cpp:144
clang::BinaryConditionalOperator::getCond
Expr * getCond() const
getCond - Return the condition expression; this is defined in terms of the opaque value.
Definition: Expr.h:4245
OV_Transparent
@ OV_Transparent
Definition: ParentMap.cpp:25
clang::PseudoObjectExpr::semantics_end
semantics_iterator semantics_end()
Definition: Expr.h:6170
clang::BinaryOperator::getOpcode
Opcode getOpcode() const
Definition: Expr.h:3847
Paren
@ Paren
Definition: PPMacroExpansion.cpp:636
clang::OpaqueValueExpr
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
Definition: Expr.h:1135
Decl.h
OpaqueValueMode
OpaqueValueMode
Definition: ParentMap.cpp:24
clang::BinaryOperator
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3803
clang::PseudoObjectExpr
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:6096
clang::BinaryConditionalOperator
BinaryConditionalOperator - The GNU extension to the conditional operator which allows the middle ope...
Definition: Expr.h:4203
clang::ParentMap::setParent
void setParent(const Stmt *S, const Stmt *Parent)
Manually sets the parent of S to Parent.
Definition: ParentMap.cpp:127
Expr.h
clang::ParentMap::getParentIgnoreParenImpCasts
Stmt * getParentIgnoreParenImpCasts(Stmt *) const
Definition: ParentMap.cpp:153
clang::OpaqueValueExpr::getSourceExpr
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Definition: Expr.h:1185
MapTy
llvm::DenseMap< Stmt *, Stmt * > MapTy
Definition: ParentMap.cpp:22
BuildParentMap
static void BuildParentMap(MapTy &M, Stmt *S, OpaqueValueMode OVMode=OV_Transparent)
Definition: ParentMap.cpp:29
ExprCXX.h
StmtObjC.h
clang::PseudoObjectExpr::semantics_iterator
Expr *const * semantics_iterator
Definition: Expr.h:6162
clang::ParentMap::getOuterParenParent
Stmt * getOuterParenParent(Stmt *) const
Definition: ParentMap.cpp:161
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::PseudoObjectExpr::getSyntacticForm
Expr * getSyntacticForm()
Return the syntactic form of this expression, i.e.
Definition: Expr.h:6140
clang::ParentMap::getParent
Stmt * getParent(Stmt *) const
Definition: ParentMap.cpp:134
clang::BinaryConditionalOperator::getFalseExpr
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression which will be evaluated if the condnition evaluates to false;...
Definition: Expr.h:4257
ParentMap.h
clang
Definition: CalledOnceCheck.h:17
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:70
clang::BinaryOperator::getRHS
Expr * getRHS() const
Definition: Expr.h:3854
clang::ParentMap::getParentIgnoreParens
Stmt * getParentIgnoreParens(Stmt *) const
Definition: ParentMap.cpp:139
Parent
NodeId Parent
Definition: ASTDiff.cpp:192
clang::ParentMap::isConsumedExpr
bool isConsumedExpr(Expr *E) const
Definition: ParentMap.cpp:170
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::ParentMap::ParentMap
ParentMap(Stmt *ASTRoot)
Definition: ParentMap.cpp:109