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