2018-03-31 06:03:29 +08:00
|
|
|
//===- unittest/AST/DeclMatcher.h - AST unit test support ---------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-03-31 06:03:29 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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 <typename NodeType, DeclMatcherKind MatcherKind>
|
|
|
|
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<NodeType *>(Result.Nodes.getNodeAs<NodeType>(""));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
// Returns the first/last matched node under the tree rooted in `D`.
|
|
|
|
template <typename MatcherType>
|
|
|
|
NodeType *match(const Decl *D, const MatcherType &AMatcher) {
|
|
|
|
MatchFinder Finder;
|
|
|
|
Finder.addMatcher(AMatcher.bind(""), this);
|
|
|
|
Finder.matchAST(D->getASTContext());
|
|
|
|
assert(Node);
|
|
|
|
return Node;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <typename NodeType>
|
|
|
|
using LastDeclMatcher = DeclMatcher<NodeType, DeclMatcherKind::Last>;
|
|
|
|
template <typename NodeType>
|
|
|
|
using FirstDeclMatcher = DeclMatcher<NodeType, DeclMatcherKind::First>;
|
|
|
|
|
|
|
|
template <typename NodeType>
|
2018-05-23 21:53:36 +08:00
|
|
|
class DeclCounterWithPredicate : public MatchFinder::MatchCallback {
|
|
|
|
using UnaryPredicate = std::function<bool(const NodeType *)>;
|
|
|
|
UnaryPredicate Predicate;
|
2018-03-31 06:03:29 +08:00
|
|
|
unsigned Count = 0;
|
|
|
|
void run(const MatchFinder::MatchResult &Result) override {
|
2018-05-23 21:53:36 +08:00
|
|
|
if (auto N = Result.Nodes.getNodeAs<NodeType>("")) {
|
|
|
|
if (Predicate(N))
|
2018-03-31 06:03:29 +08:00
|
|
|
++Count;
|
2018-05-23 21:53:36 +08:00
|
|
|
}
|
2018-03-31 06:03:29 +08:00
|
|
|
}
|
2018-05-23 21:53:36 +08:00
|
|
|
|
2018-03-31 06:03:29 +08:00
|
|
|
public:
|
2018-05-23 21:53:36 +08:00
|
|
|
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`.
|
2018-03-31 06:03:29 +08:00
|
|
|
template <typename MatcherType>
|
|
|
|
unsigned match(const Decl *D, const MatcherType &AMatcher) {
|
|
|
|
MatchFinder Finder;
|
|
|
|
Finder.addMatcher(AMatcher.bind(""), this);
|
|
|
|
Finder.matchAST(D->getASTContext());
|
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-23 21:53:36 +08:00
|
|
|
template <typename NodeType>
|
|
|
|
using DeclCounter = DeclCounterWithPredicate<NodeType>;
|
|
|
|
|
2018-03-31 06:03:29 +08:00
|
|
|
} // end namespace ast_matchers
|
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
#endif
|