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) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 24 | for (Stmt::child_range I = S->children(); I; ++I) |
Ted Kremenek | f8e32cf | 2008-06-20 21:40:36 +0000 | [diff] [blame] | 25 | if (*I) { |
| 26 | M[*I] = S; |
| 27 | BuildParentMap(M, *I); |
| 28 | } |
Ted Kremenek | e215ba1 | 2012-02-18 22:02:57 +0000 | [diff] [blame] | 29 | |
| 30 | // Also include the source expr tree of an OpaqueValueExpr in the map. |
| 31 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(S)) |
| 32 | BuildParentMap(M, OVE->getSourceExpr()); |
Ted Kremenek | f8e32cf | 2008-06-20 21:40:36 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | ParentMap::ParentMap(Stmt* S) : Impl(0) { |
| 36 | if (S) { |
| 37 | MapTy *M = new MapTy(); |
| 38 | BuildParentMap(*M, S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | Impl = M; |
Ted Kremenek | f8e32cf | 2008-06-20 21:40:36 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | ParentMap::~ParentMap() { |
| 44 | delete (MapTy*) Impl; |
| 45 | } |
| 46 | |
Ted Kremenek | d6543f8 | 2010-11-15 20:54:24 +0000 | [diff] [blame] | 47 | void ParentMap::addStmt(Stmt* S) { |
| 48 | if (S) { |
| 49 | BuildParentMap(*(MapTy*) Impl, S); |
| 50 | } |
| 51 | } |
| 52 | |
Ted Kremenek | f8e32cf | 2008-06-20 21:40:36 +0000 | [diff] [blame] | 53 | Stmt* ParentMap::getParent(Stmt* S) const { |
| 54 | MapTy* M = (MapTy*) Impl; |
| 55 | MapTy::iterator I = M->find(S); |
| 56 | return I == M->end() ? 0 : I->second; |
| 57 | } |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 58 | |
Ted Kremenek | b1b9f68 | 2009-05-11 19:49:27 +0000 | [diff] [blame] | 59 | Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const { |
| 60 | do { S = getParent(S); } while (S && isa<ParenExpr>(S)); |
| 61 | return S; |
| 62 | } |
| 63 | |
Ted Kremenek | f4e532b | 2011-02-12 00:17:19 +0000 | [diff] [blame] | 64 | Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const { |
| 65 | do { |
| 66 | S = getParent(S); |
| 67 | } |
| 68 | while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S))); |
| 69 | |
| 70 | return S; |
| 71 | } |
| 72 | |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 73 | Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const { |
| 74 | do { |
| 75 | S = getParent(S); |
| 76 | } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S); |
| 77 | |
| 78 | return S; |
| 79 | } |
| 80 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 81 | Stmt *ParentMap::getOuterParenParent(Stmt *S) const { |
| 82 | Stmt *Paren = 0; |
| 83 | while (isa<ParenExpr>(S)) { |
| 84 | Paren = S; |
| 85 | S = getParent(S); |
| 86 | }; |
| 87 | return Paren; |
| 88 | } |
| 89 | |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 90 | bool ParentMap::isConsumedExpr(Expr* E) const { |
| 91 | Stmt *P = getParent(E); |
| 92 | Stmt *DirectChild = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 94 | // Ignore parents that are parentheses or casts. |
Ted Kremenek | ade9eca | 2009-05-05 22:16:12 +0000 | [diff] [blame] | 95 | while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P))) { |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 96 | DirectChild = P; |
| 97 | P = getParent(P); |
| 98 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 100 | if (!P) |
| 101 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 103 | switch (P->getStmtClass()) { |
| 104 | default: |
| 105 | return isa<Expr>(P); |
| 106 | case Stmt::DeclStmtClass: |
| 107 | return true; |
| 108 | case Stmt::BinaryOperatorClass: { |
| 109 | BinaryOperator *BE = cast<BinaryOperator>(P); |
Ted Kremenek | 24ae89a | 2009-04-09 05:34:31 +0000 | [diff] [blame] | 110 | // If it is a comma, only the right side is consumed. |
Ted Kremenek | e42ac98 | 2009-04-08 18:49:36 +0000 | [diff] [blame] | 111 | // If it isn't a comma, both sides are consumed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 112 | return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS(); |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 113 | } |
| 114 | case Stmt::ForStmtClass: |
| 115 | return DirectChild == cast<ForStmt>(P)->getCond(); |
| 116 | case Stmt::WhileStmtClass: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | return DirectChild == cast<WhileStmt>(P)->getCond(); |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 118 | case Stmt::DoStmtClass: |
| 119 | return DirectChild == cast<DoStmt>(P)->getCond(); |
| 120 | case Stmt::IfStmtClass: |
| 121 | return DirectChild == cast<IfStmt>(P)->getCond(); |
| 122 | case Stmt::IndirectGotoStmtClass: |
| 123 | return DirectChild == cast<IndirectGotoStmt>(P)->getTarget(); |
| 124 | case Stmt::SwitchStmtClass: |
| 125 | return DirectChild == cast<SwitchStmt>(P)->getCond(); |
| 126 | case Stmt::ReturnStmtClass: |
| 127 | return true; |
| 128 | } |
| 129 | } |
| 130 | |