blob: 5eef83a81a8b25cbb7ea1cda2949eb403fa99914 [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) {
26 M[*I] = S;
27 BuildParentMap(M, *I);
28 }
29}
30
31ParentMap::ParentMap(Stmt* S) : Impl(0) {
32 if (S) {
33 MapTy *M = new MapTy();
34 BuildParentMap(*M, S);
Mike Stump1eb44332009-09-09 15:08:12 +000035 Impl = M;
Ted Kremenekf8e32cf2008-06-20 21:40:36 +000036 }
37}
38
39ParentMap::~ParentMap() {
40 delete (MapTy*) Impl;
41}
42
Ted Kremenekd6543f82010-11-15 20:54:24 +000043void ParentMap::addStmt(Stmt* S) {
44 if (S) {
45 BuildParentMap(*(MapTy*) Impl, S);
46 }
47}
48
Ted Kremenekf8e32cf2008-06-20 21:40:36 +000049Stmt* ParentMap::getParent(Stmt* S) const {
50 MapTy* M = (MapTy*) Impl;
51 MapTy::iterator I = M->find(S);
52 return I == M->end() ? 0 : I->second;
53}
Ted Kremenekb930d7a2009-04-01 06:52:48 +000054
Ted Kremenekb1b9f682009-05-11 19:49:27 +000055Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
56 do { S = getParent(S); } while (S && isa<ParenExpr>(S));
57 return S;
58}
59
Ted Kremenekf4e532b2011-02-12 00:17:19 +000060Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
61 do {
62 S = getParent(S);
63 }
64 while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
65
66 return S;
67}
68
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +000069Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const {
70 do {
71 S = getParent(S);
72 } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S);
73
74 return S;
75}
76
John McCallf85e1932011-06-15 23:02:42 +000077Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
78 Stmt *Paren = 0;
79 while (isa<ParenExpr>(S)) {
80 Paren = S;
81 S = getParent(S);
82 };
83 return Paren;
84}
85
Ted Kremenekb930d7a2009-04-01 06:52:48 +000086bool ParentMap::isConsumedExpr(Expr* E) const {
87 Stmt *P = getParent(E);
88 Stmt *DirectChild = E;
Mike Stump1eb44332009-09-09 15:08:12 +000089
Ted Kremenekb930d7a2009-04-01 06:52:48 +000090 // Ignore parents that are parentheses or casts.
Ted Kremenekade9eca2009-05-05 22:16:12 +000091 while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P))) {
Ted Kremenekb930d7a2009-04-01 06:52:48 +000092 DirectChild = P;
93 P = getParent(P);
94 }
Mike Stump1eb44332009-09-09 15:08:12 +000095
Ted Kremenekb930d7a2009-04-01 06:52:48 +000096 if (!P)
97 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremenekb930d7a2009-04-01 06:52:48 +000099 switch (P->getStmtClass()) {
100 default:
101 return isa<Expr>(P);
102 case Stmt::DeclStmtClass:
103 return true;
104 case Stmt::BinaryOperatorClass: {
105 BinaryOperator *BE = cast<BinaryOperator>(P);
Ted Kremenek24ae89a2009-04-09 05:34:31 +0000106 // If it is a comma, only the right side is consumed.
Ted Kremeneke42ac982009-04-08 18:49:36 +0000107 // If it isn't a comma, both sides are consumed.
John McCall2de56d12010-08-25 11:45:40 +0000108 return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000109 }
110 case Stmt::ForStmtClass:
111 return DirectChild == cast<ForStmt>(P)->getCond();
112 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000113 return DirectChild == cast<WhileStmt>(P)->getCond();
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000114 case Stmt::DoStmtClass:
115 return DirectChild == cast<DoStmt>(P)->getCond();
116 case Stmt::IfStmtClass:
117 return DirectChild == cast<IfStmt>(P)->getCond();
118 case Stmt::IndirectGotoStmtClass:
119 return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
120 case Stmt::SwitchStmtClass:
121 return DirectChild == cast<SwitchStmt>(P)->getCond();
122 case Stmt::ReturnStmtClass:
123 return true;
124 }
125}
126