ParentMap.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #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 "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. switch (S->getStmtClass()) {
  27. case Stmt::PseudoObjectExprClass: {
  28. assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
  29. PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S);
  30. // If we are rebuilding the map, clear out any existing state.
  31. if (M[POE->getSyntacticForm()])
  32. for (Stmt *SubStmt : S->children())
  33. M[SubStmt] = nullptr;
  34. M[POE->getSyntacticForm()] = S;
  35. BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent);
  36. for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(),
  37. E = POE->semantics_end();
  38. I != E; ++I) {
  39. M[*I] = S;
  40. BuildParentMap(M, *I, OV_Opaque);
  41. }
  42. break;
  43. }
  44. case Stmt::BinaryConditionalOperatorClass: {
  45. assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
  46. BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S);
  47. M[BCO->getCommon()] = S;
  48. BuildParentMap(M, BCO->getCommon(), OV_Transparent);
  49. M[BCO->getCond()] = S;
  50. BuildParentMap(M, BCO->getCond(), OV_Opaque);
  51. M[BCO->getTrueExpr()] = S;
  52. BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque);
  53. M[BCO->getFalseExpr()] = S;
  54. BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent);
  55. break;
  56. }
  57. case Stmt::OpaqueValueExprClass: {
  58. // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs
  59. // share a single source expression, but in the AST a single
  60. // OpaqueValueExpr is shared among multiple parent expressions.
  61. // The right thing to do is to give the OpaqueValueExpr its syntactic
  62. // parent, then not reassign that when traversing the semantic expressions.
  63. OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S);
  64. if (OVMode == OV_Transparent || !M[OVE->getSourceExpr()]) {
  65. M[OVE->getSourceExpr()] = S;
  66. BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent);
  67. }
  68. break;
  69. }
  70. default:
  71. for (Stmt *SubStmt : S->children()) {
  72. if (SubStmt) {
  73. M[SubStmt] = S;
  74. BuildParentMap(M, SubStmt, OVMode);
  75. }
  76. }
  77. break;
  78. }
  79. }
  80. ParentMap::ParentMap(Stmt *S) : Impl(nullptr) {
  81. if (S) {
  82. MapTy *M = new MapTy();
  83. BuildParentMap(*M, S);
  84. Impl = M;
  85. }
  86. }
  87. ParentMap::~ParentMap() {
  88. delete (MapTy*) Impl;
  89. }
  90. void ParentMap::addStmt(Stmt* S) {
  91. if (S) {
  92. BuildParentMap(*(MapTy*) Impl, S);
  93. }
  94. }
  95. void ParentMap::setParent(const Stmt *S, const Stmt *Parent) {
  96. assert(S);
  97. assert(Parent);
  98. MapTy *M = reinterpret_cast<MapTy *>(Impl);
  99. M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent)));
  100. }
  101. Stmt* ParentMap::getParent(Stmt* S) const {
  102. MapTy* M = (MapTy*) Impl;
  103. MapTy::iterator I = M->find(S);
  104. return I == M->end() ? nullptr : I->second;
  105. }
  106. Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
  107. do { S = getParent(S); } while (S && isa<ParenExpr>(S));
  108. return S;
  109. }
  110. Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
  111. do {
  112. S = getParent(S);
  113. }
  114. while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
  115. return S;
  116. }
  117. Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const {
  118. do {
  119. S = getParent(S);
  120. } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S);
  121. return S;
  122. }
  123. Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
  124. Stmt *Paren = nullptr;
  125. while (isa<ParenExpr>(S)) {
  126. Paren = S;
  127. S = getParent(S);
  128. };
  129. return Paren;
  130. }
  131. bool ParentMap::isConsumedExpr(Expr* E) const {
  132. Stmt *P = getParent(E);
  133. Stmt *DirectChild = E;
  134. // Ignore parents that don't guarantee consumption.
  135. while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P) ||
  136. isa<ExprWithCleanups>(P))) {
  137. DirectChild = P;
  138. P = getParent(P);
  139. }
  140. if (!P)
  141. return false;
  142. switch (P->getStmtClass()) {
  143. default:
  144. return isa<Expr>(P);
  145. case Stmt::DeclStmtClass:
  146. return true;
  147. case Stmt::BinaryOperatorClass: {
  148. BinaryOperator *BE = cast<BinaryOperator>(P);
  149. // If it is a comma, only the right side is consumed.
  150. // If it isn't a comma, both sides are consumed.
  151. return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
  152. }
  153. case Stmt::ForStmtClass:
  154. return DirectChild == cast<ForStmt>(P)->getCond();
  155. case Stmt::WhileStmtClass:
  156. return DirectChild == cast<WhileStmt>(P)->getCond();
  157. case Stmt::DoStmtClass:
  158. return DirectChild == cast<DoStmt>(P)->getCond();
  159. case Stmt::IfStmtClass:
  160. return DirectChild == cast<IfStmt>(P)->getCond();
  161. case Stmt::IndirectGotoStmtClass:
  162. return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
  163. case Stmt::SwitchStmtClass:
  164. return DirectChild == cast<SwitchStmt>(P)->getCond();
  165. case Stmt::ReturnStmtClass:
  166. return true;
  167. }
  168. }