1 //===--- ParentMap.cpp - Mappings from Stmts to their Parents ---*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the ParentMap class.
11 //===----------------------------------------------------------------------===//
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"
20 using namespace clang
;
22 typedef llvm::DenseMap
<Stmt
*, Stmt
*> MapTy
;
24 enum OpaqueValueMode
{
29 static void BuildParentMap(MapTy
& M
, Stmt
* S
,
30 OpaqueValueMode OVMode
= OV_Transparent
) {
34 switch (S
->getStmtClass()) {
35 case Stmt::PseudoObjectExprClass
: {
36 PseudoObjectExpr
*POE
= cast
<PseudoObjectExpr
>(S
);
38 if (OVMode
== OV_Opaque
&& M
[POE
->getSyntacticForm()])
41 // If we are rebuilding the map, clear out any existing state.
42 if (M
[POE
->getSyntacticForm()])
43 for (Stmt
*SubStmt
: S
->children())
46 M
[POE
->getSyntacticForm()] = S
;
47 BuildParentMap(M
, POE
->getSyntacticForm(), OV_Transparent
);
49 for (PseudoObjectExpr::semantics_iterator I
= POE
->semantics_begin(),
50 E
= POE
->semantics_end();
53 BuildParentMap(M
, *I
, OV_Opaque
);
57 case Stmt::BinaryConditionalOperatorClass
: {
58 assert(OVMode
== OV_Transparent
&& "Should not appear alongside OVEs");
59 BinaryConditionalOperator
*BCO
= cast
<BinaryConditionalOperator
>(S
);
61 M
[BCO
->getCommon()] = S
;
62 BuildParentMap(M
, BCO
->getCommon(), OV_Transparent
);
64 M
[BCO
->getCond()] = S
;
65 BuildParentMap(M
, BCO
->getCond(), OV_Opaque
);
67 M
[BCO
->getTrueExpr()] = S
;
68 BuildParentMap(M
, BCO
->getTrueExpr(), OV_Opaque
);
70 M
[BCO
->getFalseExpr()] = S
;
71 BuildParentMap(M
, BCO
->getFalseExpr(), OV_Transparent
);
75 case Stmt::OpaqueValueExprClass
: {
76 // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs
77 // share a single source expression, but in the AST a single
78 // OpaqueValueExpr is shared among multiple parent expressions.
79 // The right thing to do is to give the OpaqueValueExpr its syntactic
80 // parent, then not reassign that when traversing the semantic expressions.
81 OpaqueValueExpr
*OVE
= cast
<OpaqueValueExpr
>(S
);
82 if (OVMode
== OV_Transparent
|| !M
[OVE
->getSourceExpr()]) {
83 M
[OVE
->getSourceExpr()] = S
;
84 BuildParentMap(M
, OVE
->getSourceExpr(), OV_Transparent
);
88 case Stmt::CapturedStmtClass
:
89 for (Stmt
*SubStmt
: S
->children()) {
92 BuildParentMap(M
, SubStmt
, OVMode
);
95 if (Stmt
*SubStmt
= cast
<CapturedStmt
>(S
)->getCapturedStmt()) {
97 BuildParentMap(M
, SubStmt
, OVMode
);
101 for (Stmt
*SubStmt
: S
->children()) {
104 BuildParentMap(M
, SubStmt
, OVMode
);
111 ParentMap::ParentMap(Stmt
*S
) : Impl(nullptr) {
113 MapTy
*M
= new MapTy();
114 BuildParentMap(*M
, S
);
119 ParentMap::~ParentMap() {
120 delete (MapTy
*) Impl
;
123 void ParentMap::addStmt(Stmt
* S
) {
125 BuildParentMap(*(MapTy
*) Impl
, S
);
129 void ParentMap::setParent(const Stmt
*S
, const Stmt
*Parent
) {
132 MapTy
*M
= reinterpret_cast<MapTy
*>(Impl
);
133 M
->insert(std::make_pair(const_cast<Stmt
*>(S
), const_cast<Stmt
*>(Parent
)));
136 Stmt
* ParentMap::getParent(Stmt
* S
) const {
137 MapTy
* M
= (MapTy
*) Impl
;
141 Stmt
*ParentMap::getParentIgnoreParens(Stmt
*S
) const {
142 do { S
= getParent(S
); } while (S
&& isa
<ParenExpr
>(S
));
146 Stmt
*ParentMap::getParentIgnoreParenCasts(Stmt
*S
) const {
150 while (S
&& (isa
<ParenExpr
>(S
) || isa
<CastExpr
>(S
)));
155 Stmt
*ParentMap::getParentIgnoreParenImpCasts(Stmt
*S
) const {
158 } while (S
&& isa
<Expr
>(S
) && cast
<Expr
>(S
)->IgnoreParenImpCasts() != S
);
163 Stmt
*ParentMap::getOuterParenParent(Stmt
*S
) const {
164 Stmt
*Paren
= nullptr;
165 while (isa
<ParenExpr
>(S
)) {
172 bool ParentMap::isConsumedExpr(Expr
* E
) const {
173 Stmt
*P
= getParent(E
);
174 Stmt
*DirectChild
= E
;
176 // Ignore parents that don't guarantee consumption.
177 while (P
&& (isa
<ParenExpr
>(P
) || isa
<CastExpr
>(P
) ||
186 switch (P
->getStmtClass()) {
189 case Stmt::DeclStmtClass
:
191 case Stmt::BinaryOperatorClass
: {
192 BinaryOperator
*BE
= cast
<BinaryOperator
>(P
);
193 // If it is a comma, only the right side is consumed.
194 // If it isn't a comma, both sides are consumed.
195 return BE
->getOpcode()!=BO_Comma
||DirectChild
==BE
->getRHS();
197 case Stmt::ForStmtClass
:
198 return DirectChild
== cast
<ForStmt
>(P
)->getCond();
199 case Stmt::WhileStmtClass
:
200 return DirectChild
== cast
<WhileStmt
>(P
)->getCond();
201 case Stmt::DoStmtClass
:
202 return DirectChild
== cast
<DoStmt
>(P
)->getCond();
203 case Stmt::IfStmtClass
:
204 return DirectChild
== cast
<IfStmt
>(P
)->getCond();
205 case Stmt::IndirectGotoStmtClass
:
206 return DirectChild
== cast
<IndirectGotoStmt
>(P
)->getTarget();
207 case Stmt::SwitchStmtClass
:
208 return DirectChild
== cast
<SwitchStmt
>(P
)->getCond();
209 case Stmt::ObjCForCollectionStmtClass
:
210 return DirectChild
== cast
<ObjCForCollectionStmt
>(P
)->getCollection();
211 case Stmt::ReturnStmtClass
: