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