Ted Kremenek | f8e32cf | 2008-06-20 21:40:36 +0000 | [diff] [blame] | 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" |
Daniel Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
Ted Kremenek | f8e32cf | 2008-06-20 21:40:36 +0000 | [diff] [blame] | 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_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++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 | 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; |
| 47 | } |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 48 | |
| 49 | bool ParentMap::isConsumedExpr(Expr* E) const { |
| 50 | Stmt *P = getParent(E); |
| 51 | Stmt *DirectChild = E; |
| 52 | |
| 53 | // Ignore parents that are parentheses or casts. |
Ted Kremenek | ade9eca | 2009-05-05 22:16:12 +0000 | [diff] [blame] | 54 | while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P))) { |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 55 | DirectChild = P; |
| 56 | P = getParent(P); |
| 57 | } |
| 58 | |
| 59 | if (!P) |
| 60 | return false; |
| 61 | |
| 62 | switch (P->getStmtClass()) { |
| 63 | default: |
| 64 | return isa<Expr>(P); |
| 65 | case Stmt::DeclStmtClass: |
| 66 | return true; |
| 67 | case Stmt::BinaryOperatorClass: { |
| 68 | BinaryOperator *BE = cast<BinaryOperator>(P); |
Ted Kremenek | 24ae89a | 2009-04-09 05:34:31 +0000 | [diff] [blame] | 69 | // If it is a comma, only the right side is consumed. |
Ted Kremenek | e42ac98 | 2009-04-08 18:49:36 +0000 | [diff] [blame] | 70 | // If it isn't a comma, both sides are consumed. |
Ted Kremenek | 24ae89a | 2009-04-09 05:34:31 +0000 | [diff] [blame] | 71 | return BE->getOpcode()!=BinaryOperator::Comma ||DirectChild==BE->getRHS(); |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 72 | } |
| 73 | case Stmt::ForStmtClass: |
| 74 | return DirectChild == cast<ForStmt>(P)->getCond(); |
| 75 | case Stmt::WhileStmtClass: |
| 76 | return DirectChild == cast<WhileStmt>(P)->getCond(); |
| 77 | case Stmt::DoStmtClass: |
| 78 | return DirectChild == cast<DoStmt>(P)->getCond(); |
| 79 | case Stmt::IfStmtClass: |
| 80 | return DirectChild == cast<IfStmt>(P)->getCond(); |
| 81 | case Stmt::IndirectGotoStmtClass: |
| 82 | return DirectChild == cast<IndirectGotoStmt>(P)->getTarget(); |
| 83 | case Stmt::SwitchStmtClass: |
| 84 | return DirectChild == cast<SwitchStmt>(P)->getCond(); |
| 85 | case Stmt::ReturnStmtClass: |
| 86 | return true; |
| 87 | } |
| 88 | } |
| 89 | |