Ted Kremenek | 99ecdd1 | 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 | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
Ted Kremenek | 99ecdd1 | 2008-06-20 21:40:36 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
Jordan Rose | 69dd5fc | 2013-06-06 01:57:24 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
George Karpenkov | 04b9dc5 | 2018-03-08 02:53:39 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtObjC.h" |
Ted Kremenek | 99ecdd1 | 2008-06-20 21:40:36 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | |
| 21 | using namespace clang; |
| 22 | |
| 23 | typedef llvm::DenseMap<Stmt*, Stmt*> MapTy; |
| 24 | |
Jordan Rose | adb5354 | 2012-10-06 01:19:36 +0000 | [diff] [blame] | 25 | enum OpaqueValueMode { |
| 26 | OV_Transparent, |
| 27 | OV_Opaque |
| 28 | }; |
| 29 | |
Jordan Rose | 433b0f5 | 2013-05-18 02:26:50 +0000 | [diff] [blame] | 30 | static void BuildParentMap(MapTy& M, Stmt* S, |
Jordan Rose | adb5354 | 2012-10-06 01:19:36 +0000 | [diff] [blame] | 31 | OpaqueValueMode OVMode = OV_Transparent) { |
Argyrios Kyrtzidis | 6f10b74 | 2016-07-14 20:21:16 +0000 | [diff] [blame] | 32 | if (!S) |
| 33 | return; |
Jordan Rose | adb5354 | 2012-10-06 01:19:36 +0000 | [diff] [blame] | 34 | |
| 35 | switch (S->getStmtClass()) { |
| 36 | case Stmt::PseudoObjectExprClass: { |
| 37 | assert(OVMode == OV_Transparent && "Should not appear alongside OVEs"); |
| 38 | PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S); |
| 39 | |
Jordan Rose | c3f1cb3 | 2013-05-18 02:27:09 +0000 | [diff] [blame] | 40 | // If we are rebuilding the map, clear out any existing state. |
| 41 | if (M[POE->getSyntacticForm()]) |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 42 | for (Stmt *SubStmt : S->children()) |
| 43 | M[SubStmt] = nullptr; |
Jordan Rose | c3f1cb3 | 2013-05-18 02:27:09 +0000 | [diff] [blame] | 44 | |
Jordan Rose | 433b0f5 | 2013-05-18 02:26:50 +0000 | [diff] [blame] | 45 | M[POE->getSyntacticForm()] = S; |
| 46 | BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent); |
Jordan Rose | adb5354 | 2012-10-06 01:19:36 +0000 | [diff] [blame] | 47 | |
| 48 | for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(), |
| 49 | E = POE->semantics_end(); |
| 50 | I != E; ++I) { |
| 51 | M[*I] = S; |
Jordan Rose | 433b0f5 | 2013-05-18 02:26:50 +0000 | [diff] [blame] | 52 | BuildParentMap(M, *I, OV_Opaque); |
Jordan Rose | adb5354 | 2012-10-06 01:19:36 +0000 | [diff] [blame] | 53 | } |
| 54 | break; |
| 55 | } |
| 56 | case Stmt::BinaryConditionalOperatorClass: { |
| 57 | assert(OVMode == OV_Transparent && "Should not appear alongside OVEs"); |
| 58 | BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S); |
| 59 | |
| 60 | M[BCO->getCommon()] = S; |
Jordan Rose | 433b0f5 | 2013-05-18 02:26:50 +0000 | [diff] [blame] | 61 | BuildParentMap(M, BCO->getCommon(), OV_Transparent); |
Jordan Rose | adb5354 | 2012-10-06 01:19:36 +0000 | [diff] [blame] | 62 | |
| 63 | M[BCO->getCond()] = S; |
Jordan Rose | 433b0f5 | 2013-05-18 02:26:50 +0000 | [diff] [blame] | 64 | BuildParentMap(M, BCO->getCond(), OV_Opaque); |
Jordan Rose | adb5354 | 2012-10-06 01:19:36 +0000 | [diff] [blame] | 65 | |
| 66 | M[BCO->getTrueExpr()] = S; |
Jordan Rose | 433b0f5 | 2013-05-18 02:26:50 +0000 | [diff] [blame] | 67 | BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque); |
Jordan Rose | adb5354 | 2012-10-06 01:19:36 +0000 | [diff] [blame] | 68 | |
| 69 | M[BCO->getFalseExpr()] = S; |
Jordan Rose | 433b0f5 | 2013-05-18 02:26:50 +0000 | [diff] [blame] | 70 | BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent); |
Jordan Rose | adb5354 | 2012-10-06 01:19:36 +0000 | [diff] [blame] | 71 | |
| 72 | break; |
| 73 | } |
Jordan Rose | c3f1cb3 | 2013-05-18 02:27:09 +0000 | [diff] [blame] | 74 | case Stmt::OpaqueValueExprClass: { |
| 75 | // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs |
| 76 | // share a single source expression, but in the AST a single |
| 77 | // OpaqueValueExpr is shared among multiple parent expressions. |
| 78 | // The right thing to do is to give the OpaqueValueExpr its syntactic |
| 79 | // parent, then not reassign that when traversing the semantic expressions. |
| 80 | OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S); |
Serge Pavlov | 11120067 | 2013-05-18 04:32:15 +0000 | [diff] [blame] | 81 | if (OVMode == OV_Transparent || !M[OVE->getSourceExpr()]) { |
Jordan Rose | adb5354 | 2012-10-06 01:19:36 +0000 | [diff] [blame] | 82 | M[OVE->getSourceExpr()] = S; |
Jordan Rose | 433b0f5 | 2013-05-18 02:26:50 +0000 | [diff] [blame] | 83 | BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent); |
Jordan Rose | adb5354 | 2012-10-06 01:19:36 +0000 | [diff] [blame] | 84 | } |
| 85 | break; |
Jordan Rose | c3f1cb3 | 2013-05-18 02:27:09 +0000 | [diff] [blame] | 86 | } |
Jordan Rose | adb5354 | 2012-10-06 01:19:36 +0000 | [diff] [blame] | 87 | default: |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 88 | for (Stmt *SubStmt : S->children()) { |
| 89 | if (SubStmt) { |
| 90 | M[SubStmt] = S; |
| 91 | BuildParentMap(M, SubStmt, OVMode); |
Jordan Rose | e32e153 | 2012-08-06 21:28:11 +0000 | [diff] [blame] | 92 | } |
Ted Kremenek | 99ecdd1 | 2008-06-20 21:40:36 +0000 | [diff] [blame] | 93 | } |
Jordan Rose | adb5354 | 2012-10-06 01:19:36 +0000 | [diff] [blame] | 94 | break; |
Jordan Rose | e32e153 | 2012-08-06 21:28:11 +0000 | [diff] [blame] | 95 | } |
Ted Kremenek | 99ecdd1 | 2008-06-20 21:40:36 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 98 | ParentMap::ParentMap(Stmt *S) : Impl(nullptr) { |
Ted Kremenek | 99ecdd1 | 2008-06-20 21:40:36 +0000 | [diff] [blame] | 99 | if (S) { |
| 100 | MapTy *M = new MapTy(); |
Jordan Rose | 433b0f5 | 2013-05-18 02:26:50 +0000 | [diff] [blame] | 101 | BuildParentMap(*M, S); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | Impl = M; |
Ted Kremenek | 99ecdd1 | 2008-06-20 21:40:36 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | ParentMap::~ParentMap() { |
| 107 | delete (MapTy*) Impl; |
| 108 | } |
| 109 | |
Ted Kremenek | 43f5069 | 2010-11-15 20:54:24 +0000 | [diff] [blame] | 110 | void ParentMap::addStmt(Stmt* S) { |
| 111 | if (S) { |
Jordan Rose | 433b0f5 | 2013-05-18 02:26:50 +0000 | [diff] [blame] | 112 | BuildParentMap(*(MapTy*) Impl, S); |
Ted Kremenek | 43f5069 | 2010-11-15 20:54:24 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Jordan Rose | cf10ea8 | 2013-06-06 21:53:45 +0000 | [diff] [blame] | 116 | void ParentMap::setParent(const Stmt *S, const Stmt *Parent) { |
| 117 | assert(S); |
| 118 | assert(Parent); |
| 119 | MapTy *M = reinterpret_cast<MapTy *>(Impl); |
| 120 | M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent))); |
| 121 | } |
| 122 | |
Ted Kremenek | 99ecdd1 | 2008-06-20 21:40:36 +0000 | [diff] [blame] | 123 | Stmt* ParentMap::getParent(Stmt* S) const { |
| 124 | MapTy* M = (MapTy*) Impl; |
| 125 | MapTy::iterator I = M->find(S); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 126 | return I == M->end() ? nullptr : I->second; |
Ted Kremenek | 99ecdd1 | 2008-06-20 21:40:36 +0000 | [diff] [blame] | 127 | } |
Ted Kremenek | 8b0dba3 | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 128 | |
Ted Kremenek | d4dacb0 | 2009-05-11 19:49:27 +0000 | [diff] [blame] | 129 | Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const { |
| 130 | do { S = getParent(S); } while (S && isa<ParenExpr>(S)); |
| 131 | return S; |
| 132 | } |
| 133 | |
Ted Kremenek | b1c392a | 2011-02-12 00:17:19 +0000 | [diff] [blame] | 134 | Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const { |
| 135 | do { |
| 136 | S = getParent(S); |
| 137 | } |
| 138 | while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S))); |
| 139 | |
| 140 | return S; |
| 141 | } |
| 142 | |
Argyrios Kyrtzidis | 9390747 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 143 | Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const { |
| 144 | do { |
| 145 | S = getParent(S); |
| 146 | } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S); |
| 147 | |
| 148 | return S; |
| 149 | } |
| 150 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 151 | Stmt *ParentMap::getOuterParenParent(Stmt *S) const { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 152 | Stmt *Paren = nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 153 | while (isa<ParenExpr>(S)) { |
| 154 | Paren = S; |
| 155 | S = getParent(S); |
| 156 | }; |
| 157 | return Paren; |
| 158 | } |
| 159 | |
Ted Kremenek | 8b0dba3 | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 160 | bool ParentMap::isConsumedExpr(Expr* E) const { |
| 161 | Stmt *P = getParent(E); |
| 162 | Stmt *DirectChild = E; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | |
Jordan Rose | 69dd5fc | 2013-06-06 01:57:24 +0000 | [diff] [blame] | 164 | // Ignore parents that don't guarantee consumption. |
| 165 | while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P) || |
| 166 | isa<ExprWithCleanups>(P))) { |
Ted Kremenek | 8b0dba3 | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 167 | DirectChild = P; |
| 168 | P = getParent(P); |
| 169 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | |
Ted Kremenek | 8b0dba3 | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 171 | if (!P) |
| 172 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Ted Kremenek | 8b0dba3 | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 174 | switch (P->getStmtClass()) { |
| 175 | default: |
| 176 | return isa<Expr>(P); |
| 177 | case Stmt::DeclStmtClass: |
| 178 | return true; |
| 179 | case Stmt::BinaryOperatorClass: { |
| 180 | BinaryOperator *BE = cast<BinaryOperator>(P); |
Ted Kremenek | 042befd | 2009-04-09 05:34:31 +0000 | [diff] [blame] | 181 | // If it is a comma, only the right side is consumed. |
Ted Kremenek | d43aaad | 2009-04-08 18:49:36 +0000 | [diff] [blame] | 182 | // If it isn't a comma, both sides are consumed. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 183 | return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS(); |
Ted Kremenek | 8b0dba3 | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 184 | } |
| 185 | case Stmt::ForStmtClass: |
| 186 | return DirectChild == cast<ForStmt>(P)->getCond(); |
| 187 | case Stmt::WhileStmtClass: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | return DirectChild == cast<WhileStmt>(P)->getCond(); |
Ted Kremenek | 8b0dba3 | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 189 | case Stmt::DoStmtClass: |
| 190 | return DirectChild == cast<DoStmt>(P)->getCond(); |
| 191 | case Stmt::IfStmtClass: |
| 192 | return DirectChild == cast<IfStmt>(P)->getCond(); |
| 193 | case Stmt::IndirectGotoStmtClass: |
| 194 | return DirectChild == cast<IndirectGotoStmt>(P)->getTarget(); |
| 195 | case Stmt::SwitchStmtClass: |
| 196 | return DirectChild == cast<SwitchStmt>(P)->getCond(); |
George Karpenkov | 04b9dc5 | 2018-03-08 02:53:39 +0000 | [diff] [blame] | 197 | case Stmt::ObjCForCollectionStmtClass: |
| 198 | return DirectChild == cast<ObjCForCollectionStmt>(P)->getCollection(); |
Ted Kremenek | 8b0dba3 | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 199 | case Stmt::ReturnStmtClass: |
| 200 | return true; |
| 201 | } |
| 202 | } |
| 203 | |