//===- unittest/AST/DeclMatcher.h - AST unit test support ---------------===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_UNITTESTS_AST_DECLMATCHER_H #define LLVM_CLANG_UNITTESTS_AST_DECLMATCHER_H #include "clang/ASTMatchers/ASTMatchFinder.h" namespace clang { namespace ast_matchers { enum class DeclMatcherKind { First, Last }; // Matcher class to retrieve the first/last matched node under a given AST. template class DeclMatcher : public MatchFinder::MatchCallback { NodeType *Node = nullptr; void run(const MatchFinder::MatchResult &Result) override { if ((MatcherKind == DeclMatcherKind::First && Node == nullptr) || MatcherKind == DeclMatcherKind::Last) { Node = const_cast(Result.Nodes.getNodeAs("")); } } public: // Returns the first/last matched node under the tree rooted in `D`. template NodeType *match(const Decl *D, const MatcherType &AMatcher) { MatchFinder Finder; Finder.addMatcher(AMatcher.bind(""), this); Finder.matchAST(D->getASTContext()); assert(Node); return Node; } }; template using LastDeclMatcher = DeclMatcher; template using FirstDeclMatcher = DeclMatcher; template class DeclCounterWithPredicate : public MatchFinder::MatchCallback { using UnaryPredicate = std::function; UnaryPredicate Predicate; unsigned Count = 0; void run(const MatchFinder::MatchResult &Result) override { if (auto N = Result.Nodes.getNodeAs("")) { if (Predicate(N)) ++Count; } } public: DeclCounterWithPredicate() : Predicate([](const NodeType *) { return true; }) {} DeclCounterWithPredicate(UnaryPredicate P) : Predicate(P) {} // Returns the number of matched nodes which satisfy the predicate under the // tree rooted in `D`. template unsigned match(const Decl *D, const MatcherType &AMatcher) { MatchFinder Finder; Finder.addMatcher(AMatcher.bind(""), this); Finder.matchAST(D->getASTContext()); return Count; } }; template using DeclCounter = DeclCounterWithPredicate; } // end namespace ast_matchers } // end namespace clang #endif