blob: 113592860b77eee8a6e66a4a44ce3c65228b6d9f [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
28static void BuildParentMap(MapTy& M, Stmt* S,
29 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
36 M[POE->getSyntacticForm()] = S;
37 BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent);
38
39 for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(),
40 E = POE->semantics_end();
41 I != E; ++I) {
42 M[*I] = S;
43 BuildParentMap(M, *I, OV_Opaque);
44 }
45 break;
46 }
47 case Stmt::BinaryConditionalOperatorClass: {
48 assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
49 BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S);
50
51 M[BCO->getCommon()] = S;
52 BuildParentMap(M, BCO->getCommon(), OV_Transparent);
53
54 M[BCO->getCond()] = S;
55 BuildParentMap(M, BCO->getCond(), OV_Opaque);
56
57 M[BCO->getTrueExpr()] = S;
58 BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque);
59
60 M[BCO->getFalseExpr()] = S;
61 BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent);
62
63 break;
64 }
65 case Stmt::OpaqueValueExprClass:
66 if (OVMode == OV_Transparent) {
67 OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S);
68 M[OVE->getSourceExpr()] = S;
69 BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent);
70 }
71 break;
72 default:
73 for (Stmt::child_range I = S->children(); I; ++I) {
74 if (*I) {
75 M[*I] = S;
76 BuildParentMap(M, *I, OVMode);
Jordan Rose2b1b0252012-08-06 21:28:11 +000077 }
Ted Kremenekf8e32cf2008-06-20 21:40:36 +000078 }
Jordan Rose1e5101e2012-10-06 01:19:36 +000079 break;
Jordan Rose2b1b0252012-08-06 21:28:11 +000080 }
Ted Kremenekf8e32cf2008-06-20 21:40:36 +000081}
82
83ParentMap::ParentMap(Stmt* S) : Impl(0) {
84 if (S) {
85 MapTy *M = new MapTy();
86 BuildParentMap(*M, S);
Mike Stump1eb44332009-09-09 15:08:12 +000087 Impl = M;
Ted Kremenekf8e32cf2008-06-20 21:40:36 +000088 }
89}
90
91ParentMap::~ParentMap() {
92 delete (MapTy*) Impl;
93}
94
Ted Kremenekd6543f82010-11-15 20:54:24 +000095void ParentMap::addStmt(Stmt* S) {
96 if (S) {
97 BuildParentMap(*(MapTy*) Impl, S);
98 }
99}
100
Ted Kremenekf8e32cf2008-06-20 21:40:36 +0000101Stmt* ParentMap::getParent(Stmt* S) const {
102 MapTy* M = (MapTy*) Impl;
103 MapTy::iterator I = M->find(S);
104 return I == M->end() ? 0 : I->second;
105}
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000106
Ted Kremenekb1b9f682009-05-11 19:49:27 +0000107Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
108 do { S = getParent(S); } while (S && isa<ParenExpr>(S));
109 return S;
110}
111
Ted Kremenekf4e532b2011-02-12 00:17:19 +0000112Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
113 do {
114 S = getParent(S);
115 }
116 while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
117
118 return S;
119}
120
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000121Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const {
122 do {
123 S = getParent(S);
124 } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S);
125
126 return S;
127}
128
John McCallf85e1932011-06-15 23:02:42 +0000129Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
130 Stmt *Paren = 0;
131 while (isa<ParenExpr>(S)) {
132 Paren = S;
133 S = getParent(S);
134 };
135 return Paren;
136}
137
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000138bool ParentMap::isConsumedExpr(Expr* E) const {
139 Stmt *P = getParent(E);
140 Stmt *DirectChild = E;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000142 // Ignore parents that are parentheses or casts.
Ted Kremenekade9eca2009-05-05 22:16:12 +0000143 while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P))) {
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000144 DirectChild = P;
145 P = getParent(P);
146 }
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000148 if (!P)
149 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000151 switch (P->getStmtClass()) {
152 default:
153 return isa<Expr>(P);
154 case Stmt::DeclStmtClass:
155 return true;
156 case Stmt::BinaryOperatorClass: {
157 BinaryOperator *BE = cast<BinaryOperator>(P);
Ted Kremenek24ae89a2009-04-09 05:34:31 +0000158 // If it is a comma, only the right side is consumed.
Ted Kremeneke42ac982009-04-08 18:49:36 +0000159 // If it isn't a comma, both sides are consumed.
John McCall2de56d12010-08-25 11:45:40 +0000160 return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000161 }
162 case Stmt::ForStmtClass:
163 return DirectChild == cast<ForStmt>(P)->getCond();
164 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000165 return DirectChild == cast<WhileStmt>(P)->getCond();
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000166 case Stmt::DoStmtClass:
167 return DirectChild == cast<DoStmt>(P)->getCond();
168 case Stmt::IfStmtClass:
169 return DirectChild == cast<IfStmt>(P)->getCond();
170 case Stmt::IndirectGotoStmtClass:
171 return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
172 case Stmt::SwitchStmtClass:
173 return DirectChild == cast<SwitchStmt>(P)->getCond();
174 case Stmt::ReturnStmtClass:
175 return true;
176 }
177}
178