1 //===--- ParentMap.cpp - Mappings from Stmts to their Parents ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ParentMap class.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ParentMap.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/Expr.h"
17 #include "llvm/ADT/DenseMap.h"
19 using namespace clang;
21 typedef llvm::DenseMap<Stmt*, Stmt*> MapTy;
23 static void BuildParentMap(MapTy& M, Stmt* S) {
24 for (Stmt::child_range I = S->children(); I; ++I)
27 BuildParentMap(M, *I);
31 ParentMap::ParentMap(Stmt* S) : Impl(0) {
33 MapTy *M = new MapTy();
34 BuildParentMap(*M, S);
39 ParentMap::~ParentMap() {
43 void ParentMap::addStmt(Stmt* S) {
45 BuildParentMap(*(MapTy*) Impl, S);
49 Stmt* 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;
55 Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
56 do { S = getParent(S); } while (S && isa<ParenExpr>(S));
60 Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
64 while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
69 bool ParentMap::isConsumedExpr(Expr* E) const {
70 Stmt *P = getParent(E);
71 Stmt *DirectChild = E;
73 // Ignore parents that are parentheses or casts.
74 while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P))) {
82 switch (P->getStmtClass()) {
85 case Stmt::DeclStmtClass:
87 case Stmt::BinaryOperatorClass: {
88 BinaryOperator *BE = cast<BinaryOperator>(P);
89 // If it is a comma, only the right side is consumed.
90 // If it isn't a comma, both sides are consumed.
91 return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
93 case Stmt::ForStmtClass:
94 return DirectChild == cast<ForStmt>(P)->getCond();
95 case Stmt::WhileStmtClass:
96 return DirectChild == cast<WhileStmt>(P)->getCond();
97 case Stmt::DoStmtClass:
98 return DirectChild == cast<DoStmt>(P)->getCond();
99 case Stmt::IfStmtClass:
100 return DirectChild == cast<IfStmt>(P)->getCond();
101 case Stmt::IndirectGotoStmtClass:
102 return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
103 case Stmt::SwitchStmtClass:
104 return DirectChild == cast<SwitchStmt>(P)->getCond();
105 case Stmt::ReturnStmtClass: