DeclMatcher.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //===- unittest/AST/DeclMatcher.h - AST unit test support ---------------===//
  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. #ifndef LLVM_CLANG_UNITTESTS_AST_DECLMATCHER_H
  9. #define LLVM_CLANG_UNITTESTS_AST_DECLMATCHER_H
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. namespace clang {
  12. namespace ast_matchers {
  13. enum class DeclMatcherKind { First, Last };
  14. // Matcher class to retrieve the first/last matched node under a given AST.
  15. template <typename NodeType, DeclMatcherKind MatcherKind>
  16. class DeclMatcher : public MatchFinder::MatchCallback {
  17. NodeType *Node = nullptr;
  18. void run(const MatchFinder::MatchResult &Result) override {
  19. if ((MatcherKind == DeclMatcherKind::First && Node == nullptr) ||
  20. MatcherKind == DeclMatcherKind::Last) {
  21. Node = const_cast<NodeType *>(Result.Nodes.getNodeAs<NodeType>(""));
  22. }
  23. }
  24. public:
  25. // Returns the first/last matched node under the tree rooted in `D`.
  26. template <typename MatcherType>
  27. NodeType *match(const Decl *D, const MatcherType &AMatcher) {
  28. MatchFinder Finder;
  29. Finder.addMatcher(AMatcher.bind(""), this);
  30. Finder.matchAST(D->getASTContext());
  31. assert(Node);
  32. return Node;
  33. }
  34. };
  35. template <typename NodeType>
  36. using LastDeclMatcher = DeclMatcher<NodeType, DeclMatcherKind::Last>;
  37. template <typename NodeType>
  38. using FirstDeclMatcher = DeclMatcher<NodeType, DeclMatcherKind::First>;
  39. template <typename NodeType>
  40. class DeclCounterWithPredicate : public MatchFinder::MatchCallback {
  41. using UnaryPredicate = std::function<bool(const NodeType *)>;
  42. UnaryPredicate Predicate;
  43. unsigned Count = 0;
  44. void run(const MatchFinder::MatchResult &Result) override {
  45. if (auto N = Result.Nodes.getNodeAs<NodeType>("")) {
  46. if (Predicate(N))
  47. ++Count;
  48. }
  49. }
  50. public:
  51. DeclCounterWithPredicate()
  52. : Predicate([](const NodeType *) { return true; }) {}
  53. DeclCounterWithPredicate(UnaryPredicate P) : Predicate(P) {}
  54. // Returns the number of matched nodes which satisfy the predicate under the
  55. // tree rooted in `D`.
  56. template <typename MatcherType>
  57. unsigned match(const Decl *D, const MatcherType &AMatcher) {
  58. MatchFinder Finder;
  59. Finder.addMatcher(AMatcher.bind(""), this);
  60. Finder.matchAST(D->getASTContext());
  61. return Count;
  62. }
  63. };
  64. template <typename NodeType>
  65. using DeclCounter = DeclCounterWithPredicate<NodeType>;
  66. } // end namespace ast_matchers
  67. } // end namespace clang
  68. #endif