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