Skip to content

Instantly share code, notes, and snippets.

@dougpuob
Created October 27, 2020 14:18
Show Gist options
  • Save dougpuob/f05ba7a711fa83c67ba67877d3895bb2 to your computer and use it in GitHub Desktop.
Save dougpuob/f05ba7a711fa83c67ba67877d3895bb2 to your computer and use it in GitHub Desktop.
/// AST_MATCHER_P(Type, DefineMatcher, ParamType, Param) { ... }
/// defines a single-parameter function named DefineMatcher() that returns a
/// Matcher<Type> object.
///
/// The code between the curly braces has access to the following variables:
///
/// Node: the AST node being matched; its type is Type.
/// Param: the parameter passed to the function; its type
/// is ParamType.
/// Finder: an ASTMatchFinder*.
/// Builder: a BoundNodesTreeBuilder*.
///
/// The code should return true if 'Node' matches.
#define AST_MATCHER_P(Type, DefineMatcher, ParamType, Param) \
AST_MATCHER_P_OVERLOAD(Type, DefineMatcher, ParamType, Param, 0)
#define AST_MATCHER_P_OVERLOAD(Type, DefineMatcher, ParamType, Param, \
OverloadId) \
namespace internal { \
class matcher_##DefineMatcher##OverloadId##Matcher \
: public ::clang::ast_matchers::internal::MatcherInterface<Type> { \
public: \
explicit matcher_##DefineMatcher##OverloadId##Matcher(ParamType A##Param) \
: Param(std::move(A##Param)) {} \
bool matches(const Type &Node, \
::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
::clang::ast_matchers::internal::BoundNodesTreeBuilder \
*Builder) const override; \
\
private: \
ParamType const Param; \
}; \
} \
inline ::clang::ast_matchers::internal::Matcher<Type> DefineMatcher( \
ParamType Param) { \
return ::clang::ast_matchers::internal::makeMatcher( \
new internal::matcher_##DefineMatcher##OverloadId##Matcher( \
std::move(Param))); \
} \
typedef ::clang::ast_matchers::internal::Matcher<Type> ( \
&DefineMatcher##_Type##OverloadId)(ParamType Param); \
inline bool internal::matcher_##DefineMatcher##OverloadId##Matcher::matches( \
const Type &Node, \
::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
::clang::ast_matchers::internal::BoundNodesTreeBuilder *Builder) const
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment