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