1 //===--- 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" 15 #include "clang/AST/Decl.h" 16 #include "clang/AST/Expr.h" 17 #include "llvm/ADT/DenseMap.h" 18 19 using namespace clang; 20 21 typedef llvm::DenseMap<Stmt*, Stmt*> MapTy; 22 23 static void BuildParentMap(MapTy& M, Stmt* S) { 24 for (Stmt::child_range I = S->children(); I; ++I) 25 if (*I) { 26 M[*I] = S; 27 BuildParentMap(M, *I); 28 } 29 } 30 31 ParentMap::ParentMap(Stmt* S) : Impl(0) { 32 if (S) { 33 MapTy *M = new MapTy(); 34 BuildParentMap(*M, S); 35 Impl = M; 36 } 37 } 38 39 ParentMap::~ParentMap() { 40 delete (MapTy*) Impl; 41 } 42 43 void ParentMap::addStmt(Stmt* S) { 44 if (S) { 45 BuildParentMap(*(MapTy*) Impl, S); 46 } 47 } 48 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; 53 } 54 55 Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const { 56 do { S = getParent(S); } while (S && isa<ParenExpr>(S)); 57 return S; 58 } 59 60 Stmt *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 69 Stmt *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 77 Stmt *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 86 bool ParentMap::isConsumedExpr(Expr* E) const { 87 Stmt *P = getParent(E); 88 Stmt *DirectChild = E; 89 90 // Ignore parents that are parentheses or casts. 91 while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P))) { 92 DirectChild = P; 93 P = getParent(P); 94 } 95 96 if (!P) 97 return false; 98 99 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); 106 // If it is a comma, only the right side is consumed. 107 // If it isn't a comma, both sides are consumed. 108 return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS(); 109 } 110 case Stmt::ForStmtClass: 111 return DirectChild == cast<ForStmt>(P)->getCond(); 112 case Stmt::WhileStmtClass: 113 return DirectChild == cast<WhileStmt>(P)->getCond(); 114 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 127