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 assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
37 PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S);
39 // If we are rebuilding the map, clear out any existing state.
40 if (M[POE->getSyntacticForm()])
41 for (Stmt *SubStmt : S->children())
44 M[POE->getSyntacticForm()] = S;
45 BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent);
47 for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(),
48 E = POE->semantics_end();
51 BuildParentMap(M, *I, OV_Opaque);
55 case Stmt::BinaryConditionalOperatorClass: {
56 assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
57 BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S);
59 M[BCO->getCommon()] = S;
60 BuildParentMap(M, BCO->getCommon(), OV_Transparent);
62 M[BCO->getCond()] = S;
63 BuildParentMap(M, BCO->getCond(), OV_Opaque);
65 M[BCO->getTrueExpr()] = S;
66 BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque);
68 M[BCO->getFalseExpr()] = S;
69 BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent);
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;
82 BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent);
86 case Stmt::CapturedStmtClass:
87 for (Stmt *SubStmt : S->children()) {
90 BuildParentMap(M, SubStmt, OVMode);
93 if (Stmt *SubStmt = cast<CapturedStmt>(S)->getCapturedStmt()) {
95 BuildParentMap(M, SubStmt, OVMode);
99 for (Stmt *SubStmt : S->children()) {
102 BuildParentMap(M, SubStmt, OVMode);
109 ParentMap::ParentMap(Stmt *S) : Impl(nullptr) {
111 MapTy *M = new MapTy();
112 BuildParentMap(*M, S);
117 ParentMap::~ParentMap() {
118 delete (MapTy*) Impl;
121 void ParentMap::addStmt(Stmt* S) {
123 BuildParentMap(*(MapTy*) Impl, S);
127 void ParentMap::setParent(const Stmt *S, const Stmt *Parent) {
130 MapTy *M = reinterpret_cast<MapTy *>(Impl);
131 M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent)));
134 Stmt* ParentMap::getParent(Stmt* S) const {
135 MapTy* M = (MapTy*) Impl;
136 MapTy::iterator I = M->find(S);
137 return I == M->end() ? nullptr : I->second;
140 Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
141 do { S = getParent(S); } while (S && isa<ParenExpr>(S));
145 Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
149 while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
154 Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const {
157 } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S);
162 Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
163 Stmt *Paren = nullptr;
164 while (isa<ParenExpr>(S)) {
171 bool ParentMap::isConsumedExpr(Expr* E) const {
172 Stmt *P = getParent(E);
173 Stmt *DirectChild = E;
175 // Ignore parents that don't guarantee consumption.
176 while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P) ||
185 switch (P->getStmtClass()) {
188 case Stmt::DeclStmtClass:
190 case Stmt::BinaryOperatorClass: {
191 BinaryOperator *BE = cast<BinaryOperator>(P);
192 // If it is a comma, only the right side is consumed.
193 // If it isn't a comma, both sides are consumed.
194 return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
196 case Stmt::ForStmtClass:
197 return DirectChild == cast<ForStmt>(P)->getCond();
198 case Stmt::WhileStmtClass:
199 return DirectChild == cast<WhileStmt>(P)->getCond();
200 case Stmt::DoStmtClass:
201 return DirectChild == cast<DoStmt>(P)->getCond();
202 case Stmt::IfStmtClass:
203 return DirectChild == cast<IfStmt>(P)->getCond();
204 case Stmt::IndirectGotoStmtClass:
205 return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
206 case Stmt::SwitchStmtClass:
207 return DirectChild == cast<SwitchStmt>(P)->getCond();
208 case Stmt::ObjCForCollectionStmtClass:
209 return DirectChild == cast<ObjCForCollectionStmt>(P)->getCollection();
210 case Stmt::ReturnStmtClass: