blob: fa87afd0fa2278e3f276fa85f18b065ef1d4025e [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
23static void BuildParentMap(MapTy& M, Stmt* S) {
John McCall7502c1d2011-02-13 04:07:26 +000024 for (Stmt::child_range I = S->children(); I; ++I)
Ted Kremenekf8e32cf2008-06-20 21:40:36 +000025 if (*I) {
Jordan Rose2b1b0252012-08-06 21:28:11 +000026 // Prefer the first time we see this statement in the traversal.
27 // This is important for PseudoObjectExprs.
28 Stmt *&Parent = M[*I];
29 if (!Parent) {
30 Parent = S;
31 BuildParentMap(M, *I);
32 }
Ted Kremenekf8e32cf2008-06-20 21:40:36 +000033 }
Ted Kremeneke215ba12012-02-18 22:02:57 +000034
35 // Also include the source expr tree of an OpaqueValueExpr in the map.
Jordan Rose2b1b0252012-08-06 21:28:11 +000036 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(S)) {
37 M[OVE->getSourceExpr()] = S;
Ted Kremeneke215ba12012-02-18 22:02:57 +000038 BuildParentMap(M, OVE->getSourceExpr());
Jordan Rose2b1b0252012-08-06 21:28:11 +000039 }
Ted Kremenekf8e32cf2008-06-20 21:40:36 +000040}
41
42ParentMap::ParentMap(Stmt* S) : Impl(0) {
43 if (S) {
44 MapTy *M = new MapTy();
45 BuildParentMap(*M, S);
Mike Stump1eb44332009-09-09 15:08:12 +000046 Impl = M;
Ted Kremenekf8e32cf2008-06-20 21:40:36 +000047 }
48}
49
50ParentMap::~ParentMap() {
51 delete (MapTy*) Impl;
52}
53
Ted Kremenekd6543f82010-11-15 20:54:24 +000054void ParentMap::addStmt(Stmt* S) {
55 if (S) {
56 BuildParentMap(*(MapTy*) Impl, S);
57 }
58}
59
Ted Kremenekf8e32cf2008-06-20 21:40:36 +000060Stmt* ParentMap::getParent(Stmt* S) const {
61 MapTy* M = (MapTy*) Impl;
62 MapTy::iterator I = M->find(S);
63 return I == M->end() ? 0 : I->second;
64}
Ted Kremenekb930d7a2009-04-01 06:52:48 +000065
Ted Kremenekb1b9f682009-05-11 19:49:27 +000066Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
67 do { S = getParent(S); } while (S && isa<ParenExpr>(S));
68 return S;
69}
70
Ted Kremenekf4e532b2011-02-12 00:17:19 +000071Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
72 do {
73 S = getParent(S);
74 }
75 while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
76
77 return S;
78}
79
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +000080Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const {
81 do {
82 S = getParent(S);
83 } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S);
84
85 return S;
86}
87
John McCallf85e1932011-06-15 23:02:42 +000088Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
89 Stmt *Paren = 0;
90 while (isa<ParenExpr>(S)) {
91 Paren = S;
92 S = getParent(S);
93 };
94 return Paren;
95}
96
Ted Kremenekb930d7a2009-04-01 06:52:48 +000097bool ParentMap::isConsumedExpr(Expr* E) const {
98 Stmt *P = getParent(E);
99 Stmt *DirectChild = E;
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000101 // Ignore parents that are parentheses or casts.
Ted Kremenekade9eca2009-05-05 22:16:12 +0000102 while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P))) {
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000103 DirectChild = P;
104 P = getParent(P);
105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000107 if (!P)
108 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000110 switch (P->getStmtClass()) {
111 default:
112 return isa<Expr>(P);
113 case Stmt::DeclStmtClass:
114 return true;
115 case Stmt::BinaryOperatorClass: {
116 BinaryOperator *BE = cast<BinaryOperator>(P);
Ted Kremenek24ae89a2009-04-09 05:34:31 +0000117 // If it is a comma, only the right side is consumed.
Ted Kremeneke42ac982009-04-08 18:49:36 +0000118 // If it isn't a comma, both sides are consumed.
John McCall2de56d12010-08-25 11:45:40 +0000119 return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000120 }
121 case Stmt::ForStmtClass:
122 return DirectChild == cast<ForStmt>(P)->getCond();
123 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000124 return DirectChild == cast<WhileStmt>(P)->getCond();
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000125 case Stmt::DoStmtClass:
126 return DirectChild == cast<DoStmt>(P)->getCond();
127 case Stmt::IfStmtClass:
128 return DirectChild == cast<IfStmt>(P)->getCond();
129 case Stmt::IndirectGotoStmtClass:
130 return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
131 case Stmt::SwitchStmtClass:
132 return DirectChild == cast<SwitchStmt>(P)->getCond();
133 case Stmt::ReturnStmtClass:
134 return true;
135 }
136}
137