clang  5.0.0svn
ASTMatchers.h
Go to the documentation of this file.
1 //===--- ASTMatchers.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 // This file implements matchers to be used together with the MatchFinder to
11 // match AST nodes.
12 //
13 // Matchers are created by generator functions, which can be combined in
14 // a functional in-language DSL to express queries over the C++ AST.
15 //
16 // For example, to match a class with a certain name, one would call:
17 // cxxRecordDecl(hasName("MyClass"))
18 // which returns a matcher that can be used to find all AST nodes that declare
19 // a class named 'MyClass'.
20 //
21 // For more complicated match expressions we're often interested in accessing
22 // multiple parts of the matched AST nodes once a match is found. In that case,
23 // use the id(...) matcher around the match expressions that match the nodes
24 // you want to access.
25 //
26 // For example, when we're interested in child classes of a certain class, we
27 // would write:
28 // cxxRecordDecl(hasName("MyClass"), hasChild(id("child", recordDecl())))
29 // When the match is found via the MatchFinder, a user provided callback will
30 // be called with a BoundNodes instance that contains a mapping from the
31 // strings that we provided for the id(...) calls to the nodes that were
32 // matched.
33 // In the given example, each time our matcher finds a match we get a callback
34 // where "child" is bound to the RecordDecl node of the matching child
35 // class declaration.
36 //
37 // See ASTMatchersInternal.h for a more in-depth explanation of the
38 // implementation details of the matcher framework.
39 //
40 // See ASTMatchFinder.h for how to use the generated matchers to run over
41 // an AST.
42 //
43 //===----------------------------------------------------------------------===//
44 
45 #ifndef LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H
46 #define LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H
47 
48 #include "clang/AST/ASTContext.h"
49 #include "clang/AST/DeclFriend.h"
50 #include "clang/AST/DeclObjC.h"
51 #include "clang/AST/DeclTemplate.h"
54 #include "llvm/Support/Regex.h"
55 #include <iterator>
56 
57 namespace clang {
58 namespace ast_matchers {
59 
60 /// \brief Maps string IDs to AST nodes matched by parts of a matcher.
61 ///
62 /// The bound nodes are generated by calling \c bind("id") on the node matchers
63 /// of the nodes we want to access later.
64 ///
65 /// The instances of BoundNodes are created by \c MatchFinder when the user's
66 /// callbacks are executed every time a match is found.
67 class BoundNodes {
68 public:
69  /// \brief Returns the AST node bound to \c ID.
70  ///
71  /// Returns NULL if there was no node bound to \c ID or if there is a node but
72  /// it cannot be converted to the specified type.
73  template <typename T>
74  const T *getNodeAs(StringRef ID) const {
75  return MyBoundNodes.getNodeAs<T>(ID);
76  }
77 
78  /// \brief Type of mapping from binding identifiers to bound nodes. This type
79  /// is an associative container with a key type of \c std::string and a value
80  /// type of \c clang::ast_type_traits::DynTypedNode
81  typedef internal::BoundNodesMap::IDToNodeMap IDToNodeMap;
82 
83  /// \brief Retrieve mapping from binding identifiers to bound nodes.
84  const IDToNodeMap &getMap() const {
85  return MyBoundNodes.getMap();
86  }
87 
88 private:
89  /// \brief Create BoundNodes from a pre-filled map of bindings.
90  BoundNodes(internal::BoundNodesMap &MyBoundNodes)
91  : MyBoundNodes(MyBoundNodes) {}
92 
93  internal::BoundNodesMap MyBoundNodes;
94 
96 };
97 
98 /// \brief If the provided matcher matches a node, binds the node to \c ID.
99 ///
100 /// FIXME: Do we want to support this now that we have bind()?
101 template <typename T>
102 internal::Matcher<T> id(StringRef ID,
103  const internal::BindableMatcher<T> &InnerMatcher) {
104  return InnerMatcher.bind(ID);
105 }
106 
107 /// \brief Types of matchers for the top-level classes in the AST class
108 /// hierarchy.
109 /// @{
110 typedef internal::Matcher<Decl> DeclarationMatcher;
111 typedef internal::Matcher<Stmt> StatementMatcher;
112 typedef internal::Matcher<QualType> TypeMatcher;
113 typedef internal::Matcher<TypeLoc> TypeLocMatcher;
114 typedef internal::Matcher<NestedNameSpecifier> NestedNameSpecifierMatcher;
115 typedef internal::Matcher<NestedNameSpecifierLoc> NestedNameSpecifierLocMatcher;
116 typedef internal::Matcher<CXXCtorInitializer> CXXCtorInitializerMatcher;
117 /// @}
118 
119 /// \brief Matches any node.
120 ///
121 /// Useful when another matcher requires a child matcher, but there's no
122 /// additional constraint. This will often be used with an explicit conversion
123 /// to an \c internal::Matcher<> type such as \c TypeMatcher.
124 ///
125 /// Example: \c DeclarationMatcher(anything()) matches all declarations, e.g.,
126 /// \code
127 /// "int* p" and "void f()" in
128 /// int* p;
129 /// void f();
130 /// \endcode
131 ///
132 /// Usable as: Any Matcher
133 inline internal::TrueMatcher anything() { return internal::TrueMatcher(); }
134 
135 /// \brief Matches the top declaration context.
136 ///
137 /// Given
138 /// \code
139 /// int X;
140 /// namespace NS {
141 /// int Y;
142 /// } // namespace NS
143 /// \endcode
144 /// decl(hasDeclContext(translationUnitDecl()))
145 /// matches "int X", but not "int Y".
146 const internal::VariadicDynCastAllOfMatcher<Decl, TranslationUnitDecl>
148 
149 /// \brief Matches typedef declarations.
150 ///
151 /// Given
152 /// \code
153 /// typedef int X;
154 /// using Y = int;
155 /// \endcode
156 /// typedefDecl()
157 /// matches "typedef int X", but not "using Y = int"
158 const internal::VariadicDynCastAllOfMatcher<Decl, TypedefDecl> typedefDecl;
159 
160 /// \brief Matches typedef name declarations.
161 ///
162 /// Given
163 /// \code
164 /// typedef int X;
165 /// using Y = int;
166 /// \endcode
167 /// typedefNameDecl()
168 /// matches "typedef int X" and "using Y = int"
169 const internal::VariadicDynCastAllOfMatcher<Decl, TypedefNameDecl>
171 
172 /// \brief Matches type alias declarations.
173 ///
174 /// Given
175 /// \code
176 /// typedef int X;
177 /// using Y = int;
178 /// \endcode
179 /// typeAliasDecl()
180 /// matches "using Y = int", but not "typedef int X"
181 const internal::VariadicDynCastAllOfMatcher<Decl, TypeAliasDecl> typeAliasDecl;
182 
183 /// \brief Matches AST nodes that were expanded within the main-file.
184 ///
185 /// Example matches X but not Y
186 /// (matcher = cxxRecordDecl(isExpansionInMainFile())
187 /// \code
188 /// #include <Y.h>
189 /// class X {};
190 /// \endcode
191 /// Y.h:
192 /// \code
193 /// class Y {};
194 /// \endcode
195 ///
196 /// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>
197 AST_POLYMORPHIC_MATCHER(isExpansionInMainFile,
199  auto &SourceManager = Finder->getASTContext().getSourceManager();
201  SourceManager.getExpansionLoc(Node.getLocStart()));
202 }
203 
204 /// \brief Matches AST nodes that were expanded within system-header-files.
205 ///
206 /// Example matches Y but not X
207 /// (matcher = cxxRecordDecl(isExpansionInSystemHeader())
208 /// \code
209 /// #include <SystemHeader.h>
210 /// class X {};
211 /// \endcode
212 /// SystemHeader.h:
213 /// \code
214 /// class Y {};
215 /// \endcode
216 ///
217 /// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>
218 AST_POLYMORPHIC_MATCHER(isExpansionInSystemHeader,
220  auto &SourceManager = Finder->getASTContext().getSourceManager();
221  auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getLocStart());
222  if (ExpansionLoc.isInvalid()) {
223  return false;
224  }
225  return SourceManager.isInSystemHeader(ExpansionLoc);
226 }
227 
228 /// \brief Matches AST nodes that were expanded within files whose name is
229 /// partially matching a given regex.
230 ///
231 /// Example matches Y but not X
232 /// (matcher = cxxRecordDecl(isExpansionInFileMatching("AST.*"))
233 /// \code
234 /// #include "ASTMatcher.h"
235 /// class X {};
236 /// \endcode
237 /// ASTMatcher.h:
238 /// \code
239 /// class Y {};
240 /// \endcode
241 ///
242 /// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>
243 AST_POLYMORPHIC_MATCHER_P(isExpansionInFileMatching,
245  std::string, RegExp) {
246  auto &SourceManager = Finder->getASTContext().getSourceManager();
247  auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getLocStart());
248  if (ExpansionLoc.isInvalid()) {
249  return false;
250  }
251  auto FileEntry =
253  if (!FileEntry) {
254  return false;
255  }
256 
257  auto Filename = FileEntry->getName();
258  llvm::Regex RE(RegExp);
259  return RE.match(Filename);
260 }
261 
262 /// \brief Matches declarations.
263 ///
264 /// Examples matches \c X, \c C, and the friend declaration inside \c C;
265 /// \code
266 /// void X();
267 /// class C {
268 /// friend X;
269 /// };
270 /// \endcode
271 const internal::VariadicAllOfMatcher<Decl> decl;
272 
273 /// \brief Matches a declaration of a linkage specification.
274 ///
275 /// Given
276 /// \code
277 /// extern "C" {}
278 /// \endcode
279 /// linkageSpecDecl()
280 /// matches "extern "C" {}"
281 const internal::VariadicDynCastAllOfMatcher<Decl, LinkageSpecDecl>
283 
284 /// \brief Matches a declaration of anything that could have a name.
285 ///
286 /// Example matches \c X, \c S, the anonymous union type, \c i, and \c U;
287 /// \code
288 /// typedef int X;
289 /// struct S {
290 /// union {
291 /// int i;
292 /// } U;
293 /// };
294 /// \endcode
295 const internal::VariadicDynCastAllOfMatcher<Decl, NamedDecl> namedDecl;
296 
297 /// \brief Matches a declaration of label.
298 ///
299 /// Given
300 /// \code
301 /// goto FOO;
302 /// FOO: bar();
303 /// \endcode
304 /// labelDecl()
305 /// matches 'FOO:'
306 const internal::VariadicDynCastAllOfMatcher<Decl, LabelDecl> labelDecl;
307 
308 /// \brief Matches a declaration of a namespace.
309 ///
310 /// Given
311 /// \code
312 /// namespace {}
313 /// namespace test {}
314 /// \endcode
315 /// namespaceDecl()
316 /// matches "namespace {}" and "namespace test {}"
317 const internal::VariadicDynCastAllOfMatcher<Decl, NamespaceDecl> namespaceDecl;
318 
319 /// \brief Matches a declaration of a namespace alias.
320 ///
321 /// Given
322 /// \code
323 /// namespace test {}
324 /// namespace alias = ::test;
325 /// \endcode
326 /// namespaceAliasDecl()
327 /// matches "namespace alias" but not "namespace test"
328 const internal::VariadicDynCastAllOfMatcher<Decl, NamespaceAliasDecl>
330 
331 /// \brief Matches class, struct, and union declarations.
332 ///
333 /// Example matches \c X, \c Z, \c U, and \c S
334 /// \code
335 /// class X;
336 /// template<class T> class Z {};
337 /// struct S {};
338 /// union U {};
339 /// \endcode
340 const internal::VariadicDynCastAllOfMatcher<
341  Decl,
343 
344 /// \brief Matches C++ class declarations.
345 ///
346 /// Example matches \c X, \c Z
347 /// \code
348 /// class X;
349 /// template<class T> class Z {};
350 /// \endcode
351 const internal::VariadicDynCastAllOfMatcher<
352  Decl,
354 
355 /// \brief Matches C++ class template declarations.
356 ///
357 /// Example matches \c Z
358 /// \code
359 /// template<class T> class Z {};
360 /// \endcode
361 const internal::VariadicDynCastAllOfMatcher<
362  Decl,
364 
365 /// \brief Matches C++ class template specializations.
366 ///
367 /// Given
368 /// \code
369 /// template<typename T> class A {};
370 /// template<> class A<double> {};
371 /// A<int> a;
372 /// \endcode
373 /// classTemplateSpecializationDecl()
374 /// matches the specializations \c A<int> and \c A<double>
375 const internal::VariadicDynCastAllOfMatcher<
376  Decl,
378 
379 /// \brief Matches declarator declarations (field, variable, function
380 /// and non-type template parameter declarations).
381 ///
382 /// Given
383 /// \code
384 /// class X { int y; };
385 /// \endcode
386 /// declaratorDecl()
387 /// matches \c int y.
388 const internal::VariadicDynCastAllOfMatcher<Decl, DeclaratorDecl>
390 
391 /// \brief Matches parameter variable declarations.
392 ///
393 /// Given
394 /// \code
395 /// void f(int x);
396 /// \endcode
397 /// parmVarDecl()
398 /// matches \c int x.
399 const internal::VariadicDynCastAllOfMatcher<Decl, ParmVarDecl> parmVarDecl;
400 
401 /// \brief Matches C++ access specifier declarations.
402 ///
403 /// Given
404 /// \code
405 /// class C {
406 /// public:
407 /// int a;
408 /// };
409 /// \endcode
410 /// accessSpecDecl()
411 /// matches 'public:'
412 const internal::VariadicDynCastAllOfMatcher<
413  Decl,
415 
416 /// \brief Matches constructor initializers.
417 ///
418 /// Examples matches \c i(42).
419 /// \code
420 /// class C {
421 /// C() : i(42) {}
422 /// int i;
423 /// };
424 /// \endcode
425 const internal::VariadicAllOfMatcher<CXXCtorInitializer> cxxCtorInitializer;
426 
427 /// \brief Matches template arguments.
428 ///
429 /// Given
430 /// \code
431 /// template <typename T> struct C {};
432 /// C<int> c;
433 /// \endcode
434 /// templateArgument()
435 /// matches 'int' in C<int>.
436 const internal::VariadicAllOfMatcher<TemplateArgument> templateArgument;
437 
438 /// \brief Matches template name.
439 ///
440 /// Given
441 /// \code
442 /// template <typename T> class X { };
443 /// X<int> xi;
444 /// \endcode
445 /// templateName()
446 /// matches 'X' in X<int>.
447 const internal::VariadicAllOfMatcher<TemplateName> templateName;
448 
449 /// \brief Matches non-type template parameter declarations.
450 ///
451 /// Given
452 /// \code
453 /// template <typename T, int N> struct C {};
454 /// \endcode
455 /// nonTypeTemplateParmDecl()
456 /// matches 'N', but not 'T'.
457 const internal::VariadicDynCastAllOfMatcher<
458  Decl,
460 
461 /// \brief Matches template type parameter declarations.
462 ///
463 /// Given
464 /// \code
465 /// template <typename T, int N> struct C {};
466 /// \endcode
467 /// templateTypeParmDecl()
468 /// matches 'T', but not 'N'.
469 const internal::VariadicDynCastAllOfMatcher<
470  Decl,
472 
473 /// \brief Matches public C++ declarations.
474 ///
475 /// Given
476 /// \code
477 /// class C {
478 /// public: int a;
479 /// protected: int b;
480 /// private: int c;
481 /// };
482 /// \endcode
483 /// fieldDecl(isPublic())
484 /// matches 'int a;'
485 AST_MATCHER(Decl, isPublic) {
486  return Node.getAccess() == AS_public;
487 }
488 
489 /// \brief Matches protected C++ declarations.
490 ///
491 /// Given
492 /// \code
493 /// class C {
494 /// public: int a;
495 /// protected: int b;
496 /// private: int c;
497 /// };
498 /// \endcode
499 /// fieldDecl(isProtected())
500 /// matches 'int b;'
501 AST_MATCHER(Decl, isProtected) {
502  return Node.getAccess() == AS_protected;
503 }
504 
505 /// \brief Matches private C++ declarations.
506 ///
507 /// Given
508 /// \code
509 /// class C {
510 /// public: int a;
511 /// protected: int b;
512 /// private: int c;
513 /// };
514 /// \endcode
515 /// fieldDecl(isPrivate())
516 /// matches 'int c;'
517 AST_MATCHER(Decl, isPrivate) {
518  return Node.getAccess() == AS_private;
519 }
520 
521 /// \brief Matches non-static data members that are bit-fields.
522 ///
523 /// Given
524 /// \code
525 /// class C {
526 /// int a : 2;
527 /// int b;
528 /// };
529 /// \endcode
530 /// fieldDecl(isBitField())
531 /// matches 'int a;' but not 'int b;'.
532 AST_MATCHER(FieldDecl, isBitField) {
533  return Node.isBitField();
534 }
535 
536 /// \brief Matches non-static data members that are bit-fields of the specified
537 /// bit width.
538 ///
539 /// Given
540 /// \code
541 /// class C {
542 /// int a : 2;
543 /// int b : 4;
544 /// int c : 2;
545 /// };
546 /// \endcode
547 /// fieldDecl(hasBitWidth(2))
548 /// matches 'int a;' and 'int c;' but not 'int b;'.
549 AST_MATCHER_P(FieldDecl, hasBitWidth, unsigned, Width) {
550  return Node.isBitField() &&
551  Node.getBitWidthValue(Finder->getASTContext()) == Width;
552 }
553 
554 /// \brief Matches non-static data members that have an in-class initializer.
555 ///
556 /// Given
557 /// \code
558 /// class C {
559 /// int a = 2;
560 /// int b = 3;
561 /// int c;
562 /// };
563 /// \endcode
564 /// fieldDecl(hasInClassInitializer(integerLiteral(equals(2))))
565 /// matches 'int a;' but not 'int b;'.
566 /// fieldDecl(hasInClassInitializer(anything()))
567 /// matches 'int a;' and 'int b;' but not 'int c;'.
568 AST_MATCHER_P(FieldDecl, hasInClassInitializer, internal::Matcher<Expr>,
569  InnerMatcher) {
570  const Expr *Initializer = Node.getInClassInitializer();
571  return (Initializer != nullptr &&
572  InnerMatcher.matches(*Initializer, Finder, Builder));
573 }
574 
575 /// \brief Matches a declaration that has been implicitly added
576 /// by the compiler (eg. implicit default/copy constructors).
577 AST_MATCHER(Decl, isImplicit) {
578  return Node.isImplicit();
579 }
580 
581 /// \brief Matches classTemplateSpecializations, templateSpecializationType and
582 /// functionDecl that have at least one TemplateArgument matching the given
583 /// InnerMatcher.
584 ///
585 /// Given
586 /// \code
587 /// template<typename T> class A {};
588 /// template<> class A<double> {};
589 /// A<int> a;
590 ///
591 /// template<typename T> f() {};
592 /// void func() { f<int>(); };
593 /// \endcode
594 ///
595 /// \endcode
596 /// classTemplateSpecializationDecl(hasAnyTemplateArgument(
597 /// refersToType(asString("int"))))
598 /// matches the specialization \c A<int>
599 ///
600 /// functionDecl(hasAnyTemplateArgument(refersToType(asString("int"))))
601 /// matches the specialization \c f<int>
603  hasAnyTemplateArgument,
606  FunctionDecl),
607  internal::Matcher<TemplateArgument>, InnerMatcher) {
609  internal::getTemplateSpecializationArgs(Node);
610  return matchesFirstInRange(InnerMatcher, List.begin(), List.end(), Finder,
611  Builder);
612 }
613 
614 /// \brief Matches expressions that match InnerMatcher after any implicit AST
615 /// nodes are stripped off.
616 ///
617 /// Parentheses and explicit casts are not discarded.
618 /// Given
619 /// \code
620 /// class C {};
621 /// C a = C();
622 /// C b;
623 /// C c = b;
624 /// \endcode
625 /// The matchers
626 /// \code
627 /// varDecl(hasInitializer(ignoringImplicit(cxxConstructExpr())))
628 /// \endcode
629 /// would match the declarations for a, b, and c.
630 /// While
631 /// \code
632 /// varDecl(hasInitializer(cxxConstructExpr()))
633 /// \endcode
634 /// only match the declarations for b and c.
635 AST_MATCHER_P(Expr, ignoringImplicit, ast_matchers::internal::Matcher<Expr>,
636  InnerMatcher) {
637  return InnerMatcher.matches(*Node.IgnoreImplicit(), Finder, Builder);
638 }
639 
640 /// \brief Matches expressions that match InnerMatcher after any implicit casts
641 /// are stripped off.
642 ///
643 /// Parentheses and explicit casts are not discarded.
644 /// Given
645 /// \code
646 /// int arr[5];
647 /// int a = 0;
648 /// char b = 0;
649 /// const int c = a;
650 /// int *d = arr;
651 /// long e = (long) 0l;
652 /// \endcode
653 /// The matchers
654 /// \code
655 /// varDecl(hasInitializer(ignoringImpCasts(integerLiteral())))
656 /// varDecl(hasInitializer(ignoringImpCasts(declRefExpr())))
657 /// \endcode
658 /// would match the declarations for a, b, c, and d, but not e.
659 /// While
660 /// \code
661 /// varDecl(hasInitializer(integerLiteral()))
662 /// varDecl(hasInitializer(declRefExpr()))
663 /// \endcode
664 /// only match the declarations for b, c, and d.
665 AST_MATCHER_P(Expr, ignoringImpCasts,
666  internal::Matcher<Expr>, InnerMatcher) {
667  return InnerMatcher.matches(*Node.IgnoreImpCasts(), Finder, Builder);
668 }
669 
670 /// \brief Matches expressions that match InnerMatcher after parentheses and
671 /// casts are stripped off.
672 ///
673 /// Implicit and non-C Style casts are also discarded.
674 /// Given
675 /// \code
676 /// int a = 0;
677 /// char b = (0);
678 /// void* c = reinterpret_cast<char*>(0);
679 /// char d = char(0);
680 /// \endcode
681 /// The matcher
682 /// varDecl(hasInitializer(ignoringParenCasts(integerLiteral())))
683 /// would match the declarations for a, b, c, and d.
684 /// while
685 /// varDecl(hasInitializer(integerLiteral()))
686 /// only match the declaration for a.
687 AST_MATCHER_P(Expr, ignoringParenCasts, internal::Matcher<Expr>, InnerMatcher) {
688  return InnerMatcher.matches(*Node.IgnoreParenCasts(), Finder, Builder);
689 }
690 
691 /// \brief Matches expressions that match InnerMatcher after implicit casts and
692 /// parentheses are stripped off.
693 ///
694 /// Explicit casts are not discarded.
695 /// Given
696 /// \code
697 /// int arr[5];
698 /// int a = 0;
699 /// char b = (0);
700 /// const int c = a;
701 /// int *d = (arr);
702 /// long e = ((long) 0l);
703 /// \endcode
704 /// The matchers
705 /// varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral())))
706 /// varDecl(hasInitializer(ignoringParenImpCasts(declRefExpr())))
707 /// would match the declarations for a, b, c, and d, but not e.
708 /// while
709 /// varDecl(hasInitializer(integerLiteral()))
710 /// varDecl(hasInitializer(declRefExpr()))
711 /// would only match the declaration for a.
712 AST_MATCHER_P(Expr, ignoringParenImpCasts,
713  internal::Matcher<Expr>, InnerMatcher) {
714  return InnerMatcher.matches(*Node.IgnoreParenImpCasts(), Finder, Builder);
715 }
716 
717 /// \brief Matches types that match InnerMatcher after any parens are stripped.
718 ///
719 /// Given
720 /// \code
721 /// void (*fp)(void);
722 /// \endcode
723 /// The matcher
724 /// \code
725 /// varDecl(hasType(pointerType(pointee(ignoringParens(functionType())))))
726 /// \endcode
727 /// would match the declaration for fp.
728 AST_MATCHER_P(QualType, ignoringParens,
729  internal::Matcher<QualType>, InnerMatcher) {
730  return InnerMatcher.matches(Node.IgnoreParens(), Finder, Builder);
731 }
732 
733 /// \brief Matches classTemplateSpecializations, templateSpecializationType and
734 /// functionDecl where the n'th TemplateArgument matches the given InnerMatcher.
735 ///
736 /// Given
737 /// \code
738 /// template<typename T, typename U> class A {};
739 /// A<bool, int> b;
740 /// A<int, bool> c;
741 ///
742 /// template<typename T> f() {};
743 /// void func() { f<int>(); };
744 /// \endcode
745 /// classTemplateSpecializationDecl(hasTemplateArgument(
746 /// 1, refersToType(asString("int"))))
747 /// matches the specialization \c A<bool, int>
748 ///
749 /// functionDecl(hasTemplateArgument(0, refersToType(asString("int"))))
750 /// matches the specialization \c f<int>
752  hasTemplateArgument,
755  FunctionDecl),
756  unsigned, N, internal::Matcher<TemplateArgument>, InnerMatcher) {
758  internal::getTemplateSpecializationArgs(Node);
759  if (List.size() <= N)
760  return false;
761  return InnerMatcher.matches(List[N], Finder, Builder);
762 }
763 
764 /// \brief Matches if the number of template arguments equals \p N.
765 ///
766 /// Given
767 /// \code
768 /// template<typename T> struct C {};
769 /// C<int> c;
770 /// \endcode
771 /// classTemplateSpecializationDecl(templateArgumentCountIs(1))
772 /// matches C<int>.
774  templateArgumentCountIs,
777  unsigned, N) {
778  return internal::getTemplateSpecializationArgs(Node).size() == N;
779 }
780 
781 /// \brief Matches a TemplateArgument that refers to a certain type.
782 ///
783 /// Given
784 /// \code
785 /// struct X {};
786 /// template<typename T> struct A {};
787 /// A<X> a;
788 /// \endcode
789 /// classTemplateSpecializationDecl(hasAnyTemplateArgument(
790 /// refersToType(class(hasName("X")))))
791 /// matches the specialization \c A<X>
793  internal::Matcher<QualType>, InnerMatcher) {
794  if (Node.getKind() != TemplateArgument::Type)
795  return false;
796  return InnerMatcher.matches(Node.getAsType(), Finder, Builder);
797 }
798 
799 /// \brief Matches a TemplateArgument that refers to a certain template.
800 ///
801 /// Given
802 /// \code
803 /// template<template <typename> class S> class X {};
804 /// template<typename T> class Y {};"
805 /// X<Y> xi;
806 /// \endcode
807 /// classTemplateSpecializationDecl(hasAnyTemplateArgument(
808 /// refersToTemplate(templateName())))
809 /// matches the specialization \c X<Y>
810 AST_MATCHER_P(TemplateArgument, refersToTemplate,
811  internal::Matcher<TemplateName>, InnerMatcher) {
812  if (Node.getKind() != TemplateArgument::Template)
813  return false;
814  return InnerMatcher.matches(Node.getAsTemplate(), Finder, Builder);
815 }
816 
817 /// \brief Matches a canonical TemplateArgument that refers to a certain
818 /// declaration.
819 ///
820 /// Given
821 /// \code
822 /// template<typename T> struct A {};
823 /// struct B { B* next; };
824 /// A<&B::next> a;
825 /// \endcode
826 /// classTemplateSpecializationDecl(hasAnyTemplateArgument(
827 /// refersToDeclaration(fieldDecl(hasName("next"))))
828 /// matches the specialization \c A<&B::next> with \c fieldDecl(...) matching
829 /// \c B::next
830 AST_MATCHER_P(TemplateArgument, refersToDeclaration,
831  internal::Matcher<Decl>, InnerMatcher) {
832  if (Node.getKind() == TemplateArgument::Declaration)
833  return InnerMatcher.matches(*Node.getAsDecl(), Finder, Builder);
834  return false;
835 }
836 
837 /// \brief Matches a sugar TemplateArgument that refers to a certain expression.
838 ///
839 /// Given
840 /// \code
841 /// template<typename T> struct A {};
842 /// struct B { B* next; };
843 /// A<&B::next> a;
844 /// \endcode
845 /// templateSpecializationType(hasAnyTemplateArgument(
846 /// isExpr(hasDescendant(declRefExpr(to(fieldDecl(hasName("next"))))))))
847 /// matches the specialization \c A<&B::next> with \c fieldDecl(...) matching
848 /// \c B::next
849 AST_MATCHER_P(TemplateArgument, isExpr, internal::Matcher<Expr>, InnerMatcher) {
850  if (Node.getKind() == TemplateArgument::Expression)
851  return InnerMatcher.matches(*Node.getAsExpr(), Finder, Builder);
852  return false;
853 }
854 
855 /// \brief Matches a TemplateArgument that is an integral value.
856 ///
857 /// Given
858 /// \code
859 /// template<int T> struct A {};
860 /// C<42> c;
861 /// \endcode
862 /// classTemplateSpecializationDecl(
863 /// hasAnyTemplateArgument(isIntegral()))
864 /// matches the implicit instantiation of C in C<42>
865 /// with isIntegral() matching 42.
867  return Node.getKind() == TemplateArgument::Integral;
868 }
869 
870 /// \brief Matches a TemplateArgument that referes to an integral type.
871 ///
872 /// Given
873 /// \code
874 /// template<int T> struct A {};
875 /// C<42> c;
876 /// \endcode
877 /// classTemplateSpecializationDecl(
878 /// hasAnyTemplateArgument(refersToIntegralType(asString("int"))))
879 /// matches the implicit instantiation of C in C<42>.
880 AST_MATCHER_P(TemplateArgument, refersToIntegralType,
881  internal::Matcher<QualType>, InnerMatcher) {
882  if (Node.getKind() != TemplateArgument::Integral)
883  return false;
884  return InnerMatcher.matches(Node.getIntegralType(), Finder, Builder);
885 }
886 
887 /// \brief Matches a TemplateArgument of integral type with a given value.
888 ///
889 /// Note that 'Value' is a string as the template argument's value is
890 /// an arbitrary precision integer. 'Value' must be euqal to the canonical
891 /// representation of that integral value in base 10.
892 ///
893 /// Given
894 /// \code
895 /// template<int T> struct A {};
896 /// C<42> c;
897 /// \endcode
898 /// classTemplateSpecializationDecl(
899 /// hasAnyTemplateArgument(equalsIntegralValue("42")))
900 /// matches the implicit instantiation of C in C<42>.
901 AST_MATCHER_P(TemplateArgument, equalsIntegralValue,
902  std::string, Value) {
903  if (Node.getKind() != TemplateArgument::Integral)
904  return false;
905  return Node.getAsIntegral().toString(10) == Value;
906 }
907 
908 /// \brief Matches any value declaration.
909 ///
910 /// Example matches A, B, C and F
911 /// \code
912 /// enum X { A, B, C };
913 /// void F();
914 /// \endcode
915 const internal::VariadicDynCastAllOfMatcher<Decl, ValueDecl> valueDecl;
916 
917 /// \brief Matches C++ constructor declarations.
918 ///
919 /// Example matches Foo::Foo() and Foo::Foo(int)
920 /// \code
921 /// class Foo {
922 /// public:
923 /// Foo();
924 /// Foo(int);
925 /// int DoSomething();
926 /// };
927 /// \endcode
928 const internal::VariadicDynCastAllOfMatcher<
929  Decl,
931 
932 /// \brief Matches explicit C++ destructor declarations.
933 ///
934 /// Example matches Foo::~Foo()
935 /// \code
936 /// class Foo {
937 /// public:
938 /// virtual ~Foo();
939 /// };
940 /// \endcode
941 const internal::VariadicDynCastAllOfMatcher<
942  Decl,
944 
945 /// \brief Matches enum declarations.
946 ///
947 /// Example matches X
948 /// \code
949 /// enum X {
950 /// A, B, C
951 /// };
952 /// \endcode
953 const internal::VariadicDynCastAllOfMatcher<Decl, EnumDecl> enumDecl;
954 
955 /// \brief Matches enum constants.
956 ///
957 /// Example matches A, B, C
958 /// \code
959 /// enum X {
960 /// A, B, C
961 /// };
962 /// \endcode
963 const internal::VariadicDynCastAllOfMatcher<
964  Decl,
966 
967 /// \brief Matches method declarations.
968 ///
969 /// Example matches y
970 /// \code
971 /// class X { void y(); };
972 /// \endcode
973 const internal::VariadicDynCastAllOfMatcher<Decl, CXXMethodDecl> cxxMethodDecl;
974 
975 /// \brief Matches conversion operator declarations.
976 ///
977 /// Example matches the operator.
978 /// \code
979 /// class X { operator int() const; };
980 /// \endcode
981 const internal::VariadicDynCastAllOfMatcher<Decl, CXXConversionDecl>
983 
984 /// \brief Matches variable declarations.
985 ///
986 /// Note: this does not match declarations of member variables, which are
987 /// "field" declarations in Clang parlance.
988 ///
989 /// Example matches a
990 /// \code
991 /// int a;
992 /// \endcode
993 const internal::VariadicDynCastAllOfMatcher<Decl, VarDecl> varDecl;
994 
995 /// \brief Matches field declarations.
996 ///
997 /// Given
998 /// \code
999 /// class X { int m; };
1000 /// \endcode
1001 /// fieldDecl()
1002 /// matches 'm'.
1003 const internal::VariadicDynCastAllOfMatcher<Decl, FieldDecl> fieldDecl;
1004 
1005 /// \brief Matches function declarations.
1006 ///
1007 /// Example matches f
1008 /// \code
1009 /// void f();
1010 /// \endcode
1011 const internal::VariadicDynCastAllOfMatcher<Decl, FunctionDecl> functionDecl;
1012 
1013 /// \brief Matches C++ function template declarations.
1014 ///
1015 /// Example matches f
1016 /// \code
1017 /// template<class T> void f(T t) {}
1018 /// \endcode
1019 const internal::VariadicDynCastAllOfMatcher<
1020  Decl,
1022 
1023 /// \brief Matches friend declarations.
1024 ///
1025 /// Given
1026 /// \code
1027 /// class X { friend void foo(); };
1028 /// \endcode
1029 /// friendDecl()
1030 /// matches 'friend void foo()'.
1031 const internal::VariadicDynCastAllOfMatcher<Decl, FriendDecl> friendDecl;
1032 
1033 /// \brief Matches statements.
1034 ///
1035 /// Given
1036 /// \code
1037 /// { ++a; }
1038 /// \endcode
1039 /// stmt()
1040 /// matches both the compound statement '{ ++a; }' and '++a'.
1041 const internal::VariadicAllOfMatcher<Stmt> stmt;
1042 
1043 /// \brief Matches declaration statements.
1044 ///
1045 /// Given
1046 /// \code
1047 /// int a;
1048 /// \endcode
1049 /// declStmt()
1050 /// matches 'int a'.
1051 const internal::VariadicDynCastAllOfMatcher<
1052  Stmt,
1054 
1055 /// \brief Matches member expressions.
1056 ///
1057 /// Given
1058 /// \code
1059 /// class Y {
1060 /// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; }
1061 /// int a; static int b;
1062 /// };
1063 /// \endcode
1064 /// memberExpr()
1065 /// matches this->x, x, y.x, a, this->b
1066 const internal::VariadicDynCastAllOfMatcher<Stmt, MemberExpr> memberExpr;
1067 
1068 /// \brief Matches call expressions.
1069 ///
1070 /// Example matches x.y() and y()
1071 /// \code
1072 /// X x;
1073 /// x.y();
1074 /// y();
1075 /// \endcode
1076 const internal::VariadicDynCastAllOfMatcher<Stmt, CallExpr> callExpr;
1077 
1078 /// \brief Matches lambda expressions.
1079 ///
1080 /// Example matches [&](){return 5;}
1081 /// \code
1082 /// [&](){return 5;}
1083 /// \endcode
1084 const internal::VariadicDynCastAllOfMatcher<Stmt, LambdaExpr> lambdaExpr;
1085 
1086 /// \brief Matches member call expressions.
1087 ///
1088 /// Example matches x.y()
1089 /// \code
1090 /// X x;
1091 /// x.y();
1092 /// \endcode
1093 const internal::VariadicDynCastAllOfMatcher<
1094  Stmt,
1096 
1097 /// \brief Matches ObjectiveC Message invocation expressions.
1098 ///
1099 /// The innermost message send invokes the "alloc" class method on the
1100 /// NSString class, while the outermost message send invokes the
1101 /// "initWithString" instance method on the object returned from
1102 /// NSString's "alloc". This matcher should match both message sends.
1103 /// \code
1104 /// [[NSString alloc] initWithString:@"Hello"]
1105 /// \endcode
1106 const internal::VariadicDynCastAllOfMatcher<
1107  Stmt,
1109 
1110 /// \brief Matches Objective-C interface declarations.
1111 ///
1112 /// Example matches Foo
1113 /// \code
1114 /// @interface Foo
1115 /// @end
1116 /// \endcode
1117 const internal::VariadicDynCastAllOfMatcher<
1118  Decl,
1120 
1121 /// \brief Matches Objective-C protocol declarations.
1122 ///
1123 /// Example matches FooDelegate
1124 /// \code
1125 /// @protocol FooDelegate
1126 /// @end
1127 /// \endcode
1128 const internal::VariadicDynCastAllOfMatcher<
1129  Decl,
1131 
1132 /// \brief Matches Objective-C category declarations.
1133 ///
1134 /// Example matches Foo (Additions)
1135 /// \code
1136 /// @interface Foo (Additions)
1137 /// @end
1138 /// \endcode
1139 const internal::VariadicDynCastAllOfMatcher<
1140  Decl,
1142 
1143 /// \brief Matches Objective-C method declarations.
1144 ///
1145 /// Example matches both declaration and definition of -[Foo method]
1146 /// \code
1147 /// @interface Foo
1148 /// - (void)method;
1149 /// @end
1150 ///
1151 /// @implementation Foo
1152 /// - (void)method {}
1153 /// @end
1154 /// \endcode
1155 const internal::VariadicDynCastAllOfMatcher<
1156  Decl,
1158 
1159 /// \brief Matches Objective-C instance variable declarations.
1160 ///
1161 /// Example matches _enabled
1162 /// \code
1163 /// @implementation Foo {
1164 /// BOOL _enabled;
1165 /// }
1166 /// @end
1167 /// \endcode
1168 const internal::VariadicDynCastAllOfMatcher<
1169  Decl,
1171 
1172 /// \brief Matches Objective-C property declarations.
1173 ///
1174 /// Example matches enabled
1175 /// \code
1176 /// @interface Foo
1177 /// @property BOOL enabled;
1178 /// @end
1179 /// \endcode
1180 const internal::VariadicDynCastAllOfMatcher<
1181  Decl,
1183 
1184 /// \brief Matches expressions that introduce cleanups to be run at the end
1185 /// of the sub-expression's evaluation.
1186 ///
1187 /// Example matches std::string()
1188 /// \code
1189 /// const std::string str = std::string();
1190 /// \endcode
1191 const internal::VariadicDynCastAllOfMatcher<
1192  Stmt,
1194 
1195 /// \brief Matches init list expressions.
1196 ///
1197 /// Given
1198 /// \code
1199 /// int a[] = { 1, 2 };
1200 /// struct B { int x, y; };
1201 /// B b = { 5, 6 };
1202 /// \endcode
1203 /// initListExpr()
1204 /// matches "{ 1, 2 }" and "{ 5, 6 }"
1205 const internal::VariadicDynCastAllOfMatcher<Stmt, InitListExpr> initListExpr;
1206 
1207 /// \brief Matches the syntactic form of init list expressions
1208 /// (if expression have it).
1209 AST_MATCHER_P(InitListExpr, hasSyntacticForm,
1210  internal::Matcher<Expr>, InnerMatcher) {
1211  const Expr *SyntForm = Node.getSyntacticForm();
1212  return (SyntForm != nullptr &&
1213  InnerMatcher.matches(*SyntForm, Finder, Builder));
1214 }
1215 
1216 /// \brief Matches implicit initializers of init list expressions.
1217 ///
1218 /// Given
1219 /// \code
1220 /// point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 };
1221 /// \endcode
1222 /// implicitValueInitExpr()
1223 /// matches "[0].y" (implicitly)
1224 const internal::VariadicDynCastAllOfMatcher<Stmt, ImplicitValueInitExpr>
1226 
1227 /// \brief Matches paren list expressions.
1228 /// ParenListExprs don't have a predefined type and are used for late parsing.
1229 /// In the final AST, they can be met in template declarations.
1230 ///
1231 /// Given
1232 /// \code
1233 /// template<typename T> class X {
1234 /// void f() {
1235 /// X x(*this);
1236 /// int a = 0, b = 1; int i = (a, b);
1237 /// }
1238 /// };
1239 /// \endcode
1240 /// parenListExpr() matches "*this" but NOT matches (a, b) because (a, b)
1241 /// has a predefined type and is a ParenExpr, not a ParenListExpr.
1242 const internal::VariadicDynCastAllOfMatcher<Stmt, ParenListExpr> parenListExpr;
1243 
1244 /// \brief Matches substitutions of non-type template parameters.
1245 ///
1246 /// Given
1247 /// \code
1248 /// template <int N>
1249 /// struct A { static const int n = N; };
1250 /// struct B : public A<42> {};
1251 /// \endcode
1252 /// substNonTypeTemplateParmExpr()
1253 /// matches "N" in the right-hand side of "static const int n = N;"
1254 const internal::VariadicDynCastAllOfMatcher<
1255  Stmt,
1257 
1258 /// \brief Matches using declarations.
1259 ///
1260 /// Given
1261 /// \code
1262 /// namespace X { int x; }
1263 /// using X::x;
1264 /// \endcode
1265 /// usingDecl()
1266 /// matches \code using X::x \endcode
1267 const internal::VariadicDynCastAllOfMatcher<Decl, UsingDecl> usingDecl;
1268 
1269 /// \brief Matches using namespace declarations.
1270 ///
1271 /// Given
1272 /// \code
1273 /// namespace X { int x; }
1274 /// using namespace X;
1275 /// \endcode
1276 /// usingDirectiveDecl()
1277 /// matches \code using namespace X \endcode
1278 const internal::VariadicDynCastAllOfMatcher<
1279  Decl,
1281 
1282 /// \brief Matches reference to a name that can be looked up during parsing
1283 /// but could not be resolved to a specific declaration.
1284 ///
1285 /// Given
1286 /// \code
1287 /// template<typename T>
1288 /// T foo() { T a; return a; }
1289 /// template<typename T>
1290 /// void bar() {
1291 /// foo<T>();
1292 /// }
1293 /// \endcode
1294 /// unresolvedLookupExpr()
1295 /// matches \code foo<T>() \endcode
1296 const internal::VariadicDynCastAllOfMatcher<
1297  Stmt,
1299 
1300 /// \brief Matches unresolved using value declarations.
1301 ///
1302 /// Given
1303 /// \code
1304 /// template<typename X>
1305 /// class C : private X {
1306 /// using X::x;
1307 /// };
1308 /// \endcode
1309 /// unresolvedUsingValueDecl()
1310 /// matches \code using X::x \endcode
1311 const internal::VariadicDynCastAllOfMatcher<
1312  Decl,
1314 
1315 /// \brief Matches unresolved using value declarations that involve the
1316 /// typename.
1317 ///
1318 /// Given
1319 /// \code
1320 /// template <typename T>
1321 /// struct Base { typedef T Foo; };
1322 ///
1323 /// template<typename T>
1324 /// struct S : private Base<T> {
1325 /// using typename Base<T>::Foo;
1326 /// };
1327 /// \endcode
1328 /// unresolvedUsingTypenameDecl()
1329 /// matches \code using Base<T>::Foo \endcode
1330 const internal::VariadicDynCastAllOfMatcher<
1331  Decl,
1333 
1334 /// \brief Matches parentheses used in expressions.
1335 ///
1336 /// Example matches (foo() + 1)
1337 /// \code
1338 /// int foo() { return 1; }
1339 /// int a = (foo() + 1);
1340 /// \endcode
1341 const internal::VariadicDynCastAllOfMatcher<
1342  Stmt,
1344 
1345 /// \brief Matches constructor call expressions (including implicit ones).
1346 ///
1347 /// Example matches string(ptr, n) and ptr within arguments of f
1348 /// (matcher = cxxConstructExpr())
1349 /// \code
1350 /// void f(const string &a, const string &b);
1351 /// char *ptr;
1352 /// int n;
1353 /// f(string(ptr, n), ptr);
1354 /// \endcode
1355 const internal::VariadicDynCastAllOfMatcher<
1356  Stmt,
1358 
1359 /// \brief Matches unresolved constructor call expressions.
1360 ///
1361 /// Example matches T(t) in return statement of f
1362 /// (matcher = cxxUnresolvedConstructExpr())
1363 /// \code
1364 /// template <typename T>
1365 /// void f(const T& t) { return T(t); }
1366 /// \endcode
1367 const internal::VariadicDynCastAllOfMatcher<
1368  Stmt,
1370 
1371 /// \brief Matches implicit and explicit this expressions.
1372 ///
1373 /// Example matches the implicit this expression in "return i".
1374 /// (matcher = cxxThisExpr())
1375 /// \code
1376 /// struct foo {
1377 /// int i;
1378 /// int f() { return i; }
1379 /// };
1380 /// \endcode
1381 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThisExpr> cxxThisExpr;
1382 
1383 /// \brief Matches nodes where temporaries are created.
1384 ///
1385 /// Example matches FunctionTakesString(GetStringByValue())
1386 /// (matcher = cxxBindTemporaryExpr())
1387 /// \code
1388 /// FunctionTakesString(GetStringByValue());
1389 /// FunctionTakesStringByPointer(GetStringPointer());
1390 /// \endcode
1391 const internal::VariadicDynCastAllOfMatcher<
1392  Stmt,
1394 
1395 /// \brief Matches nodes where temporaries are materialized.
1396 ///
1397 /// Example: Given
1398 /// \code
1399 /// struct T {void func()};
1400 /// T f();
1401 /// void g(T);
1402 /// \endcode
1403 /// materializeTemporaryExpr() matches 'f()' in these statements
1404 /// \code
1405 /// T u(f());
1406 /// g(f());
1407 /// \endcode
1408 /// but does not match
1409 /// \code
1410 /// f();
1411 /// f().func();
1412 /// \endcode
1413 const internal::VariadicDynCastAllOfMatcher<
1414  Stmt,
1416 
1417 /// \brief Matches new expressions.
1418 ///
1419 /// Given
1420 /// \code
1421 /// new X;
1422 /// \endcode
1423 /// cxxNewExpr()
1424 /// matches 'new X'.
1425 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNewExpr> cxxNewExpr;
1426 
1427 /// \brief Matches delete expressions.
1428 ///
1429 /// Given
1430 /// \code
1431 /// delete X;
1432 /// \endcode
1433 /// cxxDeleteExpr()
1434 /// matches 'delete X'.
1435 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDeleteExpr> cxxDeleteExpr;
1436 
1437 /// \brief Matches array subscript expressions.
1438 ///
1439 /// Given
1440 /// \code
1441 /// int i = a[1];
1442 /// \endcode
1443 /// arraySubscriptExpr()
1444 /// matches "a[1]"
1445 const internal::VariadicDynCastAllOfMatcher<
1446  Stmt,
1448 
1449 /// \brief Matches the value of a default argument at the call site.
1450 ///
1451 /// Example matches the CXXDefaultArgExpr placeholder inserted for the
1452 /// default value of the second parameter in the call expression f(42)
1453 /// (matcher = cxxDefaultArgExpr())
1454 /// \code
1455 /// void f(int x, int y = 0);
1456 /// f(42);
1457 /// \endcode
1458 const internal::VariadicDynCastAllOfMatcher<
1459  Stmt,
1461 
1462 /// \brief Matches overloaded operator calls.
1463 ///
1464 /// Note that if an operator isn't overloaded, it won't match. Instead, use
1465 /// binaryOperator matcher.
1466 /// Currently it does not match operators such as new delete.
1467 /// FIXME: figure out why these do not match?
1468 ///
1469 /// Example matches both operator<<((o << b), c) and operator<<(o, b)
1470 /// (matcher = cxxOperatorCallExpr())
1471 /// \code
1472 /// ostream &operator<< (ostream &out, int i) { };
1473 /// ostream &o; int b = 1, c = 1;
1474 /// o << b << c;
1475 /// \endcode
1476 const internal::VariadicDynCastAllOfMatcher<
1477  Stmt,
1479 
1480 /// \brief Matches expressions.
1481 ///
1482 /// Example matches x()
1483 /// \code
1484 /// void f() { x(); }
1485 /// \endcode
1486 const internal::VariadicDynCastAllOfMatcher<Stmt, Expr> expr;
1487 
1488 /// \brief Matches expressions that refer to declarations.
1489 ///
1490 /// Example matches x in if (x)
1491 /// \code
1492 /// bool x;
1493 /// if (x) {}
1494 /// \endcode
1495 const internal::VariadicDynCastAllOfMatcher<Stmt, DeclRefExpr> declRefExpr;
1496 
1497 /// \brief Matches if statements.
1498 ///
1499 /// Example matches 'if (x) {}'
1500 /// \code
1501 /// if (x) {}
1502 /// \endcode
1503 const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt;
1504 
1505 /// \brief Matches for statements.
1506 ///
1507 /// Example matches 'for (;;) {}'
1508 /// \code
1509 /// for (;;) {}
1510 /// int i[] = {1, 2, 3}; for (auto a : i);
1511 /// \endcode
1512 const internal::VariadicDynCastAllOfMatcher<Stmt, ForStmt> forStmt;
1513 
1514 /// \brief Matches the increment statement of a for loop.
1515 ///
1516 /// Example:
1517 /// forStmt(hasIncrement(unaryOperator(hasOperatorName("++"))))
1518 /// matches '++x' in
1519 /// \code
1520 /// for (x; x < N; ++x) { }
1521 /// \endcode
1522 AST_MATCHER_P(ForStmt, hasIncrement, internal::Matcher<Stmt>,
1523  InnerMatcher) {
1524  const Stmt *const Increment = Node.getInc();
1525  return (Increment != nullptr &&
1526  InnerMatcher.matches(*Increment, Finder, Builder));
1527 }
1528 
1529 /// \brief Matches the initialization statement of a for loop.
1530 ///
1531 /// Example:
1532 /// forStmt(hasLoopInit(declStmt()))
1533 /// matches 'int x = 0' in
1534 /// \code
1535 /// for (int x = 0; x < N; ++x) { }
1536 /// \endcode
1537 AST_MATCHER_P(ForStmt, hasLoopInit, internal::Matcher<Stmt>,
1538  InnerMatcher) {
1539  const Stmt *const Init = Node.getInit();
1540  return (Init != nullptr && InnerMatcher.matches(*Init, Finder, Builder));
1541 }
1542 
1543 /// \brief Matches range-based for statements.
1544 ///
1545 /// cxxForRangeStmt() matches 'for (auto a : i)'
1546 /// \code
1547 /// int i[] = {1, 2, 3}; for (auto a : i);
1548 /// for(int j = 0; j < 5; ++j);
1549 /// \endcode
1550 const internal::VariadicDynCastAllOfMatcher<
1551  Stmt,
1553 
1554 /// \brief Matches the initialization statement of a for loop.
1555 ///
1556 /// Example:
1557 /// forStmt(hasLoopVariable(anything()))
1558 /// matches 'int x' in
1559 /// \code
1560 /// for (int x : a) { }
1561 /// \endcode
1562 AST_MATCHER_P(CXXForRangeStmt, hasLoopVariable, internal::Matcher<VarDecl>,
1563  InnerMatcher) {
1564  const VarDecl *const Var = Node.getLoopVariable();
1565  return (Var != nullptr && InnerMatcher.matches(*Var, Finder, Builder));
1566 }
1567 
1568 /// \brief Matches the range initialization statement of a for loop.
1569 ///
1570 /// Example:
1571 /// forStmt(hasRangeInit(anything()))
1572 /// matches 'a' in
1573 /// \code
1574 /// for (int x : a) { }
1575 /// \endcode
1576 AST_MATCHER_P(CXXForRangeStmt, hasRangeInit, internal::Matcher<Expr>,
1577  InnerMatcher) {
1578  const Expr *const Init = Node.getRangeInit();
1579  return (Init != nullptr && InnerMatcher.matches(*Init, Finder, Builder));
1580 }
1581 
1582 /// \brief Matches while statements.
1583 ///
1584 /// Given
1585 /// \code
1586 /// while (true) {}
1587 /// \endcode
1588 /// whileStmt()
1589 /// matches 'while (true) {}'.
1590 const internal::VariadicDynCastAllOfMatcher<Stmt, WhileStmt> whileStmt;
1591 
1592 /// \brief Matches do statements.
1593 ///
1594 /// Given
1595 /// \code
1596 /// do {} while (true);
1597 /// \endcode
1598 /// doStmt()
1599 /// matches 'do {} while(true)'
1600 const internal::VariadicDynCastAllOfMatcher<Stmt, DoStmt> doStmt;
1601 
1602 /// \brief Matches break statements.
1603 ///
1604 /// Given
1605 /// \code
1606 /// while (true) { break; }
1607 /// \endcode
1608 /// breakStmt()
1609 /// matches 'break'
1610 const internal::VariadicDynCastAllOfMatcher<Stmt, BreakStmt> breakStmt;
1611 
1612 /// \brief Matches continue statements.
1613 ///
1614 /// Given
1615 /// \code
1616 /// while (true) { continue; }
1617 /// \endcode
1618 /// continueStmt()
1619 /// matches 'continue'
1620 const internal::VariadicDynCastAllOfMatcher<Stmt, ContinueStmt> continueStmt;
1621 
1622 /// \brief Matches return statements.
1623 ///
1624 /// Given
1625 /// \code
1626 /// return 1;
1627 /// \endcode
1628 /// returnStmt()
1629 /// matches 'return 1'
1630 const internal::VariadicDynCastAllOfMatcher<Stmt, ReturnStmt> returnStmt;
1631 
1632 /// \brief Matches goto statements.
1633 ///
1634 /// Given
1635 /// \code
1636 /// goto FOO;
1637 /// FOO: bar();
1638 /// \endcode
1639 /// gotoStmt()
1640 /// matches 'goto FOO'
1641 const internal::VariadicDynCastAllOfMatcher<Stmt, GotoStmt> gotoStmt;
1642 
1643 /// \brief Matches label statements.
1644 ///
1645 /// Given
1646 /// \code
1647 /// goto FOO;
1648 /// FOO: bar();
1649 /// \endcode
1650 /// labelStmt()
1651 /// matches 'FOO:'
1652 const internal::VariadicDynCastAllOfMatcher<Stmt, LabelStmt> labelStmt;
1653 
1654 /// \brief Matches address of label statements (GNU extension).
1655 ///
1656 /// Given
1657 /// \code
1658 /// FOO: bar();
1659 /// void *ptr = &&FOO;
1660 /// goto *bar;
1661 /// \endcode
1662 /// addrLabelExpr()
1663 /// matches '&&FOO'
1664 const internal::VariadicDynCastAllOfMatcher<Stmt, AddrLabelExpr> addrLabelExpr;
1665 
1666 /// \brief Matches switch statements.
1667 ///
1668 /// Given
1669 /// \code
1670 /// switch(a) { case 42: break; default: break; }
1671 /// \endcode
1672 /// switchStmt()
1673 /// matches 'switch(a)'.
1674 const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchStmt> switchStmt;
1675 
1676 /// \brief Matches case and default statements inside switch statements.
1677 ///
1678 /// Given
1679 /// \code
1680 /// switch(a) { case 42: break; default: break; }
1681 /// \endcode
1682 /// switchCase()
1683 /// matches 'case 42: break;' and 'default: break;'.
1684 const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchCase> switchCase;
1685 
1686 /// \brief Matches case statements inside switch statements.
1687 ///
1688 /// Given
1689 /// \code
1690 /// switch(a) { case 42: break; default: break; }
1691 /// \endcode
1692 /// caseStmt()
1693 /// matches 'case 42: break;'.
1694 const internal::VariadicDynCastAllOfMatcher<Stmt, CaseStmt> caseStmt;
1695 
1696 /// \brief Matches default statements inside switch statements.
1697 ///
1698 /// Given
1699 /// \code
1700 /// switch(a) { case 42: break; default: break; }
1701 /// \endcode
1702 /// defaultStmt()
1703 /// matches 'default: break;'.
1704 const internal::VariadicDynCastAllOfMatcher<Stmt, DefaultStmt> defaultStmt;
1705 
1706 /// \brief Matches compound statements.
1707 ///
1708 /// Example matches '{}' and '{{}}'in 'for (;;) {{}}'
1709 /// \code
1710 /// for (;;) {{}}
1711 /// \endcode
1712 const internal::VariadicDynCastAllOfMatcher<Stmt, CompoundStmt> compoundStmt;
1713 
1714 /// \brief Matches catch statements.
1715 ///
1716 /// \code
1717 /// try {} catch(int i) {}
1718 /// \endcode
1719 /// cxxCatchStmt()
1720 /// matches 'catch(int i)'
1721 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXCatchStmt> cxxCatchStmt;
1722 
1723 /// \brief Matches try statements.
1724 ///
1725 /// \code
1726 /// try {} catch(int i) {}
1727 /// \endcode
1728 /// cxxTryStmt()
1729 /// matches 'try {}'
1730 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTryStmt> cxxTryStmt;
1731 
1732 /// \brief Matches throw expressions.
1733 ///
1734 /// \code
1735 /// try { throw 5; } catch(int i) {}
1736 /// \endcode
1737 /// cxxThrowExpr()
1738 /// matches 'throw 5'
1739 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThrowExpr> cxxThrowExpr;
1740 
1741 /// \brief Matches null statements.
1742 ///
1743 /// \code
1744 /// foo();;
1745 /// \endcode
1746 /// nullStmt()
1747 /// matches the second ';'
1748 const internal::VariadicDynCastAllOfMatcher<Stmt, NullStmt> nullStmt;
1749 
1750 /// \brief Matches asm statements.
1751 ///
1752 /// \code
1753 /// int i = 100;
1754 /// __asm("mov al, 2");
1755 /// \endcode
1756 /// asmStmt()
1757 /// matches '__asm("mov al, 2")'
1758 const internal::VariadicDynCastAllOfMatcher<Stmt, AsmStmt> asmStmt;
1759 
1760 /// \brief Matches bool literals.
1761 ///
1762 /// Example matches true
1763 /// \code
1764 /// true
1765 /// \endcode
1766 const internal::VariadicDynCastAllOfMatcher<
1767  Stmt,
1769 
1770 /// \brief Matches string literals (also matches wide string literals).
1771 ///
1772 /// Example matches "abcd", L"abcd"
1773 /// \code
1774 /// char *s = "abcd";
1775 /// wchar_t *ws = L"abcd";
1776 /// \endcode
1777 const internal::VariadicDynCastAllOfMatcher<
1778  Stmt,
1780 
1781 /// \brief Matches character literals (also matches wchar_t).
1782 ///
1783 /// Not matching Hex-encoded chars (e.g. 0x1234, which is a IntegerLiteral),
1784 /// though.
1785 ///
1786 /// Example matches 'a', L'a'
1787 /// \code
1788 /// char ch = 'a';
1789 /// wchar_t chw = L'a';
1790 /// \endcode
1791 const internal::VariadicDynCastAllOfMatcher<
1792  Stmt,
1794 
1795 /// \brief Matches integer literals of all sizes / encodings, e.g.
1796 /// 1, 1L, 0x1 and 1U.
1797 ///
1798 /// Does not match character-encoded integers such as L'a'.
1799 const internal::VariadicDynCastAllOfMatcher<
1800  Stmt,
1802 
1803 /// \brief Matches float literals of all sizes / encodings, e.g.
1804 /// 1.0, 1.0f, 1.0L and 1e10.
1805 ///
1806 /// Does not match implicit conversions such as
1807 /// \code
1808 /// float a = 10;
1809 /// \endcode
1810 const internal::VariadicDynCastAllOfMatcher<
1811  Stmt,
1813 
1814 /// \brief Matches user defined literal operator call.
1815 ///
1816 /// Example match: "foo"_suffix
1817 const internal::VariadicDynCastAllOfMatcher<
1818  Stmt,
1820 
1821 /// \brief Matches compound (i.e. non-scalar) literals
1822 ///
1823 /// Example match: {1}, (1, 2)
1824 /// \code
1825 /// int array[4] = {1};
1826 /// vector int myvec = (vector int)(1, 2);
1827 /// \endcode
1828 const internal::VariadicDynCastAllOfMatcher<
1829  Stmt,
1831 
1832 /// \brief Matches nullptr literal.
1833 const internal::VariadicDynCastAllOfMatcher<
1834  Stmt,
1836 
1837 /// \brief Matches GNU __null expression.
1838 const internal::VariadicDynCastAllOfMatcher<Stmt, GNUNullExpr> gnuNullExpr;
1839 
1840 /// \brief Matches atomic builtins.
1841 /// Example matches __atomic_load_n(ptr, 1)
1842 /// \code
1843 /// void foo() { int *ptr; __atomic_load_n(ptr, 1); }
1844 /// \endcode
1845 const internal::VariadicDynCastAllOfMatcher<Stmt, AtomicExpr> atomicExpr;
1846 
1847 /// \brief Matches statement expression (GNU extension).
1848 ///
1849 /// Example match: ({ int X = 4; X; })
1850 /// \code
1851 /// int C = ({ int X = 4; X; });
1852 /// \endcode
1853 const internal::VariadicDynCastAllOfMatcher<Stmt, StmtExpr> stmtExpr;
1854 
1855 /// \brief Matches binary operator expressions.
1856 ///
1857 /// Example matches a || b
1858 /// \code
1859 /// !(a || b)
1860 /// \endcode
1861 const internal::VariadicDynCastAllOfMatcher<
1862  Stmt,
1864 
1865 /// \brief Matches unary operator expressions.
1866 ///
1867 /// Example matches !a
1868 /// \code
1869 /// !a || b
1870 /// \endcode
1871 const internal::VariadicDynCastAllOfMatcher<
1872  Stmt,
1874 
1875 /// \brief Matches conditional operator expressions.
1876 ///
1877 /// Example matches a ? b : c
1878 /// \code
1879 /// (a ? b : c) + 42
1880 /// \endcode
1881 const internal::VariadicDynCastAllOfMatcher<
1882  Stmt,
1884 
1885 /// \brief Matches binary conditional operator expressions (GNU extension).
1886 ///
1887 /// Example matches a ?: b
1888 /// \code
1889 /// (a ?: b) + 42;
1890 /// \endcode
1891 const internal::VariadicDynCastAllOfMatcher<
1892  Stmt,
1894 
1895 /// \brief Matches opaque value expressions. They are used as helpers
1896 /// to reference another expressions and can be met
1897 /// in BinaryConditionalOperators, for example.
1898 ///
1899 /// Example matches 'a'
1900 /// \code
1901 /// (a ?: c) + 42;
1902 /// \endcode
1903 const internal::VariadicDynCastAllOfMatcher<
1904  Stmt,
1906 
1907 /// \brief Matches a C++ static_assert declaration.
1908 ///
1909 /// Example:
1910 /// staticAssertExpr()
1911 /// matches
1912 /// static_assert(sizeof(S) == sizeof(int))
1913 /// in
1914 /// \code
1915 /// struct S {
1916 /// int x;
1917 /// };
1918 /// static_assert(sizeof(S) == sizeof(int));
1919 /// \endcode
1920 const internal::VariadicDynCastAllOfMatcher<
1921  Decl,
1923 
1924 /// \brief Matches a reinterpret_cast expression.
1925 ///
1926 /// Either the source expression or the destination type can be matched
1927 /// using has(), but hasDestinationType() is more specific and can be
1928 /// more readable.
1929 ///
1930 /// Example matches reinterpret_cast<char*>(&p) in
1931 /// \code
1932 /// void* p = reinterpret_cast<char*>(&p);
1933 /// \endcode
1934 const internal::VariadicDynCastAllOfMatcher<
1935  Stmt,
1937 
1938 /// \brief Matches a C++ static_cast expression.
1939 ///
1940 /// \see hasDestinationType
1941 /// \see reinterpretCast
1942 ///
1943 /// Example:
1944 /// cxxStaticCastExpr()
1945 /// matches
1946 /// static_cast<long>(8)
1947 /// in
1948 /// \code
1949 /// long eight(static_cast<long>(8));
1950 /// \endcode
1951 const internal::VariadicDynCastAllOfMatcher<
1952  Stmt,
1954 
1955 /// \brief Matches a dynamic_cast expression.
1956 ///
1957 /// Example:
1958 /// cxxDynamicCastExpr()
1959 /// matches
1960 /// dynamic_cast<D*>(&b);
1961 /// in
1962 /// \code
1963 /// struct B { virtual ~B() {} }; struct D : B {};
1964 /// B b;
1965 /// D* p = dynamic_cast<D*>(&b);
1966 /// \endcode
1967 const internal::VariadicDynCastAllOfMatcher<
1968  Stmt,
1970 
1971 /// \brief Matches a const_cast expression.
1972 ///
1973 /// Example: Matches const_cast<int*>(&r) in
1974 /// \code
1975 /// int n = 42;
1976 /// const int &r(n);
1977 /// int* p = const_cast<int*>(&r);
1978 /// \endcode
1979 const internal::VariadicDynCastAllOfMatcher<
1980  Stmt,
1982 
1983 /// \brief Matches a C-style cast expression.
1984 ///
1985 /// Example: Matches (int) 2.2f in
1986 /// \code
1987 /// int i = (int) 2.2f;
1988 /// \endcode
1989 const internal::VariadicDynCastAllOfMatcher<
1990  Stmt,
1992 
1993 /// \brief Matches explicit cast expressions.
1994 ///
1995 /// Matches any cast expression written in user code, whether it be a
1996 /// C-style cast, a functional-style cast, or a keyword cast.
1997 ///
1998 /// Does not match implicit conversions.
1999 ///
2000 /// Note: the name "explicitCast" is chosen to match Clang's terminology, as
2001 /// Clang uses the term "cast" to apply to implicit conversions as well as to
2002 /// actual cast expressions.
2003 ///
2004 /// \see hasDestinationType.
2005 ///
2006 /// Example: matches all five of the casts in
2007 /// \code
2008 /// int((int)(reinterpret_cast<int>(static_cast<int>(const_cast<int>(42)))))
2009 /// \endcode
2010 /// but does not match the implicit conversion in
2011 /// \code
2012 /// long ell = 42;
2013 /// \endcode
2014 const internal::VariadicDynCastAllOfMatcher<
2015  Stmt,
2017 
2018 /// \brief Matches the implicit cast nodes of Clang's AST.
2019 ///
2020 /// This matches many different places, including function call return value
2021 /// eliding, as well as any type conversions.
2022 const internal::VariadicDynCastAllOfMatcher<
2023  Stmt,
2025 
2026 /// \brief Matches any cast nodes of Clang's AST.
2027 ///
2028 /// Example: castExpr() matches each of the following:
2029 /// \code
2030 /// (int) 3;
2031 /// const_cast<Expr *>(SubExpr);
2032 /// char c = 0;
2033 /// \endcode
2034 /// but does not match
2035 /// \code
2036 /// int i = (0);
2037 /// int k = 0;
2038 /// \endcode
2039 const internal::VariadicDynCastAllOfMatcher<Stmt, CastExpr> castExpr;
2040 
2041 /// \brief Matches functional cast expressions
2042 ///
2043 /// Example: Matches Foo(bar);
2044 /// \code
2045 /// Foo f = bar;
2046 /// Foo g = (Foo) bar;
2047 /// Foo h = Foo(bar);
2048 /// \endcode
2049 const internal::VariadicDynCastAllOfMatcher<
2050  Stmt,
2052 
2053 /// \brief Matches functional cast expressions having N != 1 arguments
2054 ///
2055 /// Example: Matches Foo(bar, bar)
2056 /// \code
2057 /// Foo h = Foo(bar, bar);
2058 /// \endcode
2059 const internal::VariadicDynCastAllOfMatcher<
2060  Stmt,
2062 
2063 /// \brief Matches predefined identifier expressions [C99 6.4.2.2].
2064 ///
2065 /// Example: Matches __func__
2066 /// \code
2067 /// printf("%s", __func__);
2068 /// \endcode
2069 const internal::VariadicDynCastAllOfMatcher<
2070  Stmt,
2072 
2073 /// \brief Matches C99 designated initializer expressions [C99 6.7.8].
2074 ///
2075 /// Example: Matches { [2].y = 1.0, [0].x = 1.0 }
2076 /// \code
2077 /// point ptarray[10] = { [2].y = 1.0, [0].x = 1.0 };
2078 /// \endcode
2079 const internal::VariadicDynCastAllOfMatcher<
2080  Stmt,
2082 
2083 /// \brief Matches designated initializer expressions that contain
2084 /// a specific number of designators.
2085 ///
2086 /// Example: Given
2087 /// \code
2088 /// point ptarray[10] = { [2].y = 1.0, [0].x = 1.0 };
2089 /// point ptarray2[10] = { [2].y = 1.0, [2].x = 0.0, [0].x = 1.0 };
2090 /// \endcode
2091 /// designatorCountIs(2)
2092 /// matches '{ [2].y = 1.0, [0].x = 1.0 }',
2093 /// but not '{ [2].y = 1.0, [2].x = 0.0, [0].x = 1.0 }'.
2094 AST_MATCHER_P(DesignatedInitExpr, designatorCountIs, unsigned, N) {
2095  return Node.size() == N;
2096 }
2097 
2098 /// \brief Matches \c QualTypes in the clang AST.
2099 const internal::VariadicAllOfMatcher<QualType> qualType;
2100 
2101 /// \brief Matches \c Types in the clang AST.
2102 const internal::VariadicAllOfMatcher<Type> type;
2103 
2104 /// \brief Matches \c TypeLocs in the clang AST.
2105 const internal::VariadicAllOfMatcher<TypeLoc> typeLoc;
2106 
2107 /// \brief Matches if any of the given matchers matches.
2108 ///
2109 /// Unlike \c anyOf, \c eachOf will generate a match result for each
2110 /// matching submatcher.
2111 ///
2112 /// For example, in:
2113 /// \code
2114 /// class A { int a; int b; };
2115 /// \endcode
2116 /// The matcher:
2117 /// \code
2118 /// cxxRecordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2119 /// has(fieldDecl(hasName("b")).bind("v"))))
2120 /// \endcode
2121 /// will generate two results binding "v", the first of which binds
2122 /// the field declaration of \c a, the second the field declaration of
2123 /// \c b.
2124 ///
2125 /// Usable as: Any Matcher
2126 const internal::VariadicOperatorMatcherFunc<2, UINT_MAX> eachOf = {
2127  internal::DynTypedMatcher::VO_EachOf
2128 };
2129 
2130 /// \brief Matches if any of the given matchers matches.
2131 ///
2132 /// Usable as: Any Matcher
2133 const internal::VariadicOperatorMatcherFunc<2, UINT_MAX> anyOf = {
2134  internal::DynTypedMatcher::VO_AnyOf
2135 };
2136 
2137 /// \brief Matches if all given matchers match.
2138 ///
2139 /// Usable as: Any Matcher
2140 const internal::VariadicOperatorMatcherFunc<2, UINT_MAX> allOf = {
2141  internal::DynTypedMatcher::VO_AllOf
2142 };
2143 
2144 /// \brief Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL)
2145 ///
2146 /// Given
2147 /// \code
2148 /// Foo x = bar;
2149 /// int y = sizeof(x) + alignof(x);
2150 /// \endcode
2151 /// unaryExprOrTypeTraitExpr()
2152 /// matches \c sizeof(x) and \c alignof(x)
2153 const internal::VariadicDynCastAllOfMatcher<
2154  Stmt,
2156 
2157 /// \brief Matches unary expressions that have a specific type of argument.
2158 ///
2159 /// Given
2160 /// \code
2161 /// int a, c; float b; int s = sizeof(a) + sizeof(b) + alignof(c);
2162 /// \endcode
2163 /// unaryExprOrTypeTraitExpr(hasArgumentOfType(asString("int"))
2164 /// matches \c sizeof(a) and \c alignof(c)
2166  internal::Matcher<QualType>, InnerMatcher) {
2167  const QualType ArgumentType = Node.getTypeOfArgument();
2168  return InnerMatcher.matches(ArgumentType, Finder, Builder);
2169 }
2170 
2171 /// \brief Matches unary expressions of a certain kind.
2172 ///
2173 /// Given
2174 /// \code
2175 /// int x;
2176 /// int s = sizeof(x) + alignof(x)
2177 /// \endcode
2178 /// unaryExprOrTypeTraitExpr(ofKind(UETT_SizeOf))
2179 /// matches \c sizeof(x)
2181  return Node.getKind() == Kind;
2182 }
2183 
2184 /// \brief Same as unaryExprOrTypeTraitExpr, but only matching
2185 /// alignof.
2186 inline internal::Matcher<Stmt> alignOfExpr(
2187  const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) {
2189  ofKind(UETT_AlignOf), InnerMatcher)));
2190 }
2191 
2192 /// \brief Same as unaryExprOrTypeTraitExpr, but only matching
2193 /// sizeof.
2194 inline internal::Matcher<Stmt> sizeOfExpr(
2195  const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) {
2197  allOf(ofKind(UETT_SizeOf), InnerMatcher)));
2198 }
2199 
2200 /// \brief Matches NamedDecl nodes that have the specified name.
2201 ///
2202 /// Supports specifying enclosing namespaces or classes by prefixing the name
2203 /// with '<enclosing>::'.
2204 /// Does not match typedefs of an underlying type with the given name.
2205 ///
2206 /// Example matches X (Name == "X")
2207 /// \code
2208 /// class X;
2209 /// \endcode
2210 ///
2211 /// Example matches X (Name is one of "::a::b::X", "a::b::X", "b::X", "X")
2212 /// \code
2213 /// namespace a { namespace b { class X; } }
2214 /// \endcode
2215 inline internal::Matcher<NamedDecl> hasName(const std::string &Name) {
2216  std::vector<std::string> Names;
2217  Names.push_back(Name);
2218  return internal::Matcher<NamedDecl>(new internal::HasNameMatcher(Names));
2219 }
2220 
2221 /// \brief Matches NamedDecl nodes that have any of the specified names.
2222 ///
2223 /// This matcher is only provided as a performance optimization of hasName.
2224 /// \code
2225 /// hasAnyName(a, b, c)
2226 /// \endcode
2227 /// is equivalent to, but faster than
2228 /// \code
2229 /// anyOf(hasName(a), hasName(b), hasName(c))
2230 /// \endcode
2231 const internal::VariadicFunction<internal::Matcher<NamedDecl>, StringRef,
2234 
2235 /// \brief Matches NamedDecl nodes whose fully qualified names contain
2236 /// a substring matched by the given RegExp.
2237 ///
2238 /// Supports specifying enclosing namespaces or classes by
2239 /// prefixing the name with '<enclosing>::'. Does not match typedefs
2240 /// of an underlying type with the given name.
2241 ///
2242 /// Example matches X (regexp == "::X")
2243 /// \code
2244 /// class X;
2245 /// \endcode
2246 ///
2247 /// Example matches X (regexp is one of "::X", "^foo::.*X", among others)
2248 /// \code
2249 /// namespace foo { namespace bar { class X; } }
2250 /// \endcode
2251 AST_MATCHER_P(NamedDecl, matchesName, std::string, RegExp) {
2252  assert(!RegExp.empty());
2253  std::string FullNameString = "::" + Node.getQualifiedNameAsString();
2254  llvm::Regex RE(RegExp);
2255  return RE.match(FullNameString);
2256 }
2257 
2258 /// \brief Matches overloaded operator names.
2259 ///
2260 /// Matches overloaded operator names specified in strings without the
2261 /// "operator" prefix: e.g. "<<".
2262 ///
2263 /// Given:
2264 /// \code
2265 /// class A { int operator*(); };
2266 /// const A &operator<<(const A &a, const A &b);
2267 /// A a;
2268 /// a << a; // <-- This matches
2269 /// \endcode
2270 ///
2271 /// \c cxxOperatorCallExpr(hasOverloadedOperatorName("<<"))) matches the
2272 /// specified line and
2273 /// \c cxxRecordDecl(hasMethod(hasOverloadedOperatorName("*")))
2274 /// matches the declaration of \c A.
2275 ///
2276 /// Usable as: Matcher<CXXOperatorCallExpr>, Matcher<FunctionDecl>
2277 inline internal::PolymorphicMatcherWithParam1<
2278  internal::HasOverloadedOperatorNameMatcher, StringRef,
2280 hasOverloadedOperatorName(StringRef Name) {
2281  return internal::PolymorphicMatcherWithParam1<
2282  internal::HasOverloadedOperatorNameMatcher, StringRef,
2284 }
2285 
2286 /// \brief Matches C++ classes that are directly or indirectly derived from
2287 /// a class matching \c Base.
2288 ///
2289 /// Note that a class is not considered to be derived from itself.
2290 ///
2291 /// Example matches Y, Z, C (Base == hasName("X"))
2292 /// \code
2293 /// class X;
2294 /// class Y : public X {}; // directly derived
2295 /// class Z : public Y {}; // indirectly derived
2296 /// typedef X A;
2297 /// typedef A B;
2298 /// class C : public B {}; // derived from a typedef of X
2299 /// \endcode
2300 ///
2301 /// In the following example, Bar matches isDerivedFrom(hasName("X")):
2302 /// \code
2303 /// class Foo;
2304 /// typedef Foo X;
2305 /// class Bar : public Foo {}; // derived from a type that X is a typedef of
2306 /// \endcode
2308  internal::Matcher<NamedDecl>, Base) {
2309  return Finder->classIsDerivedFrom(&Node, Base, Builder);
2310 }
2311 
2312 /// \brief Overloaded method as shortcut for \c isDerivedFrom(hasName(...)).
2313 AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isDerivedFrom, std::string, BaseName, 1) {
2314  assert(!BaseName.empty());
2315  return isDerivedFrom(hasName(BaseName)).matches(Node, Finder, Builder);
2316 }
2317 
2318 /// \brief Similar to \c isDerivedFrom(), but also matches classes that directly
2319 /// match \c Base.
2321  internal::Matcher<NamedDecl>, Base, 0) {
2322  return Matcher<CXXRecordDecl>(anyOf(Base, isDerivedFrom(Base)))
2323  .matches(Node, Finder, Builder);
2324 }
2325 
2326 /// \brief Overloaded method as shortcut for
2327 /// \c isSameOrDerivedFrom(hasName(...)).
2328 AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isSameOrDerivedFrom, std::string,
2329  BaseName, 1) {
2330  assert(!BaseName.empty());
2331  return isSameOrDerivedFrom(hasName(BaseName)).matches(Node, Finder, Builder);
2332 }
2333 
2334 /// \brief Matches the first method of a class or struct that satisfies \c
2335 /// InnerMatcher.
2336 ///
2337 /// Given:
2338 /// \code
2339 /// class A { void func(); };
2340 /// class B { void member(); };
2341 /// \endcode
2342 ///
2343 /// \c cxxRecordDecl(hasMethod(hasName("func"))) matches the declaration of
2344 /// \c A but not \c B.
2345 AST_MATCHER_P(CXXRecordDecl, hasMethod, internal::Matcher<CXXMethodDecl>,
2346  InnerMatcher) {
2347  return matchesFirstInPointerRange(InnerMatcher, Node.method_begin(),
2348  Node.method_end(), Finder, Builder);
2349 }
2350 
2351 /// \brief Matches the generated class of lambda expressions.
2352 ///
2353 /// Given:
2354 /// \code
2355 /// auto x = []{};
2356 /// \endcode
2357 ///
2358 /// \c cxxRecordDecl(isLambda()) matches the implicit class declaration of
2359 /// \c decltype(x)
2361  return Node.isLambda();
2362 }
2363 
2364 /// \brief Matches AST nodes that have child AST nodes that match the
2365 /// provided matcher.
2366 ///
2367 /// Example matches X, Y
2368 /// (matcher = cxxRecordDecl(has(cxxRecordDecl(hasName("X")))
2369 /// \code
2370 /// class X {}; // Matches X, because X::X is a class of name X inside X.
2371 /// class Y { class X {}; };
2372 /// class Z { class Y { class X {}; }; }; // Does not match Z.
2373 /// \endcode
2374 ///
2375 /// ChildT must be an AST base type.
2376 ///
2377 /// Usable as: Any Matcher
2378 /// Note that has is direct matcher, so it also matches things like implicit
2379 /// casts and paren casts. If you are matching with expr then you should
2380 /// probably consider using ignoringParenImpCasts like:
2381 /// has(ignoringParenImpCasts(expr())).
2382 const internal::ArgumentAdaptingMatcherFunc<internal::HasMatcher>
2383 LLVM_ATTRIBUTE_UNUSED has = {};
2384 
2385 /// \brief Matches AST nodes that have descendant AST nodes that match the
2386 /// provided matcher.
2387 ///
2388 /// Example matches X, Y, Z
2389 /// (matcher = cxxRecordDecl(hasDescendant(cxxRecordDecl(hasName("X")))))
2390 /// \code
2391 /// class X {}; // Matches X, because X::X is a class of name X inside X.
2392 /// class Y { class X {}; };
2393 /// class Z { class Y { class X {}; }; };
2394 /// \endcode
2395 ///
2396 /// DescendantT must be an AST base type.
2397 ///
2398 /// Usable as: Any Matcher
2399 const internal::ArgumentAdaptingMatcherFunc<internal::HasDescendantMatcher>
2400 LLVM_ATTRIBUTE_UNUSED hasDescendant = {};
2401 
2402 /// \brief Matches AST nodes that have child AST nodes that match the
2403 /// provided matcher.
2404 ///
2405 /// Example matches X, Y
2406 /// (matcher = cxxRecordDecl(forEach(cxxRecordDecl(hasName("X")))
2407 /// \code
2408 /// class X {}; // Matches X, because X::X is a class of name X inside X.
2409 /// class Y { class X {}; };
2410 /// class Z { class Y { class X {}; }; }; // Does not match Z.
2411 /// \endcode
2412 ///
2413 /// ChildT must be an AST base type.
2414 ///
2415 /// As opposed to 'has', 'forEach' will cause a match for each result that
2416 /// matches instead of only on the first one.
2417 ///
2418 /// Usable as: Any Matcher
2419 const internal::ArgumentAdaptingMatcherFunc<internal::ForEachMatcher>
2420 LLVM_ATTRIBUTE_UNUSED forEach = {};
2421 
2422 /// \brief Matches AST nodes that have descendant AST nodes that match the
2423 /// provided matcher.
2424 ///
2425 /// Example matches X, A, B, C
2426 /// (matcher = cxxRecordDecl(forEachDescendant(cxxRecordDecl(hasName("X")))))
2427 /// \code
2428 /// class X {}; // Matches X, because X::X is a class of name X inside X.
2429 /// class A { class X {}; };
2430 /// class B { class C { class X {}; }; };
2431 /// \endcode
2432 ///
2433 /// DescendantT must be an AST base type.
2434 ///
2435 /// As opposed to 'hasDescendant', 'forEachDescendant' will cause a match for
2436 /// each result that matches instead of only on the first one.
2437 ///
2438 /// Note: Recursively combined ForEachDescendant can cause many matches:
2439 /// cxxRecordDecl(forEachDescendant(cxxRecordDecl(
2440 /// forEachDescendant(cxxRecordDecl())
2441 /// )))
2442 /// will match 10 times (plus injected class name matches) on:
2443 /// \code
2444 /// class A { class B { class C { class D { class E {}; }; }; }; };
2445 /// \endcode
2446 ///
2447 /// Usable as: Any Matcher
2448 const internal::ArgumentAdaptingMatcherFunc<internal::ForEachDescendantMatcher>
2449 LLVM_ATTRIBUTE_UNUSED forEachDescendant = {};
2450 
2451 /// \brief Matches if the node or any descendant matches.
2452 ///
2453 /// Generates results for each match.
2454 ///
2455 /// For example, in:
2456 /// \code
2457 /// class A { class B {}; class C {}; };
2458 /// \endcode
2459 /// The matcher:
2460 /// \code
2461 /// cxxRecordDecl(hasName("::A"),
2462 /// findAll(cxxRecordDecl(isDefinition()).bind("m")))
2463 /// \endcode
2464 /// will generate results for \c A, \c B and \c C.
2465 ///
2466 /// Usable as: Any Matcher
2467 template <typename T>
2468 internal::Matcher<T> findAll(const internal::Matcher<T> &Matcher) {
2469  return eachOf(Matcher, forEachDescendant(Matcher));
2470 }
2471 
2472 /// \brief Matches AST nodes that have a parent that matches the provided
2473 /// matcher.
2474 ///
2475 /// Given
2476 /// \code
2477 /// void f() { for (;;) { int x = 42; if (true) { int x = 43; } } }
2478 /// \endcode
2479 /// \c compoundStmt(hasParent(ifStmt())) matches "{ int x = 43; }".
2480 ///
2481 /// Usable as: Any Matcher
2482 const internal::ArgumentAdaptingMatcherFunc<
2483  internal::HasParentMatcher,
2484  internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>,
2485  internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>>
2486  LLVM_ATTRIBUTE_UNUSED hasParent = {};
2487 
2488 /// \brief Matches AST nodes that have an ancestor that matches the provided
2489 /// matcher.
2490 ///
2491 /// Given
2492 /// \code
2493 /// void f() { if (true) { int x = 42; } }
2494 /// void g() { for (;;) { int x = 43; } }
2495 /// \endcode
2496 /// \c expr(integerLiteral(hasAncestor(ifStmt()))) matches \c 42, but not 43.
2497 ///
2498 /// Usable as: Any Matcher
2499 const internal::ArgumentAdaptingMatcherFunc<
2500  internal::HasAncestorMatcher,
2501  internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>,
2502  internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>>
2503  LLVM_ATTRIBUTE_UNUSED hasAncestor = {};
2504 
2505 /// \brief Matches if the provided matcher does not match.
2506 ///
2507 /// Example matches Y (matcher = cxxRecordDecl(unless(hasName("X"))))
2508 /// \code
2509 /// class X {};
2510 /// class Y {};
2511 /// \endcode
2512 ///
2513 /// Usable as: Any Matcher
2514 const internal::VariadicOperatorMatcherFunc<1, 1> unless = {
2515  internal::DynTypedMatcher::VO_UnaryNot
2516 };
2517 
2518 /// \brief Matches a node if the declaration associated with that node
2519 /// matches the given matcher.
2520 ///
2521 /// The associated declaration is:
2522 /// - for type nodes, the declaration of the underlying type
2523 /// - for CallExpr, the declaration of the callee
2524 /// - for MemberExpr, the declaration of the referenced member
2525 /// - for CXXConstructExpr, the declaration of the constructor
2526 /// - for CXXNewExpr, the declaration of the operator new
2527 ///
2528 /// Also usable as Matcher<T> for any T supporting the getDecl() member
2529 /// function. e.g. various subtypes of clang::Type and various expressions.
2530 ///
2531 /// Usable as: Matcher<AddrLabelExpr>, Matcher<CallExpr>,
2532 /// Matcher<CXXConstructExpr>, Matcher<CXXNewExpr>, Matcher<DeclRefExpr>,
2533 /// Matcher<EnumType>, Matcher<InjectedClassNameType>, Matcher<LabelStmt>,
2534 /// Matcher<MemberExpr>, Matcher<QualType>, Matcher<RecordType>,
2535 /// Matcher<TagType>, Matcher<TemplateSpecializationType>,
2536 /// Matcher<TemplateTypeParmType>, Matcher<TypedefType>,
2537 /// Matcher<UnresolvedUsingType>
2538 inline internal::PolymorphicMatcherWithParam1<
2539  internal::HasDeclarationMatcher, internal::Matcher<Decl>,
2540  void(internal::HasDeclarationSupportedTypes)>
2541 hasDeclaration(const internal::Matcher<Decl> &InnerMatcher) {
2542  return internal::PolymorphicMatcherWithParam1<
2543  internal::HasDeclarationMatcher, internal::Matcher<Decl>,
2544  void(internal::HasDeclarationSupportedTypes)>(InnerMatcher);
2545 }
2546 
2547 /// \brief Matches a \c NamedDecl whose underlying declaration matches the given
2548 /// matcher.
2549 ///
2550 /// Given
2551 /// \code
2552 /// namespace N { template<class T> void f(T t); }
2553 /// template <class T> void g() { using N::f; f(T()); }
2554 /// \endcode
2555 /// \c unresolvedLookupExpr(hasAnyDeclaration(
2556 /// namedDecl(hasUnderlyingDecl(hasName("::N::f")))))
2557 /// matches the use of \c f in \c g() .
2558 AST_MATCHER_P(NamedDecl, hasUnderlyingDecl, internal::Matcher<NamedDecl>,
2559  InnerMatcher) {
2560  const NamedDecl *UnderlyingDecl = Node.getUnderlyingDecl();
2561 
2562  return UnderlyingDecl != nullptr &&
2563  InnerMatcher.matches(*UnderlyingDecl, Finder, Builder);
2564 }
2565 
2566 /// \brief Matches on the implicit object argument of a member call expression.
2567 ///
2568 /// Example matches y.x()
2569 /// (matcher = cxxMemberCallExpr(on(hasType(cxxRecordDecl(hasName("Y"))))))
2570 /// \code
2571 /// class Y { public: void x(); };
2572 /// void z() { Y y; y.x(); }",
2573 /// \endcode
2574 ///
2575 /// FIXME: Overload to allow directly matching types?
2576 AST_MATCHER_P(CXXMemberCallExpr, on, internal::Matcher<Expr>,
2577  InnerMatcher) {
2578  const Expr *ExprNode = Node.getImplicitObjectArgument()
2579  ->IgnoreParenImpCasts();
2580  return (ExprNode != nullptr &&
2581  InnerMatcher.matches(*ExprNode, Finder, Builder));
2582 }
2583 
2584 
2585 /// \brief Matches on the receiver of an ObjectiveC Message expression.
2586 ///
2587 /// Example
2588 /// matcher = objCMessageExpr(hasRecieverType(asString("UIWebView *")));
2589 /// matches the [webView ...] message invocation.
2590 /// \code
2591 /// NSString *webViewJavaScript = ...
2592 /// UIWebView *webView = ...
2593 /// [webView stringByEvaluatingJavaScriptFromString:webViewJavascript];
2594 /// \endcode
2595 AST_MATCHER_P(ObjCMessageExpr, hasReceiverType, internal::Matcher<QualType>,
2596  InnerMatcher) {
2597  const QualType TypeDecl = Node.getReceiverType();
2598  return InnerMatcher.matches(TypeDecl, Finder, Builder);
2599 }
2600 
2601 /// \brief Matches when BaseName == Selector.getAsString()
2602 ///
2603 /// matcher = objCMessageExpr(hasSelector("loadHTMLString:baseURL:"));
2604 /// matches the outer message expr in the code below, but NOT the message
2605 /// invocation for self.bodyView.
2606 /// \code
2607 /// [self.bodyView loadHTMLString:html baseURL:NULL];
2608 /// \endcode
2609 AST_MATCHER_P(ObjCMessageExpr, hasSelector, std::string, BaseName) {
2610  Selector Sel = Node.getSelector();
2611  return BaseName.compare(Sel.getAsString()) == 0;
2612 }
2613 
2614 
2615 /// \brief Matches ObjC selectors whose name contains
2616 /// a substring matched by the given RegExp.
2617 /// matcher = objCMessageExpr(matchesSelector("loadHTMLString\:baseURL?"));
2618 /// matches the outer message expr in the code below, but NOT the message
2619 /// invocation for self.bodyView.
2620 /// \code
2621 /// [self.bodyView loadHTMLString:html baseURL:NULL];
2622 /// \endcode
2623 AST_MATCHER_P(ObjCMessageExpr, matchesSelector, std::string, RegExp) {
2624  assert(!RegExp.empty());
2625  std::string SelectorString = Node.getSelector().getAsString();
2626  llvm::Regex RE(RegExp);
2627  return RE.match(SelectorString);
2628 }
2629 
2630 /// \brief Matches when the selector is the empty selector
2631 ///
2632 /// Matches only when the selector of the objCMessageExpr is NULL. This may
2633 /// represent an error condition in the tree!
2634 AST_MATCHER(ObjCMessageExpr, hasNullSelector) {
2635  return Node.getSelector().isNull();
2636 }
2637 
2638 /// \brief Matches when the selector is a Unary Selector
2639 ///
2640 /// matcher = objCMessageExpr(matchesSelector(hasUnarySelector());
2641 /// matches self.bodyView in the code below, but NOT the outer message
2642 /// invocation of "loadHTMLString:baseURL:".
2643 /// \code
2644 /// [self.bodyView loadHTMLString:html baseURL:NULL];
2645 /// \endcode
2646 AST_MATCHER(ObjCMessageExpr, hasUnarySelector) {
2647  return Node.getSelector().isUnarySelector();
2648 }
2649 
2650 /// \brief Matches when the selector is a keyword selector
2651 ///
2652 /// objCMessageExpr(hasKeywordSelector()) matches the generated setFrame
2653 /// message expression in
2654 ///
2655 /// \code
2656 /// UIWebView *webView = ...;
2657 /// CGRect bodyFrame = webView.frame;
2658 /// bodyFrame.size.height = self.bodyContentHeight;
2659 /// webView.frame = bodyFrame;
2660 /// // ^---- matches here
2661 /// \endcode
2662 AST_MATCHER(ObjCMessageExpr, hasKeywordSelector) {
2663  return Node.getSelector().isKeywordSelector();
2664 }
2665 
2666 /// \brief Matches when the selector has the specified number of arguments
2667 ///
2668 /// matcher = objCMessageExpr(numSelectorArgs(0));
2669 /// matches self.bodyView in the code below
2670 ///
2671 /// matcher = objCMessageExpr(numSelectorArgs(2));
2672 /// matches the invocation of "loadHTMLString:baseURL:" but not that
2673 /// of self.bodyView
2674 /// \code
2675 /// [self.bodyView loadHTMLString:html baseURL:NULL];
2676 /// \endcode
2677 AST_MATCHER_P(ObjCMessageExpr, numSelectorArgs, unsigned, N) {
2678  return Node.getSelector().getNumArgs() == N;
2679 }
2680 
2681 /// \brief Matches if the call expression's callee expression matches.
2682 ///
2683 /// Given
2684 /// \code
2685 /// class Y { void x() { this->x(); x(); Y y; y.x(); } };
2686 /// void f() { f(); }
2687 /// \endcode
2688 /// callExpr(callee(expr()))
2689 /// matches this->x(), x(), y.x(), f()
2690 /// with callee(...)
2691 /// matching this->x, x, y.x, f respectively
2692 ///
2693 /// Note: Callee cannot take the more general internal::Matcher<Expr>
2694 /// because this introduces ambiguous overloads with calls to Callee taking a
2695 /// internal::Matcher<Decl>, as the matcher hierarchy is purely
2696 /// implemented in terms of implicit casts.
2697 AST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>,
2698  InnerMatcher) {
2699  const Expr *ExprNode = Node.getCallee();
2700  return (ExprNode != nullptr &&
2701  InnerMatcher.matches(*ExprNode, Finder, Builder));
2702 }
2703 
2704 /// \brief Matches if the call expression's callee's declaration matches the
2705 /// given matcher.
2706 ///
2707 /// Example matches y.x() (matcher = callExpr(callee(
2708 /// cxxMethodDecl(hasName("x")))))
2709 /// \code
2710 /// class Y { public: void x(); };
2711 /// void z() { Y y; y.x(); }
2712 /// \endcode
2713 AST_MATCHER_P_OVERLOAD(CallExpr, callee, internal::Matcher<Decl>, InnerMatcher,
2714  1) {
2715  return callExpr(hasDeclaration(InnerMatcher)).matches(Node, Finder, Builder);
2716 }
2717 
2718 /// \brief Matches if the expression's or declaration's type matches a type
2719 /// matcher.
2720 ///
2721 /// Example matches x (matcher = expr(hasType(cxxRecordDecl(hasName("X")))))
2722 /// and z (matcher = varDecl(hasType(cxxRecordDecl(hasName("X")))))
2723 /// and U (matcher = typedefDecl(hasType(asString("int")))
2724 /// \code
2725 /// class X {};
2726 /// void y(X &x) { x; X z; }
2727 /// typedef int U;
2728 /// \endcode
2731  internal::Matcher<QualType>, InnerMatcher, 0) {
2732  return InnerMatcher.matches(internal::getUnderlyingType(Node),
2733  Finder, Builder);
2734 }
2735 
2736 /// \brief Overloaded to match the declaration of the expression's or value
2737 /// declaration's type.
2738 ///
2739 /// In case of a value declaration (for example a variable declaration),
2740 /// this resolves one layer of indirection. For example, in the value
2741 /// declaration "X x;", cxxRecordDecl(hasName("X")) matches the declaration of
2742 /// X, while varDecl(hasType(cxxRecordDecl(hasName("X")))) matches the
2743 /// declaration of x.
2744 ///
2745 /// Example matches x (matcher = expr(hasType(cxxRecordDecl(hasName("X")))))
2746 /// and z (matcher = varDecl(hasType(cxxRecordDecl(hasName("X")))))
2747 /// \code
2748 /// class X {};
2749 /// void y(X &x) { x; X z; }
2750 /// \endcode
2751 ///
2752 /// Usable as: Matcher<Expr>, Matcher<ValueDecl>
2755  ValueDecl),
2756  internal::Matcher<Decl>, InnerMatcher, 1) {
2757  return qualType(hasDeclaration(InnerMatcher))
2758  .matches(Node.getType(), Finder, Builder);
2759 }
2760 
2761 /// \brief Matches if the type location of the declarator decl's type matches
2762 /// the inner matcher.
2763 ///
2764 /// Given
2765 /// \code
2766 /// int x;
2767 /// \endcode
2768 /// declaratorDecl(hasTypeLoc(loc(asString("int"))))
2769 /// matches int x
2770 AST_MATCHER_P(DeclaratorDecl, hasTypeLoc, internal::Matcher<TypeLoc>, Inner) {
2771  if (!Node.getTypeSourceInfo())
2772  // This happens for example for implicit destructors.
2773  return false;
2774  return Inner.matches(Node.getTypeSourceInfo()->getTypeLoc(), Finder, Builder);
2775 }
2776 
2777 /// \brief Matches if the matched type is represented by the given string.
2778 ///
2779 /// Given
2780 /// \code
2781 /// class Y { public: void x(); };
2782 /// void z() { Y* y; y->x(); }
2783 /// \endcode
2784 /// cxxMemberCallExpr(on(hasType(asString("class Y *"))))
2785 /// matches y->x()
2786 AST_MATCHER_P(QualType, asString, std::string, Name) {
2787  return Name == Node.getAsString();
2788 }
2789 
2790 /// \brief Matches if the matched type is a pointer type and the pointee type
2791 /// matches the specified matcher.
2792 ///
2793 /// Example matches y->x()
2794 /// (matcher = cxxMemberCallExpr(on(hasType(pointsTo
2795 /// cxxRecordDecl(hasName("Y")))))))
2796 /// \code
2797 /// class Y { public: void x(); };
2798 /// void z() { Y *y; y->x(); }
2799 /// \endcode
2801  QualType, pointsTo, internal::Matcher<QualType>,
2802  InnerMatcher) {
2803  return (!Node.isNull() && Node->isAnyPointerType() &&
2804  InnerMatcher.matches(Node->getPointeeType(), Finder, Builder));
2805 }
2806 
2807 /// \brief Overloaded to match the pointee type's declaration.
2808 AST_MATCHER_P_OVERLOAD(QualType, pointsTo, internal::Matcher<Decl>,
2809  InnerMatcher, 1) {
2810  return pointsTo(qualType(hasDeclaration(InnerMatcher)))
2811  .matches(Node, Finder, Builder);
2812 }
2813 
2814 /// \brief Matches if the matched type matches the unqualified desugared
2815 /// type of the matched node.
2816 ///
2817 /// For example, in:
2818 /// \code
2819 /// class A {};
2820 /// using B = A;
2821 /// \endcode
2822 /// The matcher type(hasUniqualifeidDesugaredType(recordType())) matches
2823 /// both B and A.
2824 AST_MATCHER_P(Type, hasUnqualifiedDesugaredType, internal::Matcher<Type>,
2825  InnerMatcher) {
2826  return InnerMatcher.matches(*Node.getUnqualifiedDesugaredType(), Finder,
2827  Builder);
2828 }
2829 
2830 /// \brief Matches if the matched type is a reference type and the referenced
2831 /// type matches the specified matcher.
2832 ///
2833 /// Example matches X &x and const X &y
2834 /// (matcher = varDecl(hasType(references(cxxRecordDecl(hasName("X"))))))
2835 /// \code
2836 /// class X {
2837 /// void a(X b) {
2838 /// X &x = b;
2839 /// const X &y = b;
2840 /// }
2841 /// };
2842 /// \endcode
2843 AST_MATCHER_P(QualType, references, internal::Matcher<QualType>,
2844  InnerMatcher) {
2845  return (!Node.isNull() && Node->isReferenceType() &&
2846  InnerMatcher.matches(Node->getPointeeType(), Finder, Builder));
2847 }
2848 
2849 /// \brief Matches QualTypes whose canonical type matches InnerMatcher.
2850 ///
2851 /// Given:
2852 /// \code
2853 /// typedef int &int_ref;
2854 /// int a;
2855 /// int_ref b = a;
2856 /// \endcode
2857 ///
2858 /// \c varDecl(hasType(qualType(referenceType()))))) will not match the
2859 /// declaration of b but \c
2860 /// varDecl(hasType(qualType(hasCanonicalType(referenceType())))))) does.
2861 AST_MATCHER_P(QualType, hasCanonicalType, internal::Matcher<QualType>,
2862  InnerMatcher) {
2863  if (Node.isNull())
2864  return false;
2865  return InnerMatcher.matches(Node.getCanonicalType(), Finder, Builder);
2866 }
2867 
2868 /// \brief Overloaded to match the referenced type's declaration.
2869 AST_MATCHER_P_OVERLOAD(QualType, references, internal::Matcher<Decl>,
2870  InnerMatcher, 1) {
2871  return references(qualType(hasDeclaration(InnerMatcher)))
2872  .matches(Node, Finder, Builder);
2873 }
2874 
2875 AST_MATCHER_P(CXXMemberCallExpr, onImplicitObjectArgument,
2876  internal::Matcher<Expr>, InnerMatcher) {
2877  const Expr *ExprNode = Node.getImplicitObjectArgument();
2878  return (ExprNode != nullptr &&
2879  InnerMatcher.matches(*ExprNode, Finder, Builder));
2880 }
2881 
2882 /// \brief Matches if the expression's type either matches the specified
2883 /// matcher, or is a pointer to a type that matches the InnerMatcher.
2885  internal::Matcher<QualType>, InnerMatcher, 0) {
2886  return onImplicitObjectArgument(
2887  anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher))))
2888  .matches(Node, Finder, Builder);
2889 }
2890 
2891 /// \brief Overloaded to match the type's declaration.
2893  internal::Matcher<Decl>, InnerMatcher, 1) {
2894  return onImplicitObjectArgument(
2895  anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher))))
2896  .matches(Node, Finder, Builder);
2897 }
2898 
2899 /// \brief Matches a DeclRefExpr that refers to a declaration that matches the
2900 /// specified matcher.
2901 ///
2902 /// Example matches x in if(x)
2903 /// (matcher = declRefExpr(to(varDecl(hasName("x")))))
2904 /// \code
2905 /// bool x;
2906 /// if (x) {}
2907 /// \endcode
2908 AST_MATCHER_P(DeclRefExpr, to, internal::Matcher<Decl>,
2909  InnerMatcher) {
2910  const Decl *DeclNode = Node.getDecl();
2911  return (DeclNode != nullptr &&
2912  InnerMatcher.matches(*DeclNode, Finder, Builder));
2913 }
2914 
2915 /// \brief Matches a \c DeclRefExpr that refers to a declaration through a
2916 /// specific using shadow declaration.
2917 ///
2918 /// Given
2919 /// \code
2920 /// namespace a { void f() {} }
2921 /// using a::f;
2922 /// void g() {
2923 /// f(); // Matches this ..
2924 /// a::f(); // .. but not this.
2925 /// }
2926 /// \endcode
2927 /// declRefExpr(throughUsingDecl(anything()))
2928 /// matches \c f()
2929 AST_MATCHER_P(DeclRefExpr, throughUsingDecl,
2930  internal::Matcher<UsingShadowDecl>, InnerMatcher) {
2931  const NamedDecl *FoundDecl = Node.getFoundDecl();
2932  if (const UsingShadowDecl *UsingDecl = dyn_cast<UsingShadowDecl>(FoundDecl))
2933  return InnerMatcher.matches(*UsingDecl, Finder, Builder);
2934  return false;
2935 }
2936 
2937 /// \brief Matches an \c OverloadExpr if any of the declarations in the set of
2938 /// overloads matches the given matcher.
2939 ///
2940 /// Given
2941 /// \code
2942 /// template <typename T> void foo(T);
2943 /// template <typename T> void bar(T);
2944 /// template <typename T> void baz(T t) {
2945 /// foo(t);
2946 /// bar(t);
2947 /// }
2948 /// \endcode
2949 /// unresolvedLookupExpr(hasAnyDeclaration(
2950 /// functionTemplateDecl(hasName("foo"))))
2951 /// matches \c foo in \c foo(t); but not \c bar in \c bar(t);
2952 AST_MATCHER_P(OverloadExpr, hasAnyDeclaration, internal::Matcher<Decl>,
2953  InnerMatcher) {
2954  return matchesFirstInPointerRange(InnerMatcher, Node.decls_begin(),
2955  Node.decls_end(), Finder, Builder);
2956 }
2957 
2958 /// \brief Matches the Decl of a DeclStmt which has a single declaration.
2959 ///
2960 /// Given
2961 /// \code
2962 /// int a, b;
2963 /// int c;
2964 /// \endcode
2965 /// declStmt(hasSingleDecl(anything()))
2966 /// matches 'int c;' but not 'int a, b;'.
2967 AST_MATCHER_P(DeclStmt, hasSingleDecl, internal::Matcher<Decl>, InnerMatcher) {
2968  if (Node.isSingleDecl()) {
2969  const Decl *FoundDecl = Node.getSingleDecl();
2970  return InnerMatcher.matches(*FoundDecl, Finder, Builder);
2971  }
2972  return false;
2973 }
2974 
2975 /// \brief Matches a variable declaration that has an initializer expression
2976 /// that matches the given matcher.
2977 ///
2978 /// Example matches x (matcher = varDecl(hasInitializer(callExpr())))
2979 /// \code
2980 /// bool y() { return true; }
2981 /// bool x = y();
2982 /// \endcode
2984  VarDecl, hasInitializer, internal::Matcher<Expr>,
2985  InnerMatcher) {
2986  const Expr *Initializer = Node.getAnyInitializer();
2987  return (Initializer != nullptr &&
2988  InnerMatcher.matches(*Initializer, Finder, Builder));
2989 }
2990 
2991 /// \brief Matches a variable declaration that has function scope and is a
2992 /// non-static local variable.
2993 ///
2994 /// Example matches x (matcher = varDecl(hasLocalStorage())
2995 /// \code
2996 /// void f() {
2997 /// int x;
2998 /// static int y;
2999 /// }
3000 /// int z;
3001 /// \endcode
3002 AST_MATCHER(VarDecl, hasLocalStorage) {
3003  return Node.hasLocalStorage();
3004 }
3005 
3006 /// \brief Matches a variable declaration that does not have local storage.
3007 ///
3008 /// Example matches y and z (matcher = varDecl(hasGlobalStorage())
3009 /// \code
3010 /// void f() {
3011 /// int x;
3012 /// static int y;
3013 /// }
3014 /// int z;
3015 /// \endcode
3016 AST_MATCHER(VarDecl, hasGlobalStorage) {
3017  return Node.hasGlobalStorage();
3018 }
3019 
3020 /// \brief Matches a variable declaration that has automatic storage duration.
3021 ///
3022 /// Example matches x, but not y, z, or a.
3023 /// (matcher = varDecl(hasAutomaticStorageDuration())
3024 /// \code
3025 /// void f() {
3026 /// int x;
3027 /// static int y;
3028 /// thread_local int z;
3029 /// }
3030 /// int a;
3031 /// \endcode
3032 AST_MATCHER(VarDecl, hasAutomaticStorageDuration) {
3033  return Node.getStorageDuration() == SD_Automatic;
3034 }
3035 
3036 /// \brief Matches a variable declaration that has static storage duration.
3037 /// It includes the variable declared at namespace scope and those declared
3038 /// with "static" and "extern" storage class specifiers.
3039 ///
3040 /// \code
3041 /// void f() {
3042 /// int x;
3043 /// static int y;
3044 /// thread_local int z;
3045 /// }
3046 /// int a;
3047 /// static int b;
3048 /// extern int c;
3049 /// varDecl(hasStaticStorageDuration())
3050 /// matches the function declaration y, a, b and c.
3051 /// \endcode
3052 AST_MATCHER(VarDecl, hasStaticStorageDuration) {
3053  return Node.getStorageDuration() == SD_Static;
3054 }
3055 
3056 /// \brief Matches a variable declaration that has thread storage duration.
3057 ///
3058 /// Example matches z, but not x, z, or a.
3059 /// (matcher = varDecl(hasThreadStorageDuration())
3060 /// \code
3061 /// void f() {
3062 /// int x;
3063 /// static int y;
3064 /// thread_local int z;
3065 /// }
3066 /// int a;
3067 /// \endcode
3068 AST_MATCHER(VarDecl, hasThreadStorageDuration) {
3069  return Node.getStorageDuration() == SD_Thread;
3070 }
3071 
3072 /// \brief Matches a variable declaration that is an exception variable from
3073 /// a C++ catch block, or an Objective-C \@catch statement.
3074 ///
3075 /// Example matches x (matcher = varDecl(isExceptionVariable())
3076 /// \code
3077 /// void f(int y) {
3078 /// try {
3079 /// } catch (int x) {
3080 /// }
3081 /// }
3082 /// \endcode
3083 AST_MATCHER(VarDecl, isExceptionVariable) {
3084  return Node.isExceptionVariable();
3085 }
3086 
3087 /// \brief Checks that a call expression or a constructor call expression has
3088 /// a specific number of arguments (including absent default arguments).
3089 ///
3090 /// Example matches f(0, 0) (matcher = callExpr(argumentCountIs(2)))
3091 /// \code
3092 /// void f(int x, int y);
3093 /// f(0, 0);
3094 /// \endcode
3098  ObjCMessageExpr),
3099  unsigned, N) {
3100  return Node.getNumArgs() == N;
3101 }
3102 
3103 /// \brief Matches the n'th argument of a call expression or a constructor
3104 /// call expression.
3105 ///
3106 /// Example matches y in x(y)
3107 /// (matcher = callExpr(hasArgument(0, declRefExpr())))
3108 /// \code
3109 /// void x(int) { int y; x(y); }
3110 /// \endcode
3114  ObjCMessageExpr),
3115  unsigned, N, internal::Matcher<Expr>, InnerMatcher) {
3116  return (N < Node.getNumArgs() &&
3117  InnerMatcher.matches(
3118  *Node.getArg(N)->IgnoreParenImpCasts(), Finder, Builder));
3119 }
3120 
3121 /// \brief Matches declaration statements that contain a specific number of
3122 /// declarations.
3123 ///
3124 /// Example: Given
3125 /// \code
3126 /// int a, b;
3127 /// int c;
3128 /// int d = 2, e;
3129 /// \endcode
3130 /// declCountIs(2)
3131 /// matches 'int a, b;' and 'int d = 2, e;', but not 'int c;'.
3132 AST_MATCHER_P(DeclStmt, declCountIs, unsigned, N) {
3133  return std::distance(Node.decl_begin(), Node.decl_end()) == (ptrdiff_t)N;
3134 }
3135 
3136 /// \brief Matches the n'th declaration of a declaration statement.
3137 ///
3138 /// Note that this does not work for global declarations because the AST
3139 /// breaks up multiple-declaration DeclStmt's into multiple single-declaration
3140 /// DeclStmt's.
3141 /// Example: Given non-global declarations
3142 /// \code
3143 /// int a, b = 0;
3144 /// int c;
3145 /// int d = 2, e;
3146 /// \endcode
3147 /// declStmt(containsDeclaration(
3148 /// 0, varDecl(hasInitializer(anything()))))
3149 /// matches only 'int d = 2, e;', and
3150 /// declStmt(containsDeclaration(1, varDecl()))
3151 /// \code
3152 /// matches 'int a, b = 0' as well as 'int d = 2, e;'
3153 /// but 'int c;' is not matched.
3154 /// \endcode
3155 AST_MATCHER_P2(DeclStmt, containsDeclaration, unsigned, N,
3156  internal::Matcher<Decl>, InnerMatcher) {
3157  const unsigned NumDecls = std::distance(Node.decl_begin(), Node.decl_end());
3158  if (N >= NumDecls)
3159  return false;
3160  DeclStmt::const_decl_iterator Iterator = Node.decl_begin();
3161  std::advance(Iterator, N);
3162  return InnerMatcher.matches(**Iterator, Finder, Builder);
3163 }
3164 
3165 /// \brief Matches a C++ catch statement that has a catch-all handler.
3166 ///
3167 /// Given
3168 /// \code
3169 /// try {
3170 /// // ...
3171 /// } catch (int) {
3172 /// // ...
3173 /// } catch (...) {
3174 /// // ...
3175 /// }
3176 /// /endcode
3177 /// cxxCatchStmt(isCatchAll()) matches catch(...) but not catch(int).
3178 AST_MATCHER(CXXCatchStmt, isCatchAll) {
3179  return Node.getExceptionDecl() == nullptr;
3180 }
3181 
3182 /// \brief Matches a constructor initializer.
3183 ///
3184 /// Given
3185 /// \code
3186 /// struct Foo {
3187 /// Foo() : foo_(1) { }
3188 /// int foo_;
3189 /// };
3190 /// \endcode
3191 /// cxxRecordDecl(has(cxxConstructorDecl(
3192 /// hasAnyConstructorInitializer(anything())
3193 /// )))
3194 /// record matches Foo, hasAnyConstructorInitializer matches foo_(1)
3195 AST_MATCHER_P(CXXConstructorDecl, hasAnyConstructorInitializer,
3196  internal::Matcher<CXXCtorInitializer>, InnerMatcher) {
3197  return matchesFirstInPointerRange(InnerMatcher, Node.init_begin(),
3198  Node.init_end(), Finder, Builder);
3199 }
3200 
3201 /// \brief Matches the field declaration of a constructor initializer.
3202 ///
3203 /// Given
3204 /// \code
3205 /// struct Foo {
3206 /// Foo() : foo_(1) { }
3207 /// int foo_;
3208 /// };
3209 /// \endcode
3210 /// cxxRecordDecl(has(cxxConstructorDecl(hasAnyConstructorInitializer(
3211 /// forField(hasName("foo_"))))))
3212 /// matches Foo
3213 /// with forField matching foo_
3215  internal::Matcher<FieldDecl>, InnerMatcher) {
3216  const FieldDecl *NodeAsDecl = Node.getMember();
3217  return (NodeAsDecl != nullptr &&
3218  InnerMatcher.matches(*NodeAsDecl, Finder, Builder));
3219 }
3220 
3221 /// \brief Matches the initializer expression of a constructor initializer.
3222 ///
3223 /// Given
3224 /// \code
3225 /// struct Foo {
3226 /// Foo() : foo_(1) { }
3227 /// int foo_;
3228 /// };
3229 /// \endcode
3230 /// cxxRecordDecl(has(cxxConstructorDecl(hasAnyConstructorInitializer(
3231 /// withInitializer(integerLiteral(equals(1)))))))
3232 /// matches Foo
3233 /// with withInitializer matching (1)
3235  internal::Matcher<Expr>, InnerMatcher) {
3236  const Expr* NodeAsExpr = Node.getInit();
3237  return (NodeAsExpr != nullptr &&
3238  InnerMatcher.matches(*NodeAsExpr, Finder, Builder));
3239 }
3240 
3241 /// \brief Matches a constructor initializer if it is explicitly written in
3242 /// code (as opposed to implicitly added by the compiler).
3243 ///
3244 /// Given
3245 /// \code
3246 /// struct Foo {
3247 /// Foo() { }
3248 /// Foo(int) : foo_("A") { }
3249 /// string foo_;
3250 /// };
3251 /// \endcode
3252 /// cxxConstructorDecl(hasAnyConstructorInitializer(isWritten()))
3253 /// will match Foo(int), but not Foo()
3255  return Node.isWritten();
3256 }
3257 
3258 /// \brief Matches a constructor initializer if it is initializing a base, as
3259 /// opposed to a member.
3260 ///
3261 /// Given
3262 /// \code
3263 /// struct B {};
3264 /// struct D : B {
3265 /// int I;
3266 /// D(int i) : I(i) {}
3267 /// };
3268 /// struct E : B {
3269 /// E() : B() {}
3270 /// };
3271 /// \endcode
3272 /// cxxConstructorDecl(hasAnyConstructorInitializer(isBaseInitializer()))
3273 /// will match E(), but not match D(int).
3274 AST_MATCHER(CXXCtorInitializer, isBaseInitializer) {
3275  return Node.isBaseInitializer();
3276 }
3277 
3278 /// \brief Matches a constructor initializer if it is initializing a member, as
3279 /// opposed to a base.
3280 ///
3281 /// Given
3282 /// \code
3283 /// struct B {};
3284 /// struct D : B {
3285 /// int I;
3286 /// D(int i) : I(i) {}
3287 /// };
3288 /// struct E : B {
3289 /// E() : B() {}
3290 /// };
3291 /// \endcode
3292 /// cxxConstructorDecl(hasAnyConstructorInitializer(isMemberInitializer()))
3293 /// will match D(int), but not match E().
3294 AST_MATCHER(CXXCtorInitializer, isMemberInitializer) {
3295  return Node.isMemberInitializer();
3296 }
3297 
3298 /// \brief Matches any argument of a call expression or a constructor call
3299 /// expression.
3300 ///
3301 /// Given
3302 /// \code
3303 /// void x(int, int, int) { int y; x(1, y, 42); }
3304 /// \endcode
3305 /// callExpr(hasAnyArgument(declRefExpr()))
3306 /// matches x(1, y, 42)
3307 /// with hasAnyArgument(...)
3308 /// matching y
3312  internal::Matcher<Expr>, InnerMatcher) {
3313  for (const Expr *Arg : Node.arguments()) {
3314  BoundNodesTreeBuilder Result(*Builder);
3315  if (InnerMatcher.matches(*Arg, Finder, &Result)) {
3316  *Builder = std::move(Result);
3317  return true;
3318  }
3319  }
3320  return false;
3321 }
3322 
3323 /// \brief Matches a constructor call expression which uses list initialization.
3324 AST_MATCHER(CXXConstructExpr, isListInitialization) {
3325  return Node.isListInitialization();
3326 }
3327 
3328 /// \brief Matches a constructor call expression which requires
3329 /// zero initialization.
3330 ///
3331 /// Given
3332 /// \code
3333 /// void foo() {
3334 /// struct point { double x; double y; };
3335 /// point pt[2] = { { 1.0, 2.0 } };
3336 /// }
3337 /// \endcode
3338 /// initListExpr(has(cxxConstructExpr(requiresZeroInitialization()))
3339 /// will match the implicit array filler for pt[1].
3340 AST_MATCHER(CXXConstructExpr, requiresZeroInitialization) {
3341  return Node.requiresZeroInitialization();
3342 }
3343 
3344 /// \brief Matches the n'th parameter of a function declaration.
3345 ///
3346 /// Given
3347 /// \code
3348 /// class X { void f(int x) {} };
3349 /// \endcode
3350 /// cxxMethodDecl(hasParameter(0, hasType(varDecl())))
3351 /// matches f(int x) {}
3352 /// with hasParameter(...)
3353 /// matching int x
3355  unsigned, N, internal::Matcher<ParmVarDecl>,
3356  InnerMatcher) {
3357  return (N < Node.getNumParams() &&
3358  InnerMatcher.matches(
3359  *Node.getParamDecl(N), Finder, Builder));
3360 }
3361 
3362 /// \brief Matches all arguments and their respective ParmVarDecl.
3363 ///
3364 /// Given
3365 /// \code
3366 /// void f(int i);
3367 /// int y;
3368 /// f(y);
3369 /// \endcode
3370 /// callExpr(
3371 /// forEachArgumentWithParam(
3372 /// declRefExpr(to(varDecl(hasName("y")))),
3373 /// parmVarDecl(hasType(isInteger()))
3374 /// ))
3375 /// matches f(y);
3376 /// with declRefExpr(...)
3377 /// matching int y
3378 /// and parmVarDecl(...)
3379 /// matching int i
3380 AST_POLYMORPHIC_MATCHER_P2(forEachArgumentWithParam,
3383  internal::Matcher<Expr>, ArgMatcher,
3384  internal::Matcher<ParmVarDecl>, ParamMatcher) {
3385  BoundNodesTreeBuilder Result;
3386  // The first argument of an overloaded member operator is the implicit object
3387  // argument of the method which should not be matched against a parameter, so
3388  // we skip over it here.
3389  BoundNodesTreeBuilder Matches;
3390  unsigned ArgIndex = cxxOperatorCallExpr(callee(cxxMethodDecl()))
3391  .matches(Node, Finder, &Matches)
3392  ? 1
3393  : 0;
3394  int ParamIndex = 0;
3395  bool Matched = false;
3396  for (; ArgIndex < Node.getNumArgs(); ++ArgIndex) {
3397  BoundNodesTreeBuilder ArgMatches(*Builder);
3398  if (ArgMatcher.matches(*(Node.getArg(ArgIndex)->IgnoreParenCasts()),
3399  Finder, &ArgMatches)) {
3400  BoundNodesTreeBuilder ParamMatches(ArgMatches);
3402  hasParameter(ParamIndex, ParamMatcher)))),
3403  callExpr(callee(functionDecl(
3404  hasParameter(ParamIndex, ParamMatcher))))))
3405  .matches(Node, Finder, &ParamMatches)) {
3406  Result.addMatch(ParamMatches);
3407  Matched = true;
3408  }
3409  }
3410  ++ParamIndex;
3411  }
3412  *Builder = std::move(Result);
3413  return Matched;
3414 }
3415 
3416 /// \brief Matches any parameter of a function declaration.
3417 ///
3418 /// Does not match the 'this' parameter of a method.
3419 ///
3420 /// Given
3421 /// \code
3422 /// class X { void f(int x, int y, int z) {} };
3423 /// \endcode
3424 /// cxxMethodDecl(hasAnyParameter(hasName("y")))
3425 /// matches f(int x, int y, int z) {}
3426 /// with hasAnyParameter(...)
3427 /// matching int y
3428 AST_MATCHER_P(FunctionDecl, hasAnyParameter,
3429  internal::Matcher<ParmVarDecl>, InnerMatcher) {
3430  return matchesFirstInPointerRange(InnerMatcher, Node.param_begin(),
3431  Node.param_end(), Finder, Builder);
3432 }
3433 
3434 /// \brief Matches \c FunctionDecls and \c FunctionProtoTypes that have a
3435 /// specific parameter count.
3436 ///
3437 /// Given
3438 /// \code
3439 /// void f(int i) {}
3440 /// void g(int i, int j) {}
3441 /// void h(int i, int j);
3442 /// void j(int i);
3443 /// void k(int x, int y, int z, ...);
3444 /// \endcode
3445 /// functionDecl(parameterCountIs(2))
3446 /// matches void g(int i, int j) {}
3447 /// functionProtoType(parameterCountIs(2))
3448 /// matches void h(int i, int j)
3449 /// functionProtoType(parameterCountIs(3))
3450 /// matches void k(int x, int y, int z, ...);
3451 AST_POLYMORPHIC_MATCHER_P(parameterCountIs,
3454  unsigned, N) {
3455  return Node.getNumParams() == N;
3456 }
3457 
3458 /// \brief Matches the return type of a function declaration.
3459 ///
3460 /// Given:
3461 /// \code
3462 /// class X { int f() { return 1; } };
3463 /// \endcode
3464 /// cxxMethodDecl(returns(asString("int")))
3465 /// matches int f() { return 1; }
3467  internal::Matcher<QualType>, InnerMatcher) {
3468  return InnerMatcher.matches(Node.getReturnType(), Finder, Builder);
3469 }
3470 
3471 /// \brief Matches extern "C" function declarations.
3472 ///
3473 /// Given:
3474 /// \code
3475 /// extern "C" void f() {}
3476 /// extern "C" { void g() {} }
3477 /// void h() {}
3478 /// \endcode
3479 /// functionDecl(isExternC())
3480 /// matches the declaration of f and g, but not the declaration h
3482  VarDecl)) {
3483  return Node.isExternC();
3484 }
3485 
3486 /// \brief Matches variable/function declarations that have "static" storage
3487 /// class specifier ("static" keyword) written in the source.
3488 ///
3489 /// Given:
3490 /// \code
3491 /// static void f() {}
3492 /// static int i = 0;
3493 /// extern int j;
3494 /// int k;
3495 /// \endcode
3496 /// functionDecl(isStaticStorageClass())
3497 /// matches the function declaration f.
3498 /// varDecl(isStaticStorageClass())
3499 /// matches the variable declaration i.
3500 AST_POLYMORPHIC_MATCHER(isStaticStorageClass,
3502  VarDecl)) {
3503  return Node.getStorageClass() == SC_Static;
3504 }
3505 
3506 /// \brief Matches deleted function declarations.
3507 ///
3508 /// Given:
3509 /// \code
3510 /// void Func();
3511 /// void DeletedFunc() = delete;
3512 /// \endcode
3513 /// functionDecl(isDeleted())
3514 /// matches the declaration of DeletedFunc, but not Func.
3516  return Node.isDeleted();
3517 }
3518 
3519 /// \brief Matches defaulted function declarations.
3520 ///
3521 /// Given:
3522 /// \code
3523 /// class A { ~A(); };
3524 /// class B { ~B() = default; };
3525 /// \endcode
3526 /// functionDecl(isDefaulted())
3527 /// matches the declaration of ~B, but not ~A.
3528 AST_MATCHER(FunctionDecl, isDefaulted) {
3529  return Node.isDefaulted();
3530 }
3531 
3532 /// \brief Matches functions that have a dynamic exception specification.
3533 ///
3534 /// Given:
3535 /// \code
3536 /// void f();
3537 /// void g() noexcept;
3538 /// void h() noexcept(true);
3539 /// void i() noexcept(false);
3540 /// void j() throw();
3541 /// void k() throw(int);
3542 /// void l() throw(...);
3543 /// \endcode
3544 /// functionDecl(hasDynamicExceptionSpec()) and
3545 /// functionProtoType(hasDynamicExceptionSpec())
3546 /// match the declarations of j, k, and l, but not f, g, h, or i.
3547 AST_POLYMORPHIC_MATCHER(hasDynamicExceptionSpec,
3549  FunctionProtoType)) {
3550  if (const FunctionProtoType *FnTy = internal::getFunctionProtoType(Node))
3551  return FnTy->hasDynamicExceptionSpec();
3552  return false;
3553 }
3554 
3555 /// \brief Matches functions that have a non-throwing exception specification.
3556 ///
3557 /// Given:
3558 /// \code
3559 /// void f();
3560 /// void g() noexcept;
3561 /// void h() throw();
3562 /// void i() throw(int);
3563 /// void j() noexcept(false);
3564 /// \endcode
3565 /// functionDecl(isNoThrow()) and functionProtoType(isNoThrow())
3566 /// match the declarations of g, and h, but not f, i or j.
3569  FunctionProtoType)) {
3570  const FunctionProtoType *FnTy = internal::getFunctionProtoType(Node);
3571 
3572  // If the function does not have a prototype, then it is assumed to be a
3573  // throwing function (as it would if the function did not have any exception
3574  // specification).
3575  if (!FnTy)
3576  return false;
3577 
3578  // Assume the best for any unresolved exception specification.
3580  return true;
3581 
3582  return FnTy->isNothrow(Finder->getASTContext());
3583 }
3584 
3585 /// \brief Matches constexpr variable and function declarations.
3586 ///
3587 /// Given:
3588 /// \code
3589 /// constexpr int foo = 42;
3590 /// constexpr int bar();
3591 /// \endcode
3592 /// varDecl(isConstexpr())
3593 /// matches the declaration of foo.
3594 /// functionDecl(isConstexpr())
3595 /// matches the declaration of bar.
3598  FunctionDecl)) {
3599  return Node.isConstexpr();
3600 }
3601 
3602 /// \brief Matches the condition expression of an if statement, for loop,
3603 /// switch statement or conditional operator.
3604 ///
3605 /// Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
3606 /// \code
3607 /// if (true) {}
3608 /// \endcode
3610  hasCondition,
3613  internal::Matcher<Expr>, InnerMatcher) {
3614  const Expr *const Condition = Node.getCond();
3615  return (Condition != nullptr &&
3616  InnerMatcher.matches(*Condition, Finder, Builder));
3617 }
3618 
3619 /// \brief Matches the then-statement of an if statement.
3620 ///
3621 /// Examples matches the if statement
3622 /// (matcher = ifStmt(hasThen(cxxBoolLiteral(equals(true)))))
3623 /// \code
3624 /// if (false) true; else false;
3625 /// \endcode
3626 AST_MATCHER_P(IfStmt, hasThen, internal::Matcher<Stmt>, InnerMatcher) {
3627  const Stmt *const Then = Node.getThen();
3628  return (Then != nullptr && InnerMatcher.matches(*Then, Finder, Builder));
3629 }
3630 
3631 /// \brief Matches the else-statement of an if statement.
3632 ///
3633 /// Examples matches the if statement
3634 /// (matcher = ifStmt(hasElse(cxxBoolLiteral(equals(true)))))
3635 /// \code
3636 /// if (false) false; else true;
3637 /// \endcode
3638 AST_MATCHER_P(IfStmt, hasElse, internal::Matcher<Stmt>, InnerMatcher) {
3639  const Stmt *const Else = Node.getElse();
3640  return (Else != nullptr && InnerMatcher.matches(*Else, Finder, Builder));
3641 }
3642 
3643 /// \brief Matches if a node equals a previously bound node.
3644 ///
3645 /// Matches a node if it equals the node previously bound to \p ID.
3646 ///
3647 /// Given
3648 /// \code
3649 /// class X { int a; int b; };
3650 /// \endcode
3651 /// cxxRecordDecl(
3652 /// has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
3653 /// has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))
3654 /// matches the class \c X, as \c a and \c b have the same type.
3655 ///
3656 /// Note that when multiple matches are involved via \c forEach* matchers,
3657 /// \c equalsBoundNodes acts as a filter.
3658 /// For example:
3659 /// compoundStmt(
3660 /// forEachDescendant(varDecl().bind("d")),
3661 /// forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d"))))))
3662 /// will trigger a match for each combination of variable declaration
3663 /// and reference to that variable declaration within a compound statement.
3666  QualType),
3667  std::string, ID) {
3668  // FIXME: Figure out whether it makes sense to allow this
3669  // on any other node types.
3670  // For *Loc it probably does not make sense, as those seem
3671  // unique. For NestedNameSepcifier it might make sense, as
3672  // those also have pointer identity, but I'm not sure whether
3673  // they're ever reused.
3674  internal::NotEqualsBoundNodePredicate Predicate;
3675  Predicate.ID = ID;
3676  Predicate.Node = ast_type_traits::DynTypedNode::create(Node);
3677  return Builder->removeBindings(Predicate);
3678 }
3679 
3680 /// \brief Matches the condition variable statement in an if statement.
3681 ///
3682 /// Given
3683 /// \code
3684 /// if (A* a = GetAPointer()) {}
3685 /// \endcode
3686 /// hasConditionVariableStatement(...)
3687 /// matches 'A* a = GetAPointer()'.
3688 AST_MATCHER_P(IfStmt, hasConditionVariableStatement,
3689  internal::Matcher<DeclStmt>, InnerMatcher) {
3690  const DeclStmt* const DeclarationStatement =
3691  Node.getConditionVariableDeclStmt();
3692  return DeclarationStatement != nullptr &&
3693  InnerMatcher.matches(*DeclarationStatement, Finder, Builder);
3694 }
3695 
3696 /// \brief Matches the index expression of an array subscript expression.
3697 ///
3698 /// Given
3699 /// \code
3700 /// int i[5];
3701 /// void f() { i[1] = 42; }
3702 /// \endcode
3703 /// arraySubscriptExpression(hasIndex(integerLiteral()))
3704 /// matches \c i[1] with the \c integerLiteral() matching \c 1
3706  internal::Matcher<Expr>, InnerMatcher) {
3707  if (const Expr* Expression = Node.getIdx())
3708  return InnerMatcher.matches(*Expression, Finder, Builder);
3709  return false;
3710 }
3711 
3712 /// \brief Matches the base expression of an array subscript expression.
3713 ///
3714 /// Given
3715 /// \code
3716 /// int i[5];
3717 /// void f() { i[1] = 42; }
3718 /// \endcode
3719 /// arraySubscriptExpression(hasBase(implicitCastExpr(
3720 /// hasSourceExpression(declRefExpr()))))
3721 /// matches \c i[1] with the \c declRefExpr() matching \c i
3723  internal::Matcher<Expr>, InnerMatcher) {
3724  if (const Expr* Expression = Node.getBase())
3725  return InnerMatcher.matches(*Expression, Finder, Builder);
3726  return false;
3727 }
3728 
3729 /// \brief Matches a 'for', 'while', 'do while' statement or a function
3730 /// definition that has a given body.
3731 ///
3732 /// Given
3733 /// \code
3734 /// for (;;) {}
3735 /// \endcode
3736 /// hasBody(compoundStmt())
3737 /// matches 'for (;;) {}'
3738 /// with compoundStmt()
3739 /// matching '{}'
3742  WhileStmt,
3744  FunctionDecl),
3745  internal::Matcher<Stmt>, InnerMatcher) {
3746  const Stmt *const Statement = internal::GetBodyMatcher<NodeType>::get(Node);
3747  return (Statement != nullptr &&
3748  InnerMatcher.matches(*Statement, Finder, Builder));
3749 }
3750 
3751 /// \brief Matches compound statements where at least one substatement matches
3752 /// a given matcher. Also matches StmtExprs that have CompoundStmt as children.
3753 ///
3754 /// Given
3755 /// \code
3756 /// { {}; 1+2; }
3757 /// \endcode
3758 /// hasAnySubstatement(compoundStmt())
3759 /// matches '{ {}; 1+2; }'
3760 /// with compoundStmt()
3761 /// matching '{}'
3762 AST_POLYMORPHIC_MATCHER_P(hasAnySubstatement,
3764  StmtExpr),
3765  internal::Matcher<Stmt>, InnerMatcher) {
3766  const CompoundStmt *CS = CompoundStmtMatcher<NodeType>::get(Node);
3767  return CS && matchesFirstInPointerRange(InnerMatcher, CS->body_begin(),
3768  CS->body_end(), Finder, Builder);
3769 }
3770 
3771 /// \brief Checks that a compound statement contains a specific number of
3772 /// child statements.
3773 ///
3774 /// Example: Given
3775 /// \code
3776 /// { for (;;) {} }
3777 /// \endcode
3778 /// compoundStmt(statementCountIs(0)))
3779 /// matches '{}'
3780 /// but does not match the outer compound statement.
3781 AST_MATCHER_P(CompoundStmt, statementCountIs, unsigned, N) {
3782  return Node.size() == N;
3783 }
3784 
3785 /// \brief Matches literals that are equal to the given value.
3786 ///
3787 /// Example matches true (matcher = cxxBoolLiteral(equals(true)))
3788 /// \code
3789 /// true
3790 /// \endcode
3791 ///
3792 /// Usable as: Matcher<CharacterLiteral>, Matcher<CXXBoolLiteral>,
3793 /// Matcher<FloatingLiteral>, Matcher<IntegerLiteral>
3794 template <typename ValueT>
3795 internal::PolymorphicMatcherWithParam1<internal::ValueEqualsMatcher, ValueT>
3796 equals(const ValueT &Value) {
3797  return internal::PolymorphicMatcherWithParam1<
3798  internal::ValueEqualsMatcher,
3799  ValueT>(Value);
3800 }
3801 
3802 /// \brief Matches the operator Name of operator expressions (binary or
3803 /// unary).
3804 ///
3805 /// Example matches a || b (matcher = binaryOperator(hasOperatorName("||")))
3806 /// \code
3807 /// !(a || b)
3808 /// \endcode
3811  UnaryOperator),
3812  std::string, Name) {
3813  return Name == Node.getOpcodeStr(Node.getOpcode());
3814 }
3815 
3816 /// \brief Matches the left hand side of binary operator expressions.
3817 ///
3818 /// Example matches a (matcher = binaryOperator(hasLHS()))
3819 /// \code
3820 /// a || b
3821 /// \endcode
3825  internal::Matcher<Expr>, InnerMatcher) {
3826  const Expr *LeftHandSide = Node.getLHS();
3827  return (LeftHandSide != nullptr &&
3828  InnerMatcher.matches(*LeftHandSide, Finder, Builder));
3829 }
3830 
3831 /// \brief Matches the right hand side of binary operator expressions.
3832 ///
3833 /// Example matches b (matcher = binaryOperator(hasRHS()))
3834 /// \code
3835 /// a || b
3836 /// \endcode
3840  internal::Matcher<Expr>, InnerMatcher) {
3841  const Expr *RightHandSide = Node.getRHS();
3842  return (RightHandSide != nullptr &&
3843  InnerMatcher.matches(*RightHandSide, Finder, Builder));
3844 }
3845 
3846 /// \brief Matches if either the left hand side or the right hand side of a
3847 /// binary operator matches.
3848 inline internal::Matcher<BinaryOperator> hasEitherOperand(
3849  const internal::Matcher<Expr> &InnerMatcher) {
3850  return anyOf(hasLHS(InnerMatcher), hasRHS(InnerMatcher));
3851 }
3852 
3853 /// \brief Matches if the operand of a unary operator matches.
3854 ///
3855 /// Example matches true (matcher = hasUnaryOperand(
3856 /// cxxBoolLiteral(equals(true))))
3857 /// \code
3858 /// !true
3859 /// \endcode
3860 AST_MATCHER_P(UnaryOperator, hasUnaryOperand,
3861  internal::Matcher<Expr>, InnerMatcher) {
3862  const Expr * const Operand = Node.getSubExpr();
3863  return (Operand != nullptr &&
3864  InnerMatcher.matches(*Operand, Finder, Builder));
3865 }
3866 
3867 /// \brief Matches if the cast's source expression
3868 /// or opaque value's source expression matches the given matcher.
3869 ///
3870 /// Example 1: matches "a string"
3871 /// (matcher = castExpr(hasSourceExpression(cxxConstructExpr())))
3872 /// \code
3873 /// class URL { URL(string); };
3874 /// URL url = "a string";
3875 /// \endcode
3876 ///
3877 /// Example 2: matches 'b' (matcher =
3878 /// opaqueValueExpr(hasSourceExpression(implicitCastExpr(declRefExpr())))
3879 /// \code
3880 /// int a = b ?: 1;
3881 /// \endcode
3882 
3883 AST_POLYMORPHIC_MATCHER_P(hasSourceExpression,
3885  OpaqueValueExpr),
3886  internal::Matcher<Expr>, InnerMatcher) {
3887  const Expr *const SubExpression =
3888  internal::GetSourceExpressionMatcher<NodeType>::get(Node);
3889  return (SubExpression != nullptr &&
3890  InnerMatcher.matches(*SubExpression, Finder, Builder));
3891 }
3892 
3893 /// \brief Matches casts that has a given cast kind.
3894 ///
3895 /// Example: matches the implicit cast around \c 0
3896 /// (matcher = castExpr(hasCastKind(CK_NullToPointer)))
3897 /// \code
3898 /// int *p = 0;
3899 /// \endcode
3901  return Node.getCastKind() == Kind;
3902 }
3903 
3904 /// \brief Matches casts whose destination type matches a given matcher.
3905 ///
3906 /// (Note: Clang's AST refers to other conversions as "casts" too, and calls
3907 /// actual casts "explicit" casts.)
3908 AST_MATCHER_P(ExplicitCastExpr, hasDestinationType,
3909  internal::Matcher<QualType>, InnerMatcher) {
3910  const QualType NodeType = Node.getTypeAsWritten();
3911  return InnerMatcher.matches(NodeType, Finder, Builder);
3912 }
3913 
3914 /// \brief Matches implicit casts whose destination type matches a given
3915 /// matcher.
3916 ///
3917 /// FIXME: Unit test this matcher
3918 AST_MATCHER_P(ImplicitCastExpr, hasImplicitDestinationType,
3919  internal::Matcher<QualType>, InnerMatcher) {
3920  return InnerMatcher.matches(Node.getType(), Finder, Builder);
3921 }
3922 
3923 /// \brief Matches RecordDecl object that are spelled with "struct."
3924 ///
3925 /// Example matches S, but not C or U.
3926 /// \code
3927 /// struct S {};
3928 /// class C {};
3929 /// union U {};
3930 /// \endcode
3932  return Node.isStruct();
3933 }
3934 
3935 /// \brief Matches RecordDecl object that are spelled with "union."
3936 ///
3937 /// Example matches U, but not C or S.
3938 /// \code
3939 /// struct S {};
3940 /// class C {};
3941 /// union U {};
3942 /// \endcode
3944  return Node.isUnion();
3945 }
3946 
3947 /// \brief Matches RecordDecl object that are spelled with "class."
3948 ///
3949 /// Example matches C, but not S or U.
3950 /// \code
3951 /// struct S {};
3952 /// class C {};
3953 /// union U {};
3954 /// \endcode
3956  return Node.isClass();
3957 }
3958 
3959 /// \brief Matches the true branch expression of a conditional operator.
3960 ///
3961 /// Example 1 (conditional ternary operator): matches a
3962 /// \code
3963 /// condition ? a : b
3964 /// \endcode
3965 ///
3966 /// Example 2 (conditional binary operator): matches opaqueValueExpr(condition)
3967 /// \code
3968 /// condition ?: b
3969 /// \endcode
3971  internal::Matcher<Expr>, InnerMatcher) {
3972  const Expr *Expression = Node.getTrueExpr();
3973  return (Expression != nullptr &&
3974  InnerMatcher.matches(*Expression, Finder, Builder));
3975 }
3976 
3977 /// \brief Matches the false branch expression of a conditional operator
3978 /// (binary or ternary).
3979 ///
3980 /// Example matches b
3981 /// \code
3982 /// condition ? a : b
3983 /// condition ?: b
3984 /// \endcode
3986  internal::Matcher<Expr>, InnerMatcher) {
3987  const Expr *Expression = Node.getFalseExpr();
3988  return (Expression != nullptr &&
3989  InnerMatcher.matches(*Expression, Finder, Builder));
3990 }
3991 
3992 /// \brief Matches if a declaration has a body attached.
3993 ///
3994 /// Example matches A, va, fa
3995 /// \code
3996 /// class A {};
3997 /// class B; // Doesn't match, as it has no body.
3998 /// int va;
3999 /// extern int vb; // Doesn't match, as it doesn't define the variable.
4000 /// void fa() {}
4001 /// void fb(); // Doesn't match, as it has no body.
4002 /// \endcode
4003 ///
4004 /// Usable as: Matcher<TagDecl>, Matcher<VarDecl>, Matcher<FunctionDecl>
4007  FunctionDecl)) {
4008  return Node.isThisDeclarationADefinition();
4009 }
4010 
4011 /// \brief Matches if a function declaration is variadic.
4012 ///
4013 /// Example matches f, but not g or h. The function i will not match, even when
4014 /// compiled in C mode.
4015 /// \code
4016 /// void f(...);
4017 /// void g(int);
4018 /// template <typename... Ts> void h(Ts...);
4019 /// void i();
4020 /// \endcode
4022  return Node.isVariadic();
4023 }
4024 
4025 /// \brief Matches the class declaration that the given method declaration
4026 /// belongs to.
4027 ///
4028 /// FIXME: Generalize this for other kinds of declarations.
4029 /// FIXME: What other kind of declarations would we need to generalize
4030 /// this to?
4031 ///
4032 /// Example matches A() in the last line
4033 /// (matcher = cxxConstructExpr(hasDeclaration(cxxMethodDecl(
4034 /// ofClass(hasName("A"))))))
4035 /// \code
4036 /// class A {
4037 /// public:
4038 /// A();
4039 /// };
4040 /// A a = A();
4041 /// \endcode
4043  internal::Matcher<CXXRecordDecl>, InnerMatcher) {
4044  const CXXRecordDecl *Parent = Node.getParent();
4045  return (Parent != nullptr &&
4046  InnerMatcher.matches(*Parent, Finder, Builder));
4047 }
4048 
4049 /// \brief Matches each method overriden by the given method. This matcher may
4050 /// produce multiple matches.
4051 ///
4052 /// Given
4053 /// \code
4054 /// class A { virtual void f(); };
4055 /// class B : public A { void f(); };
4056 /// class C : public B { void f(); };
4057 /// \endcode
4058 /// cxxMethodDecl(ofClass(hasName("C")),
4059 /// forEachOverridden(cxxMethodDecl().bind("b"))).bind("d")
4060 /// matches once, with "b" binding "A::f" and "d" binding "C::f" (Note
4061 /// that B::f is not overridden by C::f).
4062 ///
4063 /// The check can produce multiple matches in case of multiple inheritance, e.g.
4064 /// \code
4065 /// class A1 { virtual void f(); };
4066 /// class A2 { virtual void f(); };
4067 /// class C : public A1, public A2 { void f(); };
4068 /// \endcode
4069 /// cxxMethodDecl(ofClass(hasName("C")),
4070 /// forEachOverridden(cxxMethodDecl().bind("b"))).bind("d")
4071 /// matches twice, once with "b" binding "A1::f" and "d" binding "C::f", and
4072 /// once with "b" binding "A2::f" and "d" binding "C::f".
4073 AST_MATCHER_P(CXXMethodDecl, forEachOverridden,
4074  internal::Matcher<CXXMethodDecl>, InnerMatcher) {
4075  BoundNodesTreeBuilder Result;
4076  bool Matched = false;
4077  for (const auto *Overridden : Node.overridden_methods()) {
4078  BoundNodesTreeBuilder OverriddenBuilder(*Builder);
4079  const bool OverriddenMatched =
4080  InnerMatcher.matches(*Overridden, Finder, &OverriddenBuilder);
4081  if (OverriddenMatched) {
4082  Matched = true;
4083  Result.addMatch(OverriddenBuilder);
4084  }
4085  }
4086  *Builder = std::move(Result);
4087  return Matched;
4088 }
4089 
4090 /// \brief Matches if the given method declaration is virtual.
4091 ///
4092 /// Given
4093 /// \code
4094 /// class A {
4095 /// public:
4096 /// virtual void x();
4097 /// };
4098 /// \endcode
4099 /// matches A::x
4101  return Node.isVirtual();
4102 }
4103 
4104 /// \brief Matches if the given method declaration has an explicit "virtual".
4105 ///
4106 /// Given
4107 /// \code
4108 /// class A {
4109 /// public:
4110 /// virtual void x();
4111 /// };
4112 /// class B : public A {
4113 /// public:
4114 /// void x();
4115 /// };
4116 /// \endcode
4117 /// matches A::x but not B::x
4118 AST_MATCHER(CXXMethodDecl, isVirtualAsWritten) {
4119  return Node.isVirtualAsWritten();
4120 }
4121 
4122 /// \brief Matches if the given method or class declaration is final.
4123 ///
4124 /// Given:
4125 /// \code
4126 /// class A final {};
4127 ///
4128 /// struct B {
4129 /// virtual void f();
4130 /// };
4131 ///
4132 /// struct C : B {
4133 /// void f() final;
4134 /// };
4135 /// \endcode
4136 /// matches A and C::f, but not B, C, or B::f
4139  CXXMethodDecl)) {
4140  return Node.template hasAttr<FinalAttr>();
4141 }
4142 
4143 /// \brief Matches if the given method declaration is pure.
4144 ///
4145 /// Given
4146 /// \code
4147 /// class A {
4148 /// public:
4149 /// virtual void x() = 0;
4150 /// };
4151 /// \endcode
4152 /// matches A::x
4154  return Node.isPure();
4155 }
4156 
4157 /// \brief Matches if the given method declaration is const.
4158 ///
4159 /// Given
4160 /// \code
4161 /// struct A {
4162 /// void foo() const;
4163 /// void bar();
4164 /// };
4165 /// \endcode
4166 ///
4167 /// cxxMethodDecl(isConst()) matches A::foo() but not A::bar()
4169  return Node.isConst();
4170 }
4171 
4172 /// \brief Matches if the given method declaration declares a copy assignment
4173 /// operator.
4174 ///
4175 /// Given
4176 /// \code
4177 /// struct A {
4178 /// A &operator=(const A &);
4179 /// A &operator=(A &&);
4180 /// };
4181 /// \endcode
4182 ///
4183 /// cxxMethodDecl(isCopyAssignmentOperator()) matches the first method but not
4184 /// the second one.
4185 AST_MATCHER(CXXMethodDecl, isCopyAssignmentOperator) {
4186  return Node.isCopyAssignmentOperator();
4187 }
4188 
4189 /// \brief Matches if the given method declaration declares a move assignment
4190 /// operator.
4191 ///
4192 /// Given
4193 /// \code
4194 /// struct A {
4195 /// A &operator=(const A &);
4196 /// A &operator=(A &&);
4197 /// };
4198 /// \endcode
4199 ///
4200 /// cxxMethodDecl(isMoveAssignmentOperator()) matches the second method but not
4201 /// the first one.
4202 AST_MATCHER(CXXMethodDecl, isMoveAssignmentOperator) {
4203  return Node.isMoveAssignmentOperator();
4204 }
4205 
4206 /// \brief Matches if the given method declaration overrides another method.
4207 ///
4208 /// Given
4209 /// \code
4210 /// class A {
4211 /// public:
4212 /// virtual void x();
4213 /// };
4214 /// class B : public A {
4215 /// public:
4216 /// virtual void x();
4217 /// };
4218 /// \endcode
4219 /// matches B::x
4221  return Node.size_overridden_methods() > 0 || Node.hasAttr<OverrideAttr>();
4222 }
4223 
4224 /// \brief Matches method declarations that are user-provided.
4225 ///
4226 /// Given
4227 /// \code
4228 /// struct S {
4229 /// S(); // #1
4230 /// S(const S &) = default; // #2
4231 /// S(S &&) = delete; // #3
4232 /// };
4233 /// \endcode
4234 /// cxxConstructorDecl(isUserProvided()) will match #1, but not #2 or #3.
4235 AST_MATCHER(CXXMethodDecl, isUserProvided) {
4236  return Node.isUserProvided();
4237 }
4238 
4239 /// \brief Matches member expressions that are called with '->' as opposed
4240 /// to '.'.
4241 ///
4242 /// Member calls on the implicit this pointer match as called with '->'.
4243 ///
4244 /// Given
4245 /// \code
4246 /// class Y {
4247 /// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; }
4248 /// int a;
4249 /// static int b;
4250 /// };
4251 /// \endcode
4252 /// memberExpr(isArrow())
4253 /// matches this->x, x, y.x, a, this->b
4255  return Node.isArrow();
4256 }
4257 
4258 /// \brief Matches QualType nodes that are of integer type.
4259 ///
4260 /// Given
4261 /// \code
4262 /// void a(int);
4263 /// void b(long);
4264 /// void c(double);
4265 /// \endcode
4266 /// functionDecl(hasAnyParameter(hasType(isInteger())))
4267 /// matches "a(int)", "b(long)", but not "c(double)".
4268 AST_MATCHER(QualType, isInteger) {
4269  return Node->isIntegerType();
4270 }
4271 
4272 /// \brief Matches QualType nodes that are of unsigned integer type.
4273 ///
4274 /// Given
4275 /// \code
4276 /// void a(int);
4277 /// void b(unsigned long);
4278 /// void c(double);
4279 /// \endcode
4280 /// functionDecl(hasAnyParameter(hasType(isUnsignedInteger())))
4281 /// matches "b(unsigned long)", but not "a(int)" and "c(double)".
4282 AST_MATCHER(QualType, isUnsignedInteger) {
4283  return Node->isUnsignedIntegerType();
4284 }
4285 
4286 /// \brief Matches QualType nodes that are of signed integer type.
4287 ///
4288 /// Given
4289 /// \code
4290 /// void a(int);
4291 /// void b(unsigned long);
4292 /// void c(double);
4293 /// \endcode
4294 /// functionDecl(hasAnyParameter(hasType(isSignedInteger())))
4295 /// matches "a(int)", but not "b(unsigned long)" and "c(double)".
4296 AST_MATCHER(QualType, isSignedInteger) {
4297  return Node->isSignedIntegerType();
4298 }
4299 
4300 /// \brief Matches QualType nodes that are of character type.
4301 ///
4302 /// Given
4303 /// \code
4304 /// void a(char);
4305 /// void b(wchar_t);
4306 /// void c(double);
4307 /// \endcode
4308 /// functionDecl(hasAnyParameter(hasType(isAnyCharacter())))
4309 /// matches "a(char)", "b(wchar_t)", but not "c(double)".
4310 AST_MATCHER(QualType, isAnyCharacter) {
4311  return Node->isAnyCharacterType();
4312 }
4313 
4314 /// \brief Matches QualType nodes that are of any pointer type; this includes
4315 /// the Objective-C object pointer type, which is different despite being
4316 /// syntactically similar.
4317 ///
4318 /// Given
4319 /// \code
4320 /// int *i = nullptr;
4321 ///
4322 /// @interface Foo
4323 /// @end
4324 /// Foo *f;
4325 ///
4326 /// int j;
4327 /// \endcode
4328 /// varDecl(hasType(isAnyPointer()))
4329 /// matches "int *i" and "Foo *f", but not "int j".
4330 AST_MATCHER(QualType, isAnyPointer) {
4331  return Node->isAnyPointerType();
4332 }
4333 
4334 /// \brief Matches QualType nodes that are const-qualified, i.e., that
4335 /// include "top-level" const.
4336 ///
4337 /// Given
4338 /// \code
4339 /// void a(int);
4340 /// void b(int const);
4341 /// void c(const int);
4342 /// void d(const int*);
4343 /// void e(int const) {};
4344 /// \endcode
4345 /// functionDecl(hasAnyParameter(hasType(isConstQualified())))
4346 /// matches "void b(int const)", "void c(const int)" and
4347 /// "void e(int const) {}". It does not match d as there
4348 /// is no top-level const on the parameter type "const int *".
4349 AST_MATCHER(QualType, isConstQualified) {
4350  return Node.isConstQualified();
4351 }
4352 
4353 /// \brief Matches QualType nodes that are volatile-qualified, i.e., that
4354 /// include "top-level" volatile.
4355 ///
4356 /// Given
4357 /// \code
4358 /// void a(int);
4359 /// void b(int volatile);
4360 /// void c(volatile int);
4361 /// void d(volatile int*);
4362 /// void e(int volatile) {};
4363 /// \endcode
4364 /// functionDecl(hasAnyParameter(hasType(isVolatileQualified())))
4365 /// matches "void b(int volatile)", "void c(volatile int)" and
4366 /// "void e(int volatile) {}". It does not match d as there
4367 /// is no top-level volatile on the parameter type "volatile int *".
4368 AST_MATCHER(QualType, isVolatileQualified) {
4369  return Node.isVolatileQualified();
4370 }
4371 
4372 /// \brief Matches QualType nodes that have local CV-qualifiers attached to
4373 /// the node, not hidden within a typedef.
4374 ///
4375 /// Given
4376 /// \code
4377 /// typedef const int const_int;
4378 /// const_int i;
4379 /// int *const j;
4380 /// int *volatile k;
4381 /// int m;
4382 /// \endcode
4383 /// \c varDecl(hasType(hasLocalQualifiers())) matches only \c j and \c k.
4384 /// \c i is const-qualified but the qualifier is not local.
4385 AST_MATCHER(QualType, hasLocalQualifiers) {
4386  return Node.hasLocalQualifiers();
4387 }
4388 
4389 /// \brief Matches a member expression where the member is matched by a
4390 /// given matcher.
4391 ///
4392 /// Given
4393 /// \code
4394 /// struct { int first, second; } first, second;
4395 /// int i(second.first);
4396 /// int j(first.second);
4397 /// \endcode
4398 /// memberExpr(member(hasName("first")))
4399 /// matches second.first
4400 /// but not first.second (because the member name there is "second").
4402  internal::Matcher<ValueDecl>, InnerMatcher) {
4403  return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder);
4404 }
4405 
4406 /// \brief Matches a member expression where the object expression is
4407 /// matched by a given matcher.
4408 ///
4409 /// Given
4410 /// \code
4411 /// struct X { int m; };
4412 /// void f(X x) { x.m; m; }
4413 /// \endcode
4414 /// memberExpr(hasObjectExpression(hasType(cxxRecordDecl(hasName("X")))))))
4415 /// matches "x.m" and "m"
4416 /// with hasObjectExpression(...)
4417 /// matching "x" and the implicit object expression of "m" which has type X*.
4418 AST_MATCHER_P(MemberExpr, hasObjectExpression,
4419  internal::Matcher<Expr>, InnerMatcher) {
4420  return InnerMatcher.matches(*Node.getBase(), Finder, Builder);
4421 }
4422 
4423 /// \brief Matches any using shadow declaration.
4424 ///
4425 /// Given
4426 /// \code
4427 /// namespace X { void b(); }
4428 /// using X::b;
4429 /// \endcode
4430 /// usingDecl(hasAnyUsingShadowDecl(hasName("b"))))
4431 /// matches \code using X::b \endcode
4432 AST_MATCHER_P(UsingDecl, hasAnyUsingShadowDecl,
4433  internal::Matcher<UsingShadowDecl>, InnerMatcher) {
4434  return matchesFirstInPointerRange(InnerMatcher, Node.shadow_begin(),
4435  Node.shadow_end(), Finder, Builder);
4436 }
4437 
4438 /// \brief Matches a using shadow declaration where the target declaration is
4439 /// matched by the given matcher.
4440 ///
4441 /// Given
4442 /// \code
4443 /// namespace X { int a; void b(); }
4444 /// using X::a;
4445 /// using X::b;
4446 /// \endcode
4447 /// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(functionDecl())))
4448 /// matches \code using X::b \endcode
4449 /// but not \code using X::a \endcode
4451  internal::Matcher<NamedDecl>, InnerMatcher) {
4452  return InnerMatcher.matches(*Node.getTargetDecl(), Finder, Builder);
4453 }
4454 
4455 /// \brief Matches template instantiations of function, class, or static
4456 /// member variable template instantiations.
4457 ///
4458 /// Given
4459 /// \code
4460 /// template <typename T> class X {}; class A {}; X<A> x;
4461 /// \endcode
4462 /// or
4463 /// \code
4464 /// template <typename T> class X {}; class A {}; template class X<A>;
4465 /// \endcode
4466 /// cxxRecordDecl(hasName("::X"), isTemplateInstantiation())
4467 /// matches the template instantiation of X<A>.
4468 ///
4469 /// But given
4470 /// \code
4471 /// template <typename T> class X {}; class A {};
4472 /// template <> class X<A> {}; X<A> x;
4473 /// \endcode
4474 /// cxxRecordDecl(hasName("::X"), isTemplateInstantiation())
4475 /// does not match, as X<A> is an explicit template specialization.
4476 ///
4477 /// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl>
4480  CXXRecordDecl)) {
4481  return (Node.getTemplateSpecializationKind() == TSK_ImplicitInstantiation ||
4482  Node.getTemplateSpecializationKind() ==
4484 }
4485 
4486 /// \brief Matches declarations that are template instantiations or are inside
4487 /// template instantiations.
4488 ///
4489 /// Given
4490 /// \code
4491 /// template<typename T> void A(T t) { T i; }
4492 /// A(0);
4493 /// A(0U);
4494 /// \endcode
4495 /// functionDecl(isInstantiated())
4496 /// matches 'A(int) {...};' and 'A(unsigned) {...}'.
4497 AST_MATCHER_FUNCTION(internal::Matcher<Decl>, isInstantiated) {
4498  auto IsInstantiation = decl(anyOf(cxxRecordDecl(isTemplateInstantiation()),
4500  return decl(anyOf(IsInstantiation, hasAncestor(IsInstantiation)));
4501 }
4502 
4503 /// \brief Matches statements inside of a template instantiation.
4504 ///
4505 /// Given
4506 /// \code
4507 /// int j;
4508 /// template<typename T> void A(T t) { T i; j += 42;}
4509 /// A(0);
4510 /// A(0U);
4511 /// \endcode
4512 /// declStmt(isInTemplateInstantiation())
4513 /// matches 'int i;' and 'unsigned i'.
4514 /// unless(stmt(isInTemplateInstantiation()))
4515 /// will NOT match j += 42; as it's shared between the template definition and
4516 /// instantiation.
4517 AST_MATCHER_FUNCTION(internal::Matcher<Stmt>, isInTemplateInstantiation) {
4518  return stmt(
4521 }
4522 
4523 /// \brief Matches explicit template specializations of function, class, or
4524 /// static member variable template instantiations.
4525 ///
4526 /// Given
4527 /// \code
4528 /// template<typename T> void A(T t) { }
4529 /// template<> void A(int N) { }
4530 /// \endcode
4531 /// functionDecl(isExplicitTemplateSpecialization())
4532 /// matches the specialization A<int>().
4533 ///
4534 /// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl>
4535 AST_POLYMORPHIC_MATCHER(isExplicitTemplateSpecialization,
4537  CXXRecordDecl)) {
4538  return (Node.getTemplateSpecializationKind() == TSK_ExplicitSpecialization);
4539 }
4540 
4541 /// \brief Matches \c TypeLocs for which the given inner
4542 /// QualType-matcher matches.
4543 AST_MATCHER_FUNCTION_P_OVERLOAD(internal::BindableMatcher<TypeLoc>, loc,
4544  internal::Matcher<QualType>, InnerMatcher, 0) {
4545  return internal::BindableMatcher<TypeLoc>(
4546  new internal::TypeLocTypeMatcher(InnerMatcher));
4547 }
4548 
4549 /// \brief Matches type \c bool.
4550 ///
4551 /// Given
4552 /// \code
4553 /// struct S { bool func(); };
4554 /// \endcode
4555 /// functionDecl(returns(booleanType()))
4556 /// matches "bool func();"
4557 AST_MATCHER(Type, booleanType) {
4558  return Node.isBooleanType();
4559 }
4560 
4561 /// \brief Matches type \c void.
4562 ///
4563 /// Given
4564 /// \code
4565 /// struct S { void func(); };
4566 /// \endcode
4567 /// functionDecl(returns(voidType()))
4568 /// matches "void func();"
4569 AST_MATCHER(Type, voidType) {
4570  return Node.isVoidType();
4571 }
4572 
4573 /// \brief Matches builtin Types.
4574 ///
4575 /// Given
4576 /// \code
4577 /// struct A {};
4578 /// A a;
4579 /// int b;
4580 /// float c;
4581 /// bool d;
4582 /// \endcode
4583 /// builtinType()
4584 /// matches "int b", "float c" and "bool d"
4585 AST_TYPE_MATCHER(BuiltinType, builtinType);
4586 
4587 /// \brief Matches all kinds of arrays.
4588 ///
4589 /// Given
4590 /// \code
4591 /// int a[] = { 2, 3 };
4592 /// int b[4];
4593 /// void f() { int c[a[0]]; }
4594 /// \endcode
4595 /// arrayType()
4596 /// matches "int a[]", "int b[4]" and "int c[a[0]]";
4597 AST_TYPE_MATCHER(ArrayType, arrayType);
4598 
4599 /// \brief Matches C99 complex types.
4600 ///
4601 /// Given
4602 /// \code
4603 /// _Complex float f;
4604 /// \endcode
4605 /// complexType()
4606 /// matches "_Complex float f"
4607 AST_TYPE_MATCHER(ComplexType, complexType);
4608 
4609 /// \brief Matches any real floating-point type (float, double, long double).
4610 ///
4611 /// Given
4612 /// \code
4613 /// int i;
4614 /// float f;
4615 /// \endcode
4616 /// realFloatingPointType()
4617 /// matches "float f" but not "int i"
4618 AST_MATCHER(Type, realFloatingPointType) {
4619  return Node.isRealFloatingType();
4620 }
4621 
4622 /// \brief Matches arrays and C99 complex types that have a specific element
4623 /// type.
4624 ///
4625 /// Given
4626 /// \code
4627 /// struct A {};
4628 /// A a[7];
4629 /// int b[7];
4630 /// \endcode
4631 /// arrayType(hasElementType(builtinType()))
4632 /// matches "int b[7]"
4633 ///
4634 /// Usable as: Matcher<ArrayType>, Matcher<ComplexType>
4635 AST_TYPELOC_TRAVERSE_MATCHER(hasElementType, getElement,
4637  ComplexType));
4638 
4639 /// \brief Matches C arrays with a specified constant size.
4640 ///
4641 /// Given
4642 /// \code
4643 /// void() {
4644 /// int a[2];
4645 /// int b[] = { 2, 3 };
4646 /// int c[b[0]];
4647 /// }
4648 /// \endcode
4649 /// constantArrayType()
4650 /// matches "int a[2]"
4651 AST_TYPE_MATCHER(ConstantArrayType, constantArrayType);
4652 
4653 /// \brief Matches nodes that have the specified size.
4654 ///
4655 /// Given
4656 /// \code
4657 /// int a[42];
4658 /// int b[2 * 21];
4659 /// int c[41], d[43];
4660 /// char *s = "abcd";
4661 /// wchar_t *ws = L"abcd";
4662 /// char *w = "a";
4663 /// \endcode
4664 /// constantArrayType(hasSize(42))
4665 /// matches "int a[42]" and "int b[2 * 21]"
4666 /// stringLiteral(hasSize(4))
4667 /// matches "abcd", L"abcd"
4670  StringLiteral),
4671  unsigned, N) {
4672  return internal::HasSizeMatcher<NodeType>::hasSize(Node, N);
4673 }
4674 
4675 /// \brief Matches C++ arrays whose size is a value-dependent expression.
4676 ///
4677 /// Given
4678 /// \code
4679 /// template<typename T, int Size>
4680 /// class array {
4681 /// T data[Size];
4682 /// };
4683 /// \endcode
4684 /// dependentSizedArrayType
4685 /// matches "T data[Size]"
4686 AST_TYPE_MATCHER(DependentSizedArrayType, dependentSizedArrayType);
4687 
4688 /// \brief Matches C arrays with unspecified size.
4689 ///
4690 /// Given
4691 /// \code
4692 /// int a[] = { 2, 3 };
4693 /// int b[42];
4694 /// void f(int c[]) { int d[a[0]]; };
4695 /// \endcode
4696 /// incompleteArrayType()
4697 /// matches "int a[]" and "int c[]"
4698 AST_TYPE_MATCHER(IncompleteArrayType, incompleteArrayType);
4699 
4700 /// \brief Matches C arrays with a specified size that is not an
4701 /// integer-constant-expression.
4702 ///
4703 /// Given
4704 /// \code
4705 /// void f() {
4706 /// int a[] = { 2, 3 }
4707 /// int b[42];
4708 /// int c[a[0]];
4709 /// }
4710 /// \endcode
4711 /// variableArrayType()
4712 /// matches "int c[a[0]]"
4713 AST_TYPE_MATCHER(VariableArrayType, variableArrayType);
4714 
4715 /// \brief Matches \c VariableArrayType nodes that have a specific size
4716 /// expression.
4717 ///
4718 /// Given
4719 /// \code
4720 /// void f(int b) {
4721 /// int a[b];
4722 /// }
4723 /// \endcode
4724 /// variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
4725 /// varDecl(hasName("b")))))))
4726 /// matches "int a[b]"
4728  internal::Matcher<Expr>, InnerMatcher) {
4729  return InnerMatcher.matches(*Node.getSizeExpr(), Finder, Builder);
4730 }
4731 
4732 /// \brief Matches atomic types.
4733 ///
4734 /// Given
4735 /// \code
4736 /// _Atomic(int) i;
4737 /// \endcode
4738 /// atomicType()
4739 /// matches "_Atomic(int) i"
4740 AST_TYPE_MATCHER(AtomicType, atomicType);
4741 
4742 /// \brief Matches atomic types with a specific value type.
4743 ///
4744 /// Given
4745 /// \code
4746 /// _Atomic(int) i;
4747 /// _Atomic(float) f;
4748 /// \endcode
4749 /// atomicType(hasValueType(isInteger()))
4750 /// matches "_Atomic(int) i"
4751 ///
4752 /// Usable as: Matcher<AtomicType>
4755 
4756 /// \brief Matches types nodes representing C++11 auto types.
4757 ///
4758 /// Given:
4759 /// \code
4760 /// auto n = 4;
4761 /// int v[] = { 2, 3 }
4762 /// for (auto i : v) { }
4763 /// \endcode
4764 /// autoType()
4765 /// matches "auto n" and "auto i"
4766 AST_TYPE_MATCHER(AutoType, autoType);
4767 
4768 /// \brief Matches \c AutoType nodes where the deduced type is a specific type.
4769 ///
4770 /// Note: There is no \c TypeLoc for the deduced type and thus no
4771 /// \c getDeducedLoc() matcher.
4772 ///
4773 /// Given
4774 /// \code
4775 /// auto a = 1;
4776 /// auto b = 2.0;
4777 /// \endcode
4778 /// autoType(hasDeducedType(isInteger()))
4779 /// matches "auto a"
4780 ///
4781 /// Usable as: Matcher<AutoType>
4782 AST_TYPE_TRAVERSE_MATCHER(hasDeducedType, getDeducedType,
4784 
4785 /// \brief Matches \c FunctionType nodes.
4786 ///
4787 /// Given
4788 /// \code
4789 /// int (*f)(int);
4790 /// void g();
4791 /// \endcode
4792 /// functionType()
4793 /// matches "int (*f)(int)" and the type of "g".
4794 AST_TYPE_MATCHER(FunctionType, functionType);
4795 
4796 /// \brief Matches \c FunctionProtoType nodes.
4797 ///
4798 /// Given
4799 /// \code
4800 /// int (*f)(int);
4801 /// void g();
4802 /// \endcode
4803 /// functionProtoType()
4804 /// matches "int (*f)(int)" and the type of "g" in C++ mode.
4805 /// In C mode, "g" is not matched because it does not contain a prototype.
4806 AST_TYPE_MATCHER(FunctionProtoType, functionProtoType);
4807 
4808 /// \brief Matches \c ParenType nodes.
4809 ///
4810 /// Given
4811 /// \code
4812 /// int (*ptr_to_array)[4];
4813 /// int *array_of_ptrs[4];
4814 /// \endcode
4815 ///
4816 /// \c varDecl(hasType(pointsTo(parenType()))) matches \c ptr_to_array but not
4817 /// \c array_of_ptrs.
4818 AST_TYPE_MATCHER(ParenType, parenType);
4819 
4820 /// \brief Matches \c ParenType nodes where the inner type is a specific type.
4821 ///
4822 /// Given
4823 /// \code
4824 /// int (*ptr_to_array)[4];
4825 /// int (*ptr_to_func)(int);
4826 /// \endcode
4827 ///
4828 /// \c varDecl(hasType(pointsTo(parenType(innerType(functionType()))))) matches
4829 /// \c ptr_to_func but not \c ptr_to_array.
4830 ///
4831 /// Usable as: Matcher<ParenType>
4832 AST_TYPE_TRAVERSE_MATCHER(innerType, getInnerType,
4834 
4835 /// \brief Matches block pointer types, i.e. types syntactically represented as
4836 /// "void (^)(int)".
4837 ///
4838 /// The \c pointee is always required to be a \c FunctionType.
4839 AST_TYPE_MATCHER(BlockPointerType, blockPointerType);
4840 
4841 /// \brief Matches member pointer types.
4842 /// Given
4843 /// \code
4844 /// struct A { int i; }
4845 /// A::* ptr = A::i;
4846 /// \endcode
4847 /// memberPointerType()
4848 /// matches "A::* ptr"
4849 AST_TYPE_MATCHER(MemberPointerType, memberPointerType);
4850 
4851 /// \brief Matches pointer types, but does not match Objective-C object pointer
4852 /// types.
4853 ///
4854 /// Given
4855 /// \code
4856 /// int *a;
4857 /// int &b = *a;
4858 /// int c = 5;
4859 ///
4860 /// @interface Foo
4861 /// @end
4862 /// Foo *f;
4863 /// \endcode
4864 /// pointerType()
4865 /// matches "int *a", but does not match "Foo *f".
4866 AST_TYPE_MATCHER(PointerType, pointerType);
4867 
4868 /// \brief Matches an Objective-C object pointer type, which is different from
4869 /// a pointer type, despite being syntactically similar.
4870 ///
4871 /// Given
4872 /// \code
4873 /// int *a;
4874 ///
4875 /// @interface Foo
4876 /// @end
4877 /// Foo *f;
4878 /// \endcode
4879 /// pointerType()
4880 /// matches "Foo *f", but does not match "int *a".
4881 AST_TYPE_MATCHER(ObjCObjectPointerType, objcObjectPointerType);
4882 
4883 /// \brief Matches both lvalue and rvalue reference types.
4884 ///
4885 /// Given
4886 /// \code
4887 /// int *a;
4888 /// int &b = *a;
4889 /// int &&c = 1;
4890 /// auto &d = b;
4891 /// auto &&e = c;
4892 /// auto &&f = 2;
4893 /// int g = 5;
4894 /// \endcode
4895 ///
4896 /// \c referenceType() matches the types of \c b, \c c, \c d, \c e, and \c f.
4897 AST_TYPE_MATCHER(ReferenceType, referenceType);
4898 
4899 /// \brief Matches lvalue reference types.
4900 ///
4901 /// Given:
4902 /// \code
4903 /// int *a;
4904 /// int &b = *a;
4905 /// int &&c = 1;
4906 /// auto &d = b;
4907 /// auto &&e = c;
4908 /// auto &&f = 2;
4909 /// int g = 5;
4910 /// \endcode
4911 ///
4912 /// \c lValueReferenceType() matches the types of \c b, \c d, and \c e. \c e is
4913 /// matched since the type is deduced as int& by reference collapsing rules.
4914 AST_TYPE_MATCHER(LValueReferenceType, lValueReferenceType);
4915 
4916 /// \brief Matches rvalue reference types.
4917 ///
4918 /// Given:
4919 /// \code
4920 /// int *a;
4921 /// int &b = *a;
4922 /// int &&c = 1;
4923 /// auto &d = b;
4924 /// auto &&e = c;
4925 /// auto &&f = 2;
4926 /// int g = 5;
4927 /// \endcode
4928 ///
4929 /// \c rValueReferenceType() matches the types of \c c and \c f. \c e is not
4930 /// matched as it is deduced to int& by reference collapsing rules.
4931 AST_TYPE_MATCHER(RValueReferenceType, rValueReferenceType);
4932 
4933 /// \brief Narrows PointerType (and similar) matchers to those where the
4934 /// \c pointee matches a given matcher.
4935 ///
4936 /// Given
4937 /// \code
4938 /// int *a;
4939 /// int const *b;
4940 /// float const *f;
4941 /// \endcode
4942 /// pointerType(pointee(isConstQualified(), isInteger()))
4943 /// matches "int const *b"
4944 ///
4945 /// Usable as: Matcher<BlockPointerType>, Matcher<MemberPointerType>,
4946 /// Matcher<PointerType>, Matcher<ReferenceType>
4947 AST_TYPELOC_TRAVERSE_MATCHER(pointee, getPointee,
4950  PointerType,
4951  ReferenceType));
4952 
4953 /// \brief Matches typedef types.
4954 ///
4955 /// Given
4956 /// \code
4957 /// typedef int X;
4958 /// \endcode
4959 /// typedefType()
4960 /// matches "typedef int X"
4961 AST_TYPE_MATCHER(TypedefType, typedefType);
4962 
4963 /// \brief Matches enum types.
4964 ///
4965 /// Given
4966 /// \code
4967 /// enum C { Green };
4968 /// enum class S { Red };
4969 ///
4970 /// C c;
4971 /// S s;
4972 /// \endcode
4973 //
4974 /// \c enumType() matches the type of the variable declarations of both \c c and
4975 /// \c s.
4976 AST_TYPE_MATCHER(EnumType, enumType);
4977 
4978 /// \brief Matches template specialization types.
4979 ///
4980 /// Given
4981 /// \code
4982 /// template <typename T>
4983 /// class C { };
4984 ///
4985 /// template class C<int>; // A
4986 /// C<char> var; // B
4987 /// \endcode
4988 ///
4989 /// \c templateSpecializationType() matches the type of the explicit
4990 /// instantiation in \c A and the type of the variable declaration in \c B.
4991 AST_TYPE_MATCHER(TemplateSpecializationType, templateSpecializationType);
4992 
4993 /// \brief Matches types nodes representing unary type transformations.
4994 ///
4995 /// Given:
4996 /// \code
4997 /// typedef __underlying_type(T) type;
4998 /// \endcode
4999 /// unaryTransformType()
5000 /// matches "__underlying_type(T)"
5001 AST_TYPE_MATCHER(UnaryTransformType, unaryTransformType);
5002 
5003 /// \brief Matches record types (e.g. structs, classes).
5004 ///
5005 /// Given
5006 /// \code
5007 /// class C {};
5008 /// struct S {};
5009 ///
5010 /// C c;
5011 /// S s;
5012 /// \endcode
5013 ///
5014 /// \c recordType() matches the type of the variable declarations of both \c c
5015 /// and \c s.
5016 AST_TYPE_MATCHER(RecordType, recordType);
5017 
5018 /// \brief Matches types specified with an elaborated type keyword or with a
5019 /// qualified name.
5020 ///
5021 /// Given
5022 /// \code
5023 /// namespace N {
5024 /// namespace M {
5025 /// class D {};
5026 /// }
5027 /// }
5028 /// class C {};
5029 ///
5030 /// class C c;
5031 /// N::M::D d;
5032 /// \endcode
5033 ///
5034 /// \c elaboratedType() matches the type of the variable declarations of both
5035 /// \c c and \c d.
5036 AST_TYPE_MATCHER(ElaboratedType, elaboratedType);
5037 
5038 /// \brief Matches ElaboratedTypes whose qualifier, a NestedNameSpecifier,
5039 /// matches \c InnerMatcher if the qualifier exists.
5040 ///
5041 /// Given
5042 /// \code
5043 /// namespace N {
5044 /// namespace M {
5045 /// class D {};
5046 /// }
5047 /// }
5048 /// N::M::D d;
5049 /// \endcode
5050 ///
5051 /// \c elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N"))))
5052 /// matches the type of the variable declaration of \c d.
5054  internal::Matcher<NestedNameSpecifier>, InnerMatcher) {
5055  if (const NestedNameSpecifier *Qualifier = Node.getQualifier())
5056  return InnerMatcher.matches(*Qualifier, Finder, Builder);
5057 
5058  return false;
5059 }
5060 
5061 /// \brief Matches ElaboratedTypes whose named type matches \c InnerMatcher.
5062 ///
5063 /// Given
5064 /// \code
5065 /// namespace N {
5066 /// namespace M {
5067 /// class D {};
5068 /// }
5069 /// }
5070 /// N::M::D d;
5071 /// \endcode
5072 ///
5073 /// \c elaboratedType(namesType(recordType(
5074 /// hasDeclaration(namedDecl(hasName("D")))))) matches the type of the variable
5075 /// declaration of \c d.
5076 AST_MATCHER_P(ElaboratedType, namesType, internal::Matcher<QualType>,
5077  InnerMatcher) {
5078  return InnerMatcher.matches(Node.getNamedType(), Finder, Builder);
5079 }
5080 
5081 /// \brief Matches types that represent the result of substituting a type for a
5082 /// template type parameter.
5083 ///
5084 /// Given
5085 /// \code
5086 /// template <typename T>
5087 /// void F(T t) {
5088 /// int i = 1 + t;
5089 /// }
5090 /// \endcode
5091 ///
5092 /// \c substTemplateTypeParmType() matches the type of 't' but not '1'
5093 AST_TYPE_MATCHER(SubstTemplateTypeParmType, substTemplateTypeParmType);
5094 
5095 /// \brief Matches template type parameter substitutions that have a replacement
5096 /// type that matches the provided matcher.
5097 ///
5098 /// Given
5099 /// \code
5100 /// template <typename T>
5101 /// double F(T t);
5102 /// int i;
5103 /// double j = F(i);
5104 /// \endcode
5105 ///
5106 /// \c substTemplateTypeParmType(hasReplacementType(type())) matches int
5108  hasReplacementType, getReplacementType,
5110 
5111 /// \brief Matches template type parameter types.
5112 ///
5113 /// Example matches T, but not int.
5114 /// (matcher = templateTypeParmType())
5115 /// \code
5116 /// template <typename T> void f(int i);
5117 /// \endcode
5118 AST_TYPE_MATCHER(TemplateTypeParmType, templateTypeParmType);
5119 
5120 /// \brief Matches injected class name types.
5121 ///
5122 /// Example matches S s, but not S<T> s.
5123 /// (matcher = parmVarDecl(hasType(injectedClassNameType())))
5124 /// \code
5125 /// template <typename T> struct S {
5126 /// void f(S s);
5127 /// void g(S<T> s);
5128 /// };
5129 /// \endcode
5130 AST_TYPE_MATCHER(InjectedClassNameType, injectedClassNameType);
5131 
5132 /// \brief Matches decayed type
5133 /// Example matches i[] in declaration of f.
5134 /// (matcher = valueDecl(hasType(decayedType(hasDecayedType(pointerType())))))
5135 /// Example matches i[1].
5136 /// (matcher = expr(hasType(decayedType(hasDecayedType(pointerType())))))
5137 /// \code
5138 /// void f(int i[]) {
5139 /// i[1] = 0;
5140 /// }
5141 /// \endcode
5142 AST_TYPE_MATCHER(DecayedType, decayedType);
5143 
5144 /// \brief Matches the decayed type, whos decayed type matches \c InnerMatcher
5145 AST_MATCHER_P(DecayedType, hasDecayedType, internal::Matcher<QualType>,
5146  InnerType) {
5147  return InnerType.matches(Node.getDecayedType(), Finder, Builder);
5148 }
5149 
5150 /// \brief Matches declarations whose declaration context, interpreted as a
5151 /// Decl, matches \c InnerMatcher.
5152 ///
5153 /// Given
5154 /// \code
5155 /// namespace N {
5156 /// namespace M {
5157 /// class D {};
5158 /// }
5159 /// }
5160 /// \endcode
5161 ///
5162 /// \c cxxRcordDecl(hasDeclContext(namedDecl(hasName("M")))) matches the
5163 /// declaration of \c class \c D.
5164 AST_MATCHER_P(Decl, hasDeclContext, internal::Matcher<Decl>, InnerMatcher) {
5165  const DeclContext *DC = Node.getDeclContext();
5166  if (!DC) return false;
5167  return InnerMatcher.matches(*Decl::castFromDeclContext(DC), Finder, Builder);
5168 }
5169 
5170 /// \brief Matches nested name specifiers.
5171 ///
5172 /// Given
5173 /// \code
5174 /// namespace ns {
5175 /// struct A { static void f(); };
5176 /// void A::f() {}
5177 /// void g() { A::f(); }
5178 /// }
5179 /// ns::A a;
5180 /// \endcode
5181 /// nestedNameSpecifier()
5182 /// matches "ns::" and both "A::"
5183 const internal::VariadicAllOfMatcher<NestedNameSpecifier> nestedNameSpecifier;
5184 
5185 /// \brief Same as \c nestedNameSpecifier but matches \c NestedNameSpecifierLoc.
5186 const internal::VariadicAllOfMatcher<
5188 
5189 /// \brief Matches \c NestedNameSpecifierLocs for which the given inner
5190 /// NestedNameSpecifier-matcher matches.
5192  internal::BindableMatcher<NestedNameSpecifierLoc>, loc,
5193  internal::Matcher<NestedNameSpecifier>, InnerMatcher, 1) {
5194  return internal::BindableMatcher<NestedNameSpecifierLoc>(
5195  new internal::LocMatcher<NestedNameSpecifierLoc, NestedNameSpecifier>(
5196  InnerMatcher));
5197 }
5198 
5199 /// \brief Matches nested name specifiers that specify a type matching the
5200 /// given \c QualType matcher without qualifiers.
5201 ///
5202 /// Given
5203 /// \code
5204 /// struct A { struct B { struct C {}; }; };
5205 /// A::B::C c;
5206 /// \endcode
5207 /// nestedNameSpecifier(specifiesType(
5208 /// hasDeclaration(cxxRecordDecl(hasName("A")))
5209 /// ))
5210 /// matches "A::"
5212  internal::Matcher<QualType>, InnerMatcher) {
5213  if (!Node.getAsType())
5214  return false;
5215  return InnerMatcher.matches(QualType(Node.getAsType(), 0), Finder, Builder);
5216 }
5217 
5218 /// \brief Matches nested name specifier locs that specify a type matching the
5219 /// given \c TypeLoc.
5220 ///
5221 /// Given
5222 /// \code
5223 /// struct A { struct B { struct C {}; }; };
5224 /// A::B::C c;
5225 /// \endcode
5226 /// nestedNameSpecifierLoc(specifiesTypeLoc(loc(type(
5227 /// hasDeclaration(cxxRecordDecl(hasName("A")))))))
5228 /// matches "A::"
5230  internal::Matcher<TypeLoc>, InnerMatcher) {
5231  return Node && InnerMatcher.matches(Node.getTypeLoc(), Finder, Builder);
5232 }
5233 
5234 /// \brief Matches on the prefix of a \c NestedNameSpecifier.
5235 ///
5236 /// Given
5237 /// \code
5238 /// struct A { struct B { struct C {}; }; };
5239 /// A::B::C c;
5240 /// \endcode
5241 /// nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A")))) and
5242 /// matches "A::"
5244  internal::Matcher<NestedNameSpecifier>, InnerMatcher,
5245  0) {
5246  const NestedNameSpecifier *NextNode = Node.getPrefix();
5247  if (!NextNode)
5248  return false;
5249  return InnerMatcher.matches(*NextNode, Finder, Builder);
5250 }
5251 
5252 /// \brief Matches on the prefix of a \c NestedNameSpecifierLoc.
5253 ///
5254 /// Given
5255 /// \code
5256 /// struct A { struct B { struct C {}; }; };
5257 /// A::B::C c;
5258 /// \endcode
5259 /// nestedNameSpecifierLoc(hasPrefix(loc(specifiesType(asString("struct A")))))
5260 /// matches "A::"
5262  internal::Matcher<NestedNameSpecifierLoc>, InnerMatcher,
5263  1) {
5264  NestedNameSpecifierLoc NextNode = Node.getPrefix();
5265  if (!NextNode)
5266  return false;
5267  return InnerMatcher.matches(NextNode, Finder, Builder);
5268 }
5269 
5270 /// \brief Matches nested name specifiers that specify a namespace matching the
5271 /// given namespace matcher.
5272 ///
5273 /// Given
5274 /// \code
5275 /// namespace ns { struct A {}; }
5276 /// ns::A a;
5277 /// \endcode
5278 /// nestedNameSpecifier(specifiesNamespace(hasName("ns")))
5279 /// matches "ns::"
5281  internal::Matcher<NamespaceDecl>, InnerMatcher) {
5282  if (!Node.getAsNamespace())
5283  return false;
5284  return InnerMatcher.matches(*Node.getAsNamespace(), Finder, Builder);
5285 }
5286 
5287 /// \brief Overloads for the \c equalsNode matcher.
5288 /// FIXME: Implement for other node types.
5289 /// @{
5290 
5291 /// \brief Matches if a node equals another node.
5292 ///
5293 /// \c Decl has pointer identity in the AST.
5294 AST_MATCHER_P_OVERLOAD(Decl, equalsNode, const Decl*, Other, 0) {
5295  return &Node == Other;
5296 }
5297 /// \brief Matches if a node equals another node.
5298 ///
5299 /// \c Stmt has pointer identity in the AST.
5300 AST_MATCHER_P_OVERLOAD(Stmt, equalsNode, const Stmt*, Other, 1) {
5301  return &Node == Other;
5302 }
5303 /// \brief Matches if a node equals another node.
5304 ///
5305 /// \c Type has pointer identity in the AST.
5306 AST_MATCHER_P_OVERLOAD(Type, equalsNode, const Type*, Other, 2) {
5307  return &Node == Other;
5308 }
5309 
5310 /// @}
5311 
5312 /// \brief Matches each case or default statement belonging to the given switch
5313 /// statement. This matcher may produce multiple matches.
5314 ///
5315 /// Given
5316 /// \code
5317 /// switch (1) { case 1: case 2: default: switch (2) { case 3: case 4: ; } }
5318 /// \endcode
5319 /// switchStmt(forEachSwitchCase(caseStmt().bind("c"))).bind("s")
5320 /// matches four times, with "c" binding each of "case 1:", "case 2:",
5321 /// "case 3:" and "case 4:", and "s" respectively binding "switch (1)",
5322 /// "switch (1)", "switch (2)" and "switch (2)".
5323 AST_MATCHER_P(SwitchStmt, forEachSwitchCase, internal::Matcher<SwitchCase>,
5324  InnerMatcher) {
5325  BoundNodesTreeBuilder Result;
5326  // FIXME: getSwitchCaseList() does not necessarily guarantee a stable
5327  // iteration order. We should use the more general iterating matchers once
5328  // they are capable of expressing this matcher (for example, it should ignore
5329  // case statements belonging to nested switch statements).
5330  bool Matched = false;
5331  for (const SwitchCase *SC = Node.getSwitchCaseList(); SC;
5332  SC = SC->getNextSwitchCase()) {
5333  BoundNodesTreeBuilder CaseBuilder(*Builder);
5334  bool CaseMatched = InnerMatcher.matches(*SC, Finder, &CaseBuilder);
5335  if (CaseMatched) {
5336  Matched = true;
5337  Result.addMatch(CaseBuilder);
5338  }
5339  }
5340  *Builder = std::move(Result);
5341  return Matched;
5342 }
5343 
5344 /// \brief Matches each constructor initializer in a constructor definition.
5345 ///
5346 /// Given
5347 /// \code
5348 /// class A { A() : i(42), j(42) {} int i; int j; };
5349 /// \endcode
5350 /// cxxConstructorDecl(forEachConstructorInitializer(
5351 /// forField(decl().bind("x"))
5352 /// ))
5353 /// will trigger two matches, binding for 'i' and 'j' respectively.
5354 AST_MATCHER_P(CXXConstructorDecl, forEachConstructorInitializer,
5355  internal::Matcher<CXXCtorInitializer>, InnerMatcher) {
5356  BoundNodesTreeBuilder Result;
5357  bool Matched = false;
5358  for (const auto *I : Node.inits()) {
5359  BoundNodesTreeBuilder InitBuilder(*Builder);
5360  if (InnerMatcher.matches(*I, Finder, &InitBuilder)) {
5361  Matched = true;
5362  Result.addMatch(InitBuilder);
5363  }
5364  }
5365  *Builder = std::move(Result);
5366  return Matched;
5367 }
5368 
5369 /// \brief Matches constructor declarations that are copy constructors.
5370 ///
5371 /// Given
5372 /// \code
5373 /// struct S {
5374 /// S(); // #1
5375 /// S(const S &); // #2
5376 /// S(S &&); // #3
5377 /// };
5378 /// \endcode
5379 /// cxxConstructorDecl(isCopyConstructor()) will match #2, but not #1 or #3.
5380 AST_MATCHER(CXXConstructorDecl, isCopyConstructor) {
5381  return Node.isCopyConstructor();
5382 }
5383 
5384 /// \brief Matches constructor declarations that are move constructors.
5385 ///
5386 /// Given
5387 /// \code
5388 /// struct S {
5389 /// S(); // #1
5390 /// S(const S &); // #2
5391 /// S(S &&); // #3
5392 /// };
5393 /// \endcode
5394 /// cxxConstructorDecl(isMoveConstructor()) will match #3, but not #1 or #2.
5395 AST_MATCHER(CXXConstructorDecl, isMoveConstructor) {
5396  return Node.isMoveConstructor();
5397 }
5398 
5399 /// \brief Matches constructor declarations that are default constructors.
5400 ///
5401 /// Given
5402 /// \code
5403 /// struct S {
5404 /// S(); // #1
5405 /// S(const S &); // #2
5406 /// S(S &&); // #3
5407 /// };
5408 /// \endcode
5409 /// cxxConstructorDecl(isDefaultConstructor()) will match #1, but not #2 or #3.
5410 AST_MATCHER(CXXConstructorDecl, isDefaultConstructor) {
5411  return Node.isDefaultConstructor();
5412 }
5413 
5414 /// \brief Matches constructors that delegate to another constructor.
5415 ///
5416 /// Given
5417 /// \code
5418 /// struct S {
5419 /// S(); // #1
5420 /// S(int) {} // #2
5421 /// S(S &&) : S() {} // #3
5422 /// };
5423 /// S::S() : S(0) {} // #4
5424 /// \endcode
5425 /// cxxConstructorDecl(isDelegatingConstructor()) will match #3 and #4, but not
5426 /// #1 or #2.
5427 AST_MATCHER(CXXConstructorDecl, isDelegatingConstructor) {
5428  return Node.isDelegatingConstructor();
5429 }
5430 
5431 /// \brief Matches constructor and conversion declarations that are marked with
5432 /// the explicit keyword.
5433 ///
5434 /// Given
5435 /// \code
5436 /// struct S {
5437 /// S(int); // #1
5438 /// explicit S(double); // #2
5439 /// operator int(); // #3
5440 /// explicit operator bool(); // #4
5441 /// };
5442 /// \endcode
5443 /// cxxConstructorDecl(isExplicit()) will match #2, but not #1.
5444 /// cxxConversionDecl(isExplicit()) will match #4, but not #3.
5447  CXXConversionDecl)) {
5448  return Node.isExplicit();
5449 }
5450 
5451 /// \brief Matches function and namespace declarations that are marked with
5452 /// the inline keyword.
5453 ///
5454 /// Given
5455 /// \code
5456 /// inline void f();
5457 /// void g();
5458 /// namespace n {
5459 /// inline namespace m {}
5460 /// }
5461 /// \endcode
5462 /// functionDecl(isInline()) will match ::f().
5463 /// namespaceDecl(isInline()) will match n::m.
5466  FunctionDecl)) {
5467  // This is required because the spelling of the function used to determine
5468  // whether inline is specified or not differs between the polymorphic types.
5469  if (const auto *FD = dyn_cast<FunctionDecl>(&Node))
5470  return FD->isInlineSpecified();
5471  else if (const auto *NSD = dyn_cast<NamespaceDecl>(&Node))
5472  return NSD->isInline();
5473  llvm_unreachable("Not a valid polymorphic type");
5474 }
5475 
5476 /// \brief Matches anonymous namespace declarations.
5477 ///
5478 /// Given
5479 /// \code
5480 /// namespace n {
5481 /// namespace {} // #1
5482 /// }
5483 /// \endcode
5484 /// namespaceDecl(isAnonymous()) will match #1 but not ::n.
5486  return Node.isAnonymousNamespace();
5487 }
5488 
5489 /// \brief If the given case statement does not use the GNU case range
5490 /// extension, matches the constant given in the statement.
5491 ///
5492 /// Given
5493 /// \code
5494 /// switch (1) { case 1: case 1+1: case 3 ... 4: ; }
5495 /// \endcode
5496 /// caseStmt(hasCaseConstant(integerLiteral()))
5497 /// matches "case 1:"
5498 AST_MATCHER_P(CaseStmt, hasCaseConstant, internal::Matcher<Expr>,
5499  InnerMatcher) {
5500  if (Node.getRHS())
5501  return false;
5502 
5503  return InnerMatcher.matches(*Node.getLHS(), Finder, Builder);
5504 }
5505 
5506 /// \brief Matches declaration that has a given attribute.
5507 ///
5508 /// Given
5509 /// \code
5510 /// __attribute__((device)) void f() { ... }
5511 /// \endcode
5512 /// decl(hasAttr(clang::attr::CUDADevice)) matches the function declaration of
5513 /// f. If the matcher is use from clang-query, attr::Kind parameter should be
5514 /// passed as a quoted string. e.g., hasAttr("attr::CUDADevice").
5515 AST_MATCHER_P(Decl, hasAttr, attr::Kind, AttrKind) {
5516  for (const auto *Attr : Node.attrs()) {
5517  if (Attr->getKind() == AttrKind)
5518  return true;
5519  }
5520  return false;
5521 }
5522 
5523 /// \brief Matches the return value expression of a return statement
5524 ///
5525 /// Given
5526 /// \code
5527 /// return a + b;
5528 /// \endcode
5529 /// hasReturnValue(binaryOperator())
5530 /// matches 'return a + b'
5531 /// with binaryOperator()
5532 /// matching 'a + b'
5533 AST_MATCHER_P(ReturnStmt, hasReturnValue, internal::Matcher<Expr>,
5534  InnerMatcher) {
5535  if (const auto *RetValue = Node.getRetValue())
5536  return InnerMatcher.matches(*RetValue, Finder, Builder);
5537  return false;
5538 }
5539 
5540 
5541 /// \brief Matches CUDA kernel call expression.
5542 ///
5543 /// Example matches,
5544 /// \code
5545 /// kernel<<<i,j>>>();
5546 /// \endcode
5547 const internal::VariadicDynCastAllOfMatcher<
5548  Stmt,
5550 
5551 
5552 /// \brief Matches expressions that resolve to a null pointer constant, such as
5553 /// GNU's __null, C++11's nullptr, or C's NULL macro.
5554 ///
5555 /// Given:
5556 /// \code
5557 /// void *v1 = NULL;
5558 /// void *v2 = nullptr;
5559 /// void *v3 = __null; // GNU extension
5560 /// char *cp = (char *)0;
5561 /// int *ip = 0;
5562 /// int i = 0;
5563 /// \endcode
5564 /// expr(nullPointerConstant())
5565 /// matches the initializer for v1, v2, v3, cp, and ip. Does not match the
5566 /// initializer for i.
5567 AST_MATCHER_FUNCTION(internal::Matcher<Expr>, nullPointerConstant) {
5568  return anyOf(
5570  integerLiteral(equals(0), hasParent(expr(hasType(pointerType())))));
5571 }
5572 
5573 /// \brief Matches declaration of the function the statement belongs to
5574 ///
5575 /// Given:
5576 /// \code
5577 /// F& operator=(const F& o) {
5578 /// std::copy_if(o.begin(), o.end(), begin(), [](V v) { return v > 0; });
5579 /// return *this;
5580 /// }
5581 /// \endcode
5582 /// returnStmt(forFunction(hasName("operator=")))
5583 /// matches 'return *this'
5584 /// but does match 'return > 0'
5585 AST_MATCHER_P(Stmt, forFunction, internal::Matcher<FunctionDecl>,
5586  InnerMatcher) {
5587  const auto &Parents = Finder->getASTContext().getParents(Node);
5588 
5590  Parents.end());
5591  while(!Stack.empty()) {
5592  const auto &CurNode = Stack.back();
5593  Stack.pop_back();
5594  if(const auto *FuncDeclNode = CurNode.get<FunctionDecl>()) {
5595  if(InnerMatcher.matches(*FuncDeclNode, Finder, Builder)) {
5596  return true;
5597  }
5598  } else if(const auto *LambdaExprNode = CurNode.get<LambdaExpr>()) {
5599  if(InnerMatcher.matches(*LambdaExprNode->getCallOperator(),
5600  Finder, Builder)) {
5601  return true;
5602  }
5603  } else {
5604  for(const auto &Parent: Finder->getASTContext().getParents(CurNode))
5605  Stack.push_back(Parent);
5606  }
5607  }
5608  return false;
5609 }
5610 
5611 /// \brief Matches a declaration that has external formal linkage.
5612 ///
5613 /// Example matches only z (matcher = varDecl(hasExternalFormalLinkage()))
5614 /// \code
5615 /// void f() {
5616 /// int x;
5617 /// static int y;
5618 /// }
5619 /// int z;
5620 /// \endcode
5621 ///
5622 /// Example matches f() because it has external formal linkage despite being
5623 /// unique to the translation unit as though it has internal likage
5624 /// (matcher = functionDecl(hasExternalFormalLinkage()))
5625 ///
5626 /// \code
5627 /// namespace {
5628 /// void f() {}
5629 /// }
5630 /// \endcode
5631 AST_MATCHER(NamedDecl, hasExternalFormalLinkage) {
5632  return Node.hasExternalFormalLinkage();
5633 }
5634 
5635 } // end namespace ast_matchers
5636 } // end namespace clang
5637 
5638 #endif
A call to an overloaded operator written using operator syntax.
Definition: ExprCXX.h:52
internal::TrueMatcher anything()
Matches any node.
Definition: ASTMatchers.h:133
Defines the clang::ASTContext interface.
Represents a type that was referred to using an elaborated type keyword, e.g., struct S...
Definition: Type.h:4541
The null pointer literal (C++11 [lex.nullptr])
Definition: ExprCXX.h:518
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1586
const internal::ArgumentAdaptingMatcherFunc< internal::HasParentMatcher, internal::TypeList< Decl, NestedNameSpecifierLoc, Stmt, TypeLoc >, internal::TypeList< Decl, NestedNameSpecifierLoc, Stmt, TypeLoc > > LLVM_ATTRIBUTE_UNUSED hasParent
Matches AST nodes that have a parent that matches the provided matcher.
Definition: ASTMatchers.h:2486
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
Definition: ASTMatchers.h:1486
const internal::ArgumentAdaptingMatcherFunc< internal::HasAncestorMatcher, internal::TypeList< Decl, NestedNameSpecifierLoc, Stmt, TypeLoc >, internal::TypeList< Decl, NestedNameSpecifierLoc, Stmt, TypeLoc > > LLVM_ATTRIBUTE_UNUSED hasAncestor
Matches AST nodes that have an ancestor that matches the provided matcher.
Definition: ASTMatchers.h:2503
body_iterator body_end()
Definition: Stmt.h:607
const internal::VariadicDynCastAllOfMatcher< Stmt, BreakStmt > breakStmt
Matches break statements.
Definition: ASTMatchers.h:1610
Smart pointer class that efficiently represents Objective-C method names.
const internal::VariadicDynCastAllOfMatcher< Stmt, DoStmt > doStmt
Matches do statements.
Definition: ASTMatchers.h:1600
const internal::VariadicDynCastAllOfMatcher< Stmt, AddrLabelExpr > addrLabelExpr
Matches address of label statements (GNU extension).
Definition: ASTMatchers.h:1664
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2202
const internal::VariadicDynCastAllOfMatcher< Stmt, BinaryConditionalOperator > binaryConditionalOperator
Matches binary conditional operator expressions (GNU extension).
Definition: ASTMatchers.h:1893
A (possibly-)qualified type.
Definition: Type.h:599
Static storage duration.
Definition: Specifiers.h:276
const internal::VariadicDynCastAllOfMatcher< Stmt, StmtExpr > stmtExpr
Matches statement expression (GNU extension).
Definition: ASTMatchers.h:1853
AST_MATCHER_P(FieldDecl, hasBitWidth, unsigned, Width)
Matches non-static data members that are bit-fields of the specified bit width.
Definition: ASTMatchers.h:549
const internal::ArgumentAdaptingMatcherFunc< internal::HasDescendantMatcher > LLVM_ATTRIBUTE_UNUSED hasDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher. ...
Definition: ASTMatchers.h:2400
static Decl * castFromDeclContext(const DeclContext *)
Definition: DeclBase.cpp:760
const internal::VariadicAllOfMatcher< TemplateName > templateName
Matches template name.
Definition: ASTMatchers.h:447
Stmt - This represents one statement.
Definition: Stmt.h:60
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:2901
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
Definition: ASTMatchers.h:1041
IfStmt - This represents an if/then/else.
Definition: Stmt.h:905
const internal::VariadicDynCastAllOfMatcher< Stmt, OpaqueValueExpr > opaqueValueExpr
Matches opaque value expressions.
Definition: ASTMatchers.h:1905
internal::PolymorphicMatcherWithParam1< internal::HasDeclarationMatcher, internal::Matcher< Decl >, void(internal::HasDeclarationSupportedTypes)> hasDeclaration(const internal::Matcher< Decl > &InnerMatcher)
Matches a node if the declaration associated with that node matches the given matcher.
Definition: ASTMatchers.h:2541
const internal::VariadicDynCastAllOfMatcher< Decl, CXXMethodDecl > cxxMethodDecl
Matches method declarations.
Definition: ASTMatchers.h:973
EnumConstantDecl - An instance of this object exists for each enum constant that is defined...
Definition: Decl.h:2511
The template argument is an expression, and we&#39;ve not resolved it to one of the other forms yet...
Definition: TemplateBase.h:69
const internal::VariadicDynCastAllOfMatcher< Decl, LinkageSpecDecl > linkageSpecDecl
Matches a declaration of a linkage specification.
Definition: ASTMatchers.h:282
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXNullPtrLiteralExpr > cxxNullPtrLiteralExpr
Matches nullptr literal.
Definition: ASTMatchers.h:1835
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXDynamicCastExpr > cxxDynamicCastExpr
Matches a dynamic_cast expression.
Definition: ASTMatchers.h:1969
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:80
A reference to a name which we were able to look up during parsing but could not resolve to a specifi...
Definition: ExprCXX.h:2653
Defines the C++ template declaration subclasses.
Represents a C++11 auto or C++14 decltype(auto) type.
Definition: Type.h:4171
const internal::ArgumentAdaptingMatcherFunc< internal::ForEachMatcher > LLVM_ATTRIBUTE_UNUSED forEach
Matches AST nodes that have child AST nodes that match the provided matcher.
Definition: ASTMatchers.h:2420
ParenExpr - This represents a parethesized expression, e.g.
Definition: Expr.h:1636
const internal::VariadicDynCastAllOfMatcher< Stmt, LabelStmt > labelStmt
Matches label statements.
Definition: ASTMatchers.h:1652
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXThisExpr > cxxThisExpr
Matches implicit and explicit this expressions.
Definition: ASTMatchers.h:1381
internal::Matcher< Decl > DeclarationMatcher
Types of matchers for the top-level classes in the AST class hierarchy.
Definition: ASTMatchers.h:110
The base class of the type hierarchy.
Definition: Type.h:1283
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2475
The template argument is a declaration that was provided for a pointer, reference, or pointer to member non-type template parameter.
Definition: TemplateBase.h:51
NamespaceDecl - Represent a C++ namespace.
Definition: Decl.h:471
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1175
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2221
Represents a prvalue temporary that is written into memory so that a reference can bind to it...
Definition: ExprCXX.h:3944
float __ovld __cnfn distance(float p0, float p1)
Returns the distance between p0 and p1.
const internal::VariadicDynCastAllOfMatcher< Decl, UsingDecl > usingDecl
Matches using declarations.
Definition: ASTMatchers.h:1267
internal::Matcher< CXXCtorInitializer > CXXCtorInitializerMatcher
Definition: ASTMatchers.h:116
const internal::VariadicDynCastAllOfMatcher< Decl, ValueDecl > valueDecl
Matches any value declaration.
Definition: ASTMatchers.h:915
const internal::VariadicDynCastAllOfMatcher< Decl, ClassTemplateDecl > classTemplateDecl
Matches C++ class template declarations.
Definition: ASTMatchers.h:363
const internal::VariadicDynCastAllOfMatcher< Decl, EnumConstantDecl > enumConstantDecl
Matches enum constants.
Definition: ASTMatchers.h:965
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:768
CompoundLiteralExpr - [C99 6.5.2.5].
Definition: Expr.h:2579
AST_MATCHER(Decl, isPublic)
Matches public C++ declarations.
Definition: ASTMatchers.h:485
Matcher< NamedDecl > hasAnyNameFunc(ArrayRef< const StringRef *> NameRefs)
internal::PolymorphicMatcherWithParam1< internal::ValueEqualsMatcher, ValueT > equals(const ValueT &Value)
Matches literals that are equal to the given value.
Definition: ASTMatchers.h:3796
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:113
const internal::VariadicDynCastAllOfMatcher< Decl, VarDecl > varDecl
Matches variable declarations.
Definition: ASTMatchers.h:993
const internal::VariadicDynCastAllOfMatcher< Decl, TypeAliasDecl > typeAliasDecl
Matches type alias declarations.
Definition: ASTMatchers.h:181
Represents an expression – generally a full-expression – that introduces cleanups to be run at the ...
Definition: ExprCXX.h:2918
Represents the result of substituting a type for a template type parameter.
Definition: Type.h:4027
const internal::VariadicDynCastAllOfMatcher< Stmt, AsmStmt > asmStmt
Matches asm statements.
Definition: ASTMatchers.h:1758
const internal::VariadicDynCastAllOfMatcher< Stmt, DesignatedInitExpr > designatedInitExpr
Matches C99 designated initializer expressions [C99 6.7.8].
Definition: ASTMatchers.h:2081
AST_POLYMORPHIC_MATCHER_P2(hasTemplateArgument, AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl, TemplateSpecializationType, FunctionDecl), unsigned, N, internal::Matcher< TemplateArgument >, InnerMatcher)
Matches classTemplateSpecializations, templateSpecializationType and functionDecl where the n&#39;th Temp...
Definition: ASTMatchers.h:751
A C++ static_cast expression (C++ [expr.static.cast]).
Definition: ExprCXX.h:267
Base wrapper for a particular "section" of type source info.
Definition: TypeLoc.h:40
RecordDecl - Represents a struct/union/class.
Definition: Decl.h:3311
Represents a C99 designated initializer expression.
Definition: Expr.h:3984
const internal::VariadicDynCastAllOfMatcher< Stmt, ConditionalOperator > conditionalOperator
Matches conditional operator expressions.
Definition: ASTMatchers.h:1883
const internal::VariadicDynCastAllOfMatcher< Stmt, UnaryExprOrTypeTraitExpr > unaryExprOrTypeTraitExpr
Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL)
Definition: ASTMatchers.h:2155
Represents a class template specialization, which refers to a class template with a given set of temp...
const internal::VariadicAllOfMatcher< QualType > qualType
Matches QualTypes in the clang AST.
Definition: ASTMatchers.h:2099
const internal::VariadicDynCastAllOfMatcher< Stmt, CaseStmt > caseStmt
Matches case statements inside switch statements.
Definition: ASTMatchers.h:1694
A C++ nested-name-specifier augmented with source location information.
The template argument is an integral value stored in an llvm::APSInt that was provided for an integra...
Definition: TemplateBase.h:57
const IDToNodeMap & getMap() const
Retrieve mapping from binding identifiers to bound nodes.
Definition: ASTMatchers.h:84
FieldDecl - An instance of this class is created by Sema::ActOnField to represent a member of a struc...
Definition: Decl.h:2323
UnaryExprOrTypeTrait
Names for the "expression or type" traits.
Definition: TypeTraits.h:93
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
Definition: ASTMatchers.h:271
const internal::VariadicDynCastAllOfMatcher< Stmt, ObjCMessageExpr > objcMessageExpr
Matches ObjectiveC Message invocation expressions.
Definition: ASTMatchers.h:1108
const internal::VariadicDynCastAllOfMatcher< Decl, CXXDestructorDecl > cxxDestructorDecl
Matches explicit C++ destructor declarations.
Definition: ASTMatchers.h:943
const internal::VariadicDynCastAllOfMatcher< Stmt, WhileStmt > whileStmt
Matches while statements.
Definition: ASTMatchers.h:1590
Represents an access specifier followed by colon &#39;:&#39;.
Definition: DeclCXX.h:103
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXTryStmt > cxxTryStmt
Matches try statements.
Definition: ASTMatchers.h:1730
const internal::VariadicDynCastAllOfMatcher< Stmt, CompoundLiteralExpr > compoundLiteralExpr
Matches compound (i.e.
Definition: ASTMatchers.h:1830
Describes an C or C++ initializer list.
Definition: Expr.h:3763
Represents a C++ using-declaration.
Definition: DeclCXX.h:3075
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXReinterpretCastExpr > cxxReinterpretCastExpr
Matches a reinterpret_cast expression.
Definition: ASTMatchers.h:1936
An rvalue reference type, per C++11 [dcl.ref].
Definition: Type.h:2402
const internal::VariadicDynCastAllOfMatcher< Stmt, NullStmt > nullStmt
Matches null statements.
Definition: ASTMatchers.h:1748
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXNewExpr > cxxNewExpr
Matches new expressions.
Definition: ASTMatchers.h:1425
ForStmt - This represents a &#39;for (init;cond;inc)&#39; stmt.
Definition: Stmt.h:1179
const internal::VariadicDynCastAllOfMatcher< Decl, NamedDecl > namedDecl
Matches a declaration of anything that could have a name.
Definition: ASTMatchers.h:295
const internal::VariadicDynCastAllOfMatcher< Decl, TypedefDecl > typedefDecl
Matches typedef declarations.
Definition: ASTMatchers.h:158
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXMemberCallExpr > cxxMemberCallExpr
Matches member call expressions.
Definition: ASTMatchers.h:1095
const internal::VariadicOperatorMatcherFunc< 2, UINT_MAX > eachOf
Matches if any of the given matchers matches.
Definition: ASTMatchers.h:2126
AST_MATCHER_FUNCTION(internal::Matcher< Decl >, isInstantiated)
Matches declarations that are template instantiations or are inside template instantiations.
Definition: ASTMatchers.h:4497
const internal::VariadicDynCastAllOfMatcher< Stmt, DefaultStmt > defaultStmt
Matches default statements inside switch statements.
Definition: ASTMatchers.h:1704
const internal::VariadicDynCastAllOfMatcher< Stmt, ExprWithCleanups > exprWithCleanups
Matches expressions that introduce cleanups to be run at the end of the sub-expression&#39;s evaluation...
Definition: ASTMatchers.h:1193
const T * getNodeAs(StringRef ID) const
Returns the AST node bound to ID.
Definition: ASTMatchers.h:74
SourceLocation getExpansionLoc(SourceLocation Loc) const
Given a SourceLocation object Loc, return the expansion location referenced by the ID...
const internal::VariadicDynCastAllOfMatcher< Stmt, CompoundStmt > compoundStmt
Matches compound statements.
Definition: ASTMatchers.h:1712
TypeDecl - Represents a declaration of a type.
Definition: Decl.h:2599
const internal::VariadicDynCastAllOfMatcher< Decl, ParmVarDecl > parmVarDecl
Matches parameter variable declarations.
Definition: ASTMatchers.h:399
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:2914
const internal::VariadicDynCastAllOfMatcher< Stmt, ReturnStmt > returnStmt
Matches return statements.
Definition: ASTMatchers.h:1630
CXXForRangeStmt - This represents C++0x [stmt.ranged]&#39;s ranged for statement, represented as &#39;for (ra...
Definition: StmtCXX.h:128
const internal::VariadicDynCastAllOfMatcher< Decl, TranslationUnitDecl > translationUnitDecl
Matches the top declaration context.
Definition: ASTMatchers.h:147
const internal::VariadicDynCastAllOfMatcher< Decl, FriendDecl > friendDecl
Matches friend declarations.
Definition: ASTMatchers.h:1031
bool isNothrow(const ASTContext &Ctx, bool ResultIfDependent=false) const
Determine whether this function type has a non-throwing exception specification.
Definition: Type.h:3403
internal::Matcher< Stmt > sizeOfExpr(const internal::Matcher< UnaryExprOrTypeTraitExpr > &InnerMatcher)
Same as unaryExprOrTypeTraitExpr, but only matching sizeof.
Definition: ASTMatchers.h:2194
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition: Expr.h:2649
const internal::VariadicDynCastAllOfMatcher< Decl, StaticAssertDecl > staticAssertDecl
Matches a C++ static_assert declaration.
Definition: ASTMatchers.h:1922
Represents an Objective-C protocol declaration.
Definition: DeclObjC.h:1984
Represents binding an expression to a temporary.
Definition: ExprCXX.h:1132
const internal::VariadicDynCastAllOfMatcher< Decl, LabelDecl > labelDecl
Matches a declaration of label.
Definition: ASTMatchers.h:306
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition: ExprCXX.h:1517
const internal::VariadicDynCastAllOfMatcher< Stmt, DeclStmt > declStmt
Matches declaration statements.
Definition: ASTMatchers.h:1053
internal::PolymorphicMatcherWithParam1< internal::HasOverloadedOperatorNameMatcher, StringRef, AST_POLYMORPHIC_SUPPORTED_TYPES(CXXOperatorCallExpr, FunctionDecl)> hasOverloadedOperatorName(StringRef Name)
Matches overloaded operator names.
Definition: ASTMatchers.h:2280
Represents an ObjC class declaration.
Definition: DeclObjC.h:1107
internal::Matcher< T > findAll(const internal::Matcher< T > &Matcher)
Matches if the node or any descendant matches.
Definition: ASTMatchers.h:2468
internal::Matcher< Stmt > StatementMatcher
Definition: ASTMatchers.h:111
A default argument (C++ [dcl.fct.default]).
Definition: ExprCXX.h:980
const internal::VariadicDynCastAllOfMatcher< Stmt, UnaryOperator > unaryOperator
Matches unary operator expressions.
Definition: ASTMatchers.h:1873
const internal::VariadicDynCastAllOfMatcher< Stmt, MemberExpr > memberExpr
Matches member expressions.
Definition: ASTMatchers.h:1066
const internal::VariadicDynCastAllOfMatcher< Stmt, InitListExpr > initListExpr
Matches init list expressions.
Definition: ASTMatchers.h:1205
ConditionalOperator - The ?: ternary operator.
Definition: Expr.h:3187
const internal::VariadicOperatorMatcherFunc< 2, UINT_MAX > allOf
Matches if all given matchers match.
Definition: ASTMatchers.h:2140
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:575
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3095
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXFunctionalCastExpr > cxxFunctionalCastExpr
Matches functional cast expressions.
Definition: ASTMatchers.h:2051
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:646
CastKind
CastKind - The kind of operation required for a conversion.
StringRef Filename
Definition: Format.cpp:1244
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand...
Definition: Expr.h:1991
const internal::VariadicDynCastAllOfMatcher< Stmt, ArraySubscriptExpr > arraySubscriptExpr
Matches array subscript expressions.
Definition: ASTMatchers.h:1447
ID
Defines the set of possible language-specific address spaces.
Definition: AddressSpaces.h:27
internal::Matcher< NestedNameSpecifierLoc > NestedNameSpecifierLocMatcher
Definition: ASTMatchers.h:115
const internal::VariadicDynCastAllOfMatcher< Stmt, LambdaExpr > lambdaExpr
Matches lambda expressions.
Definition: ASTMatchers.h:1084
const internal::VariadicDynCastAllOfMatcher< Stmt, BinaryOperator > binaryOperator
Matches binary operator expressions.
Definition: ASTMatchers.h:1863
Represents an array type in C++ whose size is a value-dependent expression.
Definition: Type.h:2678
const internal::VariadicAllOfMatcher< NestedNameSpecifier > nestedNameSpecifier
Matches nested name specifiers.
Definition: ASTMatchers.h:5183
const internal::ArgumentAdaptingMatcherFunc< internal::ForEachDescendantMatcher > LLVM_ATTRIBUTE_UNUSED forEachDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher. ...
Definition: ASTMatchers.h:2449
ValueDecl - Represent the declaration of a variable (in which case it is an lvalue) a function (in wh...
Definition: Decl.h:590
Expr - This represents one expression.
Definition: Expr.h:105
const internal::VariadicDynCastAllOfMatcher< Stmt, IntegerLiteral > integerLiteral
Matches integer literals of all sizes / encodings, e.g.
Definition: ASTMatchers.h:1801
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
const internal::VariadicDynCastAllOfMatcher< Stmt, ForStmt > forStmt
Matches for statements.
Definition: ASTMatchers.h:1512
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
Declaration of a template type parameter.
const internal::VariadicOperatorMatcherFunc< 1, 1 > unless
Matches if the provided matcher does not match.
Definition: ASTMatchers.h:2514
Represents a C++ functional cast expression that builds a temporary object.
Definition: ExprCXX.h:1467
A C++ const_cast expression (C++ [expr.const.cast]).
Definition: ExprCXX.h:385
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2443
const internal::VariadicAllOfMatcher< NestedNameSpecifierLoc > nestedNameSpecifierLoc
Same as nestedNameSpecifier but matches NestedNameSpecifierLoc.
Definition: ASTMatchers.h:5187
const internal::VariadicDynCastAllOfMatcher< Stmt, GotoStmt > gotoStmt
Matches goto statements.
Definition: ASTMatchers.h:1641
const internal::VariadicDynCastAllOfMatcher< Decl, RecordDecl > recordDecl
Matches class, struct, and union declarations.
Definition: ASTMatchers.h:342
internal::Matcher< T > id(StringRef ID, const internal::BindableMatcher< T > &InnerMatcher)
If the provided matcher matches a node, binds the node to ID.
Definition: ASTMatchers.h:102
NonTypeTemplateParmDecl - Declares a non-type template parameter, e.g., "Size" in.
static SVal getValue(SVal val, SValBuilder &svalBuilder)
internal::BoundNodesMap::IDToNodeMap IDToNodeMap
Type of mapping from binding identifiers to bound nodes.
Definition: ASTMatchers.h:81
A unary type transform, which is a type constructed from another.
Definition: Type.h:3674
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXTemporaryObjectExpr > cxxTemporaryObjectExpr
Matches functional cast expressions having N != 1 arguments.
Definition: ASTMatchers.h:2061
const internal::VariadicDynCastAllOfMatcher< Stmt, PredefinedExpr > predefinedExpr
Matches predefined identifier expressions [C99 6.4.2.2].
Definition: ASTMatchers.h:2071
ReturnStmt - This represents a return, optionally of an expression: return; return 4;...
Definition: Stmt.h:1392
const internal::VariadicDynCastAllOfMatcher< Stmt, MaterializeTemporaryExpr > materializeTemporaryExpr
Matches nodes where temporaries are materialized.
Definition: ASTMatchers.h:1415
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:860
UnaryOperator - This represents the unary-expression&#39;s (except sizeof and alignof), the postinc/postdec operators from postfix-expression, and various extensions.
Definition: Expr.h:1685
AST_TYPE_TRAVERSE_MATCHER(hasDeducedType, getDeducedType, AST_POLYMORPHIC_SUPPORTED_TYPES(AutoType))
Matches AutoType nodes where the deduced type is a specific type.
An lvalue reference type, per C++11 [dcl.ref].
Definition: Type.h:2385
Represents a reference to a non-type template parameter that has been substituted with a template arg...
Definition: ExprCXX.h:3749
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXForRangeStmt > cxxForRangeStmt
Matches range-based for statements.
Definition: ASTMatchers.h:1552
const internal::VariadicFunction< internal::Matcher< NamedDecl >, StringRef, internal::hasAnyNameFunc > hasAnyName
Matches NamedDecl nodes that have any of the specified names.
Definition: ASTMatchers.h:2233
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXStaticCastExpr > cxxStaticCastExpr
Matches a C++ static_cast expression.
Definition: ASTMatchers.h:1953
const internal::VariadicDynCastAllOfMatcher< Stmt, AtomicExpr > atomicExpr
Matches atomic builtins.
Definition: ASTMatchers.h:1845
Represents a C++ conversion function within a class.
Definition: DeclCXX.h:2497
The result type of a method or function.
This template specialization was implicitly instantiated from a template.
Definition: Specifiers.h:148
CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style cast in C++ (C++ [expr.cast]), which uses the syntax (Type)expr.
Definition: Expr.h:2851
bool isTemplateInstantiation(TemplateSpecializationKind Kind)
Determine whether this template specialization kind refers to an instantiation of an entity (as oppos...
Definition: Specifiers.h:166
const internal::VariadicDynCastAllOfMatcher< Decl, ObjCProtocolDecl > objcProtocolDecl
Matches Objective-C protocol declarations.
Definition: ASTMatchers.h:1130
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXDeleteExpr > cxxDeleteExpr
Matches delete expressions.
Definition: ASTMatchers.h:1435
const internal::VariadicDynCastAllOfMatcher< Stmt, SwitchCase > switchCase
Matches case and default statements inside switch statements.
Definition: ASTMatchers.h:1684
AST_POLYMORPHIC_MATCHER_P_OVERLOAD(hasType, AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, TypedefNameDecl, ValueDecl), internal::Matcher< QualType >, InnerMatcher, 0)
Matches if the expression&#39;s or declaration&#39;s type matches a type matcher.
Definition: ASTMatchers.h:2729
DoStmt - This represents a &#39;do/while&#39; stmt.
Definition: Stmt.h:1128
AST_MATCHER_P2(DeclStmt, containsDeclaration, unsigned, N, internal::Matcher< Decl >, InnerMatcher)
Matches the n&#39;th declaration of a declaration statement.
Definition: ASTMatchers.h:3155
const internal::VariadicDynCastAllOfMatcher< Decl, ObjCInterfaceDecl > objcInterfaceDecl
Matches Objective-C interface declarations.
Definition: ASTMatchers.h:1119
const internal::VariadicAllOfMatcher< TypeLoc > typeLoc
Matches TypeLocs in the clang AST.
Definition: ASTMatchers.h:2105
Thread storage duration.
Definition: Specifiers.h:275
Maps string IDs to AST nodes matched by parts of a matcher.
Definition: ASTMatchers.h:67
A C++ dynamic_cast expression (C++ [expr.dynamic.cast]).
Definition: ExprCXX.h:303
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXBindTemporaryExpr > cxxBindTemporaryExpr
Matches nodes where temporaries are created.
Definition: ASTMatchers.h:1393
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class...
Definition: Expr.h:865
std::string getAsString() const
Derive the full selector name (e.g.
friend class internal::BoundNodesTreeBuilder
Definition: ASTMatchers.h:95
const internal::VariadicDynCastAllOfMatcher< Decl, UnresolvedUsingValueDecl > unresolvedUsingValueDecl
Matches unresolved using value declarations.
Definition: ASTMatchers.h:1313
Kind
A reference to an overloaded function set, either an UnresolvedLookupExpr or an UnresolvedMemberExpr...
Definition: ExprCXX.h:2465
AST_MATCHER_FUNCTION_P_OVERLOAD(internal::BindableMatcher< TypeLoc >, loc, internal::Matcher< QualType >, InnerMatcher, 0)
Matches TypeLocs for which the given inner QualType-matcher matches.
Definition: ASTMatchers.h:4543
const internal::VariadicDynCastAllOfMatcher< Stmt, CharacterLiteral > characterLiteral
Matches character literals (also matches wchar_t).
Definition: ASTMatchers.h:1793
const internal::VariadicDynCastAllOfMatcher< Stmt, DeclRefExpr > declRefExpr
Matches expressions that refer to declarations.
Definition: ASTMatchers.h:1495
const internal::VariadicAllOfMatcher< CXXCtorInitializer > cxxCtorInitializer
Matches constructor initializers.
Definition: ASTMatchers.h:425
Sugar for parentheses used when specifying types.
Definition: Type.h:2171
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums...
Definition: Type.h:3776
StringRef getName() const
Definition: FileManager.h:84
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXThrowExpr > cxxThrowExpr
Matches throw expressions.
Definition: ASTMatchers.h:1739
const internal::VariadicDynCastAllOfMatcher< Stmt, ImplicitCastExpr > implicitCastExpr
Matches the implicit cast nodes of Clang&#39;s AST.
Definition: ASTMatchers.h:2024
A call to a literal operator (C++11 [over.literal]) written as a user-defined literal (C++11 [lit...
Definition: ExprCXX.h:422
const internal::ArgumentAdaptingMatcherFunc< internal::HasMatcher > LLVM_ATTRIBUTE_UNUSED has
Matches AST nodes that have child AST nodes that match the provided matcher.
Definition: ASTMatchers.h:2383
TagDecl - Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:2773
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXOperatorCallExpr > cxxOperatorCallExpr
Matches overloaded operator calls.
Definition: ASTMatchers.h:1478
Represents a call to a member function that may be written either with member call syntax (e...
Definition: ExprCXX.h:134
DeclStmt - Adaptor class for mixing declarations with statements and expressions. ...
Definition: Stmt.h:467
static QualType getUnderlyingType(const SubRegion *R)
Represents a dependent using declaration which was not marked with typename.
Definition: DeclCXX.h:3292
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:59
const internal::VariadicDynCastAllOfMatcher< Decl, ClassTemplateSpecializationDecl > classTemplateSpecializationDecl
Matches C++ class template specializations.
Definition: ASTMatchers.h:377
AST_POLYMORPHIC_MATCHER_P(isExpansionInFileMatching, AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc), std::string, RegExp)
Matches AST nodes that were expanded within files whose name is partially matching a given regex...
Definition: ASTMatchers.h:243
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1811
internal::Matcher< Stmt > alignOfExpr(const internal::Matcher< UnaryExprOrTypeTraitExpr > &InnerMatcher)
Same as unaryExprOrTypeTraitExpr, but only matching alignof.
Definition: ASTMatchers.h:2186
static bool hasAttr(const FunctionDecl *D, bool IgnoreImplicitAttr)
Definition: SemaCUDA.cpp:97
const internal::VariadicDynCastAllOfMatcher< Decl, FunctionDecl > functionDecl
Matches function declarations.
Definition: ASTMatchers.h:1011
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
ObjCCategoryDecl - Represents a category declaration.
Definition: DeclObjC.h:2187
internal::Matcher< TypeLoc > TypeLocMatcher
Definition: ASTMatchers.h:113
bool isInMainFile(SourceLocation Loc) const
Returns whether the PresumedLoc for a given SourceLocation is in the main file.
const internal::VariadicDynCastAllOfMatcher< Decl, CXXConstructorDecl > cxxConstructorDecl
Matches C++ constructor declarations.
Definition: ASTMatchers.h:930
const internal::VariadicDynCastAllOfMatcher< Decl, AccessSpecDecl > accessSpecDecl
Matches C++ access specifier declarations.
Definition: ASTMatchers.h:414
Represents one property declaration in an Objective-C interface.
Definition: DeclObjC.h:702
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
Definition: Expr.h:2751
#define AST_POLYMORPHIC_SUPPORTED_TYPES(...)
Construct a type-list to be passed to the AST_POLYMORPHIC_MATCHER* macros.
const internal::VariadicDynCastAllOfMatcher< Decl, ObjCPropertyDecl > objcPropertyDecl
Matches Objective-C property declarations.
Definition: ASTMatchers.h:1182
This template specialization was instantiated from a template due to an explicit instantiation defini...
Definition: Specifiers.h:160
Represents a C++11 static_assert declaration.
Definition: DeclCXX.h:3468
const internal::VariadicDynCastAllOfMatcher< Stmt, ExplicitCastExpr > explicitCastExpr
Matches explicit cast expressions.
Definition: ASTMatchers.h:2016
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition: Expr.h:3397
static DynTypedNode create(const T &Node)
Creates a DynTypedNode from Node.
const internal::VariadicDynCastAllOfMatcher< Stmt, SubstNonTypeTemplateParmExpr > substNonTypeTemplateParmExpr
Matches substitutions of non-type template parameters.
Definition: ASTMatchers.h:1256
__PTRDIFF_TYPE__ ptrdiff_t
A signed integer type that is the result of subtracting two pointers.
Definition: opencl-c.h:68
ExceptionSpecificationType getExceptionSpecType() const
Get the kind of exception specification on this function.
Definition: Type.h:3337
Represents a pointer type decayed from an array or function type.
Definition: Type.h:2286
The injected class name of a C++ class template or class template partial specialization.
Definition: Type.h:4402
const internal::VariadicDynCastAllOfMatcher< Stmt, StringLiteral > stringLiteral
Matches string literals (also matches wide string literals).
Definition: ASTMatchers.h:1779
AST_TYPELOC_TRAVERSE_MATCHER(hasElementType, getElement, AST_POLYMORPHIC_SUPPORTED_TYPES(ArrayType, ComplexType))
Matches arrays and C99 complex types that have a specific element type.
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:2639
ast_type_traits::DynTypedNode Node
Represents a template argument.
Definition: