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_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++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 Stmt* ParentMap::getParent(Stmt* S) const {
44 MapTy* M = (MapTy*) Impl;
45 MapTy::iterator I = M->find(S);
46 return I == M->end() ? 0 : I->second;
49 Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
50 do { S = getParent(S); } while (S && isa<ParenExpr>(S));
54 bool ParentMap::isConsumedExpr(Expr* E) const {
55 Stmt *P = getParent(E);
56 Stmt *DirectChild = E;
58 // Ignore parents that are parentheses or casts.
59 while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P))) {
67 switch (P->getStmtClass()) {
70 case Stmt::DeclStmtClass:
72 case Stmt::BinaryOperatorClass: {
73 BinaryOperator *BE = cast<BinaryOperator>(P);
74 // If it is a comma, only the right side is consumed.
75 // If it isn't a comma, both sides are consumed.
76 return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
78 case Stmt::ForStmtClass:
79 return DirectChild == cast<ForStmt>(P)->getCond();
80 case Stmt::WhileStmtClass:
81 return DirectChild == cast<WhileStmt>(P)->getCond();
82 case Stmt::DoStmtClass:
83 return DirectChild == cast<DoStmt>(P)->getCond();
84 case Stmt::IfStmtClass:
85 return DirectChild == cast<IfStmt>(P)->getCond();
86 case Stmt::IndirectGotoStmtClass:
87 return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
88 case Stmt::SwitchStmtClass:
89 return DirectChild == cast<SwitchStmt>(P)->getCond();
90 case Stmt::ReturnStmtClass: