ASTMatchersMacros.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- ASTMatchersMacros.h - Structural query framework -------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Defines macros that enable us to define new matchers in a single place.
  15. // Since a matcher is a function which returns a Matcher<T> object, where
  16. // T is the type of the actual implementation of the matcher, the macros allow
  17. // us to write matchers like functions and take care of the definition of the
  18. // class boilerplate.
  19. //
  20. // Note that when you define a matcher with an AST_MATCHER* macro, only the
  21. // function which creates the matcher goes into the current namespace - the
  22. // class that implements the actual matcher, which gets returned by the
  23. // generator function, is put into the 'internal' namespace. This allows us
  24. // to only have the functions (which is all the user cares about) in the
  25. // 'ast_matchers' namespace and hide the boilerplate.
  26. //
  27. // To define a matcher in user code, put it into your own namespace. This would
  28. // help to prevent ODR violations in case a matcher with the same name is
  29. // defined in multiple translation units:
  30. //
  31. // namespace my_matchers {
  32. // AST_MATCHER_P(clang::MemberExpr, Member,
  33. // clang::ast_matchers::internal::Matcher<clang::ValueDecl>,
  34. // InnerMatcher) {
  35. // return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder);
  36. // }
  37. // } // namespace my_matchers
  38. //
  39. // Alternatively, an unnamed namespace may be used:
  40. //
  41. // namespace clang {
  42. // namespace ast_matchers {
  43. // namespace {
  44. // AST_MATCHER_P(MemberExpr, Member,
  45. // internal::Matcher<ValueDecl>, InnerMatcher) {
  46. // return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder);
  47. // }
  48. // } // namespace
  49. // } // namespace ast_matchers
  50. // } // namespace clang
  51. //
  52. //===----------------------------------------------------------------------===//
  53. #ifndef LLVM_CLANG_ASTMATCHERS_ASTMATCHERSMACROS_H
  54. #define LLVM_CLANG_ASTMATCHERS_ASTMATCHERSMACROS_H
  55. /// AST_MATCHER_FUNCTION(ReturnType, DefineMatcher) { ... }
  56. /// defines a zero parameter function named DefineMatcher() that returns a
  57. /// ReturnType object.
  58. #define AST_MATCHER_FUNCTION(ReturnType, DefineMatcher) \
  59. inline ReturnType DefineMatcher##_getInstance(); \
  60. inline ReturnType DefineMatcher() { \
  61. return ::clang::ast_matchers::internal::MemoizedMatcher< \
  62. ReturnType, DefineMatcher##_getInstance>::getInstance(); \
  63. } \
  64. inline ReturnType DefineMatcher##_getInstance()
  65. /// AST_MATCHER_FUNCTION_P(ReturnType, DefineMatcher, ParamType, Param) {
  66. /// ... }
  67. /// defines a single-parameter function named DefineMatcher() that returns a
  68. /// ReturnType object.
  69. ///
  70. /// The code between the curly braces has access to the following variables:
  71. ///
  72. /// Param: the parameter passed to the function; its type
  73. /// is ParamType.
  74. ///
  75. /// The code should return an instance of ReturnType.
  76. #define AST_MATCHER_FUNCTION_P(ReturnType, DefineMatcher, ParamType, Param) \
  77. AST_MATCHER_FUNCTION_P_OVERLOAD(ReturnType, DefineMatcher, ParamType, Param, \
  78. 0)
  79. #define AST_MATCHER_FUNCTION_P_OVERLOAD(ReturnType, DefineMatcher, ParamType, \
  80. Param, OverloadId) \
  81. inline ReturnType DefineMatcher(ParamType const &Param); \
  82. typedef ReturnType (&DefineMatcher##_Type##OverloadId)(ParamType const &); \
  83. inline ReturnType DefineMatcher(ParamType const &Param)
  84. /// AST_MATCHER(Type, DefineMatcher) { ... }
  85. /// defines a zero parameter function named DefineMatcher() that returns a
  86. /// Matcher<Type> object.
  87. ///
  88. /// The code between the curly braces has access to the following variables:
  89. ///
  90. /// Node: the AST node being matched; its type is Type.
  91. /// Finder: an ASTMatchFinder*.
  92. /// Builder: a BoundNodesTreeBuilder*.
  93. ///
  94. /// The code should return true if 'Node' matches.
  95. #define AST_MATCHER(Type, DefineMatcher) \
  96. namespace internal { \
  97. class matcher_##DefineMatcher##Matcher \
  98. : public ::clang::ast_matchers::internal::MatcherInterface<Type> { \
  99. public: \
  100. explicit matcher_##DefineMatcher##Matcher() = default; \
  101. bool matches(const Type &Node, \
  102. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  103. ::clang::ast_matchers::internal::BoundNodesTreeBuilder \
  104. *Builder) const override; \
  105. }; \
  106. } \
  107. inline ::clang::ast_matchers::internal::Matcher<Type> DefineMatcher() { \
  108. return ::clang::ast_matchers::internal::makeMatcher( \
  109. new internal::matcher_##DefineMatcher##Matcher()); \
  110. } \
  111. inline bool internal::matcher_##DefineMatcher##Matcher::matches( \
  112. const Type &Node, \
  113. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  114. ::clang::ast_matchers::internal::BoundNodesTreeBuilder *Builder) const
  115. /// AST_MATCHER_P(Type, DefineMatcher, ParamType, Param) { ... }
  116. /// defines a single-parameter function named DefineMatcher() that returns a
  117. /// Matcher<Type> object.
  118. ///
  119. /// The code between the curly braces has access to the following variables:
  120. ///
  121. /// Node: the AST node being matched; its type is Type.
  122. /// Param: the parameter passed to the function; its type
  123. /// is ParamType.
  124. /// Finder: an ASTMatchFinder*.
  125. /// Builder: a BoundNodesTreeBuilder*.
  126. ///
  127. /// The code should return true if 'Node' matches.
  128. #define AST_MATCHER_P(Type, DefineMatcher, ParamType, Param) \
  129. AST_MATCHER_P_OVERLOAD(Type, DefineMatcher, ParamType, Param, 0)
  130. #define AST_MATCHER_P_OVERLOAD(Type, DefineMatcher, ParamType, Param, \
  131. OverloadId) \
  132. namespace internal { \
  133. class matcher_##DefineMatcher##OverloadId##Matcher \
  134. : public ::clang::ast_matchers::internal::MatcherInterface<Type> { \
  135. public: \
  136. explicit matcher_##DefineMatcher##OverloadId##Matcher( \
  137. ParamType const &A##Param) \
  138. : Param(A##Param) {} \
  139. bool matches(const Type &Node, \
  140. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  141. ::clang::ast_matchers::internal::BoundNodesTreeBuilder \
  142. *Builder) const override; \
  143. \
  144. private: \
  145. ParamType Param; \
  146. }; \
  147. } \
  148. inline ::clang::ast_matchers::internal::Matcher<Type> DefineMatcher( \
  149. ParamType const &Param) { \
  150. return ::clang::ast_matchers::internal::makeMatcher( \
  151. new internal::matcher_##DefineMatcher##OverloadId##Matcher(Param)); \
  152. } \
  153. typedef ::clang::ast_matchers::internal::Matcher<Type> ( \
  154. &DefineMatcher##_Type##OverloadId)(ParamType const &Param); \
  155. inline bool internal::matcher_##DefineMatcher##OverloadId##Matcher::matches( \
  156. const Type &Node, \
  157. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  158. ::clang::ast_matchers::internal::BoundNodesTreeBuilder *Builder) const
  159. /// AST_MATCHER_P2(
  160. /// Type, DefineMatcher, ParamType1, Param1, ParamType2, Param2) { ... }
  161. /// defines a two-parameter function named DefineMatcher() that returns a
  162. /// Matcher<Type> object.
  163. ///
  164. /// The code between the curly braces has access to the following variables:
  165. ///
  166. /// Node: the AST node being matched; its type is Type.
  167. /// Param1, Param2: the parameters passed to the function; their types
  168. /// are ParamType1 and ParamType2.
  169. /// Finder: an ASTMatchFinder*.
  170. /// Builder: a BoundNodesTreeBuilder*.
  171. ///
  172. /// The code should return true if 'Node' matches.
  173. #define AST_MATCHER_P2(Type, DefineMatcher, ParamType1, Param1, ParamType2, \
  174. Param2) \
  175. AST_MATCHER_P2_OVERLOAD(Type, DefineMatcher, ParamType1, Param1, ParamType2, \
  176. Param2, 0)
  177. #define AST_MATCHER_P2_OVERLOAD(Type, DefineMatcher, ParamType1, Param1, \
  178. ParamType2, Param2, OverloadId) \
  179. namespace internal { \
  180. class matcher_##DefineMatcher##OverloadId##Matcher \
  181. : public ::clang::ast_matchers::internal::MatcherInterface<Type> { \
  182. public: \
  183. matcher_##DefineMatcher##OverloadId##Matcher(ParamType1 const &A##Param1, \
  184. ParamType2 const &A##Param2) \
  185. : Param1(A##Param1), Param2(A##Param2) {} \
  186. bool matches(const Type &Node, \
  187. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  188. ::clang::ast_matchers::internal::BoundNodesTreeBuilder \
  189. *Builder) const override; \
  190. \
  191. private: \
  192. ParamType1 Param1; \
  193. ParamType2 Param2; \
  194. }; \
  195. } \
  196. inline ::clang::ast_matchers::internal::Matcher<Type> DefineMatcher( \
  197. ParamType1 const &Param1, ParamType2 const &Param2) { \
  198. return ::clang::ast_matchers::internal::makeMatcher( \
  199. new internal::matcher_##DefineMatcher##OverloadId##Matcher(Param1, \
  200. Param2)); \
  201. } \
  202. typedef ::clang::ast_matchers::internal::Matcher<Type> ( \
  203. &DefineMatcher##_Type##OverloadId)(ParamType1 const &Param1, \
  204. ParamType2 const &Param2); \
  205. inline bool internal::matcher_##DefineMatcher##OverloadId##Matcher::matches( \
  206. const Type &Node, \
  207. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  208. ::clang::ast_matchers::internal::BoundNodesTreeBuilder *Builder) const
  209. /// Construct a type-list to be passed to the AST_POLYMORPHIC_MATCHER*
  210. /// macros.
  211. ///
  212. /// You can't pass something like \c TypeList<Foo, Bar> to a macro, because it
  213. /// will look at that as two arguments. However, you can pass
  214. /// \c void(TypeList<Foo, Bar>), which works thanks to the parenthesis.
  215. /// The \c PolymorphicMatcherWithParam* classes will unpack the function type to
  216. /// extract the TypeList object.
  217. #define AST_POLYMORPHIC_SUPPORTED_TYPES(...) \
  218. void(::clang::ast_matchers::internal::TypeList<__VA_ARGS__>)
  219. /// AST_POLYMORPHIC_MATCHER(DefineMatcher) { ... }
  220. /// defines a single-parameter function named DefineMatcher() that is
  221. /// polymorphic in the return type.
  222. ///
  223. /// The variables are the same as for AST_MATCHER, but NodeType will be deduced
  224. /// from the calling context.
  225. #define AST_POLYMORPHIC_MATCHER(DefineMatcher, ReturnTypesF) \
  226. namespace internal { \
  227. template <typename NodeType> \
  228. class matcher_##DefineMatcher##Matcher \
  229. : public ::clang::ast_matchers::internal::MatcherInterface<NodeType> { \
  230. public: \
  231. bool matches(const NodeType &Node, \
  232. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  233. ::clang::ast_matchers::internal::BoundNodesTreeBuilder \
  234. *Builder) const override; \
  235. }; \
  236. } \
  237. inline ::clang::ast_matchers::internal::PolymorphicMatcher< \
  238. internal::matcher_##DefineMatcher##Matcher, ReturnTypesF> \
  239. DefineMatcher() { \
  240. return ::clang::ast_matchers::internal::PolymorphicMatcher< \
  241. internal::matcher_##DefineMatcher##Matcher, ReturnTypesF>(); \
  242. } \
  243. template <typename NodeType> \
  244. bool internal::matcher_##DefineMatcher##Matcher<NodeType>::matches( \
  245. const NodeType &Node, \
  246. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  247. ::clang::ast_matchers::internal::BoundNodesTreeBuilder *Builder) const
  248. /// AST_POLYMORPHIC_MATCHER_P(DefineMatcher, ParamType, Param) { ... }
  249. /// defines a single-parameter function named DefineMatcher() that is
  250. /// polymorphic in the return type.
  251. ///
  252. /// The variables are the same as for
  253. /// AST_MATCHER_P, with the addition of NodeType, which specifies the node type
  254. /// of the matcher Matcher<NodeType> returned by the function matcher().
  255. ///
  256. /// FIXME: Pull out common code with above macro?
  257. #define AST_POLYMORPHIC_MATCHER_P(DefineMatcher, ReturnTypesF, ParamType, \
  258. Param) \
  259. AST_POLYMORPHIC_MATCHER_P_OVERLOAD(DefineMatcher, ReturnTypesF, ParamType, \
  260. Param, 0)
  261. #define AST_POLYMORPHIC_MATCHER_P_OVERLOAD(DefineMatcher, ReturnTypesF, \
  262. ParamType, Param, OverloadId) \
  263. namespace internal { \
  264. template <typename NodeType, typename ParamT> \
  265. class matcher_##DefineMatcher##OverloadId##Matcher \
  266. : public ::clang::ast_matchers::internal::MatcherInterface<NodeType> { \
  267. public: \
  268. explicit matcher_##DefineMatcher##OverloadId##Matcher( \
  269. ParamType const &A##Param) \
  270. : Param(A##Param) {} \
  271. bool matches(const NodeType &Node, \
  272. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  273. ::clang::ast_matchers::internal::BoundNodesTreeBuilder \
  274. *Builder) const override; \
  275. \
  276. private: \
  277. ParamType Param; \
  278. }; \
  279. } \
  280. inline ::clang::ast_matchers::internal::PolymorphicMatcher< \
  281. internal::matcher_##DefineMatcher##OverloadId##Matcher, ReturnTypesF, \
  282. ParamType> \
  283. DefineMatcher(ParamType const &Param) { \
  284. return ::clang::ast_matchers::internal::PolymorphicMatcher< \
  285. internal::matcher_##DefineMatcher##OverloadId##Matcher, ReturnTypesF, \
  286. ParamType>(Param); \
  287. } \
  288. typedef ::clang::ast_matchers::internal::PolymorphicMatcher< \
  289. internal::matcher_##DefineMatcher##OverloadId##Matcher, ReturnTypesF, \
  290. ParamType> (&DefineMatcher##_Type##OverloadId)(ParamType const &Param); \
  291. template <typename NodeType, typename ParamT> \
  292. bool internal:: \
  293. matcher_##DefineMatcher##OverloadId##Matcher<NodeType, ParamT>::matches( \
  294. const NodeType &Node, \
  295. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  296. ::clang::ast_matchers::internal::BoundNodesTreeBuilder *Builder) \
  297. const
  298. /// AST_POLYMORPHIC_MATCHER_P2(
  299. /// DefineMatcher, ParamType1, Param1, ParamType2, Param2) { ... }
  300. /// defines a two-parameter function named matcher() that is polymorphic in
  301. /// the return type.
  302. ///
  303. /// The variables are the same as for AST_MATCHER_P2, with the
  304. /// addition of NodeType, which specifies the node type of the matcher
  305. /// Matcher<NodeType> returned by the function DefineMatcher().
  306. #define AST_POLYMORPHIC_MATCHER_P2(DefineMatcher, ReturnTypesF, ParamType1, \
  307. Param1, ParamType2, Param2) \
  308. AST_POLYMORPHIC_MATCHER_P2_OVERLOAD(DefineMatcher, ReturnTypesF, ParamType1, \
  309. Param1, ParamType2, Param2, 0)
  310. #define AST_POLYMORPHIC_MATCHER_P2_OVERLOAD(DefineMatcher, ReturnTypesF, \
  311. ParamType1, Param1, ParamType2, \
  312. Param2, OverloadId) \
  313. namespace internal { \
  314. template <typename NodeType, typename ParamT1, typename ParamT2> \
  315. class matcher_##DefineMatcher##OverloadId##Matcher \
  316. : public ::clang::ast_matchers::internal::MatcherInterface<NodeType> { \
  317. public: \
  318. matcher_##DefineMatcher##OverloadId##Matcher(ParamType1 const &A##Param1, \
  319. ParamType2 const &A##Param2) \
  320. : Param1(A##Param1), Param2(A##Param2) {} \
  321. bool matches(const NodeType &Node, \
  322. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  323. ::clang::ast_matchers::internal::BoundNodesTreeBuilder \
  324. *Builder) const override; \
  325. \
  326. private: \
  327. ParamType1 Param1; \
  328. ParamType2 Param2; \
  329. }; \
  330. } \
  331. inline ::clang::ast_matchers::internal::PolymorphicMatcher< \
  332. internal::matcher_##DefineMatcher##OverloadId##Matcher, ReturnTypesF, \
  333. ParamType1, ParamType2> \
  334. DefineMatcher(ParamType1 const &Param1, ParamType2 const &Param2) { \
  335. return ::clang::ast_matchers::internal::PolymorphicMatcher< \
  336. internal::matcher_##DefineMatcher##OverloadId##Matcher, ReturnTypesF, \
  337. ParamType1, ParamType2>(Param1, Param2); \
  338. } \
  339. typedef ::clang::ast_matchers::internal::PolymorphicMatcher< \
  340. internal::matcher_##DefineMatcher##OverloadId##Matcher, ReturnTypesF, \
  341. ParamType1, ParamType2> (&DefineMatcher##_Type##OverloadId)( \
  342. ParamType1 const &Param1, ParamType2 const &Param2); \
  343. template <typename NodeType, typename ParamT1, typename ParamT2> \
  344. bool internal::matcher_##DefineMatcher##OverloadId##Matcher< \
  345. NodeType, ParamT1, ParamT2>:: \
  346. matches(const NodeType &Node, \
  347. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  348. ::clang::ast_matchers::internal::BoundNodesTreeBuilder *Builder) \
  349. const
  350. // FIXME: add a matcher for TypeLoc derived classes using its custom casting
  351. // API (no longer dyn_cast) if/when we need such matching
  352. #define AST_TYPE_TRAVERSE_MATCHER_DECL(MatcherName, FunctionName, \
  353. ReturnTypesF) \
  354. namespace internal { \
  355. template <typename T> struct TypeMatcher##MatcherName##Getter { \
  356. static QualType (T::*value())() const { return &T::FunctionName; } \
  357. }; \
  358. } \
  359. extern const ::clang::ast_matchers::internal:: \
  360. TypeTraversePolymorphicMatcher< \
  361. QualType, \
  362. ::clang::ast_matchers::internal::TypeMatcher##MatcherName##Getter, \
  363. ::clang::ast_matchers::internal::TypeTraverseMatcher, \
  364. ReturnTypesF>::Func MatcherName
  365. #define AST_TYPE_TRAVERSE_MATCHER_DEF(MatcherName, ReturnTypesF) \
  366. const ::clang::ast_matchers::internal::TypeTraversePolymorphicMatcher< \
  367. QualType, \
  368. ::clang::ast_matchers::internal::TypeMatcher##MatcherName##Getter, \
  369. ::clang::ast_matchers::internal::TypeTraverseMatcher, \
  370. ReturnTypesF>::Func MatcherName
  371. /// AST_TYPE_TRAVERSE_MATCHER(MatcherName, FunctionName) defines
  372. /// the matcher \c MatcherName that can be used to traverse from one \c Type
  373. /// to another.
  374. ///
  375. /// For a specific \c SpecificType, the traversal is done using
  376. /// \c SpecificType::FunctionName. The existence of such a function determines
  377. /// whether a corresponding matcher can be used on \c SpecificType.
  378. #define AST_TYPE_TRAVERSE_MATCHER(MatcherName, FunctionName, ReturnTypesF) \
  379. namespace internal { \
  380. template <typename T> struct TypeMatcher##MatcherName##Getter { \
  381. static QualType (T::*value())() const { return &T::FunctionName; } \
  382. }; \
  383. } \
  384. const ::clang::ast_matchers::internal::TypeTraversePolymorphicMatcher< \
  385. QualType, \
  386. ::clang::ast_matchers::internal::TypeMatcher##MatcherName##Getter, \
  387. ::clang::ast_matchers::internal::TypeTraverseMatcher, \
  388. ReturnTypesF>::Func MatcherName
  389. #define AST_TYPELOC_TRAVERSE_MATCHER_DECL(MatcherName, FunctionName, \
  390. ReturnTypesF) \
  391. namespace internal { \
  392. template <typename T> struct TypeLocMatcher##MatcherName##Getter { \
  393. static TypeLoc (T::*value())() const { return &T::FunctionName##Loc; } \
  394. }; \
  395. } \
  396. extern const ::clang::ast_matchers::internal:: \
  397. TypeTraversePolymorphicMatcher< \
  398. TypeLoc, \
  399. ::clang::ast_matchers::internal:: \
  400. TypeLocMatcher##MatcherName##Getter, \
  401. ::clang::ast_matchers::internal::TypeLocTraverseMatcher, \
  402. ReturnTypesF>::Func MatcherName##Loc; \
  403. AST_TYPE_TRAVERSE_MATCHER_DECL(MatcherName, FunctionName##Type, ReturnTypesF)
  404. #define AST_TYPELOC_TRAVERSE_MATCHER_DEF(MatcherName, ReturnTypesF) \
  405. const ::clang::ast_matchers::internal::TypeTraversePolymorphicMatcher< \
  406. TypeLoc, \
  407. ::clang::ast_matchers::internal::TypeLocMatcher##MatcherName##Getter, \
  408. ::clang::ast_matchers::internal::TypeLocTraverseMatcher, \
  409. ReturnTypesF>::Func MatcherName##Loc; \
  410. AST_TYPE_TRAVERSE_MATCHER_DEF(MatcherName, ReturnTypesF)
  411. /// AST_TYPELOC_TRAVERSE_MATCHER(MatcherName, FunctionName) works
  412. /// identical to \c AST_TYPE_TRAVERSE_MATCHER but operates on \c TypeLocs.
  413. #define AST_TYPELOC_TRAVERSE_MATCHER(MatcherName, FunctionName, ReturnTypesF) \
  414. namespace internal { \
  415. template <typename T> struct TypeLocMatcher##MatcherName##Getter { \
  416. static TypeLoc (T::*value())() const { return &T::FunctionName##Loc; } \
  417. }; \
  418. } \
  419. const ::clang::ast_matchers::internal::TypeTraversePolymorphicMatcher< \
  420. TypeLoc, \
  421. ::clang::ast_matchers::internal::TypeLocMatcher##MatcherName##Getter, \
  422. ::clang::ast_matchers::internal::TypeLocTraverseMatcher, \
  423. ReturnTypesF>::Func MatcherName##Loc; \
  424. AST_TYPE_TRAVERSE_MATCHER(MatcherName, FunctionName##Type, ReturnTypesF)
  425. /// AST_MATCHER_REGEX(Type, DefineMatcher, Param) { ... }
  426. /// defines a function named DefineMatcher() that takes a regular expression
  427. /// string paramater and an optional RegexFlags parameter and returns a
  428. /// Matcher<Type> object.
  429. ///
  430. /// The code between the curly braces has access to the following variables:
  431. ///
  432. /// Node: the AST node being matched; its type is Type.
  433. /// Param: a pointer to an \ref llvm::Regex object
  434. /// Finder: an ASTMatchFinder*.
  435. /// Builder: a BoundNodesTreeBuilder*.
  436. ///
  437. /// The code should return true if 'Node' matches.
  438. #define AST_MATCHER_REGEX(Type, DefineMatcher, Param) \
  439. AST_MATCHER_REGEX_OVERLOAD(Type, DefineMatcher, Param, 0)
  440. #define AST_MATCHER_REGEX_OVERLOAD(Type, DefineMatcher, Param, OverloadId) \
  441. namespace internal { \
  442. class matcher_##DefineMatcher##OverloadId##Matcher \
  443. : public ::clang::ast_matchers::internal::MatcherInterface<Type> { \
  444. public: \
  445. explicit matcher_##DefineMatcher##OverloadId##Matcher( \
  446. std::shared_ptr<llvm::Regex> RE) \
  447. : Param(std::move(RE)) {} \
  448. bool matches(const Type &Node, \
  449. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  450. ::clang::ast_matchers::internal::BoundNodesTreeBuilder \
  451. *Builder) const override; \
  452. \
  453. private: \
  454. std::shared_ptr<llvm::Regex> Param; \
  455. }; \
  456. } \
  457. inline ::clang::ast_matchers::internal::Matcher<Type> DefineMatcher( \
  458. llvm::StringRef Param, llvm::Regex::RegexFlags RegexFlags) { \
  459. return ::clang::ast_matchers::internal::makeMatcher( \
  460. new internal::matcher_##DefineMatcher##OverloadId##Matcher( \
  461. ::clang::ast_matchers::internal::createAndVerifyRegex( \
  462. Param, RegexFlags, #DefineMatcher))); \
  463. } \
  464. inline ::clang::ast_matchers::internal::Matcher<Type> DefineMatcher( \
  465. llvm::StringRef Param) { \
  466. return DefineMatcher(Param, llvm::Regex::NoFlags); \
  467. } \
  468. \
  469. typedef ::clang::ast_matchers::internal::Matcher<Type> ( \
  470. &DefineMatcher##_Type##OverloadId##Flags)(llvm::StringRef, \
  471. llvm::Regex::RegexFlags); \
  472. typedef ::clang::ast_matchers::internal::Matcher<Type> ( \
  473. &DefineMatcher##_Type##OverloadId)(llvm::StringRef); \
  474. inline bool internal::matcher_##DefineMatcher##OverloadId##Matcher::matches( \
  475. const Type &Node, \
  476. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  477. ::clang::ast_matchers::internal::BoundNodesTreeBuilder *Builder) const
  478. /// AST_POLYMORPHIC_MATCHER_REGEX(DefineMatcher, ReturnTypesF, Param) { ... }
  479. /// defines a function named DefineMatcher() that takes a regular expression
  480. /// string paramater and an optional RegexFlags parameter that is polymorphic in
  481. /// the return type.
  482. ///
  483. /// The variables are the same as for
  484. /// AST_MATCHER_REGEX, with the addition of NodeType, which specifies the node
  485. /// type of the matcher Matcher<NodeType> returned by the function matcher().
  486. #define AST_POLYMORPHIC_MATCHER_REGEX(DefineMatcher, ReturnTypesF, Param) \
  487. AST_POLYMORPHIC_MATCHER_REGEX_OVERLOAD(DefineMatcher, ReturnTypesF, Param, 0)
  488. #define AST_POLYMORPHIC_MATCHER_REGEX_OVERLOAD(DefineMatcher, ReturnTypesF, \
  489. Param, OverloadId) \
  490. namespace internal { \
  491. template <typename NodeType, typename ParamT> \
  492. class matcher_##DefineMatcher##OverloadId##Matcher \
  493. : public ::clang::ast_matchers::internal::MatcherInterface<NodeType> { \
  494. public: \
  495. explicit matcher_##DefineMatcher##OverloadId##Matcher( \
  496. std::shared_ptr<llvm::Regex> RE) \
  497. : Param(std::move(RE)) {} \
  498. bool matches(const NodeType &Node, \
  499. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  500. ::clang::ast_matchers::internal::BoundNodesTreeBuilder \
  501. *Builder) const override; \
  502. \
  503. private: \
  504. std::shared_ptr<llvm::Regex> Param; \
  505. }; \
  506. } \
  507. inline ::clang::ast_matchers::internal::PolymorphicMatcher< \
  508. internal::matcher_##DefineMatcher##OverloadId##Matcher, ReturnTypesF, \
  509. std::shared_ptr<llvm::Regex>> \
  510. DefineMatcher(llvm::StringRef Param, llvm::Regex::RegexFlags RegexFlags) { \
  511. return ::clang::ast_matchers::internal::PolymorphicMatcher< \
  512. internal::matcher_##DefineMatcher##OverloadId##Matcher, ReturnTypesF, \
  513. std::shared_ptr<llvm::Regex>>( \
  514. ::clang::ast_matchers::internal::createAndVerifyRegex( \
  515. Param, RegexFlags, #DefineMatcher)); \
  516. } \
  517. inline ::clang::ast_matchers::internal::PolymorphicMatcher< \
  518. internal::matcher_##DefineMatcher##OverloadId##Matcher, ReturnTypesF, \
  519. std::shared_ptr<llvm::Regex>> \
  520. DefineMatcher(llvm::StringRef Param) { \
  521. return DefineMatcher(Param, llvm::Regex::NoFlags); \
  522. } \
  523. typedef ::clang::ast_matchers::internal::PolymorphicMatcher< \
  524. internal::matcher_##DefineMatcher##OverloadId##Matcher, ReturnTypesF, \
  525. std::shared_ptr<llvm::Regex>> ( \
  526. &DefineMatcher##_Type##OverloadId##Flags)( \
  527. llvm::StringRef Param, llvm::Regex::RegexFlags RegexFlags); \
  528. typedef ::clang::ast_matchers::internal::PolymorphicMatcher< \
  529. internal::matcher_##DefineMatcher##OverloadId##Matcher, ReturnTypesF, \
  530. std::shared_ptr<llvm::Regex>> (&DefineMatcher##_Type##OverloadId)( \
  531. llvm::StringRef Param); \
  532. template <typename NodeType, typename ParamT> \
  533. bool internal:: \
  534. matcher_##DefineMatcher##OverloadId##Matcher<NodeType, ParamT>::matches( \
  535. const NodeType &Node, \
  536. ::clang::ast_matchers::internal::ASTMatchFinder *Finder, \
  537. ::clang::ast_matchers::internal::BoundNodesTreeBuilder *Builder) \
  538. const
  539. #endif // LLVM_CLANG_ASTMATCHERS_ASTMATCHERSMACROS_H
  540. #ifdef __GNUC__
  541. #pragma GCC diagnostic pop
  542. #endif