ExprSequence.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //===---------- ExprSequence.cpp - clang-tidy -----------------------------===//
  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. #include "ExprSequence.h"
  9. #include "clang/AST/ParentMapContext.h"
  10. #include <optional>
  11. namespace clang::tidy::utils {
  12. // Returns the Stmt nodes that are parents of 'S', skipping any potential
  13. // intermediate non-Stmt nodes.
  14. //
  15. // In almost all cases, this function returns a single parent or no parents at
  16. // all.
  17. //
  18. // The case that a Stmt has multiple parents is rare but does actually occur in
  19. // the parts of the AST that we're interested in. Specifically, InitListExpr
  20. // nodes cause ASTContext::getParent() to return multiple parents for certain
  21. // nodes in their subtree because RecursiveASTVisitor visits both the syntactic
  22. // and semantic forms of InitListExpr, and the parent-child relationships are
  23. // different between the two forms.
  24. static SmallVector<const Stmt *, 1> getParentStmts(const Stmt *S,
  25. ASTContext *Context) {
  26. SmallVector<const Stmt *, 1> Result;
  27. TraversalKindScope RAII(*Context, TK_AsIs);
  28. DynTypedNodeList Parents = Context->getParents(*S);
  29. SmallVector<DynTypedNode, 1> NodesToProcess(Parents.begin(), Parents.end());
  30. while (!NodesToProcess.empty()) {
  31. DynTypedNode Node = NodesToProcess.back();
  32. NodesToProcess.pop_back();
  33. if (const auto *S = Node.get<Stmt>()) {
  34. Result.push_back(S);
  35. } else {
  36. Parents = Context->getParents(Node);
  37. NodesToProcess.append(Parents.begin(), Parents.end());
  38. }
  39. }
  40. return Result;
  41. }
  42. namespace {
  43. bool isDescendantOrEqual(const Stmt *Descendant, const Stmt *Ancestor,
  44. ASTContext *Context) {
  45. if (Descendant == Ancestor)
  46. return true;
  47. for (const Stmt *Parent : getParentStmts(Descendant, Context)) {
  48. if (isDescendantOrEqual(Parent, Ancestor, Context))
  49. return true;
  50. }
  51. return false;
  52. }
  53. } // namespace
  54. ExprSequence::ExprSequence(const CFG *TheCFG, const Stmt *Root,
  55. ASTContext *TheContext)
  56. : Context(TheContext), Root(Root) {
  57. for (const auto &SyntheticStmt : TheCFG->synthetic_stmts()) {
  58. SyntheticStmtSourceMap[SyntheticStmt.first] = SyntheticStmt.second;
  59. }
  60. }
  61. bool ExprSequence::inSequence(const Stmt *Before, const Stmt *After) const {
  62. Before = resolveSyntheticStmt(Before);
  63. After = resolveSyntheticStmt(After);
  64. // If 'After' is in the subtree of the siblings that follow 'Before' in the
  65. // chain of successors, we know that 'After' is sequenced after 'Before'.
  66. for (const Stmt *Successor = getSequenceSuccessor(Before); Successor;
  67. Successor = getSequenceSuccessor(Successor)) {
  68. if (isDescendantOrEqual(After, Successor, Context))
  69. return true;
  70. }
  71. // If 'After' is a parent of 'Before' or is sequenced after one of these
  72. // parents, we know that it is sequenced after 'Before'.
  73. for (const Stmt *Parent : getParentStmts(Before, Context)) {
  74. if (Parent == After || inSequence(Parent, After))
  75. return true;
  76. }
  77. return false;
  78. }
  79. bool ExprSequence::potentiallyAfter(const Stmt *After,
  80. const Stmt *Before) const {
  81. return !inSequence(After, Before);
  82. }
  83. const Stmt *ExprSequence::getSequenceSuccessor(const Stmt *S) const {
  84. for (const Stmt *Parent : getParentStmts(S, Context)) {
  85. // If a statement has multiple parents, make sure we're using the parent
  86. // that lies within the sub-tree under Root.
  87. if (!isDescendantOrEqual(Parent, Root, Context))
  88. continue;
  89. if (const auto *BO = dyn_cast<BinaryOperator>(Parent)) {
  90. // Comma operator: Right-hand side is sequenced after the left-hand side.
  91. if (BO->getLHS() == S && BO->getOpcode() == BO_Comma)
  92. return BO->getRHS();
  93. } else if (const auto *InitList = dyn_cast<InitListExpr>(Parent)) {
  94. // Initializer list: Each initializer clause is sequenced after the
  95. // clauses that precede it.
  96. for (unsigned I = 1; I < InitList->getNumInits(); ++I) {
  97. if (InitList->getInit(I - 1) == S)
  98. return InitList->getInit(I);
  99. }
  100. } else if (const auto *Compound = dyn_cast<CompoundStmt>(Parent)) {
  101. // Compound statement: Each sub-statement is sequenced after the
  102. // statements that precede it.
  103. const Stmt *Previous = nullptr;
  104. for (const auto *Child : Compound->body()) {
  105. if (Previous == S)
  106. return Child;
  107. Previous = Child;
  108. }
  109. } else if (const auto *TheDeclStmt = dyn_cast<DeclStmt>(Parent)) {
  110. // Declaration: Every initializer expression is sequenced after the
  111. // initializer expressions that precede it.
  112. const Expr *PreviousInit = nullptr;
  113. for (const Decl *TheDecl : TheDeclStmt->decls()) {
  114. if (const auto *TheVarDecl = dyn_cast<VarDecl>(TheDecl)) {
  115. if (const Expr *Init = TheVarDecl->getInit()) {
  116. if (PreviousInit == S)
  117. return Init;
  118. PreviousInit = Init;
  119. }
  120. }
  121. }
  122. } else if (const auto *ForRange = dyn_cast<CXXForRangeStmt>(Parent)) {
  123. // Range-based for: Loop variable declaration is sequenced before the
  124. // body. (We need this rule because these get placed in the same
  125. // CFGBlock.)
  126. if (S == ForRange->getLoopVarStmt())
  127. return ForRange->getBody();
  128. } else if (const auto *TheIfStmt = dyn_cast<IfStmt>(Parent)) {
  129. // If statement:
  130. // - Sequence init statement before variable declaration, if present;
  131. // before condition evaluation, otherwise.
  132. // - Sequence variable declaration (along with the expression used to
  133. // initialize it) before the evaluation of the condition.
  134. if (S == TheIfStmt->getInit()) {
  135. if (TheIfStmt->getConditionVariableDeclStmt() != nullptr)
  136. return TheIfStmt->getConditionVariableDeclStmt();
  137. return TheIfStmt->getCond();
  138. }
  139. if (S == TheIfStmt->getConditionVariableDeclStmt())
  140. return TheIfStmt->getCond();
  141. } else if (const auto *TheSwitchStmt = dyn_cast<SwitchStmt>(Parent)) {
  142. // Ditto for switch statements.
  143. if (S == TheSwitchStmt->getInit()) {
  144. if (TheSwitchStmt->getConditionVariableDeclStmt() != nullptr)
  145. return TheSwitchStmt->getConditionVariableDeclStmt();
  146. return TheSwitchStmt->getCond();
  147. }
  148. if (S == TheSwitchStmt->getConditionVariableDeclStmt())
  149. return TheSwitchStmt->getCond();
  150. } else if (const auto *TheWhileStmt = dyn_cast<WhileStmt>(Parent)) {
  151. // While statement: Sequence variable declaration (along with the
  152. // expression used to initialize it) before the evaluation of the
  153. // condition.
  154. if (S == TheWhileStmt->getConditionVariableDeclStmt())
  155. return TheWhileStmt->getCond();
  156. }
  157. }
  158. return nullptr;
  159. }
  160. const Stmt *ExprSequence::resolveSyntheticStmt(const Stmt *S) const {
  161. if (SyntheticStmtSourceMap.count(S))
  162. return SyntheticStmtSourceMap.lookup(S);
  163. return S;
  164. }
  165. StmtToBlockMap::StmtToBlockMap(const CFG *TheCFG, ASTContext *TheContext)
  166. : Context(TheContext) {
  167. for (const auto *B : *TheCFG) {
  168. for (const auto &Elem : *B) {
  169. if (std::optional<CFGStmt> S = Elem.getAs<CFGStmt>())
  170. Map[S->getStmt()] = B;
  171. }
  172. }
  173. }
  174. const CFGBlock *StmtToBlockMap::blockContainingStmt(const Stmt *S) const {
  175. while (!Map.count(S)) {
  176. SmallVector<const Stmt *, 1> Parents = getParentStmts(S, Context);
  177. if (Parents.empty())
  178. return nullptr;
  179. S = Parents[0];
  180. }
  181. return Map.lookup(S);
  182. }
  183. } // namespace clang::tidy::utils