ASTMatchersInternal.h revision cfa88f893915ceb8ae4ce2f17c46c24a4d67502f
1//===--- ASTMatchersInternal.h - Structural query framework -----*- C++ -*-===// 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// Implements the base layer of the matcher framework. 11// 12// Matchers are methods that return a Matcher<T> which provides a method 13// Matches(...) which is a predicate on an AST node. The Matches method's 14// parameters define the context of the match, which allows matchers to recurse 15// or store the current node as bound to a specific string, so that it can be 16// retrieved later. 17// 18// In general, matchers have two parts: 19// 1. A function Matcher<T> MatcherName(<arguments>) which returns a Matcher<T> 20// based on the arguments and optionally on template type deduction based 21// on the arguments. Matcher<T>s form an implicit reverse hierarchy 22// to clang's AST class hierarchy, meaning that you can use a Matcher<Base> 23// everywhere a Matcher<Derived> is required. 24// 2. An implementation of a class derived from MatcherInterface<T>. 25// 26// The matcher functions are defined in ASTMatchers.h. To make it possible 27// to implement both the matcher function and the implementation of the matcher 28// interface in one place, ASTMatcherMacros.h defines macros that allow 29// implementing a matcher in a single place. 30// 31// This file contains the base classes needed to construct the actual matchers. 32// 33//===----------------------------------------------------------------------===// 34 35#ifndef LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_INTERNAL_H 36#define LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_INTERNAL_H 37 38#include "clang/AST/Decl.h" 39#include "clang/AST/DeclCXX.h" 40#include "clang/AST/ExprCXX.h" 41#include "clang/AST/Stmt.h" 42#include "clang/AST/StmtCXX.h" 43#include "clang/AST/Type.h" 44#include "clang/ASTMatchers/ASTTypeTraits.h" 45#include "llvm/ADT/VariadicFunction.h" 46#include "llvm/Support/type_traits.h" 47#include <map> 48#include <string> 49#include <vector> 50 51namespace clang { 52namespace ast_matchers { 53 54/// FIXME: Move into the llvm support library. 55template <bool> struct CompileAssert {}; 56#define TOOLING_COMPILE_ASSERT(Expr, Msg) \ 57 typedef CompileAssert<(bool(Expr))> Msg[bool(Expr) ? 1 : -1] 58 59class BoundNodes; 60 61namespace internal { 62 63class BoundNodesTreeBuilder; 64/// \brief Internal version of BoundNodes. Holds all the bound nodes. 65class BoundNodesMap { 66public: 67 /// \brief Adds \c Node to the map with key \c ID. 68 /// 69 /// The node's base type should be in NodeBaseType or it will be unaccessible. 70 template <typename T> 71 void addNode(StringRef ID, const T* Node) { 72 NodeMap[ID] = ast_type_traits::DynTypedNode::create(*Node); 73 } 74 void addNode(StringRef ID, ast_type_traits::DynTypedNode Node) { 75 NodeMap[ID] = Node; 76 } 77 78 /// \brief Returns the AST node bound to \c ID. 79 /// 80 /// Returns NULL if there was no node bound to \c ID or if there is a node but 81 /// it cannot be converted to the specified type. 82 template <typename T> 83 const T *getNodeAs(StringRef ID) const { 84 IDToNodeMap::const_iterator It = NodeMap.find(ID); 85 if (It == NodeMap.end()) { 86 return NULL; 87 } 88 return It->second.get<T>(); 89 } 90 91 /// \brief Copies all ID/Node pairs to BoundNodesTreeBuilder \c Builder. 92 void copyTo(BoundNodesTreeBuilder *Builder) const; 93 94 /// \brief Copies all ID/Node pairs to BoundNodesMap \c Other. 95 void copyTo(BoundNodesMap *Other) const; 96 97private: 98 /// \brief A map from IDs to the bound nodes. 99 typedef std::map<std::string, ast_type_traits::DynTypedNode> IDToNodeMap; 100 101 IDToNodeMap NodeMap; 102}; 103 104/// \brief A tree of bound nodes in match results. 105/// 106/// If a match can contain multiple matches on the same node with different 107/// matching subexpressions, BoundNodesTree contains a branch for each of 108/// those matching subexpressions. 109/// 110/// BoundNodesTree's are created during the matching process; when a match 111/// is found, we iterate over the tree and create a BoundNodes object containing 112/// the union of all bound nodes on the path from the root to a each leaf. 113class BoundNodesTree { 114public: 115 /// \brief A visitor interface to visit all BoundNodes results for a 116 /// BoundNodesTree. 117 class Visitor { 118 public: 119 virtual ~Visitor() {} 120 121 /// \brief Called multiple times during a single call to VisitMatches(...). 122 /// 123 /// 'BoundNodesView' contains the bound nodes for a single match. 124 virtual void visitMatch(const BoundNodes& BoundNodesView) = 0; 125 }; 126 127 BoundNodesTree(); 128 129 /// \brief Create a BoundNodesTree from pre-filled maps of bindings. 130 BoundNodesTree(const BoundNodesMap& Bindings, 131 const std::vector<BoundNodesTree> RecursiveBindings); 132 133 /// \brief Adds all bound nodes to \c Builder. 134 void copyTo(BoundNodesTreeBuilder* Builder) const; 135 136 /// \brief Visits all matches that this BoundNodesTree represents. 137 /// 138 /// The ownership of 'ResultVisitor' remains at the caller. 139 void visitMatches(Visitor* ResultVisitor); 140 141private: 142 void visitMatchesRecursively( 143 Visitor* ResultVistior, 144 const BoundNodesMap& AggregatedBindings); 145 146 // FIXME: Find out whether we want to use different data structures here - 147 // first benchmarks indicate that it doesn't matter though. 148 149 BoundNodesMap Bindings; 150 151 std::vector<BoundNodesTree> RecursiveBindings; 152}; 153 154/// \brief Creates BoundNodesTree objects. 155/// 156/// The tree builder is used during the matching process to insert the bound 157/// nodes from the Id matcher. 158class BoundNodesTreeBuilder { 159public: 160 BoundNodesTreeBuilder(); 161 162 /// \brief Add a binding from an id to a node. 163 template <typename T> 164 void setBinding(const std::string &Id, const T *Node) { 165 Bindings.addNode(Id, Node); 166 } 167 void setBinding(const std::string &Id, ast_type_traits::DynTypedNode Node) { 168 Bindings.addNode(Id, Node); 169 } 170 171 /// \brief Adds a branch in the tree. 172 void addMatch(const BoundNodesTree& Bindings); 173 174 /// \brief Returns a BoundNodes object containing all current bindings. 175 BoundNodesTree build() const; 176 177private: 178 BoundNodesTreeBuilder(const BoundNodesTreeBuilder &) LLVM_DELETED_FUNCTION; 179 void operator=(const BoundNodesTreeBuilder &) LLVM_DELETED_FUNCTION; 180 181 BoundNodesMap Bindings; 182 183 std::vector<BoundNodesTree> RecursiveBindings; 184}; 185 186class ASTMatchFinder; 187 188/// \brief Generic interface for matchers on an AST node of type T. 189/// 190/// Implement this if your matcher may need to inspect the children or 191/// descendants of the node or bind matched nodes to names. If you are 192/// writing a simple matcher that only inspects properties of the 193/// current node and doesn't care about its children or descendants, 194/// implement SingleNodeMatcherInterface instead. 195template <typename T> 196class MatcherInterface : public RefCountedBaseVPTR { 197public: 198 virtual ~MatcherInterface() {} 199 200 /// \brief Returns true if 'Node' can be matched. 201 /// 202 /// May bind 'Node' to an ID via 'Builder', or recurse into 203 /// the AST via 'Finder'. 204 virtual bool matches(const T &Node, 205 ASTMatchFinder *Finder, 206 BoundNodesTreeBuilder *Builder) const = 0; 207}; 208 209/// \brief Interface for matchers that only evaluate properties on a single 210/// node. 211template <typename T> 212class SingleNodeMatcherInterface : public MatcherInterface<T> { 213public: 214 /// \brief Returns true if the matcher matches the provided node. 215 /// 216 /// A subclass must implement this instead of Matches(). 217 virtual bool matchesNode(const T &Node) const = 0; 218 219private: 220 /// Implements MatcherInterface::Matches. 221 virtual bool matches(const T &Node, 222 ASTMatchFinder * /* Finder */, 223 BoundNodesTreeBuilder * /* Builder */) const { 224 return matchesNode(Node); 225 } 226}; 227 228/// \brief Base class for all matchers that works on a \c DynTypedNode. 229/// 230/// Matcher implementations will check whether the \c DynTypedNode is 231/// convertible into the respecitve types and then do the actual match 232/// on the actual node, or return false if it is not convertible. 233class DynTypedMatcher { 234public: 235 virtual ~DynTypedMatcher() {} 236 237 /// \brief Returns true if the matcher matches the given \c DynNode. 238 virtual bool matches(const ast_type_traits::DynTypedNode DynNode, 239 ASTMatchFinder *Finder, 240 BoundNodesTreeBuilder *Builder) const = 0; 241 242 /// \brief Returns a unique ID for the matcher. 243 virtual uint64_t getID() const = 0; 244}; 245 246/// \brief Wrapper of a MatcherInterface<T> *that allows copying. 247/// 248/// A Matcher<Base> can be used anywhere a Matcher<Derived> is 249/// required. This establishes an is-a relationship which is reverse 250/// to the AST hierarchy. In other words, Matcher<T> is contravariant 251/// with respect to T. The relationship is built via a type conversion 252/// operator rather than a type hierarchy to be able to templatize the 253/// type hierarchy instead of spelling it out. 254template <typename T> 255class Matcher : public DynTypedMatcher { 256public: 257 /// \brief Takes ownership of the provided implementation pointer. 258 explicit Matcher(MatcherInterface<T> *Implementation) 259 : Implementation(Implementation) {} 260 261 /// \brief Implicitly converts \c Other to a Matcher<T>. 262 /// 263 /// Requires \c T to be derived from \c From. 264 template <typename From> 265 Matcher(const Matcher<From> &Other, 266 typename llvm::enable_if_c< 267 llvm::is_base_of<From, T>::value && 268 !llvm::is_same<From, T>::value >::type* = 0) 269 : Implementation(new ImplicitCastMatcher<From>(Other)) {} 270 271 /// \brief Implicitly converts \c Matcher<Type> to \c Matcher<QualType>. 272 /// 273 /// The resulting matcher is not strict, i.e. ignores qualifiers. 274 template <typename TypeT> 275 Matcher(const Matcher<TypeT> &Other, 276 typename llvm::enable_if_c< 277 llvm::is_same<T, QualType>::value && 278 llvm::is_same<TypeT, Type>::value >::type* = 0) 279 : Implementation(new TypeToQualType<TypeT>(Other)) {} 280 281 /// \brief Forwards the call to the underlying MatcherInterface<T> pointer. 282 bool matches(const T &Node, 283 ASTMatchFinder *Finder, 284 BoundNodesTreeBuilder *Builder) const { 285 return Implementation->matches(Node, Finder, Builder); 286 } 287 288 /// \brief Returns an ID that uniquely identifies the matcher. 289 uint64_t getID() const { 290 /// FIXME: Document the requirements this imposes on matcher 291 /// implementations (no new() implementation_ during a Matches()). 292 return reinterpret_cast<uint64_t>(Implementation.getPtr()); 293 } 294 295 /// \brief Returns whether the matcher matches on the given \c DynNode. 296 virtual bool matches(const ast_type_traits::DynTypedNode DynNode, 297 ASTMatchFinder *Finder, 298 BoundNodesTreeBuilder *Builder) const { 299 const T *Node = DynNode.get<T>(); 300 if (!Node) return false; 301 return matches(*Node, Finder, Builder); 302 } 303 304 /// \brief Allows the conversion of a \c Matcher<Type> to a \c 305 /// Matcher<QualType>. 306 /// 307 /// Depending on the constructor argument, the matcher is either strict, i.e. 308 /// does only matches in the absence of qualifiers, or not, i.e. simply 309 /// ignores any qualifiers. 310 template <typename TypeT> 311 class TypeToQualType : public MatcherInterface<QualType> { 312 public: 313 TypeToQualType(const Matcher<TypeT> &InnerMatcher) 314 : InnerMatcher(InnerMatcher) {} 315 316 virtual bool matches(const QualType &Node, 317 ASTMatchFinder *Finder, 318 BoundNodesTreeBuilder *Builder) const { 319 if (Node.isNull()) 320 return false; 321 return InnerMatcher.matches(*Node, Finder, Builder); 322 } 323 private: 324 const Matcher<TypeT> InnerMatcher; 325 }; 326 327private: 328 /// \brief Allows conversion from Matcher<Base> to Matcher<T> if T 329 /// is derived from Base. 330 template <typename Base> 331 class ImplicitCastMatcher : public MatcherInterface<T> { 332 public: 333 explicit ImplicitCastMatcher(const Matcher<Base> &From) 334 : From(From) {} 335 336 virtual bool matches(const T &Node, 337 ASTMatchFinder *Finder, 338 BoundNodesTreeBuilder *Builder) const { 339 return From.matches(Node, Finder, Builder); 340 } 341 342 private: 343 const Matcher<Base> From; 344 }; 345 346 IntrusiveRefCntPtr< MatcherInterface<T> > Implementation; 347}; // class Matcher 348 349/// \brief A convenient helper for creating a Matcher<T> without specifying 350/// the template type argument. 351template <typename T> 352inline Matcher<T> makeMatcher(MatcherInterface<T> *Implementation) { 353 return Matcher<T>(Implementation); 354} 355 356/// \brief Matches declarations for QualType and CallExpr. 357/// 358/// Type argument DeclMatcherT is required by PolymorphicMatcherWithParam1 but 359/// not actually used. 360template <typename T, typename DeclMatcherT> 361class HasDeclarationMatcher : public MatcherInterface<T> { 362 TOOLING_COMPILE_ASSERT((llvm::is_same< DeclMatcherT, 363 Matcher<Decl> >::value), 364 instantiated_with_wrong_types); 365public: 366 explicit HasDeclarationMatcher(const Matcher<Decl> &InnerMatcher) 367 : InnerMatcher(InnerMatcher) {} 368 369 virtual bool matches(const T &Node, 370 ASTMatchFinder *Finder, 371 BoundNodesTreeBuilder *Builder) const { 372 return matchesSpecialized(Node, Finder, Builder); 373 } 374 375private: 376 /// \brief Extracts the CXXRecordDecl or EnumDecl of a QualType and returns 377 /// whether the inner matcher matches on it. 378 bool matchesSpecialized(const QualType &Node, ASTMatchFinder *Finder, 379 BoundNodesTreeBuilder *Builder) const { 380 /// FIXME: Add other ways to convert... 381 if (Node.isNull()) 382 return false; 383 if (const EnumType *AsEnum = dyn_cast<EnumType>(Node.getTypePtr())) 384 return matchesDecl(AsEnum->getDecl(), Finder, Builder); 385 return matchesDecl(Node->getAsCXXRecordDecl(), Finder, Builder); 386 } 387 388 /// \brief Extracts the Decl of the callee of a CallExpr and returns whether 389 /// the inner matcher matches on it. 390 bool matchesSpecialized(const CallExpr &Node, ASTMatchFinder *Finder, 391 BoundNodesTreeBuilder *Builder) const { 392 return matchesDecl(Node.getCalleeDecl(), Finder, Builder); 393 } 394 395 /// \brief Extracts the Decl of the constructor call and returns whether the 396 /// inner matcher matches on it. 397 bool matchesSpecialized(const CXXConstructExpr &Node, 398 ASTMatchFinder *Finder, 399 BoundNodesTreeBuilder *Builder) const { 400 return matchesDecl(Node.getConstructor(), Finder, Builder); 401 } 402 403 /// \brief Extracts the \c ValueDecl a \c MemberExpr refers to and returns 404 /// whether the inner matcher matches on it. 405 bool matchesSpecialized(const MemberExpr &Node, 406 ASTMatchFinder *Finder, 407 BoundNodesTreeBuilder *Builder) const { 408 return matchesDecl(Node.getMemberDecl(), Finder, Builder); 409 } 410 411 /// \brief Returns whether the inner matcher \c Node. Returns false if \c Node 412 /// is \c NULL. 413 bool matchesDecl(const Decl *Node, 414 ASTMatchFinder *Finder, 415 BoundNodesTreeBuilder *Builder) const { 416 return Node != NULL && InnerMatcher.matches(*Node, Finder, Builder); 417 } 418 419 const Matcher<Decl> InnerMatcher; 420}; 421 422/// \brief IsBaseType<T>::value is true if T is a "base" type in the AST 423/// node class hierarchies. 424template <typename T> 425struct IsBaseType { 426 static const bool value = 427 (llvm::is_same<T, Decl>::value || 428 llvm::is_same<T, Stmt>::value || 429 llvm::is_same<T, QualType>::value || 430 llvm::is_same<T, Type>::value || 431 llvm::is_same<T, TypeLoc>::value || 432 llvm::is_same<T, NestedNameSpecifier>::value || 433 llvm::is_same<T, NestedNameSpecifierLoc>::value || 434 llvm::is_same<T, CXXCtorInitializer>::value); 435}; 436template <typename T> 437const bool IsBaseType<T>::value; 438 439/// \brief Interface that allows matchers to traverse the AST. 440/// FIXME: Find a better name. 441/// 442/// This provides three entry methods for each base node type in the AST: 443/// - \c matchesChildOf: 444/// Matches a matcher on every child node of the given node. Returns true 445/// if at least one child node could be matched. 446/// - \c matchesDescendantOf: 447/// Matches a matcher on all descendant nodes of the given node. Returns true 448/// if at least one descendant matched. 449/// - \c matchesAncestorOf: 450/// Matches a matcher on all ancestors of the given node. Returns true if 451/// at least one ancestor matched. 452/// 453/// FIXME: Currently we only allow Stmt and Decl nodes to start a traversal. 454/// In the future, we wan to implement this for all nodes for which it makes 455/// sense. In the case of matchesAncestorOf, we'll want to implement it for 456/// all nodes, as all nodes have ancestors. 457class ASTMatchFinder { 458public: 459 /// \brief Defines how we descend a level in the AST when we pass 460 /// through expressions. 461 enum TraversalKind { 462 /// Will traverse any child nodes. 463 TK_AsIs, 464 /// Will not traverse implicit casts and parentheses. 465 TK_IgnoreImplicitCastsAndParentheses 466 }; 467 468 /// \brief Defines how bindings are processed on recursive matches. 469 enum BindKind { 470 /// Stop at the first match and only bind the first match. 471 BK_First, 472 /// Create results for all combinations of bindings that match. 473 BK_All 474 }; 475 476 /// \brief Defines which ancestors are considered for a match. 477 enum AncestorMatchMode { 478 /// All ancestors. 479 AMM_All, 480 /// Direct parent only. 481 AMM_ParentOnly 482 }; 483 484 virtual ~ASTMatchFinder() {} 485 486 /// \brief Returns true if the given class is directly or indirectly derived 487 /// from a base type matching \c base. 488 /// 489 /// A class is considered to be also derived from itself. 490 virtual bool classIsDerivedFrom(const CXXRecordDecl *Declaration, 491 const Matcher<NamedDecl> &Base, 492 BoundNodesTreeBuilder *Builder) = 0; 493 494 template <typename T> 495 bool matchesChildOf(const T &Node, 496 const DynTypedMatcher &Matcher, 497 BoundNodesTreeBuilder *Builder, 498 TraversalKind Traverse, 499 BindKind Bind) { 500 TOOLING_COMPILE_ASSERT( 501 (llvm::is_base_of<Decl, T>::value || 502 llvm::is_base_of<Stmt, T>::value || 503 llvm::is_base_of<NestedNameSpecifier, T>::value || 504 llvm::is_base_of<NestedNameSpecifierLoc, T>::value || 505 llvm::is_base_of<TypeLoc, T>::value || 506 llvm::is_base_of<QualType, T>::value), 507 unsupported_type_for_recursive_matching); 508 return matchesChildOf(ast_type_traits::DynTypedNode::create(Node), 509 Matcher, Builder, Traverse, Bind); 510 } 511 512 template <typename T> 513 bool matchesDescendantOf(const T &Node, 514 const DynTypedMatcher &Matcher, 515 BoundNodesTreeBuilder *Builder, 516 BindKind Bind) { 517 TOOLING_COMPILE_ASSERT( 518 (llvm::is_base_of<Decl, T>::value || 519 llvm::is_base_of<Stmt, T>::value || 520 llvm::is_base_of<NestedNameSpecifier, T>::value || 521 llvm::is_base_of<NestedNameSpecifierLoc, T>::value || 522 llvm::is_base_of<TypeLoc, T>::value || 523 llvm::is_base_of<QualType, T>::value), 524 unsupported_type_for_recursive_matching); 525 return matchesDescendantOf(ast_type_traits::DynTypedNode::create(Node), 526 Matcher, Builder, Bind); 527 } 528 529 // FIXME: Implement support for BindKind. 530 template <typename T> 531 bool matchesAncestorOf(const T &Node, 532 const DynTypedMatcher &Matcher, 533 BoundNodesTreeBuilder *Builder, 534 AncestorMatchMode MatchMode) { 535 TOOLING_COMPILE_ASSERT((llvm::is_base_of<Decl, T>::value || 536 llvm::is_base_of<Stmt, T>::value), 537 only_Decl_or_Stmt_allowed_for_recursive_matching); 538 return matchesAncestorOf(ast_type_traits::DynTypedNode::create(Node), 539 Matcher, Builder, MatchMode); 540 } 541 542 virtual ASTContext &getASTContext() const = 0; 543 544protected: 545 virtual bool matchesChildOf(const ast_type_traits::DynTypedNode &Node, 546 const DynTypedMatcher &Matcher, 547 BoundNodesTreeBuilder *Builder, 548 TraversalKind Traverse, 549 BindKind Bind) = 0; 550 551 virtual bool matchesDescendantOf(const ast_type_traits::DynTypedNode &Node, 552 const DynTypedMatcher &Matcher, 553 BoundNodesTreeBuilder *Builder, 554 BindKind Bind) = 0; 555 556 virtual bool matchesAncestorOf(const ast_type_traits::DynTypedNode &Node, 557 const DynTypedMatcher &Matcher, 558 BoundNodesTreeBuilder *Builder, 559 AncestorMatchMode MatchMode) = 0; 560}; 561 562/// \brief Converts a \c Matcher<T> to a matcher of desired type \c To by 563/// "adapting" a \c To into a \c T. 564/// 565/// The \c ArgumentAdapterT argument specifies how the adaptation is done. 566/// 567/// For example: 568/// \c ArgumentAdaptingMatcher<HasMatcher, T>(InnerMatcher); 569/// Given that \c InnerMatcher is of type \c Matcher<T>, this returns a matcher 570/// that is convertible into any matcher of type \c To by constructing 571/// \c HasMatcher<To, T>(InnerMatcher). 572/// 573/// If a matcher does not need knowledge about the inner type, prefer to use 574/// PolymorphicMatcherWithParam1. 575template <template <typename ToArg, typename FromArg> class ArgumentAdapterT, 576 typename T> 577class ArgumentAdaptingMatcher { 578public: 579 explicit ArgumentAdaptingMatcher(const Matcher<T> &InnerMatcher) 580 : InnerMatcher(InnerMatcher) {} 581 582 template <typename To> 583 operator Matcher<To>() const { 584 return Matcher<To>(new ArgumentAdapterT<To, T>(InnerMatcher)); 585 } 586 587private: 588 const Matcher<T> InnerMatcher; 589}; 590 591/// \brief A PolymorphicMatcherWithParamN<MatcherT, P1, ..., PN> object can be 592/// created from N parameters p1, ..., pN (of type P1, ..., PN) and 593/// used as a Matcher<T> where a MatcherT<T, P1, ..., PN>(p1, ..., pN) 594/// can be constructed. 595/// 596/// For example: 597/// - PolymorphicMatcherWithParam0<IsDefinitionMatcher>() 598/// creates an object that can be used as a Matcher<T> for any type T 599/// where an IsDefinitionMatcher<T>() can be constructed. 600/// - PolymorphicMatcherWithParam1<ValueEqualsMatcher, int>(42) 601/// creates an object that can be used as a Matcher<T> for any type T 602/// where a ValueEqualsMatcher<T, int>(42) can be constructed. 603template <template <typename T> class MatcherT> 604class PolymorphicMatcherWithParam0 { 605public: 606 template <typename T> 607 operator Matcher<T>() const { 608 return Matcher<T>(new MatcherT<T>()); 609 } 610}; 611 612template <template <typename T, typename P1> class MatcherT, 613 typename P1> 614class PolymorphicMatcherWithParam1 { 615public: 616 explicit PolymorphicMatcherWithParam1(const P1 &Param1) 617 : Param1(Param1) {} 618 619 template <typename T> 620 operator Matcher<T>() const { 621 return Matcher<T>(new MatcherT<T, P1>(Param1)); 622 } 623 624private: 625 const P1 Param1; 626}; 627 628template <template <typename T, typename P1, typename P2> class MatcherT, 629 typename P1, typename P2> 630class PolymorphicMatcherWithParam2 { 631public: 632 PolymorphicMatcherWithParam2(const P1 &Param1, const P2 &Param2) 633 : Param1(Param1), Param2(Param2) {} 634 635 template <typename T> 636 operator Matcher<T>() const { 637 return Matcher<T>(new MatcherT<T, P1, P2>(Param1, Param2)); 638 } 639 640private: 641 const P1 Param1; 642 const P2 Param2; 643}; 644 645/// \brief Matches any instance of the given NodeType. 646/// 647/// This is useful when a matcher syntactically requires a child matcher, 648/// but the context doesn't care. See for example: anything(). 649/// 650/// FIXME: Alternatively we could also create a IsAMatcher or something 651/// that checks that a dyn_cast is possible. This is purely needed for the 652/// difference between calling for example: 653/// record() 654/// and 655/// record(SomeMatcher) 656/// In the second case we need the correct type we were dyn_cast'ed to in order 657/// to get the right type for the inner matcher. In the first case we don't need 658/// that, but we use the type conversion anyway and insert a TrueMatcher. 659template <typename T> 660class TrueMatcher : public SingleNodeMatcherInterface<T> { 661public: 662 virtual bool matchesNode(const T &Node) const { 663 return true; 664 } 665}; 666 667/// \brief Provides a MatcherInterface<T> for a Matcher<To> that matches if T is 668/// dyn_cast'able into To and the given Matcher<To> matches on the dyn_cast'ed 669/// node. 670template <typename T, typename To> 671class DynCastMatcher : public MatcherInterface<T> { 672public: 673 explicit DynCastMatcher(const Matcher<To> &InnerMatcher) 674 : InnerMatcher(InnerMatcher) {} 675 676 virtual bool matches(const T &Node, 677 ASTMatchFinder *Finder, 678 BoundNodesTreeBuilder *Builder) const { 679 const To *InnerMatchValue = dyn_cast<To>(&Node); 680 return InnerMatchValue != NULL && 681 InnerMatcher.matches(*InnerMatchValue, Finder, Builder); 682 } 683 684private: 685 const Matcher<To> InnerMatcher; 686}; 687 688/// \brief Matcher<T> that wraps an inner Matcher<T> and binds the matched node 689/// to an ID if the inner matcher matches on the node. 690template <typename T> 691class IdMatcher : public MatcherInterface<T> { 692public: 693 /// \brief Creates an IdMatcher that binds to 'ID' if 'InnerMatcher' matches 694 /// the node. 695 IdMatcher(StringRef ID, const Matcher<T> &InnerMatcher) 696 : ID(ID), InnerMatcher(InnerMatcher) {} 697 698 virtual bool matches(const T &Node, 699 ASTMatchFinder *Finder, 700 BoundNodesTreeBuilder *Builder) const { 701 bool Result = InnerMatcher.matches(Node, Finder, Builder); 702 if (Result) { 703 Builder->setBinding(ID, &Node); 704 } 705 return Result; 706 } 707 708private: 709 const std::string ID; 710 const Matcher<T> InnerMatcher; 711}; 712 713/// \brief A Matcher that allows binding the node it matches to an id. 714/// 715/// BindableMatcher provides a \a bind() method that allows binding the 716/// matched node to an id if the match was successful. 717template <typename T> 718class BindableMatcher : public Matcher<T> { 719public: 720 BindableMatcher(MatcherInterface<T> *Implementation) 721 : Matcher<T>(Implementation) {} 722 723 /// \brief Returns a matcher that will bind the matched node on a match. 724 /// 725 /// The returned matcher is equivalent to this matcher, but will 726 /// bind the matched node on a match. 727 Matcher<T> bind(StringRef ID) const { 728 return Matcher<T>(new IdMatcher<T>(ID, *this)); 729 } 730}; 731 732/// \brief Matches nodes of type T that have child nodes of type ChildT for 733/// which a specified child matcher matches. 734/// 735/// ChildT must be an AST base type. 736template <typename T, typename ChildT> 737class HasMatcher : public MatcherInterface<T> { 738 TOOLING_COMPILE_ASSERT(IsBaseType<ChildT>::value, 739 has_only_accepts_base_type_matcher); 740public: 741 explicit HasMatcher(const Matcher<ChildT> &ChildMatcher) 742 : ChildMatcher(ChildMatcher) {} 743 744 virtual bool matches(const T &Node, 745 ASTMatchFinder *Finder, 746 BoundNodesTreeBuilder *Builder) const { 747 return Finder->matchesChildOf( 748 Node, ChildMatcher, Builder, 749 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses, 750 ASTMatchFinder::BK_First); 751 } 752 753 private: 754 const Matcher<ChildT> ChildMatcher; 755}; 756 757/// \brief Matches nodes of type T that have child nodes of type ChildT for 758/// which a specified child matcher matches. ChildT must be an AST base 759/// type. 760/// As opposed to the HasMatcher, the ForEachMatcher will produce a match 761/// for each child that matches. 762template <typename T, typename ChildT> 763class ForEachMatcher : public MatcherInterface<T> { 764 TOOLING_COMPILE_ASSERT(IsBaseType<ChildT>::value, 765 for_each_only_accepts_base_type_matcher); 766 public: 767 explicit ForEachMatcher(const Matcher<ChildT> &ChildMatcher) 768 : ChildMatcher(ChildMatcher) {} 769 770 virtual bool matches(const T& Node, 771 ASTMatchFinder* Finder, 772 BoundNodesTreeBuilder* Builder) const { 773 return Finder->matchesChildOf( 774 Node, ChildMatcher, Builder, 775 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses, 776 ASTMatchFinder::BK_All); 777 } 778 779private: 780 const Matcher<ChildT> ChildMatcher; 781}; 782 783/// \brief Matches nodes of type T if the given Matcher<T> does not match. 784/// 785/// Type argument MatcherT is required by PolymorphicMatcherWithParam1 786/// but not actually used. It will always be instantiated with a type 787/// convertible to Matcher<T>. 788template <typename T, typename MatcherT> 789class NotMatcher : public MatcherInterface<T> { 790public: 791 explicit NotMatcher(const Matcher<T> &InnerMatcher) 792 : InnerMatcher(InnerMatcher) {} 793 794 virtual bool matches(const T &Node, 795 ASTMatchFinder *Finder, 796 BoundNodesTreeBuilder *Builder) const { 797 return !InnerMatcher.matches(Node, Finder, Builder); 798 } 799 800private: 801 const Matcher<T> InnerMatcher; 802}; 803 804/// \brief Matches nodes of type T for which both provided matchers match. 805/// 806/// Type arguments MatcherT1 and MatcherT2 are required by 807/// PolymorphicMatcherWithParam2 but not actually used. They will 808/// always be instantiated with types convertible to Matcher<T>. 809template <typename T, typename MatcherT1, typename MatcherT2> 810class AllOfMatcher : public MatcherInterface<T> { 811public: 812 AllOfMatcher(const Matcher<T> &InnerMatcher1, const Matcher<T> &InnerMatcher2) 813 : InnerMatcher1(InnerMatcher1), InnerMatcher2(InnerMatcher2) {} 814 815 virtual bool matches(const T &Node, 816 ASTMatchFinder *Finder, 817 BoundNodesTreeBuilder *Builder) const { 818 return InnerMatcher1.matches(Node, Finder, Builder) && 819 InnerMatcher2.matches(Node, Finder, Builder); 820 } 821 822private: 823 const Matcher<T> InnerMatcher1; 824 const Matcher<T> InnerMatcher2; 825}; 826 827/// \brief Matches nodes of type T for which at least one of the two provided 828/// matchers matches. 829/// 830/// Type arguments MatcherT1 and MatcherT2 are 831/// required by PolymorphicMatcherWithParam2 but not actually 832/// used. They will always be instantiated with types convertible to 833/// Matcher<T>. 834template <typename T, typename MatcherT1, typename MatcherT2> 835class AnyOfMatcher : public MatcherInterface<T> { 836public: 837 AnyOfMatcher(const Matcher<T> &InnerMatcher1, const Matcher<T> &InnerMatcher2) 838 : InnerMatcher1(InnerMatcher1), InnertMatcher2(InnerMatcher2) {} 839 840 virtual bool matches(const T &Node, 841 ASTMatchFinder *Finder, 842 BoundNodesTreeBuilder *Builder) const { 843 return InnerMatcher1.matches(Node, Finder, Builder) || 844 InnertMatcher2.matches(Node, Finder, Builder); 845 } 846 847private: 848 const Matcher<T> InnerMatcher1; 849 const Matcher<T> InnertMatcher2; 850}; 851 852/// \brief Creates a Matcher<T> that matches if all inner matchers match. 853template<typename T> 854BindableMatcher<T> makeAllOfComposite( 855 ArrayRef<const Matcher<T> *> InnerMatchers) { 856 if (InnerMatchers.empty()) 857 return BindableMatcher<T>(new TrueMatcher<T>); 858 MatcherInterface<T> *InnerMatcher = new TrueMatcher<T>; 859 for (int i = InnerMatchers.size() - 1; i >= 0; --i) { 860 InnerMatcher = new AllOfMatcher<T, Matcher<T>, Matcher<T> >( 861 *InnerMatchers[i], makeMatcher(InnerMatcher)); 862 } 863 return BindableMatcher<T>(InnerMatcher); 864} 865 866/// \brief Creates a Matcher<T> that matches if 867/// T is dyn_cast'able into InnerT and all inner matchers match. 868/// 869/// Returns BindableMatcher, as matchers that use dyn_cast have 870/// the same object both to match on and to run submatchers on, 871/// so there is no ambiguity with what gets bound. 872template<typename T, typename InnerT> 873BindableMatcher<T> makeDynCastAllOfComposite( 874 ArrayRef<const Matcher<InnerT> *> InnerMatchers) { 875 return BindableMatcher<T>(new DynCastMatcher<T, InnerT>( 876 makeAllOfComposite(InnerMatchers))); 877} 878 879/// \brief Matches nodes of type T that have at least one descendant node of 880/// type DescendantT for which the given inner matcher matches. 881/// 882/// DescendantT must be an AST base type. 883template <typename T, typename DescendantT> 884class HasDescendantMatcher : public MatcherInterface<T> { 885 TOOLING_COMPILE_ASSERT(IsBaseType<DescendantT>::value, 886 has_descendant_only_accepts_base_type_matcher); 887public: 888 explicit HasDescendantMatcher(const Matcher<DescendantT> &DescendantMatcher) 889 : DescendantMatcher(DescendantMatcher) {} 890 891 virtual bool matches(const T &Node, 892 ASTMatchFinder *Finder, 893 BoundNodesTreeBuilder *Builder) const { 894 return Finder->matchesDescendantOf( 895 Node, DescendantMatcher, Builder, ASTMatchFinder::BK_First); 896 } 897 898 private: 899 const Matcher<DescendantT> DescendantMatcher; 900}; 901 902/// \brief Matches nodes of type \c T that have a parent node of type \c ParentT 903/// for which the given inner matcher matches. 904/// 905/// \c ParentT must be an AST base type. 906template <typename T, typename ParentT> 907class HasParentMatcher : public MatcherInterface<T> { 908 TOOLING_COMPILE_ASSERT(IsBaseType<ParentT>::value, 909 has_parent_only_accepts_base_type_matcher); 910public: 911 explicit HasParentMatcher(const Matcher<ParentT> &ParentMatcher) 912 : ParentMatcher(ParentMatcher) {} 913 914 virtual bool matches(const T &Node, 915 ASTMatchFinder *Finder, 916 BoundNodesTreeBuilder *Builder) const { 917 return Finder->matchesAncestorOf( 918 Node, ParentMatcher, Builder, ASTMatchFinder::AMM_ParentOnly); 919 } 920 921 private: 922 const Matcher<ParentT> ParentMatcher; 923}; 924 925/// \brief Matches nodes of type \c T that have at least one ancestor node of 926/// type \c AncestorT for which the given inner matcher matches. 927/// 928/// \c AncestorT must be an AST base type. 929template <typename T, typename AncestorT> 930class HasAncestorMatcher : public MatcherInterface<T> { 931 TOOLING_COMPILE_ASSERT(IsBaseType<AncestorT>::value, 932 has_ancestor_only_accepts_base_type_matcher); 933public: 934 explicit HasAncestorMatcher(const Matcher<AncestorT> &AncestorMatcher) 935 : AncestorMatcher(AncestorMatcher) {} 936 937 virtual bool matches(const T &Node, 938 ASTMatchFinder *Finder, 939 BoundNodesTreeBuilder *Builder) const { 940 return Finder->matchesAncestorOf( 941 Node, AncestorMatcher, Builder, ASTMatchFinder::AMM_All); 942 } 943 944 private: 945 const Matcher<AncestorT> AncestorMatcher; 946}; 947 948/// \brief Matches nodes of type T that have at least one descendant node of 949/// type DescendantT for which the given inner matcher matches. 950/// 951/// DescendantT must be an AST base type. 952/// As opposed to HasDescendantMatcher, ForEachDescendantMatcher will match 953/// for each descendant node that matches instead of only for the first. 954template <typename T, typename DescendantT> 955class ForEachDescendantMatcher : public MatcherInterface<T> { 956 TOOLING_COMPILE_ASSERT(IsBaseType<DescendantT>::value, 957 for_each_descendant_only_accepts_base_type_matcher); 958 public: 959 explicit ForEachDescendantMatcher( 960 const Matcher<DescendantT>& DescendantMatcher) 961 : DescendantMatcher(DescendantMatcher) {} 962 963 virtual bool matches(const T& Node, 964 ASTMatchFinder* Finder, 965 BoundNodesTreeBuilder* Builder) const { 966 return Finder->matchesDescendantOf(Node, DescendantMatcher, Builder, 967 ASTMatchFinder::BK_All); 968 } 969 970private: 971 const Matcher<DescendantT> DescendantMatcher; 972}; 973 974/// \brief Matches on nodes that have a getValue() method if getValue() equals 975/// the value the ValueEqualsMatcher was constructed with. 976template <typename T, typename ValueT> 977class ValueEqualsMatcher : public SingleNodeMatcherInterface<T> { 978 TOOLING_COMPILE_ASSERT((llvm::is_base_of<CharacterLiteral, T>::value || 979 llvm::is_base_of<CXXBoolLiteralExpr, 980 T>::value || 981 llvm::is_base_of<FloatingLiteral, T>::value || 982 llvm::is_base_of<IntegerLiteral, T>::value), 983 the_node_must_have_a_getValue_method); 984public: 985 explicit ValueEqualsMatcher(const ValueT &ExpectedValue) 986 : ExpectedValue(ExpectedValue) {} 987 988 virtual bool matchesNode(const T &Node) const { 989 return Node.getValue() == ExpectedValue; 990 } 991 992private: 993 const ValueT ExpectedValue; 994}; 995 996template <typename T> 997class IsDefinitionMatcher : public SingleNodeMatcherInterface<T> { 998 TOOLING_COMPILE_ASSERT( 999 (llvm::is_base_of<TagDecl, T>::value) || 1000 (llvm::is_base_of<VarDecl, T>::value) || 1001 (llvm::is_base_of<FunctionDecl, T>::value), 1002 is_definition_requires_isThisDeclarationADefinition_method); 1003public: 1004 virtual bool matchesNode(const T &Node) const { 1005 return Node.isThisDeclarationADefinition(); 1006 } 1007}; 1008 1009/// \brief Matches on template instantiations for FunctionDecl, VarDecl or 1010/// CXXRecordDecl nodes. 1011template <typename T> 1012class IsTemplateInstantiationMatcher : public MatcherInterface<T> { 1013 TOOLING_COMPILE_ASSERT((llvm::is_base_of<FunctionDecl, T>::value) || 1014 (llvm::is_base_of<VarDecl, T>::value) || 1015 (llvm::is_base_of<CXXRecordDecl, T>::value), 1016 requires_getTemplateSpecializationKind_method); 1017 public: 1018 virtual bool matches(const T& Node, 1019 ASTMatchFinder* Finder, 1020 BoundNodesTreeBuilder* Builder) const { 1021 return (Node.getTemplateSpecializationKind() == 1022 TSK_ImplicitInstantiation || 1023 Node.getTemplateSpecializationKind() == 1024 TSK_ExplicitInstantiationDefinition); 1025 } 1026}; 1027 1028/// \brief Matches on explicit template specializations for FunctionDecl, 1029/// VarDecl or CXXRecordDecl nodes. 1030template <typename T> 1031class IsExplicitTemplateSpecializationMatcher : public MatcherInterface<T> { 1032 TOOLING_COMPILE_ASSERT((llvm::is_base_of<FunctionDecl, T>::value) || 1033 (llvm::is_base_of<VarDecl, T>::value) || 1034 (llvm::is_base_of<CXXRecordDecl, T>::value), 1035 requires_getTemplateSpecializationKind_method); 1036 public: 1037 virtual bool matches(const T& Node, 1038 ASTMatchFinder* Finder, 1039 BoundNodesTreeBuilder* Builder) const { 1040 return (Node.getTemplateSpecializationKind() == TSK_ExplicitSpecialization); 1041 } 1042}; 1043 1044class IsArrowMatcher : public SingleNodeMatcherInterface<MemberExpr> { 1045public: 1046 virtual bool matchesNode(const MemberExpr &Node) const { 1047 return Node.isArrow(); 1048 } 1049}; 1050 1051class IsConstQualifiedMatcher 1052 : public SingleNodeMatcherInterface<QualType> { 1053 public: 1054 virtual bool matchesNode(const QualType& Node) const { 1055 return Node.isConstQualified(); 1056 } 1057}; 1058 1059/// \brief A VariadicDynCastAllOfMatcher<SourceT, TargetT> object is a 1060/// variadic functor that takes a number of Matcher<TargetT> and returns a 1061/// Matcher<SourceT> that matches TargetT nodes that are matched by all of the 1062/// given matchers, if SourceT can be dynamically casted into TargetT. 1063/// 1064/// For example: 1065/// const VariadicDynCastAllOfMatcher< 1066/// Decl, CXXRecordDecl> record; 1067/// Creates a functor record(...) that creates a Matcher<Decl> given 1068/// a variable number of arguments of type Matcher<CXXRecordDecl>. 1069/// The returned matcher matches if the given Decl can by dynamically 1070/// casted to CXXRecordDecl and all given matchers match. 1071template <typename SourceT, typename TargetT> 1072class VariadicDynCastAllOfMatcher 1073 : public llvm::VariadicFunction< 1074 BindableMatcher<SourceT>, Matcher<TargetT>, 1075 makeDynCastAllOfComposite<SourceT, TargetT> > { 1076public: 1077 VariadicDynCastAllOfMatcher() {} 1078}; 1079 1080/// \brief A \c VariadicAllOfMatcher<T> object is a variadic functor that takes 1081/// a number of \c Matcher<T> and returns a \c Matcher<T> that matches \c T 1082/// nodes that are matched by all of the given matchers. 1083/// 1084/// For example: 1085/// const VariadicAllOfMatcher<NestedNameSpecifier> nestedNameSpecifier; 1086/// Creates a functor nestedNameSpecifier(...) that creates a 1087/// \c Matcher<NestedNameSpecifier> given a variable number of arguments of type 1088/// \c Matcher<NestedNameSpecifier>. 1089/// The returned matcher matches if all given matchers match. 1090template <typename T> 1091class VariadicAllOfMatcher : public llvm::VariadicFunction< 1092 BindableMatcher<T>, Matcher<T>, 1093 makeAllOfComposite<T> > { 1094public: 1095 VariadicAllOfMatcher() {} 1096}; 1097 1098/// \brief Matches nodes of type \c TLoc for which the inner 1099/// \c Matcher<T> matches. 1100template <typename TLoc, typename T> 1101class LocMatcher : public MatcherInterface<TLoc> { 1102public: 1103 explicit LocMatcher(const Matcher<T> &InnerMatcher) 1104 : InnerMatcher(InnerMatcher) {} 1105 1106 virtual bool matches(const TLoc &Node, 1107 ASTMatchFinder *Finder, 1108 BoundNodesTreeBuilder *Builder) const { 1109 if (!Node) 1110 return false; 1111 return InnerMatcher.matches(*extract(Node), Finder, Builder); 1112 } 1113 1114private: 1115 const NestedNameSpecifier *extract(const NestedNameSpecifierLoc &Loc) const { 1116 return Loc.getNestedNameSpecifier(); 1117 } 1118 1119 const Matcher<T> InnerMatcher; 1120}; 1121 1122/// \brief Matches \c NestedNameSpecifiers with a prefix matching another 1123/// \c Matcher<NestedNameSpecifier>. 1124class NestedNameSpecifierPrefixMatcher 1125 : public MatcherInterface<NestedNameSpecifier> { 1126public: 1127 explicit NestedNameSpecifierPrefixMatcher( 1128 const Matcher<NestedNameSpecifier> &InnerMatcher) 1129 : InnerMatcher(InnerMatcher) {} 1130 1131 virtual bool matches(const NestedNameSpecifier &Node, 1132 ASTMatchFinder *Finder, 1133 BoundNodesTreeBuilder *Builder) const { 1134 NestedNameSpecifier *NextNode = Node.getPrefix(); 1135 if (NextNode == NULL) 1136 return false; 1137 return InnerMatcher.matches(*NextNode, Finder, Builder); 1138 } 1139 1140private: 1141 const Matcher<NestedNameSpecifier> InnerMatcher; 1142}; 1143 1144/// \brief Matches \c NestedNameSpecifierLocs with a prefix matching another 1145/// \c Matcher<NestedNameSpecifierLoc>. 1146class NestedNameSpecifierLocPrefixMatcher 1147 : public MatcherInterface<NestedNameSpecifierLoc> { 1148public: 1149 explicit NestedNameSpecifierLocPrefixMatcher( 1150 const Matcher<NestedNameSpecifierLoc> &InnerMatcher) 1151 : InnerMatcher(InnerMatcher) {} 1152 1153 virtual bool matches(const NestedNameSpecifierLoc &Node, 1154 ASTMatchFinder *Finder, 1155 BoundNodesTreeBuilder *Builder) const { 1156 NestedNameSpecifierLoc NextNode = Node.getPrefix(); 1157 if (!NextNode) 1158 return false; 1159 return InnerMatcher.matches(NextNode, Finder, Builder); 1160 } 1161 1162private: 1163 const Matcher<NestedNameSpecifierLoc> InnerMatcher; 1164}; 1165 1166/// \brief Matches \c TypeLocs based on an inner matcher matching a certain 1167/// \c QualType. 1168/// 1169/// Used to implement the \c loc() matcher. 1170class TypeLocTypeMatcher : public MatcherInterface<TypeLoc> { 1171public: 1172 explicit TypeLocTypeMatcher(const Matcher<QualType> &InnerMatcher) 1173 : InnerMatcher(InnerMatcher) {} 1174 1175 virtual bool matches(const TypeLoc &Node, 1176 ASTMatchFinder *Finder, 1177 BoundNodesTreeBuilder *Builder) const { 1178 if (!Node) 1179 return false; 1180 return InnerMatcher.matches(Node.getType(), Finder, Builder); 1181 } 1182 1183private: 1184 const Matcher<QualType> InnerMatcher; 1185}; 1186 1187/// \brief Matches nodes of type \c T for which the inner matcher matches on a 1188/// another node of type \c T that can be reached using a given traverse 1189/// function. 1190template <typename T> 1191class TypeTraverseMatcher : public MatcherInterface<T> { 1192public: 1193 explicit TypeTraverseMatcher(const Matcher<QualType> &InnerMatcher, 1194 QualType (T::*TraverseFunction)() const) 1195 : InnerMatcher(InnerMatcher), TraverseFunction(TraverseFunction) {} 1196 1197 virtual bool matches(const T &Node, 1198 ASTMatchFinder *Finder, 1199 BoundNodesTreeBuilder *Builder) const { 1200 QualType NextNode = (Node.*TraverseFunction)(); 1201 if (NextNode.isNull()) 1202 return false; 1203 return InnerMatcher.matches(NextNode, Finder, Builder); 1204 } 1205 1206private: 1207 const Matcher<QualType> InnerMatcher; 1208 QualType (T::*TraverseFunction)() const; 1209}; 1210 1211/// \brief Matches nodes of type \c T in a ..Loc hierarchy, for which the inner 1212/// matcher matches on a another node of type \c T that can be reached using a 1213/// given traverse function. 1214template <typename T> 1215class TypeLocTraverseMatcher : public MatcherInterface<T> { 1216public: 1217 explicit TypeLocTraverseMatcher(const Matcher<TypeLoc> &InnerMatcher, 1218 TypeLoc (T::*TraverseFunction)() const) 1219 : InnerMatcher(InnerMatcher), TraverseFunction(TraverseFunction) {} 1220 1221 virtual bool matches(const T &Node, 1222 ASTMatchFinder *Finder, 1223 BoundNodesTreeBuilder *Builder) const { 1224 TypeLoc NextNode = (Node.*TraverseFunction)(); 1225 if (!NextNode) 1226 return false; 1227 return InnerMatcher.matches(NextNode, Finder, Builder); 1228 } 1229 1230private: 1231 const Matcher<TypeLoc> InnerMatcher; 1232 TypeLoc (T::*TraverseFunction)() const; 1233}; 1234 1235template <typename T, typename InnerT> 1236T makeTypeAllOfComposite(ArrayRef<const Matcher<InnerT> *> InnerMatchers) { 1237 return T(makeAllOfComposite<InnerT>(InnerMatchers)); 1238} 1239 1240} // end namespace internal 1241} // end namespace ast_matchers 1242} // end namespace clang 1243 1244#endif // LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_INTERNAL_H 1245