| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #include "clang/AST/ParentMap.h" |
| 14 | #include "clang/AST/Decl.h" |
| 15 | #include "clang/AST/Expr.h" |
| 16 | #include "clang/AST/ExprCXX.h" |
| 17 | #include "clang/AST/StmtObjC.h" |
| 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | typedef llvm::DenseMap<Stmt*, Stmt*> MapTy; |
| 23 | |
| 24 | enum OpaqueValueMode { |
| 25 | OV_Transparent, |
| 26 | OV_Opaque |
| 27 | }; |
| 28 | |
| 29 | static void BuildParentMap(MapTy& M, Stmt* S, |
| 30 | OpaqueValueMode OVMode = OV_Transparent) { |
| 31 | if (!S) |
| 32 | return; |
| 33 | |
| 34 | switch (S->getStmtClass()) { |
| 35 | case Stmt::PseudoObjectExprClass: { |
| 36 | (0) . __assert_fail ("OVMode == OV_Transparent && \"Should not appear alongside OVEs\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ParentMap.cpp", 36, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(OVMode == OV_Transparent && "Should not appear alongside OVEs"); |
| 37 | PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S); |
| 38 | |
| 39 | |
| 40 | if (M[POE->getSyntacticForm()]) |
| 41 | for (Stmt *SubStmt : S->children()) |
| 42 | M[SubStmt] = nullptr; |
| 43 | |
| 44 | M[POE->getSyntacticForm()] = S; |
| 45 | BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent); |
| 46 | |
| 47 | for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(), |
| 48 | E = POE->semantics_end(); |
| 49 | I != E; ++I) { |
| 50 | M[*I] = S; |
| 51 | BuildParentMap(M, *I, OV_Opaque); |
| 52 | } |
| 53 | break; |
| 54 | } |
| 55 | case Stmt::BinaryConditionalOperatorClass: { |
| 56 | (0) . __assert_fail ("OVMode == OV_Transparent && \"Should not appear alongside OVEs\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ParentMap.cpp", 56, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(OVMode == OV_Transparent && "Should not appear alongside OVEs"); |
| 57 | BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S); |
| 58 | |
| 59 | M[BCO->getCommon()] = S; |
| 60 | BuildParentMap(M, BCO->getCommon(), OV_Transparent); |
| 61 | |
| 62 | M[BCO->getCond()] = S; |
| 63 | BuildParentMap(M, BCO->getCond(), OV_Opaque); |
| 64 | |
| 65 | M[BCO->getTrueExpr()] = S; |
| 66 | BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque); |
| 67 | |
| 68 | M[BCO->getFalseExpr()] = S; |
| 69 | BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent); |
| 70 | |
| 71 | break; |
| 72 | } |
| 73 | case Stmt::OpaqueValueExprClass: { |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S); |
| 80 | if (OVMode == OV_Transparent || !M[OVE->getSourceExpr()]) { |
| 81 | M[OVE->getSourceExpr()] = S; |
| 82 | BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent); |
| 83 | } |
| 84 | break; |
| 85 | } |
| 86 | default: |
| 87 | for (Stmt *SubStmt : S->children()) { |
| 88 | if (SubStmt) { |
| 89 | M[SubStmt] = S; |
| 90 | BuildParentMap(M, SubStmt, OVMode); |
| 91 | } |
| 92 | } |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | ParentMap::ParentMap(Stmt *S) : Impl(nullptr) { |
| 98 | if (S) { |
| 99 | MapTy *M = new MapTy(); |
| 100 | BuildParentMap(*M, S); |
| 101 | Impl = M; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | ParentMap::~ParentMap() { |
| 106 | delete (MapTy*) Impl; |
| 107 | } |
| 108 | |
| 109 | void ParentMap::addStmt(Stmt* S) { |
| 110 | if (S) { |
| 111 | BuildParentMap(*(MapTy*) Impl, S); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void ParentMap::setParent(const Stmt *S, const Stmt *Parent) { |
| 116 | assert(S); |
| 117 | assert(Parent); |
| 118 | MapTy *M = reinterpret_cast<MapTy *>(Impl); |
| 119 | M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent))); |
| 120 | } |
| 121 | |
| 122 | Stmt* ParentMap::getParent(Stmt* S) const { |
| 123 | MapTy* M = (MapTy*) Impl; |
| 124 | MapTy::iterator I = M->find(S); |
| 125 | return I == M->end() ? nullptr : I->second; |
| 126 | } |
| 127 | |
| 128 | Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const { |
| 129 | do { S = getParent(S); } while (S && isa<ParenExpr>(S)); |
| 130 | return S; |
| 131 | } |
| 132 | |
| 133 | Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const { |
| 134 | do { |
| 135 | S = getParent(S); |
| 136 | } |
| 137 | while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S))); |
| 138 | |
| 139 | return S; |
| 140 | } |
| 141 | |
| 142 | Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const { |
| 143 | do { |
| 144 | S = getParent(S); |
| 145 | } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S); |
| 146 | |
| 147 | return S; |
| 148 | } |
| 149 | |
| 150 | Stmt *ParentMap::getOuterParenParent(Stmt *S) const { |
| 151 | Stmt *Paren = nullptr; |
| 152 | while (isa<ParenExpr>(S)) { |
| 153 | Paren = S; |
| 154 | S = getParent(S); |
| 155 | }; |
| 156 | return Paren; |
| 157 | } |
| 158 | |
| 159 | bool ParentMap::isConsumedExpr(Expr* E) const { |
| 160 | Stmt *P = getParent(E); |
| 161 | Stmt *DirectChild = E; |
| 162 | |
| 163 | |
| 164 | while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P) || |
| 165 | isa<FullExpr>(P))) { |
| 166 | DirectChild = P; |
| 167 | P = getParent(P); |
| 168 | } |
| 169 | |
| 170 | if (!P) |
| 171 | return false; |
| 172 | |
| 173 | switch (P->getStmtClass()) { |
| 174 | default: |
| 175 | return isa<Expr>(P); |
| 176 | case Stmt::DeclStmtClass: |
| 177 | return true; |
| 178 | case Stmt::BinaryOperatorClass: { |
| 179 | BinaryOperator *BE = cast<BinaryOperator>(P); |
| 180 | |
| 181 | |
| 182 | return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS(); |
| 183 | } |
| 184 | case Stmt::ForStmtClass: |
| 185 | return DirectChild == cast<ForStmt>(P)->getCond(); |
| 186 | case Stmt::WhileStmtClass: |
| 187 | return DirectChild == cast<WhileStmt>(P)->getCond(); |
| 188 | case Stmt::DoStmtClass: |
| 189 | return DirectChild == cast<DoStmt>(P)->getCond(); |
| 190 | case Stmt::IfStmtClass: |
| 191 | return DirectChild == cast<IfStmt>(P)->getCond(); |
| 192 | case Stmt::IndirectGotoStmtClass: |
| 193 | return DirectChild == cast<IndirectGotoStmt>(P)->getTarget(); |
| 194 | case Stmt::SwitchStmtClass: |
| 195 | return DirectChild == cast<SwitchStmt>(P)->getCond(); |
| 196 | case Stmt::ObjCForCollectionStmtClass: |
| 197 | return DirectChild == cast<ObjCForCollectionStmt>(P)->getCollection(); |
| 198 | case Stmt::ReturnStmtClass: |
| 199 | return true; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | |