blob: d8882c9030b255935cc0ec4187ce074ffa593333 [file] [log] [blame]
Ted Kremenek99ecdd12008-06-20 21:40:36 +00001//===--- 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"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000015#include "clang/AST/Decl.h"
Ted Kremenek99ecdd12008-06-20 21:40:36 +000016#include "clang/AST/Expr.h"
Jordan Rose69dd5fc2013-06-06 01:57:24 +000017#include "clang/AST/ExprCXX.h"
Ted Kremenek99ecdd12008-06-20 21:40:36 +000018#include "llvm/ADT/DenseMap.h"
19
20using namespace clang;
21
22typedef llvm::DenseMap<Stmt*, Stmt*> MapTy;
23
Jordan Roseadb53542012-10-06 01:19:36 +000024enum OpaqueValueMode {
25 OV_Transparent,
26 OV_Opaque
27};
28
Jordan Rose433b0f52013-05-18 02:26:50 +000029static void BuildParentMap(MapTy& M, Stmt* S,
Jordan Roseadb53542012-10-06 01:19:36 +000030 OpaqueValueMode OVMode = OV_Transparent) {
Argyrios Kyrtzidis6f10b742016-07-14 20:21:16 +000031 if (!S)
32 return;
Jordan Roseadb53542012-10-06 01:19:36 +000033
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
Jordan Rosec3f1cb32013-05-18 02:27:09 +000039 // If we are rebuilding the map, clear out any existing state.
40 if (M[POE->getSyntacticForm()])
Benjamin Kramer642f1732015-07-02 21:03:14 +000041 for (Stmt *SubStmt : S->children())
42 M[SubStmt] = nullptr;
Jordan Rosec3f1cb32013-05-18 02:27:09 +000043
Jordan Rose433b0f52013-05-18 02:26:50 +000044 M[POE->getSyntacticForm()] = S;
45 BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent);
Jordan Roseadb53542012-10-06 01:19:36 +000046
47 for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(),
48 E = POE->semantics_end();
49 I != E; ++I) {
50 M[*I] = S;
Jordan Rose433b0f52013-05-18 02:26:50 +000051 BuildParentMap(M, *I, OV_Opaque);
Jordan Roseadb53542012-10-06 01:19:36 +000052 }
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;
Jordan Rose433b0f52013-05-18 02:26:50 +000060 BuildParentMap(M, BCO->getCommon(), OV_Transparent);
Jordan Roseadb53542012-10-06 01:19:36 +000061
62 M[BCO->getCond()] = S;
Jordan Rose433b0f52013-05-18 02:26:50 +000063 BuildParentMap(M, BCO->getCond(), OV_Opaque);
Jordan Roseadb53542012-10-06 01:19:36 +000064
65 M[BCO->getTrueExpr()] = S;
Jordan Rose433b0f52013-05-18 02:26:50 +000066 BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque);
Jordan Roseadb53542012-10-06 01:19:36 +000067
68 M[BCO->getFalseExpr()] = S;
Jordan Rose433b0f52013-05-18 02:26:50 +000069 BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent);
Jordan Roseadb53542012-10-06 01:19:36 +000070
71 break;
72 }
Jordan Rosec3f1cb32013-05-18 02:27:09 +000073 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);
Serge Pavlov111200672013-05-18 04:32:15 +000080 if (OVMode == OV_Transparent || !M[OVE->getSourceExpr()]) {
Jordan Roseadb53542012-10-06 01:19:36 +000081 M[OVE->getSourceExpr()] = S;
Jordan Rose433b0f52013-05-18 02:26:50 +000082 BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent);
Jordan Roseadb53542012-10-06 01:19:36 +000083 }
84 break;
Jordan Rosec3f1cb32013-05-18 02:27:09 +000085 }
Jordan Roseadb53542012-10-06 01:19:36 +000086 default:
Benjamin Kramer642f1732015-07-02 21:03:14 +000087 for (Stmt *SubStmt : S->children()) {
88 if (SubStmt) {
89 M[SubStmt] = S;
90 BuildParentMap(M, SubStmt, OVMode);
Jordan Rosee32e1532012-08-06 21:28:11 +000091 }
Ted Kremenek99ecdd12008-06-20 21:40:36 +000092 }
Jordan Roseadb53542012-10-06 01:19:36 +000093 break;
Jordan Rosee32e1532012-08-06 21:28:11 +000094 }
Ted Kremenek99ecdd12008-06-20 21:40:36 +000095}
96
Craig Topper36250ad2014-05-12 05:36:57 +000097ParentMap::ParentMap(Stmt *S) : Impl(nullptr) {
Ted Kremenek99ecdd12008-06-20 21:40:36 +000098 if (S) {
99 MapTy *M = new MapTy();
Jordan Rose433b0f52013-05-18 02:26:50 +0000100 BuildParentMap(*M, S);
Mike Stump11289f42009-09-09 15:08:12 +0000101 Impl = M;
Ted Kremenek99ecdd12008-06-20 21:40:36 +0000102 }
103}
104
105ParentMap::~ParentMap() {
106 delete (MapTy*) Impl;
107}
108
Ted Kremenek43f50692010-11-15 20:54:24 +0000109void ParentMap::addStmt(Stmt* S) {
110 if (S) {
Jordan Rose433b0f52013-05-18 02:26:50 +0000111 BuildParentMap(*(MapTy*) Impl, S);
Ted Kremenek43f50692010-11-15 20:54:24 +0000112 }
113}
114
Jordan Rosecf10ea82013-06-06 21:53:45 +0000115void ParentMap::setParent(const Stmt *S, const Stmt *Parent) {
116 assert(S);
117 assert(Parent);
118 MapTy *M = reinterpret_cast<MapTy *>(Impl);
119 M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent)));
120}
121
Ted Kremenek99ecdd12008-06-20 21:40:36 +0000122Stmt* ParentMap::getParent(Stmt* S) const {
123 MapTy* M = (MapTy*) Impl;
124 MapTy::iterator I = M->find(S);
Craig Topper36250ad2014-05-12 05:36:57 +0000125 return I == M->end() ? nullptr : I->second;
Ted Kremenek99ecdd12008-06-20 21:40:36 +0000126}
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000127
Ted Kremenekd4dacb02009-05-11 19:49:27 +0000128Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
129 do { S = getParent(S); } while (S && isa<ParenExpr>(S));
130 return S;
131}
132
Ted Kremenekb1c392a2011-02-12 00:17:19 +0000133Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
134 do {
135 S = getParent(S);
136 }
137 while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
138
139 return S;
140}
141
Argyrios Kyrtzidis93907472011-07-27 05:28:18 +0000142Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const {
143 do {
144 S = getParent(S);
145 } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S);
146
147 return S;
148}
149
John McCall31168b02011-06-15 23:02:42 +0000150Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
Craig Topper36250ad2014-05-12 05:36:57 +0000151 Stmt *Paren = nullptr;
John McCall31168b02011-06-15 23:02:42 +0000152 while (isa<ParenExpr>(S)) {
153 Paren = S;
154 S = getParent(S);
155 };
156 return Paren;
157}
158
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000159bool ParentMap::isConsumedExpr(Expr* E) const {
160 Stmt *P = getParent(E);
161 Stmt *DirectChild = E;
Mike Stump11289f42009-09-09 15:08:12 +0000162
Jordan Rose69dd5fc2013-06-06 01:57:24 +0000163 // Ignore parents that don't guarantee consumption.
164 while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P) ||
165 isa<ExprWithCleanups>(P))) {
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000166 DirectChild = P;
167 P = getParent(P);
168 }
Mike Stump11289f42009-09-09 15:08:12 +0000169
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000170 if (!P)
171 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000172
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000173 switch (P->getStmtClass()) {
174 default:
175 return isa<Expr>(P);
176 case Stmt::DeclStmtClass:
177 return true;
178 case Stmt::BinaryOperatorClass: {
179 BinaryOperator *BE = cast<BinaryOperator>(P);
Ted Kremenek042befd2009-04-09 05:34:31 +0000180 // If it is a comma, only the right side is consumed.
Ted Kremenekd43aaad2009-04-08 18:49:36 +0000181 // If it isn't a comma, both sides are consumed.
John McCalle3027922010-08-25 11:45:40 +0000182 return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000183 }
184 case Stmt::ForStmtClass:
185 return DirectChild == cast<ForStmt>(P)->getCond();
186 case Stmt::WhileStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000187 return DirectChild == cast<WhileStmt>(P)->getCond();
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000188 case Stmt::DoStmtClass:
189 return DirectChild == cast<DoStmt>(P)->getCond();
190 case Stmt::IfStmtClass:
191 return DirectChild == cast<IfStmt>(P)->getCond();
192 case Stmt::IndirectGotoStmtClass:
193 return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
194 case Stmt::SwitchStmtClass:
195 return DirectChild == cast<SwitchStmt>(P)->getCond();
196 case Stmt::ReturnStmtClass:
197 return true;
198 }
199}
200