ParentMap.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //===--- ParentMap.cpp - Mappings from Stmts to their Parents ---*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the ParentMap class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/AST/ParentMap.h"
  13. #include "clang/AST/Decl.h"
  14. #include "clang/AST/Expr.h"
  15. #include "clang/AST/ExprCXX.h"
  16. #include "clang/AST/StmtObjC.h"
  17. #include "llvm/ADT/DenseMap.h"
  18. using namespace clang;
  19. typedef llvm::DenseMap<Stmt*, Stmt*> MapTy;
  20. enum OpaqueValueMode {
  21. OV_Transparent,
  22. OV_Opaque
  23. };
  24. static void BuildParentMap(MapTy& M, Stmt* S,
  25. OpaqueValueMode OVMode = OV_Transparent) {
  26. if (!S)
  27. return;
  28. switch (S->getStmtClass()) {
  29. case Stmt::PseudoObjectExprClass: {
  30. PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S);
  31. if (OVMode == OV_Opaque && M[POE->getSyntacticForm()])
  32. break;
  33. // If we are rebuilding the map, clear out any existing state.
  34. if (M[POE->getSyntacticForm()])
  35. for (Stmt *SubStmt : S->children())
  36. M[SubStmt] = nullptr;
  37. M[POE->getSyntacticForm()] = S;
  38. BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent);
  39. for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(),
  40. E = POE->semantics_end();
  41. I != E; ++I) {
  42. M[*I] = S;
  43. BuildParentMap(M, *I, OV_Opaque);
  44. }
  45. break;
  46. }
  47. case Stmt::BinaryConditionalOperatorClass: {
  48. assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
  49. BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S);
  50. M[BCO->getCommon()] = S;
  51. BuildParentMap(M, BCO->getCommon(), OV_Transparent);
  52. M[BCO->getCond()] = S;
  53. BuildParentMap(M, BCO->getCond(), OV_Opaque);
  54. M[BCO->getTrueExpr()] = S;
  55. BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque);
  56. M[BCO->getFalseExpr()] = S;
  57. BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent);
  58. break;
  59. }
  60. case Stmt::OpaqueValueExprClass: {
  61. // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs
  62. // share a single source expression, but in the AST a single
  63. // OpaqueValueExpr is shared among multiple parent expressions.
  64. // The right thing to do is to give the OpaqueValueExpr its syntactic
  65. // parent, then not reassign that when traversing the semantic expressions.
  66. OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S);
  67. if (OVMode == OV_Transparent || !M[OVE->getSourceExpr()]) {
  68. M[OVE->getSourceExpr()] = S;
  69. BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent);
  70. }
  71. break;
  72. }
  73. case Stmt::CapturedStmtClass:
  74. for (Stmt *SubStmt : S->children()) {
  75. if (SubStmt) {
  76. M[SubStmt] = S;
  77. BuildParentMap(M, SubStmt, OVMode);
  78. }
  79. }
  80. if (Stmt *SubStmt = cast<CapturedStmt>(S)->getCapturedStmt()) {
  81. M[SubStmt] = S;
  82. BuildParentMap(M, SubStmt, OVMode);
  83. }
  84. break;
  85. default:
  86. for (Stmt *SubStmt : S->children()) {
  87. if (SubStmt) {
  88. M[SubStmt] = S;
  89. BuildParentMap(M, SubStmt, OVMode);
  90. }
  91. }
  92. break;
  93. }
  94. }
  95. ParentMap::ParentMap(Stmt *S) : Impl(nullptr) {
  96. if (S) {
  97. MapTy *M = new MapTy();
  98. BuildParentMap(*M, S);
  99. Impl = M;
  100. }
  101. }
  102. ParentMap::~ParentMap() {
  103. delete (MapTy*) Impl;
  104. }
  105. void ParentMap::addStmt(Stmt* S) {
  106. if (S) {
  107. BuildParentMap(*(MapTy*) Impl, S);
  108. }
  109. }
  110. void ParentMap::setParent(const Stmt *S, const Stmt *Parent) {
  111. assert(S);
  112. assert(Parent);
  113. MapTy *M = reinterpret_cast<MapTy *>(Impl);
  114. M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent)));
  115. }
  116. Stmt* ParentMap::getParent(Stmt* S) const {
  117. MapTy* M = (MapTy*) Impl;
  118. return M->lookup(S);
  119. }
  120. Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
  121. do { S = getParent(S); } while (S && isa<ParenExpr>(S));
  122. return S;
  123. }
  124. Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
  125. do {
  126. S = getParent(S);
  127. }
  128. while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
  129. return S;
  130. }
  131. Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const {
  132. do {
  133. S = getParent(S);
  134. } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S);
  135. return S;
  136. }
  137. Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
  138. Stmt *Paren = nullptr;
  139. while (isa<ParenExpr>(S)) {
  140. Paren = S;
  141. S = getParent(S);
  142. };
  143. return Paren;
  144. }
  145. bool ParentMap::isConsumedExpr(Expr* E) const {
  146. Stmt *P = getParent(E);
  147. Stmt *DirectChild = E;
  148. // Ignore parents that don't guarantee consumption.
  149. while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P) ||
  150. isa<FullExpr>(P))) {
  151. DirectChild = P;
  152. P = getParent(P);
  153. }
  154. if (!P)
  155. return false;
  156. switch (P->getStmtClass()) {
  157. default:
  158. return isa<Expr>(P);
  159. case Stmt::DeclStmtClass:
  160. return true;
  161. case Stmt::BinaryOperatorClass: {
  162. BinaryOperator *BE = cast<BinaryOperator>(P);
  163. // If it is a comma, only the right side is consumed.
  164. // If it isn't a comma, both sides are consumed.
  165. return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
  166. }
  167. case Stmt::ForStmtClass:
  168. return DirectChild == cast<ForStmt>(P)->getCond();
  169. case Stmt::WhileStmtClass:
  170. return DirectChild == cast<WhileStmt>(P)->getCond();
  171. case Stmt::DoStmtClass:
  172. return DirectChild == cast<DoStmt>(P)->getCond();
  173. case Stmt::IfStmtClass:
  174. return DirectChild == cast<IfStmt>(P)->getCond();
  175. case Stmt::IndirectGotoStmtClass:
  176. return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
  177. case Stmt::SwitchStmtClass:
  178. return DirectChild == cast<SwitchStmt>(P)->getCond();
  179. case Stmt::ObjCForCollectionStmtClass:
  180. return DirectChild == cast<ObjCForCollectionStmt>(P)->getCollection();
  181. case Stmt::ReturnStmtClass:
  182. return true;
  183. }
  184. }