blob: d7d5f9c692059bc704c248249253934be20cf78d [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) {
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
Jordan Rosec3f1cb32013-05-18 02:27:09 +000037 // If we are rebuilding the map, clear out any existing state.
38 if (M[POE->getSyntacticForm()])
Benjamin Kramer642f1732015-07-02 21:03:14 +000039 for (Stmt *SubStmt : S->children())
40 M[SubStmt] = nullptr;
Jordan Rosec3f1cb32013-05-18 02:27:09 +000041
Jordan Rose433b0f52013-05-18 02:26:50 +000042 M[POE->getSyntacticForm()] = S;
43 BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent);
Jordan Roseadb53542012-10-06 01:19:36 +000044
45 for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(),
46 E = POE->semantics_end();
47 I != E; ++I) {
48 M[*I] = S;
Jordan Rose433b0f52013-05-18 02:26:50 +000049 BuildParentMap(M, *I, OV_Opaque);
Jordan Roseadb53542012-10-06 01:19:36 +000050 }
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;
Jordan Rose433b0f52013-05-18 02:26:50 +000058 BuildParentMap(M, BCO->getCommon(), OV_Transparent);
Jordan Roseadb53542012-10-06 01:19:36 +000059
60 M[BCO->getCond()] = S;
Jordan Rose433b0f52013-05-18 02:26:50 +000061 BuildParentMap(M, BCO->getCond(), OV_Opaque);
Jordan Roseadb53542012-10-06 01:19:36 +000062
63 M[BCO->getTrueExpr()] = S;
Jordan Rose433b0f52013-05-18 02:26:50 +000064 BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque);
Jordan Roseadb53542012-10-06 01:19:36 +000065
66 M[BCO->getFalseExpr()] = S;
Jordan Rose433b0f52013-05-18 02:26:50 +000067 BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent);
Jordan Roseadb53542012-10-06 01:19:36 +000068
69 break;
70 }
Jordan Rosec3f1cb32013-05-18 02:27:09 +000071 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);
Serge Pavlov111200672013-05-18 04:32:15 +000078 if (OVMode == OV_Transparent || !M[OVE->getSourceExpr()]) {
Jordan Roseadb53542012-10-06 01:19:36 +000079 M[OVE->getSourceExpr()] = S;
Jordan Rose433b0f52013-05-18 02:26:50 +000080 BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent);
Jordan Roseadb53542012-10-06 01:19:36 +000081 }
82 break;
Jordan Rosec3f1cb32013-05-18 02:27:09 +000083 }
Jordan Roseadb53542012-10-06 01:19:36 +000084 default:
Benjamin Kramer642f1732015-07-02 21:03:14 +000085 for (Stmt *SubStmt : S->children()) {
86 if (SubStmt) {
87 M[SubStmt] = S;
88 BuildParentMap(M, SubStmt, OVMode);
Jordan Rosee32e1532012-08-06 21:28:11 +000089 }
Ted Kremenek99ecdd12008-06-20 21:40:36 +000090 }
Jordan Roseadb53542012-10-06 01:19:36 +000091 break;
Jordan Rosee32e1532012-08-06 21:28:11 +000092 }
Ted Kremenek99ecdd12008-06-20 21:40:36 +000093}
94
Craig Topper36250ad2014-05-12 05:36:57 +000095ParentMap::ParentMap(Stmt *S) : Impl(nullptr) {
Ted Kremenek99ecdd12008-06-20 21:40:36 +000096 if (S) {
97 MapTy *M = new MapTy();
Jordan Rose433b0f52013-05-18 02:26:50 +000098 BuildParentMap(*M, S);
Mike Stump11289f42009-09-09 15:08:12 +000099 Impl = M;
Ted Kremenek99ecdd12008-06-20 21:40:36 +0000100 }
101}
102
103ParentMap::~ParentMap() {
104 delete (MapTy*) Impl;
105}
106
Ted Kremenek43f50692010-11-15 20:54:24 +0000107void ParentMap::addStmt(Stmt* S) {
108 if (S) {
Jordan Rose433b0f52013-05-18 02:26:50 +0000109 BuildParentMap(*(MapTy*) Impl, S);
Ted Kremenek43f50692010-11-15 20:54:24 +0000110 }
111}
112
Jordan Rosecf10ea82013-06-06 21:53:45 +0000113void 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
Ted Kremenek99ecdd12008-06-20 21:40:36 +0000120Stmt* ParentMap::getParent(Stmt* S) const {
121 MapTy* M = (MapTy*) Impl;
122 MapTy::iterator I = M->find(S);
Craig Topper36250ad2014-05-12 05:36:57 +0000123 return I == M->end() ? nullptr : I->second;
Ted Kremenek99ecdd12008-06-20 21:40:36 +0000124}
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000125
Ted Kremenekd4dacb02009-05-11 19:49:27 +0000126Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
127 do { S = getParent(S); } while (S && isa<ParenExpr>(S));
128 return S;
129}
130
Ted Kremenekb1c392a2011-02-12 00:17:19 +0000131Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
132 do {
133 S = getParent(S);
134 }
135 while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
136
137 return S;
138}
139
Argyrios Kyrtzidis93907472011-07-27 05:28:18 +0000140Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const {
141 do {
142 S = getParent(S);
143 } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S);
144
145 return S;
146}
147
John McCall31168b02011-06-15 23:02:42 +0000148Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
Craig Topper36250ad2014-05-12 05:36:57 +0000149 Stmt *Paren = nullptr;
John McCall31168b02011-06-15 23:02:42 +0000150 while (isa<ParenExpr>(S)) {
151 Paren = S;
152 S = getParent(S);
153 };
154 return Paren;
155}
156
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000157bool ParentMap::isConsumedExpr(Expr* E) const {
158 Stmt *P = getParent(E);
159 Stmt *DirectChild = E;
Mike Stump11289f42009-09-09 15:08:12 +0000160
Jordan Rose69dd5fc2013-06-06 01:57:24 +0000161 // Ignore parents that don't guarantee consumption.
162 while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P) ||
163 isa<ExprWithCleanups>(P))) {
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000164 DirectChild = P;
165 P = getParent(P);
166 }
Mike Stump11289f42009-09-09 15:08:12 +0000167
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000168 if (!P)
169 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000170
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000171 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);
Ted Kremenek042befd2009-04-09 05:34:31 +0000178 // If it is a comma, only the right side is consumed.
Ted Kremenekd43aaad2009-04-08 18:49:36 +0000179 // If it isn't a comma, both sides are consumed.
John McCalle3027922010-08-25 11:45:40 +0000180 return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000181 }
182 case Stmt::ForStmtClass:
183 return DirectChild == cast<ForStmt>(P)->getCond();
184 case Stmt::WhileStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000185 return DirectChild == cast<WhileStmt>(P)->getCond();
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000186 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