blob: a9a459b602baf4e6a958865811524d3dd30f9a75 [file] [log] [blame]
Ted Kremenekf8e32cf2008-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 Dunbaracc5f3e2008-08-11 06:23:49 +000015#include "clang/AST/Decl.h"
Ted Kremenekf8e32cf2008-06-20 21:40:36 +000016#include "clang/AST/Expr.h"
17#include "llvm/ADT/DenseMap.h"
18
19using namespace clang;
20
21typedef llvm::DenseMap<Stmt*, Stmt*> MapTy;
22
Jordan Rose1e5101e2012-10-06 01:19:36 +000023enum OpaqueValueMode {
24 OV_Transparent,
25 OV_Opaque
26};
27
Jordan Rosebb518992013-05-18 02:26:50 +000028static void BuildParentMap(MapTy& M, Stmt* S,
Jordan Rose1e5101e2012-10-06 01:19:36 +000029 OpaqueValueMode OVMode = OV_Transparent) {
30
31 switch (S->getStmtClass()) {
32 case Stmt::PseudoObjectExprClass: {
33 assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
34 PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S);
35
Jordan Roseb9fdfb52013-05-18 02:27:09 +000036 // If we are rebuilding the map, clear out any existing state.
37 if (M[POE->getSyntacticForm()])
38 for (Stmt::child_range I = S->children(); I; ++I)
39 M[*I] = 0;
40
Jordan Rosebb518992013-05-18 02:26:50 +000041 M[POE->getSyntacticForm()] = S;
42 BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent);
Jordan Rose1e5101e2012-10-06 01:19:36 +000043
44 for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(),
45 E = POE->semantics_end();
46 I != E; ++I) {
47 M[*I] = S;
Jordan Rosebb518992013-05-18 02:26:50 +000048 BuildParentMap(M, *I, OV_Opaque);
Jordan Rose1e5101e2012-10-06 01:19:36 +000049 }
50 break;
51 }
52 case Stmt::BinaryConditionalOperatorClass: {
53 assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
54 BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S);
55
56 M[BCO->getCommon()] = S;
Jordan Rosebb518992013-05-18 02:26:50 +000057 BuildParentMap(M, BCO->getCommon(), OV_Transparent);
Jordan Rose1e5101e2012-10-06 01:19:36 +000058
59 M[BCO->getCond()] = S;
Jordan Rosebb518992013-05-18 02:26:50 +000060 BuildParentMap(M, BCO->getCond(), OV_Opaque);
Jordan Rose1e5101e2012-10-06 01:19:36 +000061
62 M[BCO->getTrueExpr()] = S;
Jordan Rosebb518992013-05-18 02:26:50 +000063 BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque);
Jordan Rose1e5101e2012-10-06 01:19:36 +000064
65 M[BCO->getFalseExpr()] = S;
Jordan Rosebb518992013-05-18 02:26:50 +000066 BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent);
Jordan Rose1e5101e2012-10-06 01:19:36 +000067
68 break;
69 }
Jordan Roseb9fdfb52013-05-18 02:27:09 +000070 case Stmt::OpaqueValueExprClass: {
71 // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs
72 // share a single source expression, but in the AST a single
73 // OpaqueValueExpr is shared among multiple parent expressions.
74 // The right thing to do is to give the OpaqueValueExpr its syntactic
75 // parent, then not reassign that when traversing the semantic expressions.
76 OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S);
Serge Pavlovfa047c52013-05-18 04:32:15 +000077 if (OVMode == OV_Transparent || !M[OVE->getSourceExpr()]) {
Jordan Rose1e5101e2012-10-06 01:19:36 +000078 M[OVE->getSourceExpr()] = S;
Jordan Rosebb518992013-05-18 02:26:50 +000079 BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent);
Jordan Rose1e5101e2012-10-06 01:19:36 +000080 }
81 break;
Jordan Roseb9fdfb52013-05-18 02:27:09 +000082 }
Jordan Rose1e5101e2012-10-06 01:19:36 +000083 default:
84 for (Stmt::child_range I = S->children(); I; ++I) {
85 if (*I) {
86 M[*I] = S;
Jordan Rosebb518992013-05-18 02:26:50 +000087 BuildParentMap(M, *I, OVMode);
Jordan Rose2b1b0252012-08-06 21:28:11 +000088 }
Ted Kremenekf8e32cf2008-06-20 21:40:36 +000089 }
Jordan Rose1e5101e2012-10-06 01:19:36 +000090 break;
Jordan Rose2b1b0252012-08-06 21:28:11 +000091 }
Ted Kremenekf8e32cf2008-06-20 21:40:36 +000092}
93
Jordan Rosebb518992013-05-18 02:26:50 +000094ParentMap::ParentMap(Stmt* S) : Impl(0) {
Ted Kremenekf8e32cf2008-06-20 21:40:36 +000095 if (S) {
96 MapTy *M = new MapTy();
Jordan Rosebb518992013-05-18 02:26:50 +000097 BuildParentMap(*M, S);
Mike Stump1eb44332009-09-09 15:08:12 +000098 Impl = M;
Ted Kremenekf8e32cf2008-06-20 21:40:36 +000099 }
100}
101
102ParentMap::~ParentMap() {
103 delete (MapTy*) Impl;
104}
105
Ted Kremenekd6543f82010-11-15 20:54:24 +0000106void ParentMap::addStmt(Stmt* S) {
107 if (S) {
Jordan Rosebb518992013-05-18 02:26:50 +0000108 BuildParentMap(*(MapTy*) Impl, S);
Ted Kremenekd6543f82010-11-15 20:54:24 +0000109 }
110}
111
Ted Kremenekf8e32cf2008-06-20 21:40:36 +0000112Stmt* ParentMap::getParent(Stmt* S) const {
113 MapTy* M = (MapTy*) Impl;
114 MapTy::iterator I = M->find(S);
115 return I == M->end() ? 0 : I->second;
116}
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000117
Ted Kremenekb1b9f682009-05-11 19:49:27 +0000118Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
119 do { S = getParent(S); } while (S && isa<ParenExpr>(S));
120 return S;
121}
122
Ted Kremenekf4e532b2011-02-12 00:17:19 +0000123Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
124 do {
125 S = getParent(S);
126 }
127 while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
128
129 return S;
130}
131
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000132Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const {
133 do {
134 S = getParent(S);
135 } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S);
136
137 return S;
138}
139
John McCallf85e1932011-06-15 23:02:42 +0000140Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
141 Stmt *Paren = 0;
142 while (isa<ParenExpr>(S)) {
143 Paren = S;
144 S = getParent(S);
145 };
146 return Paren;
147}
148
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000149bool ParentMap::isConsumedExpr(Expr* E) const {
150 Stmt *P = getParent(E);
151 Stmt *DirectChild = E;
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000153 // Ignore parents that are parentheses or casts.
Ted Kremenekade9eca2009-05-05 22:16:12 +0000154 while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P))) {
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000155 DirectChild = P;
156 P = getParent(P);
157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000159 if (!P)
160 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000162 switch (P->getStmtClass()) {
163 default:
164 return isa<Expr>(P);
165 case Stmt::DeclStmtClass:
166 return true;
167 case Stmt::BinaryOperatorClass: {
168 BinaryOperator *BE = cast<BinaryOperator>(P);
Ted Kremenek24ae89a2009-04-09 05:34:31 +0000169 // If it is a comma, only the right side is consumed.
Ted Kremeneke42ac982009-04-08 18:49:36 +0000170 // If it isn't a comma, both sides are consumed.
John McCall2de56d12010-08-25 11:45:40 +0000171 return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000172 }
173 case Stmt::ForStmtClass:
174 return DirectChild == cast<ForStmt>(P)->getCond();
175 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000176 return DirectChild == cast<WhileStmt>(P)->getCond();
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000177 case Stmt::DoStmtClass:
178 return DirectChild == cast<DoStmt>(P)->getCond();
179 case Stmt::IfStmtClass:
180 return DirectChild == cast<IfStmt>(P)->getCond();
181 case Stmt::IndirectGotoStmtClass:
182 return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
183 case Stmt::SwitchStmtClass:
184 return DirectChild == cast<SwitchStmt>(P)->getCond();
185 case Stmt::ReturnStmtClass:
186 return true;
187 }
188}
189