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, ast_matchers::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  std::vector<std::string> Names;
2317  Names.push_back(Name);
2318  return internal::Matcher<NamedDecl>(new internal::HasNameMatcher(Names));
2319 }
2320 
2321 /// \brief Matches NamedDecl nodes that have any of the specified names.
2322 ///
2323 /// This matcher is only provided as a performance optimization of hasName.
2324 /// \code
2325 /// hasAnyName(a, b, c)
2326 /// \endcode
2327 /// is equivalent to, but faster than
2328 /// \code
2329 /// anyOf(hasName(a), hasName(b), hasName(c))
2330 /// \endcode
2331 extern const internal::VariadicFunction<internal::Matcher<NamedDecl>, StringRef,
2333  hasAnyName;
2334 
2335 /// \brief Matches NamedDecl nodes whose fully qualified names contain
2336 /// a substring matched by the given RegExp.
2337 ///
2338 /// Supports specifying enclosing namespaces or classes by
2339 /// prefixing the name with '<enclosing>::'. Does not match typedefs
2340 /// of an underlying type with the given name.
2341 ///
2342 /// Example matches X (regexp == "::X")
2343 /// \code
2344 /// class X;
2345 /// \endcode
2346 ///
2347 /// Example matches X (regexp is one of "::X", "^foo::.*X", among others)
2348 /// \code
2349 /// namespace foo { namespace bar { class X; } }
2350 /// \endcode
2351 AST_MATCHER_P(NamedDecl, matchesName, std::string, RegExp) {
2352  assert(!RegExp.empty());
2353  std::string FullNameString = "::" + Node.getQualifiedNameAsString();
2354  llvm::Regex RE(RegExp);
2355  return RE.match(FullNameString);
2356 }
2357 
2358 /// \brief Matches overloaded operator names.
2359 ///
2360 /// Matches overloaded operator names specified in strings without the
2361 /// "operator" prefix: e.g. "<<".
2362 ///
2363 /// Given:
2364 /// \code
2365 /// class A { int operator*(); };
2366 /// const A &operator<<(const A &a, const A &b);
2367 /// A a;
2368 /// a << a; // <-- This matches
2369 /// \endcode
2370 ///
2371 /// \c cxxOperatorCallExpr(hasOverloadedOperatorName("<<"))) matches the
2372 /// specified line and
2373 /// \c cxxRecordDecl(hasMethod(hasOverloadedOperatorName("*")))
2374 /// matches the declaration of \c A.
2375 ///
2376 /// Usable as: Matcher<CXXOperatorCallExpr>, Matcher<FunctionDecl>
2377 inline internal::PolymorphicMatcherWithParam1<
2378  internal::HasOverloadedOperatorNameMatcher, StringRef,
2380 hasOverloadedOperatorName(StringRef Name) {
2381  return internal::PolymorphicMatcherWithParam1<
2382  internal::HasOverloadedOperatorNameMatcher, StringRef,
2384 }
2385 
2386 /// \brief Matches C++ classes that are directly or indirectly derived from
2387 /// a class matching \c Base.
2388 ///
2389 /// Note that a class is not considered to be derived from itself.
2390 ///
2391 /// Example matches Y, Z, C (Base == hasName("X"))
2392 /// \code
2393 /// class X;
2394 /// class Y : public X {}; // directly derived
2395 /// class Z : public Y {}; // indirectly derived
2396 /// typedef X A;
2397 /// typedef A B;
2398 /// class C : public B {}; // derived from a typedef of X
2399 /// \endcode
2400 ///
2401 /// In the following example, Bar matches isDerivedFrom(hasName("X")):
2402 /// \code
2403 /// class Foo;
2404 /// typedef Foo X;
2405 /// class Bar : public Foo {}; // derived from a type that X is a typedef of
2406 /// \endcode
2408  internal::Matcher<NamedDecl>, Base) {
2409  return Finder->classIsDerivedFrom(&Node, Base, Builder);
2410 }
2411 
2412 /// \brief Overloaded method as shortcut for \c isDerivedFrom(hasName(...)).
2413 AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isDerivedFrom, std::string, BaseName, 1) {
2414  assert(!BaseName.empty());
2415  return isDerivedFrom(hasName(BaseName)).matches(Node, Finder, Builder);
2416 }
2417 
2418 /// \brief Similar to \c isDerivedFrom(), but also matches classes that directly
2419 /// match \c Base.
2421  internal::Matcher<NamedDecl>, Base, 0) {
2422  return Matcher<CXXRecordDecl>(anyOf(Base, isDerivedFrom(Base)))
2423  .matches(Node, Finder, Builder);
2424 }
2425 
2426 /// \brief Overloaded method as shortcut for
2427 /// \c isSameOrDerivedFrom(hasName(...)).
2428 AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isSameOrDerivedFrom, std::string,
2429  BaseName, 1) {
2430  assert(!BaseName.empty());
2431  return isSameOrDerivedFrom(hasName(BaseName)).matches(Node, Finder, Builder);
2432 }
2433 
2434 /// \brief Matches the first method of a class or struct that satisfies \c
2435 /// InnerMatcher.
2436 ///
2437 /// Given:
2438 /// \code
2439 /// class A { void func(); };
2440 /// class B { void member(); };
2441 /// \endcode
2442 ///
2443 /// \c cxxRecordDecl(hasMethod(hasName("func"))) matches the declaration of
2444 /// \c A but not \c B.
2445 AST_MATCHER_P(CXXRecordDecl, hasMethod, internal::Matcher<CXXMethodDecl>,
2446  InnerMatcher) {
2447  return matchesFirstInPointerRange(InnerMatcher, Node.method_begin(),
2448  Node.method_end(), Finder, Builder);
2449 }
2450 
2451 /// \brief Matches the generated class of lambda expressions.
2452 ///
2453 /// Given:
2454 /// \code
2455 /// auto x = []{};
2456 /// \endcode
2457 ///
2458 /// \c cxxRecordDecl(isLambda()) matches the implicit class declaration of
2459 /// \c decltype(x)
2461  return Node.isLambda();
2462 }
2463 
2464 /// \brief Matches AST nodes that have child AST nodes that match the
2465 /// provided matcher.
2466 ///
2467 /// Example matches X, Y
2468 /// (matcher = cxxRecordDecl(has(cxxRecordDecl(hasName("X")))
2469 /// \code
2470 /// class X {}; // Matches X, because X::X is a class of name X inside X.
2471 /// class Y { class X {}; };
2472 /// class Z { class Y { class X {}; }; }; // Does not match Z.
2473 /// \endcode
2474 ///
2475 /// ChildT must be an AST base type.
2476 ///
2477 /// Usable as: Any Matcher
2478 /// Note that has is direct matcher, so it also matches things like implicit
2479 /// casts and paren casts. If you are matching with expr then you should
2480 /// probably consider using ignoringParenImpCasts like:
2481 /// has(ignoringParenImpCasts(expr())).
2482 extern const internal::ArgumentAdaptingMatcherFunc<internal::HasMatcher> has;
2483 
2484 /// \brief Matches AST nodes that have descendant AST nodes that match the
2485 /// provided matcher.
2486 ///
2487 /// Example matches X, Y, Z
2488 /// (matcher = cxxRecordDecl(hasDescendant(cxxRecordDecl(hasName("X")))))
2489 /// \code
2490 /// class X {}; // Matches X, because X::X is a class of name X inside X.
2491 /// class Y { class X {}; };
2492 /// class Z { class Y { class X {}; }; };
2493 /// \endcode
2494 ///
2495 /// DescendantT must be an AST base type.
2496 ///
2497 /// Usable as: Any Matcher
2498 extern const internal::ArgumentAdaptingMatcherFunc<
2499  internal::HasDescendantMatcher>
2500  hasDescendant;
2501 
2502 /// \brief Matches AST nodes that have child AST nodes that match the
2503 /// provided matcher.
2504 ///
2505 /// Example matches X, Y, Y::X, Z::Y, Z::Y::X
2506 /// (matcher = cxxRecordDecl(forEach(cxxRecordDecl(hasName("X")))
2507 /// \code
2508 /// class X {};
2509 /// class Y { class X {}; }; // Matches Y, because Y::X is a class of name X
2510 /// // inside Y.
2511 /// class Z { class Y { class X {}; }; }; // Does not match Z.
2512 /// \endcode
2513 ///
2514 /// ChildT must be an AST base type.
2515 ///
2516 /// As opposed to 'has', 'forEach' will cause a match for each result that
2517 /// matches instead of only on the first one.
2518 ///
2519 /// Usable as: Any Matcher
2520 extern const internal::ArgumentAdaptingMatcherFunc<internal::ForEachMatcher>
2521  forEach;
2522 
2523 /// \brief Matches AST nodes that have descendant AST nodes that match the
2524 /// provided matcher.
2525 ///
2526 /// Example matches X, A, A::X, B, B::C, B::C::X
2527 /// (matcher = cxxRecordDecl(forEachDescendant(cxxRecordDecl(hasName("X")))))
2528 /// \code
2529 /// class X {};
2530 /// class A { class X {}; }; // Matches A, because A::X is a class of name
2531 /// // X inside A.
2532 /// class B { class C { class X {}; }; };
2533 /// \endcode
2534 ///
2535 /// DescendantT must be an AST base type.
2536 ///
2537 /// As opposed to 'hasDescendant', 'forEachDescendant' will cause a match for
2538 /// each result that matches instead of only on the first one.
2539 ///
2540 /// Note: Recursively combined ForEachDescendant can cause many matches:
2541 /// cxxRecordDecl(forEachDescendant(cxxRecordDecl(
2542 /// forEachDescendant(cxxRecordDecl())
2543 /// )))
2544 /// will match 10 times (plus injected class name matches) on:
2545 /// \code
2546 /// class A { class B { class C { class D { class E {}; }; }; }; };
2547 /// \endcode
2548 ///
2549 /// Usable as: Any Matcher
2550 extern const internal::ArgumentAdaptingMatcherFunc<
2551  internal::ForEachDescendantMatcher>
2553 
2554 /// \brief Matches if the node or any descendant matches.
2555 ///
2556 /// Generates results for each match.
2557 ///
2558 /// For example, in:
2559 /// \code
2560 /// class A { class B {}; class C {}; };
2561 /// \endcode
2562 /// The matcher:
2563 /// \code
2564 /// cxxRecordDecl(hasName("::A"),
2565 /// findAll(cxxRecordDecl(isDefinition()).bind("m")))
2566 /// \endcode
2567 /// will generate results for \c A, \c B and \c C.
2568 ///
2569 /// Usable as: Any Matcher
2570 template <typename T>
2571 internal::Matcher<T> findAll(const internal::Matcher<T> &Matcher) {
2572  return eachOf(Matcher, forEachDescendant(Matcher));
2573 }
2574 
2575 /// \brief Matches AST nodes that have a parent that matches the provided
2576 /// matcher.
2577 ///
2578 /// Given
2579 /// \code
2580 /// void f() { for (;;) { int x = 42; if (true) { int x = 43; } } }
2581 /// \endcode
2582 /// \c compoundStmt(hasParent(ifStmt())) matches "{ int x = 43; }".
2583 ///
2584 /// Usable as: Any Matcher
2585 extern const internal::ArgumentAdaptingMatcherFunc<
2586  internal::HasParentMatcher,
2587  internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>,
2588  internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>>
2589  hasParent;
2590 
2591 /// \brief Matches AST nodes that have an ancestor that matches the provided
2592 /// matcher.
2593 ///
2594 /// Given
2595 /// \code
2596 /// void f() { if (true) { int x = 42; } }
2597 /// void g() { for (;;) { int x = 43; } }
2598 /// \endcode
2599 /// \c expr(integerLiteral(hasAncestor(ifStmt()))) matches \c 42, but not 43.
2600 ///
2601 /// Usable as: Any Matcher
2602 extern const internal::ArgumentAdaptingMatcherFunc<
2603  internal::HasAncestorMatcher,
2604  internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>,
2605  internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>>
2606  hasAncestor;
2607 
2608 /// \brief Matches if the provided matcher does not match.
2609 ///
2610 /// Example matches Y (matcher = cxxRecordDecl(unless(hasName("X"))))
2611 /// \code
2612 /// class X {};
2613 /// class Y {};
2614 /// \endcode
2615 ///
2616 /// Usable as: Any Matcher
2617 extern const internal::VariadicOperatorMatcherFunc<1, 1> unless;
2618 
2619 /// \brief Matches a node if the declaration associated with that node
2620 /// matches the given matcher.
2621 ///
2622 /// The associated declaration is:
2623 /// - for type nodes, the declaration of the underlying type
2624 /// - for CallExpr, the declaration of the callee
2625 /// - for MemberExpr, the declaration of the referenced member
2626 /// - for CXXConstructExpr, the declaration of the constructor
2627 /// - for CXXNewExpr, the declaration of the operator new
2628 ///
2629 /// For type nodes, hasDeclaration will generally match the declaration of the
2630 /// sugared type. Given
2631 /// \code
2632 /// class X {};
2633 /// typedef X Y;
2634 /// Y y;
2635 /// \endcode
2636 /// in varDecl(hasType(hasDeclaration(decl()))) the decl will match the
2637 /// typedefDecl. A common use case is to match the underlying, desugared type.
2638 /// This can be achieved by using the hasUnqualifiedDesugaredType matcher:
2639 /// \code
2640 /// varDecl(hasType(hasUnqualifiedDesugaredType(
2641 /// recordType(hasDeclaration(decl())))))
2642 /// \endcode
2643 /// In this matcher, the decl will match the CXXRecordDecl of class X.
2644 ///
2645 /// Usable as: Matcher<AddrLabelExpr>, Matcher<CallExpr>,
2646 /// Matcher<CXXConstructExpr>, Matcher<CXXNewExpr>, Matcher<DeclRefExpr>,
2647 /// Matcher<EnumType>, Matcher<InjectedClassNameType>, Matcher<LabelStmt>,
2648 /// Matcher<MemberExpr>, Matcher<QualType>, Matcher<RecordType>,
2649 /// Matcher<TagType>, Matcher<TemplateSpecializationType>,
2650 /// Matcher<TemplateTypeParmType>, Matcher<TypedefType>,
2651 /// Matcher<UnresolvedUsingType>
2652 inline internal::PolymorphicMatcherWithParam1<
2653  internal::HasDeclarationMatcher, internal::Matcher<Decl>,
2654  void(internal::HasDeclarationSupportedTypes)>
2655 hasDeclaration(const internal::Matcher<Decl> &InnerMatcher) {
2656  return internal::PolymorphicMatcherWithParam1<
2657  internal::HasDeclarationMatcher, internal::Matcher<Decl>,
2658  void(internal::HasDeclarationSupportedTypes)>(InnerMatcher);
2659 }
2660 
2661 /// \brief Matches a \c NamedDecl whose underlying declaration matches the given
2662 /// matcher.
2663 ///
2664 /// Given
2665 /// \code
2666 /// namespace N { template<class T> void f(T t); }
2667 /// template <class T> void g() { using N::f; f(T()); }
2668 /// \endcode
2669 /// \c unresolvedLookupExpr(hasAnyDeclaration(
2670 /// namedDecl(hasUnderlyingDecl(hasName("::N::f")))))
2671 /// matches the use of \c f in \c g() .
2672 AST_MATCHER_P(NamedDecl, hasUnderlyingDecl, internal::Matcher<NamedDecl>,
2673  InnerMatcher) {
2674  const NamedDecl *UnderlyingDecl = Node.getUnderlyingDecl();
2675 
2676  return UnderlyingDecl != nullptr &&
2677  InnerMatcher.matches(*UnderlyingDecl, Finder, Builder);
2678 }
2679 
2680 /// \brief Matches on the implicit object argument of a member call expression.
2681 ///
2682 /// Example matches y.x()
2683 /// (matcher = cxxMemberCallExpr(on(hasType(cxxRecordDecl(hasName("Y"))))))
2684 /// \code
2685 /// class Y { public: void x(); };
2686 /// void z() { Y y; y.x(); }
2687 /// \endcode
2688 ///
2689 /// FIXME: Overload to allow directly matching types?
2690 AST_MATCHER_P(CXXMemberCallExpr, on, internal::Matcher<Expr>,
2691  InnerMatcher) {
2692  const Expr *ExprNode = Node.getImplicitObjectArgument()
2693  ->IgnoreParenImpCasts();
2694  return (ExprNode != nullptr &&
2695  InnerMatcher.matches(*ExprNode, Finder, Builder));
2696 }
2697 
2698 
2699 /// \brief Matches on the receiver of an ObjectiveC Message expression.
2700 ///
2701 /// Example
2702 /// matcher = objCMessageExpr(hasReceiverType(asString("UIWebView *")));
2703 /// matches the [webView ...] message invocation.
2704 /// \code
2705 /// NSString *webViewJavaScript = ...
2706 /// UIWebView *webView = ...
2707 /// [webView stringByEvaluatingJavaScriptFromString:webViewJavascript];
2708 /// \endcode
2709 AST_MATCHER_P(ObjCMessageExpr, hasReceiverType, internal::Matcher<QualType>,
2710  InnerMatcher) {
2711  const QualType TypeDecl = Node.getReceiverType();
2712  return InnerMatcher.matches(TypeDecl, Finder, Builder);
2713 }
2714 
2715 /// \brief Matches when BaseName == Selector.getAsString()
2716 ///
2717 /// matcher = objCMessageExpr(hasSelector("loadHTMLString:baseURL:"));
2718 /// matches the outer message expr in the code below, but NOT the message
2719 /// invocation for self.bodyView.
2720 /// \code
2721 /// [self.bodyView loadHTMLString:html baseURL:NULL];
2722 /// \endcode
2723 AST_MATCHER_P(ObjCMessageExpr, hasSelector, std::string, BaseName) {
2724  Selector Sel = Node.getSelector();
2725  return BaseName.compare(Sel.getAsString()) == 0;
2726 }
2727 
2728 
2729 /// \brief Matches ObjC selectors whose name contains
2730 /// a substring matched by the given RegExp.
2731 /// matcher = objCMessageExpr(matchesSelector("loadHTMLString\:baseURL?"));
2732 /// matches the outer message expr in the code below, but NOT the message
2733 /// invocation for self.bodyView.
2734 /// \code
2735 /// [self.bodyView loadHTMLString:html baseURL:NULL];
2736 /// \endcode
2737 AST_MATCHER_P(ObjCMessageExpr, matchesSelector, std::string, RegExp) {
2738  assert(!RegExp.empty());
2739  std::string SelectorString = Node.getSelector().getAsString();
2740  llvm::Regex RE(RegExp);
2741  return RE.match(SelectorString);
2742 }
2743 
2744 /// \brief Matches when the selector is the empty selector
2745 ///
2746 /// Matches only when the selector of the objCMessageExpr is NULL. This may
2747 /// represent an error condition in the tree!
2748 AST_MATCHER(ObjCMessageExpr, hasNullSelector) {
2749  return Node.getSelector().isNull();
2750 }
2751 
2752 /// \brief Matches when the selector is a Unary Selector
2753 ///
2754 /// matcher = objCMessageExpr(matchesSelector(hasUnarySelector());
2755 /// matches self.bodyView in the code below, but NOT the outer message
2756 /// invocation of "loadHTMLString:baseURL:".
2757 /// \code
2758 /// [self.bodyView loadHTMLString:html baseURL:NULL];
2759 /// \endcode
2760 AST_MATCHER(ObjCMessageExpr, hasUnarySelector) {
2761  return Node.getSelector().isUnarySelector();
2762 }
2763 
2764 /// \brief Matches when the selector is a keyword selector
2765 ///
2766 /// objCMessageExpr(hasKeywordSelector()) matches the generated setFrame
2767 /// message expression in
2768 ///
2769 /// \code
2770 /// UIWebView *webView = ...;
2771 /// CGRect bodyFrame = webView.frame;
2772 /// bodyFrame.size.height = self.bodyContentHeight;
2773 /// webView.frame = bodyFrame;
2774 /// // ^---- matches here
2775 /// \endcode
2776 AST_MATCHER(ObjCMessageExpr, hasKeywordSelector) {
2777  return Node.getSelector().isKeywordSelector();
2778 }
2779 
2780 /// \brief Matches when the selector has the specified number of arguments
2781 ///
2782 /// matcher = objCMessageExpr(numSelectorArgs(0));
2783 /// matches self.bodyView in the code below
2784 ///
2785 /// matcher = objCMessageExpr(numSelectorArgs(2));
2786 /// matches the invocation of "loadHTMLString:baseURL:" but not that
2787 /// of self.bodyView
2788 /// \code
2789 /// [self.bodyView loadHTMLString:html baseURL:NULL];
2790 /// \endcode
2791 AST_MATCHER_P(ObjCMessageExpr, numSelectorArgs, unsigned, N) {
2792  return Node.getSelector().getNumArgs() == N;
2793 }
2794 
2795 /// \brief Matches if the call expression's callee expression matches.
2796 ///
2797 /// Given
2798 /// \code
2799 /// class Y { void x() { this->x(); x(); Y y; y.x(); } };
2800 /// void f() { f(); }
2801 /// \endcode
2802 /// callExpr(callee(expr()))
2803 /// matches this->x(), x(), y.x(), f()
2804 /// with callee(...)
2805 /// matching this->x, x, y.x, f respectively
2806 ///
2807 /// Note: Callee cannot take the more general internal::Matcher<Expr>
2808 /// because this introduces ambiguous overloads with calls to Callee taking a
2809 /// internal::Matcher<Decl>, as the matcher hierarchy is purely
2810 /// implemented in terms of implicit casts.
2811 AST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>,
2812  InnerMatcher) {
2813  const Expr *ExprNode = Node.getCallee();
2814  return (ExprNode != nullptr &&
2815  InnerMatcher.matches(*ExprNode, Finder, Builder));
2816 }
2817 
2818 /// \brief Matches if the call expression's callee's declaration matches the
2819 /// given matcher.
2820 ///
2821 /// Example matches y.x() (matcher = callExpr(callee(
2822 /// cxxMethodDecl(hasName("x")))))
2823 /// \code
2824 /// class Y { public: void x(); };
2825 /// void z() { Y y; y.x(); }
2826 /// \endcode
2827 AST_MATCHER_P_OVERLOAD(CallExpr, callee, internal::Matcher<Decl>, InnerMatcher,
2828  1) {
2829  return callExpr(hasDeclaration(InnerMatcher)).matches(Node, Finder, Builder);
2830 }
2831 
2832 /// \brief Matches if the expression's or declaration's type matches a type
2833 /// matcher.
2834 ///
2835 /// Example matches x (matcher = expr(hasType(cxxRecordDecl(hasName("X")))))
2836 /// and z (matcher = varDecl(hasType(cxxRecordDecl(hasName("X")))))
2837 /// and U (matcher = typedefDecl(hasType(asString("int")))
2838 /// \code
2839 /// class X {};
2840 /// void y(X &x) { x; X z; }
2841 /// typedef int U;
2842 /// \endcode
2845  internal::Matcher<QualType>, InnerMatcher, 0) {
2846  return InnerMatcher.matches(internal::getUnderlyingType(Node),
2847  Finder, Builder);
2848 }
2849 
2850 /// \brief Overloaded to match the declaration of the expression's or value
2851 /// declaration's type.
2852 ///
2853 /// In case of a value declaration (for example a variable declaration),
2854 /// this resolves one layer of indirection. For example, in the value
2855 /// declaration "X x;", cxxRecordDecl(hasName("X")) matches the declaration of
2856 /// X, while varDecl(hasType(cxxRecordDecl(hasName("X")))) matches the
2857 /// declaration of x.
2858 ///
2859 /// Example matches x (matcher = expr(hasType(cxxRecordDecl(hasName("X")))))
2860 /// and z (matcher = varDecl(hasType(cxxRecordDecl(hasName("X")))))
2861 /// \code
2862 /// class X {};
2863 /// void y(X &x) { x; X z; }
2864 /// \endcode
2865 ///
2866 /// Usable as: Matcher<Expr>, Matcher<ValueDecl>
2869  ValueDecl),
2870  internal::Matcher<Decl>, InnerMatcher, 1) {
2871  return qualType(hasDeclaration(InnerMatcher))
2872  .matches(Node.getType(), Finder, Builder);
2873 }
2874 
2875 /// \brief Matches if the type location of the declarator decl's type matches
2876 /// the inner matcher.
2877 ///
2878 /// Given
2879 /// \code
2880 /// int x;
2881 /// \endcode
2882 /// declaratorDecl(hasTypeLoc(loc(asString("int"))))
2883 /// matches int x
2884 AST_MATCHER_P(DeclaratorDecl, hasTypeLoc, internal::Matcher<TypeLoc>, Inner) {
2885  if (!Node.getTypeSourceInfo())
2886  // This happens for example for implicit destructors.
2887  return false;
2888  return Inner.matches(Node.getTypeSourceInfo()->getTypeLoc(), Finder, Builder);
2889 }
2890 
2891 /// \brief Matches if the matched type is represented by the given string.
2892 ///
2893 /// Given
2894 /// \code
2895 /// class Y { public: void x(); };
2896 /// void z() { Y* y; y->x(); }
2897 /// \endcode
2898 /// cxxMemberCallExpr(on(hasType(asString("class Y *"))))
2899 /// matches y->x()
2900 AST_MATCHER_P(QualType, asString, std::string, Name) {
2901  return Name == Node.getAsString();
2902 }
2903 
2904 /// \brief Matches if the matched type is a pointer type and the pointee type
2905 /// matches the specified matcher.
2906 ///
2907 /// Example matches y->x()
2908 /// (matcher = cxxMemberCallExpr(on(hasType(pointsTo
2909 /// cxxRecordDecl(hasName("Y")))))))
2910 /// \code
2911 /// class Y { public: void x(); };
2912 /// void z() { Y *y; y->x(); }
2913 /// \endcode
2915  QualType, pointsTo, internal::Matcher<QualType>,
2916  InnerMatcher) {
2917  return (!Node.isNull() && Node->isAnyPointerType() &&
2918  InnerMatcher.matches(Node->getPointeeType(), Finder, Builder));
2919 }
2920 
2921 /// \brief Overloaded to match the pointee type's declaration.
2922 AST_MATCHER_P_OVERLOAD(QualType, pointsTo, internal::Matcher<Decl>,
2923  InnerMatcher, 1) {
2924  return pointsTo(qualType(hasDeclaration(InnerMatcher)))
2925  .matches(Node, Finder, Builder);
2926 }
2927 
2928 /// \brief Matches if the matched type matches the unqualified desugared
2929 /// type of the matched node.
2930 ///
2931 /// For example, in:
2932 /// \code
2933 /// class A {};
2934 /// using B = A;
2935 /// \endcode
2936 /// The matcher type(hasUnqualifeidDesugaredType(recordType())) matches
2937 /// both B and A.
2938 AST_MATCHER_P(Type, hasUnqualifiedDesugaredType, internal::Matcher<Type>,
2939  InnerMatcher) {
2940  return InnerMatcher.matches(*Node.getUnqualifiedDesugaredType(), Finder,
2941  Builder);
2942 }
2943 
2944 /// \brief Matches if the matched type is a reference type and the referenced
2945 /// type matches the specified matcher.
2946 ///
2947 /// Example matches X &x and const X &y
2948 /// (matcher = varDecl(hasType(references(cxxRecordDecl(hasName("X"))))))
2949 /// \code
2950 /// class X {
2951 /// void a(X b) {
2952 /// X &x = b;
2953 /// const X &y = b;
2954 /// }
2955 /// };
2956 /// \endcode
2957 AST_MATCHER_P(QualType, references, internal::Matcher<QualType>,
2958  InnerMatcher) {
2959  return (!Node.isNull() && Node->isReferenceType() &&
2960  InnerMatcher.matches(Node->getPointeeType(), Finder, Builder));
2961 }
2962 
2963 /// \brief Matches QualTypes whose canonical type matches InnerMatcher.
2964 ///
2965 /// Given:
2966 /// \code
2967 /// typedef int &int_ref;
2968 /// int a;
2969 /// int_ref b = a;
2970 /// \endcode
2971 ///
2972 /// \c varDecl(hasType(qualType(referenceType()))))) will not match the
2973 /// declaration of b but \c
2974 /// varDecl(hasType(qualType(hasCanonicalType(referenceType())))))) does.
2975 AST_MATCHER_P(QualType, hasCanonicalType, internal::Matcher<QualType>,
2976  InnerMatcher) {
2977  if (Node.isNull())
2978  return false;
2979  return InnerMatcher.matches(Node.getCanonicalType(), Finder, Builder);
2980 }
2981 
2982 /// \brief Overloaded to match the referenced type's declaration.
2983 AST_MATCHER_P_OVERLOAD(QualType, references, internal::Matcher<Decl>,
2984  InnerMatcher, 1) {
2985  return references(qualType(hasDeclaration(InnerMatcher)))
2986  .matches(Node, Finder, Builder);
2987 }
2988 
2989 AST_MATCHER_P(CXXMemberCallExpr, onImplicitObjectArgument,
2990  internal::Matcher<Expr>, InnerMatcher) {
2991  const Expr *ExprNode = Node.getImplicitObjectArgument();
2992  return (ExprNode != nullptr &&
2993  InnerMatcher.matches(*ExprNode, Finder, Builder));
2994 }
2995 
2996 /// \brief Matches if the expression's type either matches the specified
2997 /// matcher, or is a pointer to a type that matches the InnerMatcher.
2999  internal::Matcher<QualType>, InnerMatcher, 0) {
3000  return onImplicitObjectArgument(
3001  anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher))))
3002  .matches(Node, Finder, Builder);
3003 }
3004 
3005 /// \brief Overloaded to match the type's declaration.
3007  internal::Matcher<Decl>, InnerMatcher, 1) {
3008  return onImplicitObjectArgument(
3009  anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher))))
3010  .matches(Node, Finder, Builder);
3011 }
3012 
3013 /// \brief Matches a DeclRefExpr that refers to a declaration that matches the
3014 /// specified matcher.
3015 ///
3016 /// Example matches x in if(x)
3017 /// (matcher = declRefExpr(to(varDecl(hasName("x")))))
3018 /// \code
3019 /// bool x;
3020 /// if (x) {}
3021 /// \endcode
3022 AST_MATCHER_P(DeclRefExpr, to, internal::Matcher<Decl>,
3023  InnerMatcher) {
3024  const Decl *DeclNode = Node.getDecl();
3025  return (DeclNode != nullptr &&
3026  InnerMatcher.matches(*DeclNode, Finder, Builder));
3027 }
3028 
3029 /// \brief Matches a \c DeclRefExpr that refers to a declaration through a
3030 /// specific using shadow declaration.
3031 ///
3032 /// Given
3033 /// \code
3034 /// namespace a { void f() {} }
3035 /// using a::f;
3036 /// void g() {
3037 /// f(); // Matches this ..
3038 /// a::f(); // .. but not this.
3039 /// }
3040 /// \endcode
3041 /// declRefExpr(throughUsingDecl(anything()))
3042 /// matches \c f()
3043 AST_MATCHER_P(DeclRefExpr, throughUsingDecl,
3044  internal::Matcher<UsingShadowDecl>, InnerMatcher) {
3045  const NamedDecl *FoundDecl = Node.getFoundDecl();
3046  if (const UsingShadowDecl *UsingDecl = dyn_cast<UsingShadowDecl>(FoundDecl))
3047  return InnerMatcher.matches(*UsingDecl, Finder, Builder);
3048  return false;
3049 }
3050 
3051 /// \brief Matches an \c OverloadExpr if any of the declarations in the set of
3052 /// overloads matches the given matcher.
3053 ///
3054 /// Given
3055 /// \code
3056 /// template <typename T> void foo(T);
3057 /// template <typename T> void bar(T);
3058 /// template <typename T> void baz(T t) {
3059 /// foo(t);
3060 /// bar(t);
3061 /// }
3062 /// \endcode
3063 /// unresolvedLookupExpr(hasAnyDeclaration(
3064 /// functionTemplateDecl(hasName("foo"))))
3065 /// matches \c foo in \c foo(t); but not \c bar in \c bar(t);
3066 AST_MATCHER_P(OverloadExpr, hasAnyDeclaration, internal::Matcher<Decl>,
3067  InnerMatcher) {
3068  return matchesFirstInPointerRange(InnerMatcher, Node.decls_begin(),
3069  Node.decls_end(), Finder, Builder);
3070 }
3071 
3072 /// \brief Matches the Decl of a DeclStmt which has a single declaration.
3073 ///
3074 /// Given
3075 /// \code
3076 /// int a, b;
3077 /// int c;
3078 /// \endcode
3079 /// declStmt(hasSingleDecl(anything()))
3080 /// matches 'int c;' but not 'int a, b;'.
3081 AST_MATCHER_P(DeclStmt, hasSingleDecl, internal::Matcher<Decl>, InnerMatcher) {
3082  if (Node.isSingleDecl()) {
3083  const Decl *FoundDecl = Node.getSingleDecl();
3084  return InnerMatcher.matches(*FoundDecl, Finder, Builder);
3085  }
3086  return false;
3087 }
3088 
3089 /// \brief Matches a variable declaration that has an initializer expression
3090 /// that matches the given matcher.
3091 ///
3092 /// Example matches x (matcher = varDecl(hasInitializer(callExpr())))
3093 /// \code
3094 /// bool y() { return true; }
3095 /// bool x = y();
3096 /// \endcode
3098  VarDecl, hasInitializer, internal::Matcher<Expr>,
3099  InnerMatcher) {
3100  const Expr *Initializer = Node.getAnyInitializer();
3101  return (Initializer != nullptr &&
3102  InnerMatcher.matches(*Initializer, Finder, Builder));
3103 }
3104 
3105 /// \brief Matches a variable declaration that has function scope and is a
3106 /// non-static local variable.
3107 ///
3108 /// Example matches x (matcher = varDecl(hasLocalStorage())
3109 /// \code
3110 /// void f() {
3111 /// int x;
3112 /// static int y;
3113 /// }
3114 /// int z;
3115 /// \endcode
3116 AST_MATCHER(VarDecl, hasLocalStorage) {
3117  return Node.hasLocalStorage();
3118 }
3119 
3120 /// \brief Matches a variable declaration that does not have local storage.
3121 ///
3122 /// Example matches y and z (matcher = varDecl(hasGlobalStorage())
3123 /// \code
3124 /// void f() {
3125 /// int x;
3126 /// static int y;
3127 /// }
3128 /// int z;
3129 /// \endcode
3130 AST_MATCHER(VarDecl, hasGlobalStorage) {
3131  return Node.hasGlobalStorage();
3132 }
3133 
3134 /// \brief Matches a variable declaration that has automatic storage duration.
3135 ///
3136 /// Example matches x, but not y, z, or a.
3137 /// (matcher = varDecl(hasAutomaticStorageDuration())
3138 /// \code
3139 /// void f() {
3140 /// int x;
3141 /// static int y;
3142 /// thread_local int z;
3143 /// }
3144 /// int a;
3145 /// \endcode
3146 AST_MATCHER(VarDecl, hasAutomaticStorageDuration) {
3147  return Node.getStorageDuration() == SD_Automatic;
3148 }
3149 
3150 /// \brief Matches a variable declaration that has static storage duration.
3151 /// It includes the variable declared at namespace scope and those declared
3152 /// with "static" and "extern" storage class specifiers.
3153 ///
3154 /// \code
3155 /// void f() {
3156 /// int x;
3157 /// static int y;
3158 /// thread_local int z;
3159 /// }
3160 /// int a;
3161 /// static int b;
3162 /// extern int c;
3163 /// varDecl(hasStaticStorageDuration())
3164 /// matches the function declaration y, a, b and c.
3165 /// \endcode
3166 AST_MATCHER(VarDecl, hasStaticStorageDuration) {
3167  return Node.getStorageDuration() == SD_Static;
3168 }
3169 
3170 /// \brief Matches a variable declaration that has thread storage duration.
3171 ///
3172 /// Example matches z, but not x, z, or a.
3173 /// (matcher = varDecl(hasThreadStorageDuration())
3174 /// \code
3175 /// void f() {
3176 /// int x;
3177 /// static int y;
3178 /// thread_local int z;
3179 /// }
3180 /// int a;
3181 /// \endcode
3182 AST_MATCHER(VarDecl, hasThreadStorageDuration) {
3183  return Node.getStorageDuration() == SD_Thread;
3184 }
3185 
3186 /// \brief Matches a variable declaration that is an exception variable from
3187 /// a C++ catch block, or an Objective-C \@catch statement.
3188 ///
3189 /// Example matches x (matcher = varDecl(isExceptionVariable())
3190 /// \code
3191 /// void f(int y) {
3192 /// try {
3193 /// } catch (int x) {
3194 /// }
3195 /// }
3196 /// \endcode
3197 AST_MATCHER(VarDecl, isExceptionVariable) {
3198  return Node.isExceptionVariable();
3199 }
3200 
3201 /// \brief Checks that a call expression or a constructor call expression has
3202 /// a specific number of arguments (including absent default arguments).
3203 ///
3204 /// Example matches f(0, 0) (matcher = callExpr(argumentCountIs(2)))
3205 /// \code
3206 /// void f(int x, int y);
3207 /// f(0, 0);
3208 /// \endcode
3212  ObjCMessageExpr),
3213  unsigned, N) {
3214  return Node.getNumArgs() == N;
3215 }
3216 
3217 /// \brief Matches the n'th argument of a call expression or a constructor
3218 /// call expression.
3219 ///
3220 /// Example matches y in x(y)
3221 /// (matcher = callExpr(hasArgument(0, declRefExpr())))
3222 /// \code
3223 /// void x(int) { int y; x(y); }
3224 /// \endcode
3228  ObjCMessageExpr),
3229  unsigned, N, internal::Matcher<Expr>, InnerMatcher) {
3230  return (N < Node.getNumArgs() &&
3231  InnerMatcher.matches(
3232  *Node.getArg(N)->IgnoreParenImpCasts(), Finder, Builder));
3233 }
3234 
3235 /// \brief Matches declaration statements that contain a specific number of
3236 /// declarations.
3237 ///
3238 /// Example: Given
3239 /// \code
3240 /// int a, b;
3241 /// int c;
3242 /// int d = 2, e;
3243 /// \endcode
3244 /// declCountIs(2)
3245 /// matches 'int a, b;' and 'int d = 2, e;', but not 'int c;'.
3246 AST_MATCHER_P(DeclStmt, declCountIs, unsigned, N) {
3247  return std::distance(Node.decl_begin(), Node.decl_end()) == (ptrdiff_t)N;
3248 }
3249 
3250 /// \brief Matches the n'th declaration of a declaration statement.
3251 ///
3252 /// Note that this does not work for global declarations because the AST
3253 /// breaks up multiple-declaration DeclStmt's into multiple single-declaration
3254 /// DeclStmt's.
3255 /// Example: Given non-global declarations
3256 /// \code
3257 /// int a, b = 0;
3258 /// int c;
3259 /// int d = 2, e;
3260 /// \endcode
3261 /// declStmt(containsDeclaration(
3262 /// 0, varDecl(hasInitializer(anything()))))
3263 /// matches only 'int d = 2, e;', and
3264 /// declStmt(containsDeclaration(1, varDecl()))
3265 /// \code
3266 /// matches 'int a, b = 0' as well as 'int d = 2, e;'
3267 /// but 'int c;' is not matched.
3268 /// \endcode
3269 AST_MATCHER_P2(DeclStmt, containsDeclaration, unsigned, N,
3270  internal::Matcher<Decl>, InnerMatcher) {
3271  const unsigned NumDecls = std::distance(Node.decl_begin(), Node.decl_end());
3272  if (N >= NumDecls)
3273  return false;
3274  DeclStmt::const_decl_iterator Iterator = Node.decl_begin();
3275  std::advance(Iterator, N);
3276  return InnerMatcher.matches(**Iterator, Finder, Builder);
3277 }
3278 
3279 /// \brief Matches a C++ catch statement that has a catch-all handler.
3280 ///
3281 /// Given
3282 /// \code
3283 /// try {
3284 /// // ...
3285 /// } catch (int) {
3286 /// // ...
3287 /// } catch (...) {
3288 /// // ...
3289 /// }
3290 /// /endcode
3291 /// cxxCatchStmt(isCatchAll()) matches catch(...) but not catch(int).
3292 AST_MATCHER(CXXCatchStmt, isCatchAll) {
3293  return Node.getExceptionDecl() == nullptr;
3294 }
3295 
3296 /// \brief Matches a constructor initializer.
3297 ///
3298 /// Given
3299 /// \code
3300 /// struct Foo {
3301 /// Foo() : foo_(1) { }
3302 /// int foo_;
3303 /// };
3304 /// \endcode
3305 /// cxxRecordDecl(has(cxxConstructorDecl(
3306 /// hasAnyConstructorInitializer(anything())
3307 /// )))
3308 /// record matches Foo, hasAnyConstructorInitializer matches foo_(1)
3309 AST_MATCHER_P(CXXConstructorDecl, hasAnyConstructorInitializer,
3310  internal::Matcher<CXXCtorInitializer>, InnerMatcher) {
3311  return matchesFirstInPointerRange(InnerMatcher, Node.init_begin(),
3312  Node.init_end(), Finder, Builder);
3313 }
3314 
3315 /// \brief Matches the field declaration of a constructor initializer.
3316 ///
3317 /// Given
3318 /// \code
3319 /// struct Foo {
3320 /// Foo() : foo_(1) { }
3321 /// int foo_;
3322 /// };
3323 /// \endcode
3324 /// cxxRecordDecl(has(cxxConstructorDecl(hasAnyConstructorInitializer(
3325 /// forField(hasName("foo_"))))))
3326 /// matches Foo
3327 /// with forField matching foo_
3329  internal::Matcher<FieldDecl>, InnerMatcher) {
3330  const FieldDecl *NodeAsDecl = Node.getAnyMember();
3331  return (NodeAsDecl != nullptr &&
3332  InnerMatcher.matches(*NodeAsDecl, Finder, Builder));
3333 }
3334 
3335 /// \brief Matches the initializer expression of a constructor initializer.
3336 ///
3337 /// Given
3338 /// \code
3339 /// struct Foo {
3340 /// Foo() : foo_(1) { }
3341 /// int foo_;
3342 /// };
3343 /// \endcode
3344 /// cxxRecordDecl(has(cxxConstructorDecl(hasAnyConstructorInitializer(
3345 /// withInitializer(integerLiteral(equals(1)))))))
3346 /// matches Foo
3347 /// with withInitializer matching (1)
3349  internal::Matcher<Expr>, InnerMatcher) {
3350  const Expr* NodeAsExpr = Node.getInit();
3351  return (NodeAsExpr != nullptr &&
3352  InnerMatcher.matches(*NodeAsExpr, Finder, Builder));
3353 }
3354 
3355 /// \brief Matches a constructor initializer if it is explicitly written in
3356 /// code (as opposed to implicitly added by the compiler).
3357 ///
3358 /// Given
3359 /// \code
3360 /// struct Foo {
3361 /// Foo() { }
3362 /// Foo(int) : foo_("A") { }
3363 /// string foo_;
3364 /// };
3365 /// \endcode
3366 /// cxxConstructorDecl(hasAnyConstructorInitializer(isWritten()))
3367 /// will match Foo(int), but not Foo()
3369  return Node.isWritten();
3370 }
3371 
3372 /// \brief Matches a constructor initializer if it is initializing a base, as
3373 /// opposed to a member.
3374 ///
3375 /// Given
3376 /// \code
3377 /// struct B {};
3378 /// struct D : B {
3379 /// int I;
3380 /// D(int i) : I(i) {}
3381 /// };
3382 /// struct E : B {
3383 /// E() : B() {}
3384 /// };
3385 /// \endcode
3386 /// cxxConstructorDecl(hasAnyConstructorInitializer(isBaseInitializer()))
3387 /// will match E(), but not match D(int).
3388 AST_MATCHER(CXXCtorInitializer, isBaseInitializer) {
3389  return Node.isBaseInitializer();
3390 }
3391 
3392 /// \brief Matches a constructor initializer if it is initializing a member, as
3393 /// opposed to a base.
3394 ///
3395 /// Given
3396 /// \code
3397 /// struct B {};
3398 /// struct D : B {
3399 /// int I;
3400 /// D(int i) : I(i) {}
3401 /// };
3402 /// struct E : B {
3403 /// E() : B() {}
3404 /// };
3405 /// \endcode
3406 /// cxxConstructorDecl(hasAnyConstructorInitializer(isMemberInitializer()))
3407 /// will match D(int), but not match E().
3408 AST_MATCHER(CXXCtorInitializer, isMemberInitializer) {
3409  return Node.isMemberInitializer();
3410 }
3411 
3412 /// \brief Matches any argument of a call expression or a constructor call
3413 /// expression.
3414 ///
3415 /// Given
3416 /// \code
3417 /// void x(int, int, int) { int y; x(1, y, 42); }
3418 /// \endcode
3419 /// callExpr(hasAnyArgument(declRefExpr()))
3420 /// matches x(1, y, 42)
3421 /// with hasAnyArgument(...)
3422 /// matching y
3426  internal::Matcher<Expr>, InnerMatcher) {
3427  for (const Expr *Arg : Node.arguments()) {
3428  BoundNodesTreeBuilder Result(*Builder);
3429  if (InnerMatcher.matches(*Arg, Finder, &Result)) {
3430  *Builder = std::move(Result);
3431  return true;
3432  }
3433  }
3434  return false;
3435 }
3436 
3437 /// \brief Matches a constructor call expression which uses list initialization.
3438 AST_MATCHER(CXXConstructExpr, isListInitialization) {
3439  return Node.isListInitialization();
3440 }
3441 
3442 /// \brief Matches a constructor call expression which requires
3443 /// zero initialization.
3444 ///
3445 /// Given
3446 /// \code
3447 /// void foo() {
3448 /// struct point { double x; double y; };
3449 /// point pt[2] = { { 1.0, 2.0 } };
3450 /// }
3451 /// \endcode
3452 /// initListExpr(has(cxxConstructExpr(requiresZeroInitialization()))
3453 /// will match the implicit array filler for pt[1].
3454 AST_MATCHER(CXXConstructExpr, requiresZeroInitialization) {
3455  return Node.requiresZeroInitialization();
3456 }
3457 
3458 /// \brief Matches the n'th parameter of a function declaration.
3459 ///
3460 /// Given
3461 /// \code
3462 /// class X { void f(int x) {} };
3463 /// \endcode
3464 /// cxxMethodDecl(hasParameter(0, hasType(varDecl())))
3465 /// matches f(int x) {}
3466 /// with hasParameter(...)
3467 /// matching int x
3469  unsigned, N, internal::Matcher<ParmVarDecl>,
3470  InnerMatcher) {
3471  return (N < Node.getNumParams() &&
3472  InnerMatcher.matches(
3473  *Node.getParamDecl(N), Finder, Builder));
3474 }
3475 
3476 /// \brief Matches all arguments and their respective ParmVarDecl.
3477 ///
3478 /// Given
3479 /// \code
3480 /// void f(int i);
3481 /// int y;
3482 /// f(y);
3483 /// \endcode
3484 /// callExpr(
3485 /// forEachArgumentWithParam(
3486 /// declRefExpr(to(varDecl(hasName("y")))),
3487 /// parmVarDecl(hasType(isInteger()))
3488 /// ))
3489 /// matches f(y);
3490 /// with declRefExpr(...)
3491 /// matching int y
3492 /// and parmVarDecl(...)
3493 /// matching int i
3494 AST_POLYMORPHIC_MATCHER_P2(forEachArgumentWithParam,
3497  internal::Matcher<Expr>, ArgMatcher,
3498  internal::Matcher<ParmVarDecl>, ParamMatcher) {
3499  BoundNodesTreeBuilder Result;
3500  // The first argument of an overloaded member operator is the implicit object
3501  // argument of the method which should not be matched against a parameter, so
3502  // we skip over it here.
3503  BoundNodesTreeBuilder Matches;
3504  unsigned ArgIndex = cxxOperatorCallExpr(callee(cxxMethodDecl()))
3505  .matches(Node, Finder, &Matches)
3506  ? 1
3507  : 0;
3508  int ParamIndex = 0;
3509  bool Matched = false;
3510  for (; ArgIndex < Node.getNumArgs(); ++ArgIndex) {
3511  BoundNodesTreeBuilder ArgMatches(*Builder);
3512  if (ArgMatcher.matches(*(Node.getArg(ArgIndex)->IgnoreParenCasts()),
3513  Finder, &ArgMatches)) {
3514  BoundNodesTreeBuilder ParamMatches(ArgMatches);
3516  hasParameter(ParamIndex, ParamMatcher)))),
3517  callExpr(callee(functionDecl(
3518  hasParameter(ParamIndex, ParamMatcher))))))
3519  .matches(Node, Finder, &ParamMatches)) {
3520  Result.addMatch(ParamMatches);
3521  Matched = true;
3522  }
3523  }
3524  ++ParamIndex;
3525  }
3526  *Builder = std::move(Result);
3527  return Matched;
3528 }
3529 
3530 /// \brief Matches any parameter of a function declaration.
3531 ///
3532 /// Does not match the 'this' parameter of a method.
3533 ///
3534 /// Given
3535 /// \code
3536 /// class X { void f(int x, int y, int z) {} };
3537 /// \endcode
3538 /// cxxMethodDecl(hasAnyParameter(hasName("y")))
3539 /// matches f(int x, int y, int z) {}
3540 /// with hasAnyParameter(...)
3541 /// matching int y
3542 AST_MATCHER_P(FunctionDecl, hasAnyParameter,
3543  internal::Matcher<ParmVarDecl>, InnerMatcher) {
3544  return matchesFirstInPointerRange(InnerMatcher, Node.param_begin(),
3545  Node.param_end(), Finder, Builder);
3546 }
3547 
3548 /// \brief Matches \c FunctionDecls and \c FunctionProtoTypes that have a
3549 /// specific parameter count.
3550 ///
3551 /// Given
3552 /// \code
3553 /// void f(int i) {}
3554 /// void g(int i, int j) {}
3555 /// void h(int i, int j);
3556 /// void j(int i);
3557 /// void k(int x, int y, int z, ...);
3558 /// \endcode
3559 /// functionDecl(parameterCountIs(2))
3560 /// matches \c g and \c h
3561 /// functionProtoType(parameterCountIs(2))
3562 /// matches \c g and \c h
3563 /// functionProtoType(parameterCountIs(3))
3564 /// matches \c k
3565 AST_POLYMORPHIC_MATCHER_P(parameterCountIs,
3568  unsigned, N) {
3569  return Node.getNumParams() == N;
3570 }
3571 
3572 /// \brief Matches \c FunctionDecls that have a noreturn attribute.
3573 ///
3574 /// Given
3575 /// \code
3576 /// void nope();
3577 /// [[noreturn]] void a();
3578 /// __attribute__((noreturn)) void b();
3579 /// struct c { [[noreturn]] c(); };
3580 /// \endcode
3581 /// functionDecl(isNoReturn())
3582 /// matches all of those except
3583 /// \code
3584 /// void nope();
3585 /// \endcode
3586 AST_MATCHER(FunctionDecl, isNoReturn) { return Node.isNoReturn(); }
3587 
3588 /// \brief Matches the return type of a function declaration.
3589 ///
3590 /// Given:
3591 /// \code
3592 /// class X { int f() { return 1; } };
3593 /// \endcode
3594 /// cxxMethodDecl(returns(asString("int")))
3595 /// matches int f() { return 1; }
3597  internal::Matcher<QualType>, InnerMatcher) {
3598  return InnerMatcher.matches(Node.getReturnType(), Finder, Builder);
3599 }
3600 
3601 /// \brief Matches extern "C" function or variable declarations.
3602 ///
3603 /// Given:
3604 /// \code
3605 /// extern "C" void f() {}
3606 /// extern "C" { void g() {} }
3607 /// void h() {}
3608 /// extern "C" int x = 1;
3609 /// extern "C" int y = 2;
3610 /// int z = 3;
3611 /// \endcode
3612 /// functionDecl(isExternC())
3613 /// matches the declaration of f and g, but not the declaration of h.
3614 /// varDecl(isExternC())
3615 /// matches the declaration of x and y, but not the declaration of z.
3617  VarDecl)) {
3618  return Node.isExternC();
3619 }
3620 
3621 /// \brief Matches variable/function declarations that have "static" storage
3622 /// class specifier ("static" keyword) written in the source.
3623 ///
3624 /// Given:
3625 /// \code
3626 /// static void f() {}
3627 /// static int i = 0;
3628 /// extern int j;
3629 /// int k;
3630 /// \endcode
3631 /// functionDecl(isStaticStorageClass())
3632 /// matches the function declaration f.
3633 /// varDecl(isStaticStorageClass())
3634 /// matches the variable declaration i.
3635 AST_POLYMORPHIC_MATCHER(isStaticStorageClass,
3637  VarDecl)) {
3638  return Node.getStorageClass() == SC_Static;
3639 }
3640 
3641 /// \brief Matches deleted function declarations.
3642 ///
3643 /// Given:
3644 /// \code
3645 /// void Func();
3646 /// void DeletedFunc() = delete;
3647 /// \endcode
3648 /// functionDecl(isDeleted())
3649 /// matches the declaration of DeletedFunc, but not Func.
3651  return Node.isDeleted();
3652 }
3653 
3654 /// \brief Matches defaulted function declarations.
3655 ///
3656 /// Given:
3657 /// \code
3658 /// class A { ~A(); };
3659 /// class B { ~B() = default; };
3660 /// \endcode
3661 /// functionDecl(isDefaulted())
3662 /// matches the declaration of ~B, but not ~A.
3663 AST_MATCHER(FunctionDecl, isDefaulted) {
3664  return Node.isDefaulted();
3665 }
3666 
3667 /// \brief Matches functions that have a dynamic exception specification.
3668 ///
3669 /// Given:
3670 /// \code
3671 /// void f();
3672 /// void g() noexcept;
3673 /// void h() noexcept(true);
3674 /// void i() noexcept(false);
3675 /// void j() throw();
3676 /// void k() throw(int);
3677 /// void l() throw(...);
3678 /// \endcode
3679 /// functionDecl(hasDynamicExceptionSpec()) and
3680 /// functionProtoType(hasDynamicExceptionSpec())
3681 /// match the declarations of j, k, and l, but not f, g, h, or i.
3682 AST_POLYMORPHIC_MATCHER(hasDynamicExceptionSpec,
3684  FunctionProtoType)) {
3685  if (const FunctionProtoType *FnTy = internal::getFunctionProtoType(Node))
3686  return FnTy->hasDynamicExceptionSpec();
3687  return false;
3688 }
3689 
3690 /// \brief Matches functions that have a non-throwing exception specification.
3691 ///
3692 /// Given:
3693 /// \code
3694 /// void f();
3695 /// void g() noexcept;
3696 /// void h() throw();
3697 /// void i() throw(int);
3698 /// void j() noexcept(false);
3699 /// \endcode
3700 /// functionDecl(isNoThrow()) and functionProtoType(isNoThrow())
3701 /// match the declarations of g, and h, but not f, i or j.
3704  FunctionProtoType)) {
3705  const FunctionProtoType *FnTy = internal::getFunctionProtoType(Node);
3706 
3707  // If the function does not have a prototype, then it is assumed to be a
3708  // throwing function (as it would if the function did not have any exception
3709  // specification).
3710  if (!FnTy)
3711  return false;
3712 
3713  // Assume the best for any unresolved exception specification.
3715  return true;
3716 
3717  return FnTy->isNothrow(Finder->getASTContext());
3718 }
3719 
3720 /// \brief Matches constexpr variable and function declarations.
3721 ///
3722 /// Given:
3723 /// \code
3724 /// constexpr int foo = 42;
3725 /// constexpr int bar();
3726 /// \endcode
3727 /// varDecl(isConstexpr())
3728 /// matches the declaration of foo.
3729 /// functionDecl(isConstexpr())
3730 /// matches the declaration of bar.
3733  FunctionDecl)) {
3734  return Node.isConstexpr();
3735 }
3736 
3737 /// \brief Matches the condition expression of an if statement, for loop,
3738 /// switch statement or conditional operator.
3739 ///
3740 /// Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
3741 /// \code
3742 /// if (true) {}
3743 /// \endcode
3745  hasCondition,
3748  internal::Matcher<Expr>, InnerMatcher) {
3749  const Expr *const Condition = Node.getCond();
3750  return (Condition != nullptr &&
3751  InnerMatcher.matches(*Condition, Finder, Builder));
3752 }
3753 
3754 /// \brief Matches the then-statement of an if statement.
3755 ///
3756 /// Examples matches the if statement
3757 /// (matcher = ifStmt(hasThen(cxxBoolLiteral(equals(true)))))
3758 /// \code
3759 /// if (false) true; else false;
3760 /// \endcode
3761 AST_MATCHER_P(IfStmt, hasThen, internal::Matcher<Stmt>, InnerMatcher) {
3762  const Stmt *const Then = Node.getThen();
3763  return (Then != nullptr && InnerMatcher.matches(*Then, Finder, Builder));
3764 }
3765 
3766 /// \brief Matches the else-statement of an if statement.
3767 ///
3768 /// Examples matches the if statement
3769 /// (matcher = ifStmt(hasElse(cxxBoolLiteral(equals(true)))))
3770 /// \code
3771 /// if (false) false; else true;
3772 /// \endcode
3773 AST_MATCHER_P(IfStmt, hasElse, internal::Matcher<Stmt>, InnerMatcher) {
3774  const Stmt *const Else = Node.getElse();
3775  return (Else != nullptr && InnerMatcher.matches(*Else, Finder, Builder));
3776 }
3777 
3778 /// \brief Matches if a node equals a previously bound node.
3779 ///
3780 /// Matches a node if it equals the node previously bound to \p ID.
3781 ///
3782 /// Given
3783 /// \code
3784 /// class X { int a; int b; };
3785 /// \endcode
3786 /// cxxRecordDecl(
3787 /// has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
3788 /// has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))
3789 /// matches the class \c X, as \c a and \c b have the same type.
3790 ///
3791 /// Note that when multiple matches are involved via \c forEach* matchers,
3792 /// \c equalsBoundNodes acts as a filter.
3793 /// For example:
3794 /// compoundStmt(
3795 /// forEachDescendant(varDecl().bind("d")),
3796 /// forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d"))))))
3797 /// will trigger a match for each combination of variable declaration
3798 /// and reference to that variable declaration within a compound statement.
3801  QualType),
3802  std::string, ID) {
3803  // FIXME: Figure out whether it makes sense to allow this
3804  // on any other node types.
3805  // For *Loc it probably does not make sense, as those seem
3806  // unique. For NestedNameSepcifier it might make sense, as
3807  // those also have pointer identity, but I'm not sure whether
3808  // they're ever reused.
3809  internal::NotEqualsBoundNodePredicate Predicate;
3810  Predicate.ID = ID;
3811  Predicate.Node = ast_type_traits::DynTypedNode::create(Node);
3812  return Builder->removeBindings(Predicate);
3813 }
3814 
3815 /// \brief Matches the condition variable statement in an if statement.
3816 ///
3817 /// Given
3818 /// \code
3819 /// if (A* a = GetAPointer()) {}
3820 /// \endcode
3821 /// hasConditionVariableStatement(...)
3822 /// matches 'A* a = GetAPointer()'.
3823 AST_MATCHER_P(IfStmt, hasConditionVariableStatement,
3824  internal::Matcher<DeclStmt>, InnerMatcher) {
3825  const DeclStmt* const DeclarationStatement =
3826  Node.getConditionVariableDeclStmt();
3827  return DeclarationStatement != nullptr &&
3828  InnerMatcher.matches(*DeclarationStatement, Finder, Builder);
3829 }
3830 
3831 /// \brief Matches the index expression of an array subscript expression.
3832 ///
3833 /// Given
3834 /// \code
3835 /// int i[5];
3836 /// void f() { i[1] = 42; }
3837 /// \endcode
3838 /// arraySubscriptExpression(hasIndex(integerLiteral()))
3839 /// matches \c i[1] with the \c integerLiteral() matching \c 1
3841  internal::Matcher<Expr>, InnerMatcher) {
3842  if (const Expr* Expression = Node.getIdx())
3843  return InnerMatcher.matches(*Expression, Finder, Builder);
3844  return false;
3845 }
3846 
3847 /// \brief Matches the base expression of an array subscript expression.
3848 ///
3849 /// Given
3850 /// \code
3851 /// int i[5];
3852 /// void f() { i[1] = 42; }
3853 /// \endcode
3854 /// arraySubscriptExpression(hasBase(implicitCastExpr(
3855 /// hasSourceExpression(declRefExpr()))))
3856 /// matches \c i[1] with the \c declRefExpr() matching \c i
3858  internal::Matcher<Expr>, InnerMatcher) {
3859  if (const Expr* Expression = Node.getBase())
3860  return InnerMatcher.matches(*Expression, Finder, Builder);
3861  return false;
3862 }
3863 
3864 /// \brief Matches a 'for', 'while', 'do while' statement or a function
3865 /// definition that has a given body.
3866 ///
3867 /// Given
3868 /// \code
3869 /// for (;;) {}
3870 /// \endcode
3871 /// hasBody(compoundStmt())
3872 /// matches 'for (;;) {}'
3873 /// with compoundStmt()
3874 /// matching '{}'
3877  WhileStmt,
3879  FunctionDecl),
3880  internal::Matcher<Stmt>, InnerMatcher) {
3881  const Stmt *const Statement = internal::GetBodyMatcher<NodeType>::get(Node);
3882  return (Statement != nullptr &&
3883  InnerMatcher.matches(*Statement, Finder, Builder));
3884 }
3885 
3886 /// \brief Matches compound statements where at least one substatement matches
3887 /// a given matcher. Also matches StmtExprs that have CompoundStmt as children.
3888 ///
3889 /// Given
3890 /// \code
3891 /// { {}; 1+2; }
3892 /// \endcode
3893 /// hasAnySubstatement(compoundStmt())
3894 /// matches '{ {}; 1+2; }'
3895 /// with compoundStmt()
3896 /// matching '{}'
3897 AST_POLYMORPHIC_MATCHER_P(hasAnySubstatement,
3899  StmtExpr),
3900  internal::Matcher<Stmt>, InnerMatcher) {
3901  const CompoundStmt *CS = CompoundStmtMatcher<NodeType>::get(Node);
3902  return CS && matchesFirstInPointerRange(InnerMatcher, CS->body_begin(),
3903  CS->body_end(), Finder, Builder);
3904 }
3905 
3906 /// \brief Checks that a compound statement contains a specific number of
3907 /// child statements.
3908 ///
3909 /// Example: Given
3910 /// \code
3911 /// { for (;;) {} }
3912 /// \endcode
3913 /// compoundStmt(statementCountIs(0)))
3914 /// matches '{}'
3915 /// but does not match the outer compound statement.
3916 AST_MATCHER_P(CompoundStmt, statementCountIs, unsigned, N) {
3917  return Node.size() == N;
3918 }
3919 
3920 /// \brief Matches literals that are equal to the given value of type ValueT.
3921 ///
3922 /// Given
3923 /// \code
3924 /// f('\0', false, 3.14, 42);
3925 /// \endcode
3926 /// characterLiteral(equals(0))
3927 /// matches '\0'
3928 /// cxxBoolLiteral(equals(false)) and cxxBoolLiteral(equals(0))
3929 /// match false
3930 /// floatLiteral(equals(3.14)) and floatLiteral(equals(314e-2))
3931 /// match 3.14
3932 /// integerLiteral(equals(42))
3933 /// matches 42
3934 ///
3935 /// Note that you cannot directly match a negative numeric literal because the
3936 /// minus sign is not part of the literal: It is a unary operator whose operand
3937 /// is the positive numeric literal. Instead, you must use a unaryOperator()
3938 /// matcher to match the minus sign:
3939 ///
3940 /// unaryOperator(hasOperatorName("-"),
3941 /// hasUnaryOperand(integerLiteral(equals(13))))
3942 ///
3943 /// Usable as: Matcher<CharacterLiteral>, Matcher<CXXBoolLiteralExpr>,
3944 /// Matcher<FloatingLiteral>, Matcher<IntegerLiteral>
3945 template <typename ValueT>
3946 internal::PolymorphicMatcherWithParam1<internal::ValueEqualsMatcher, ValueT>
3947 equals(const ValueT &Value) {
3948  return internal::PolymorphicMatcherWithParam1<
3949  internal::ValueEqualsMatcher,
3950  ValueT>(Value);
3951 }
3952 
3956  IntegerLiteral),
3957  bool, Value, 0) {
3958  return internal::ValueEqualsMatcher<NodeType, ParamT>(Value)
3959  .matchesNode(Node);
3960 }
3961 
3965  IntegerLiteral),
3966  unsigned, Value, 1) {
3967  return internal::ValueEqualsMatcher<NodeType, ParamT>(Value)
3968  .matchesNode(Node);
3969 }
3970 
3975  IntegerLiteral),
3976  double, Value, 2) {
3977  return internal::ValueEqualsMatcher<NodeType, ParamT>(Value)
3978  .matchesNode(Node);
3979 }
3980 
3981 /// \brief Matches the operator Name of operator expressions (binary or
3982 /// unary).
3983 ///
3984 /// Example matches a || b (matcher = binaryOperator(hasOperatorName("||")))
3985 /// \code
3986 /// !(a || b)
3987 /// \endcode
3990  UnaryOperator),
3991  std::string, Name) {
3992  return Name == Node.getOpcodeStr(Node.getOpcode());
3993 }
3994 
3995 /// \brief Matches the left hand side of binary operator expressions.
3996 ///
3997 /// Example matches a (matcher = binaryOperator(hasLHS()))
3998 /// \code
3999 /// a || b
4000 /// \endcode
4004  internal::Matcher<Expr>, InnerMatcher) {
4005  const Expr *LeftHandSide = Node.getLHS();
4006  return (LeftHandSide != nullptr &&
4007  InnerMatcher.matches(*LeftHandSide, Finder, Builder));
4008 }
4009 
4010 /// \brief Matches the right hand side of binary operator expressions.
4011 ///
4012 /// Example matches b (matcher = binaryOperator(hasRHS()))
4013 /// \code
4014 /// a || b
4015 /// \endcode
4019  internal::Matcher<Expr>, InnerMatcher) {
4020  const Expr *RightHandSide = Node.getRHS();
4021  return (RightHandSide != nullptr &&
4022  InnerMatcher.matches(*RightHandSide, Finder, Builder));
4023 }
4024 
4025 /// \brief Matches if either the left hand side or the right hand side of a
4026 /// binary operator matches.
4027 inline internal::Matcher<BinaryOperator> hasEitherOperand(
4028  const internal::Matcher<Expr> &InnerMatcher) {
4029  return anyOf(hasLHS(InnerMatcher), hasRHS(InnerMatcher));
4030 }
4031 
4032 /// \brief Matches if the operand of a unary operator matches.
4033 ///
4034 /// Example matches true (matcher = hasUnaryOperand(
4035 /// cxxBoolLiteral(equals(true))))
4036 /// \code
4037 /// !true
4038 /// \endcode
4039 AST_MATCHER_P(UnaryOperator, hasUnaryOperand,
4040  internal::Matcher<Expr>, InnerMatcher) {
4041  const Expr * const Operand = Node.getSubExpr();
4042  return (Operand != nullptr &&
4043  InnerMatcher.matches(*Operand, Finder, Builder));
4044 }
4045 
4046 /// \brief Matches if the cast's source expression
4047 /// or opaque value's source expression matches the given matcher.
4048 ///
4049 /// Example 1: matches "a string"
4050 /// (matcher = castExpr(hasSourceExpression(cxxConstructExpr())))
4051 /// \code
4052 /// class URL { URL(string); };
4053 /// URL url = "a string";
4054 /// \endcode
4055 ///
4056 /// Example 2: matches 'b' (matcher =
4057 /// opaqueValueExpr(hasSourceExpression(implicitCastExpr(declRefExpr())))
4058 /// \code
4059 /// int a = b ?: 1;
4060 /// \endcode
4061 AST_POLYMORPHIC_MATCHER_P(hasSourceExpression,
4063  OpaqueValueExpr),
4064  internal::Matcher<Expr>, InnerMatcher) {
4065  const Expr *const SubExpression =
4066  internal::GetSourceExpressionMatcher<NodeType>::get(Node);
4067  return (SubExpression != nullptr &&
4068  InnerMatcher.matches(*SubExpression, Finder, Builder));
4069 }
4070 
4071 /// \brief Matches casts that has a given cast kind.
4072 ///
4073 /// Example: matches the implicit cast around \c 0
4074 /// (matcher = castExpr(hasCastKind(CK_NullToPointer)))
4075 /// \code
4076 /// int *p = 0;
4077 /// \endcode
4079  return Node.getCastKind() == Kind;
4080 }
4081 
4082 /// \brief Matches casts whose destination type matches a given matcher.
4083 ///
4084 /// (Note: Clang's AST refers to other conversions as "casts" too, and calls
4085 /// actual casts "explicit" casts.)
4086 AST_MATCHER_P(ExplicitCastExpr, hasDestinationType,
4087  internal::Matcher<QualType>, InnerMatcher) {
4088  const QualType NodeType = Node.getTypeAsWritten();
4089  return InnerMatcher.matches(NodeType, Finder, Builder);
4090 }
4091 
4092 /// \brief Matches implicit casts whose destination type matches a given
4093 /// matcher.
4094 ///
4095 /// FIXME: Unit test this matcher
4096 AST_MATCHER_P(ImplicitCastExpr, hasImplicitDestinationType,
4097  internal::Matcher<QualType>, InnerMatcher) {
4098  return InnerMatcher.matches(Node.getType(), Finder, Builder);
4099 }
4100 
4101 /// \brief Matches RecordDecl object that are spelled with "struct."
4102 ///
4103 /// Example matches S, but not C or U.
4104 /// \code
4105 /// struct S {};
4106 /// class C {};
4107 /// union U {};
4108 /// \endcode
4110  return Node.isStruct();
4111 }
4112 
4113 /// \brief Matches RecordDecl object that are spelled with "union."
4114 ///
4115 /// Example matches U, but not C or S.
4116 /// \code
4117 /// struct S {};
4118 /// class C {};
4119 /// union U {};
4120 /// \endcode
4122  return Node.isUnion();
4123 }
4124 
4125 /// \brief Matches RecordDecl object that are spelled with "class."
4126 ///
4127 /// Example matches C, but not S or U.
4128 /// \code
4129 /// struct S {};
4130 /// class C {};
4131 /// union U {};
4132 /// \endcode
4134  return Node.isClass();
4135 }
4136 
4137 /// \brief Matches the true branch expression of a conditional operator.
4138 ///
4139 /// Example 1 (conditional ternary operator): matches a
4140 /// \code
4141 /// condition ? a : b
4142 /// \endcode
4143 ///
4144 /// Example 2 (conditional binary operator): matches opaqueValueExpr(condition)
4145 /// \code
4146 /// condition ?: b
4147 /// \endcode
4149  internal::Matcher<Expr>, InnerMatcher) {
4150  const Expr *Expression = Node.getTrueExpr();
4151  return (Expression != nullptr &&
4152  InnerMatcher.matches(*Expression, Finder, Builder));
4153 }
4154 
4155 /// \brief Matches the false branch expression of a conditional operator
4156 /// (binary or ternary).
4157 ///
4158 /// Example matches b
4159 /// \code
4160 /// condition ? a : b
4161 /// condition ?: b
4162 /// \endcode
4164  internal::Matcher<Expr>, InnerMatcher) {
4165  const Expr *Expression = Node.getFalseExpr();
4166  return (Expression != nullptr &&
4167  InnerMatcher.matches(*Expression, Finder, Builder));
4168 }
4169 
4170 /// \brief Matches if a declaration has a body attached.
4171 ///
4172 /// Example matches A, va, fa
4173 /// \code
4174 /// class A {};
4175 /// class B; // Doesn't match, as it has no body.
4176 /// int va;
4177 /// extern int vb; // Doesn't match, as it doesn't define the variable.
4178 /// void fa() {}
4179 /// void fb(); // Doesn't match, as it has no body.
4180 /// @interface X
4181 /// - (void)ma; // Doesn't match, interface is declaration.
4182 /// @end
4183 /// @implementation X
4184 /// - (void)ma {}
4185 /// @end
4186 /// \endcode
4187 ///
4188 /// Usable as: Matcher<TagDecl>, Matcher<VarDecl>, Matcher<FunctionDecl>,
4189 /// Matcher<ObjCMethodDecl>
4193  FunctionDecl)) {
4194  return Node.isThisDeclarationADefinition();
4195 }
4196 
4197 /// \brief Matches if a function declaration is variadic.
4198 ///
4199 /// Example matches f, but not g or h. The function i will not match, even when
4200 /// compiled in C mode.
4201 /// \code
4202 /// void f(...);
4203 /// void g(int);
4204 /// template <typename... Ts> void h(Ts...);
4205 /// void i();
4206 /// \endcode
4208  return Node.isVariadic();
4209 }
4210 
4211 /// \brief Matches the class declaration that the given method declaration
4212 /// belongs to.
4213 ///
4214 /// FIXME: Generalize this for other kinds of declarations.
4215 /// FIXME: What other kind of declarations would we need to generalize
4216 /// this to?
4217 ///
4218 /// Example matches A() in the last line
4219 /// (matcher = cxxConstructExpr(hasDeclaration(cxxMethodDecl(
4220 /// ofClass(hasName("A"))))))
4221 /// \code
4222 /// class A {
4223 /// public:
4224 /// A();
4225 /// };
4226 /// A a = A();
4227 /// \endcode
4229  internal::Matcher<CXXRecordDecl>, InnerMatcher) {
4230  const CXXRecordDecl *Parent = Node.getParent();
4231  return (Parent != nullptr &&
4232  InnerMatcher.matches(*Parent, Finder, Builder));
4233 }
4234 
4235 /// \brief Matches each method overriden by the given method. This matcher may
4236 /// produce multiple matches.
4237 ///
4238 /// Given
4239 /// \code
4240 /// class A { virtual void f(); };
4241 /// class B : public A { void f(); };
4242 /// class C : public B { void f(); };
4243 /// \endcode
4244 /// cxxMethodDecl(ofClass(hasName("C")),
4245 /// forEachOverridden(cxxMethodDecl().bind("b"))).bind("d")
4246 /// matches once, with "b" binding "A::f" and "d" binding "C::f" (Note
4247 /// that B::f is not overridden by C::f).
4248 ///
4249 /// The check can produce multiple matches in case of multiple inheritance, e.g.
4250 /// \code
4251 /// class A1 { virtual void f(); };
4252 /// class A2 { virtual void f(); };
4253 /// class C : public A1, public A2 { void f(); };
4254 /// \endcode
4255 /// cxxMethodDecl(ofClass(hasName("C")),
4256 /// forEachOverridden(cxxMethodDecl().bind("b"))).bind("d")
4257 /// matches twice, once with "b" binding "A1::f" and "d" binding "C::f", and
4258 /// once with "b" binding "A2::f" and "d" binding "C::f".
4259 AST_MATCHER_P(CXXMethodDecl, forEachOverridden,
4260  internal::Matcher<CXXMethodDecl>, InnerMatcher) {
4261  BoundNodesTreeBuilder Result;
4262  bool Matched = false;
4263  for (const auto *Overridden : Node.overridden_methods()) {
4264  BoundNodesTreeBuilder OverriddenBuilder(*Builder);
4265  const bool OverriddenMatched =
4266  InnerMatcher.matches(*Overridden, Finder, &OverriddenBuilder);
4267  if (OverriddenMatched) {
4268  Matched = true;
4269  Result.addMatch(OverriddenBuilder);
4270  }
4271  }
4272  *Builder = std::move(Result);
4273  return Matched;
4274 }
4275 
4276 /// \brief Matches if the given method declaration is virtual.
4277 ///
4278 /// Given
4279 /// \code
4280 /// class A {
4281 /// public:
4282 /// virtual void x();
4283 /// };
4284 /// \endcode
4285 /// matches A::x
4287  return Node.isVirtual();
4288 }
4289 
4290 /// \brief Matches if the given method declaration has an explicit "virtual".
4291 ///
4292 /// Given
4293 /// \code
4294 /// class A {
4295 /// public:
4296 /// virtual void x();
4297 /// };
4298 /// class B : public A {
4299 /// public:
4300 /// void x();
4301 /// };
4302 /// \endcode
4303 /// matches A::x but not B::x
4304 AST_MATCHER(CXXMethodDecl, isVirtualAsWritten) {
4305  return Node.isVirtualAsWritten();
4306 }
4307 
4308 /// \brief Matches if the given method or class declaration is final.
4309 ///
4310 /// Given:
4311 /// \code
4312 /// class A final {};
4313 ///
4314 /// struct B {
4315 /// virtual void f();
4316 /// };
4317 ///
4318 /// struct C : B {
4319 /// void f() final;
4320 /// };
4321 /// \endcode
4322 /// matches A and C::f, but not B, C, or B::f
4325  CXXMethodDecl)) {
4326  return Node.template hasAttr<FinalAttr>();
4327 }
4328 
4329 /// \brief Matches if the given method declaration is pure.
4330 ///
4331 /// Given
4332 /// \code
4333 /// class A {
4334 /// public:
4335 /// virtual void x() = 0;
4336 /// };
4337 /// \endcode
4338 /// matches A::x
4340  return Node.isPure();
4341 }
4342 
4343 /// \brief Matches if the given method declaration is const.
4344 ///
4345 /// Given
4346 /// \code
4347 /// struct A {
4348 /// void foo() const;
4349 /// void bar();
4350 /// };
4351 /// \endcode
4352 ///
4353 /// cxxMethodDecl(isConst()) matches A::foo() but not A::bar()
4355  return Node.isConst();
4356 }
4357 
4358 /// \brief Matches if the given method declaration declares a copy assignment
4359 /// operator.
4360 ///
4361 /// Given
4362 /// \code
4363 /// struct A {
4364 /// A &operator=(const A &);
4365 /// A &operator=(A &&);
4366 /// };
4367 /// \endcode
4368 ///
4369 /// cxxMethodDecl(isCopyAssignmentOperator()) matches the first method but not
4370 /// the second one.
4371 AST_MATCHER(CXXMethodDecl, isCopyAssignmentOperator) {
4372  return Node.isCopyAssignmentOperator();
4373 }
4374 
4375 /// \brief Matches if the given method declaration declares a move assignment
4376 /// operator.
4377 ///
4378 /// Given
4379 /// \code
4380 /// struct A {
4381 /// A &operator=(const A &);
4382 /// A &operator=(A &&);
4383 /// };
4384 /// \endcode
4385 ///
4386 /// cxxMethodDecl(isMoveAssignmentOperator()) matches the second method but not
4387 /// the first one.
4388 AST_MATCHER(CXXMethodDecl, isMoveAssignmentOperator) {
4389  return Node.isMoveAssignmentOperator();
4390 }
4391 
4392 /// \brief Matches if the given method declaration overrides another method.
4393 ///
4394 /// Given
4395 /// \code
4396 /// class A {
4397 /// public:
4398 /// virtual void x();
4399 /// };
4400 /// class B : public A {
4401 /// public:
4402 /// virtual void x();
4403 /// };
4404 /// \endcode
4405 /// matches B::x
4407  return Node.size_overridden_methods() > 0 || Node.hasAttr<OverrideAttr>();
4408 }
4409 
4410 /// \brief Matches method declarations that are user-provided.
4411 ///
4412 /// Given
4413 /// \code
4414 /// struct S {
4415 /// S(); // #1
4416 /// S(const S &) = default; // #2
4417 /// S(S &&) = delete; // #3
4418 /// };
4419 /// \endcode
4420 /// cxxConstructorDecl(isUserProvided()) will match #1, but not #2 or #3.
4421 AST_MATCHER(CXXMethodDecl, isUserProvided) {
4422  return Node.isUserProvided();
4423 }
4424 
4425 /// \brief Matches member expressions that are called with '->' as opposed
4426 /// to '.'.
4427 ///
4428 /// Member calls on the implicit this pointer match as called with '->'.
4429 ///
4430 /// Given
4431 /// \code
4432 /// class Y {
4433 /// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; }
4434 /// int a;
4435 /// static int b;
4436 /// };
4437 /// \endcode
4438 /// memberExpr(isArrow())
4439 /// matches this->x, x, y.x, a, this->b
4441  return Node.isArrow();
4442 }
4443 
4444 /// \brief Matches QualType nodes that are of integer type.
4445 ///
4446 /// Given
4447 /// \code
4448 /// void a(int);
4449 /// void b(long);
4450 /// void c(double);
4451 /// \endcode
4452 /// functionDecl(hasAnyParameter(hasType(isInteger())))
4453 /// matches "a(int)", "b(long)", but not "c(double)".
4454 AST_MATCHER(QualType, isInteger) {
4455  return Node->isIntegerType();
4456 }
4457 
4458 /// \brief Matches QualType nodes that are of unsigned integer type.
4459 ///
4460 /// Given
4461 /// \code
4462 /// void a(int);
4463 /// void b(unsigned long);
4464 /// void c(double);
4465 /// \endcode
4466 /// functionDecl(hasAnyParameter(hasType(isUnsignedInteger())))
4467 /// matches "b(unsigned long)", but not "a(int)" and "c(double)".
4468 AST_MATCHER(QualType, isUnsignedInteger) {
4469  return Node->isUnsignedIntegerType();
4470 }
4471 
4472 /// \brief Matches QualType nodes that are of signed integer type.
4473 ///
4474 /// Given
4475 /// \code
4476 /// void a(int);
4477 /// void b(unsigned long);
4478 /// void c(double);
4479 /// \endcode
4480 /// functionDecl(hasAnyParameter(hasType(isSignedInteger())))
4481 /// matches "a(int)", but not "b(unsigned long)" and "c(double)".
4482 AST_MATCHER(QualType, isSignedInteger) {
4483  return Node->isSignedIntegerType();
4484 }
4485 
4486 /// \brief Matches QualType nodes that are of character type.
4487 ///
4488 /// Given
4489 /// \code
4490 /// void a(char);
4491 /// void b(wchar_t);
4492 /// void c(double);
4493 /// \endcode
4494 /// functionDecl(hasAnyParameter(hasType(isAnyCharacter())))
4495 /// matches "a(char)", "b(wchar_t)", but not "c(double)".
4496 AST_MATCHER(QualType, isAnyCharacter) {
4497  return Node->isAnyCharacterType();
4498 }
4499 
4500 /// \brief Matches QualType nodes that are of any pointer type; this includes
4501 /// the Objective-C object pointer type, which is different despite being
4502 /// syntactically similar.
4503 ///
4504 /// Given
4505 /// \code
4506 /// int *i = nullptr;
4507 ///
4508 /// @interface Foo
4509 /// @end
4510 /// Foo *f;
4511 ///
4512 /// int j;
4513 /// \endcode
4514 /// varDecl(hasType(isAnyPointer()))
4515 /// matches "int *i" and "Foo *f", but not "int j".
4516 AST_MATCHER(QualType, isAnyPointer) {
4517  return Node->isAnyPointerType();
4518 }
4519 
4520 /// \brief Matches QualType nodes that are const-qualified, i.e., that
4521 /// include "top-level" const.
4522 ///
4523 /// Given
4524 /// \code
4525 /// void a(int);
4526 /// void b(int const);
4527 /// void c(const int);
4528 /// void d(const int*);
4529 /// void e(int const) {};
4530 /// \endcode
4531 /// functionDecl(hasAnyParameter(hasType(isConstQualified())))
4532 /// matches "void b(int const)", "void c(const int)" and
4533 /// "void e(int const) {}". It does not match d as there
4534 /// is no top-level const on the parameter type "const int *".
4535 AST_MATCHER(QualType, isConstQualified) {
4536  return Node.isConstQualified();
4537 }
4538 
4539 /// \brief Matches QualType nodes that are volatile-qualified, i.e., that
4540 /// include "top-level" volatile.
4541 ///
4542 /// Given
4543 /// \code
4544 /// void a(int);
4545 /// void b(int volatile);
4546 /// void c(volatile int);
4547 /// void d(volatile int*);
4548 /// void e(int volatile) {};
4549 /// \endcode
4550 /// functionDecl(hasAnyParameter(hasType(isVolatileQualified())))
4551 /// matches "void b(int volatile)", "void c(volatile int)" and
4552 /// "void e(int volatile) {}". It does not match d as there
4553 /// is no top-level volatile on the parameter type "volatile int *".
4554 AST_MATCHER(QualType, isVolatileQualified) {
4555  return Node.isVolatileQualified();
4556 }
4557 
4558 /// \brief Matches QualType nodes that have local CV-qualifiers attached to
4559 /// the node, not hidden within a typedef.
4560 ///
4561 /// Given
4562 /// \code
4563 /// typedef const int const_int;
4564 /// const_int i;
4565 /// int *const j;
4566 /// int *volatile k;
4567 /// int m;
4568 /// \endcode
4569 /// \c varDecl(hasType(hasLocalQualifiers())) matches only \c j and \c k.
4570 /// \c i is const-qualified but the qualifier is not local.
4571 AST_MATCHER(QualType, hasLocalQualifiers) {
4572  return Node.hasLocalQualifiers();
4573 }
4574 
4575 /// \brief Matches a member expression where the member is matched by a
4576 /// given matcher.
4577 ///
4578 /// Given
4579 /// \code
4580 /// struct { int first, second; } first, second;
4581 /// int i(second.first);
4582 /// int j(first.second);
4583 /// \endcode
4584 /// memberExpr(member(hasName("first")))
4585 /// matches second.first
4586 /// but not first.second (because the member name there is "second").
4588  internal::Matcher<ValueDecl>, InnerMatcher) {
4589  return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder);
4590 }
4591 
4592 /// \brief Matches a member expression where the object expression is
4593 /// matched by a given matcher.
4594 ///
4595 /// Given
4596 /// \code
4597 /// struct X { int m; };
4598 /// void f(X x) { x.m; m; }
4599 /// \endcode
4600 /// memberExpr(hasObjectExpression(hasType(cxxRecordDecl(hasName("X")))))))
4601 /// matches "x.m" and "m"
4602 /// with hasObjectExpression(...)
4603 /// matching "x" and the implicit object expression of "m" which has type X*.
4604 AST_MATCHER_P(MemberExpr, hasObjectExpression,
4605  internal::Matcher<Expr>, InnerMatcher) {
4606  return InnerMatcher.matches(*Node.getBase(), Finder, Builder);
4607 }
4608 
4609 /// \brief Matches any using shadow declaration.
4610 ///
4611 /// Given
4612 /// \code
4613 /// namespace X { void b(); }
4614 /// using X::b;
4615 /// \endcode
4616 /// usingDecl(hasAnyUsingShadowDecl(hasName("b"))))
4617 /// matches \code using X::b \endcode
4618 AST_MATCHER_P(UsingDecl, hasAnyUsingShadowDecl,
4619  internal::Matcher<UsingShadowDecl>, InnerMatcher) {
4620  return matchesFirstInPointerRange(InnerMatcher, Node.shadow_begin(),
4621  Node.shadow_end(), Finder, Builder);
4622 }
4623 
4624 /// \brief Matches a using shadow declaration where the target declaration is
4625 /// matched by the given matcher.
4626 ///
4627 /// Given
4628 /// \code
4629 /// namespace X { int a; void b(); }
4630 /// using X::a;
4631 /// using X::b;
4632 /// \endcode
4633 /// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(functionDecl())))
4634 /// matches \code using X::b \endcode
4635 /// but not \code using X::a \endcode
4637  internal::Matcher<NamedDecl>, InnerMatcher) {
4638  return InnerMatcher.matches(*Node.getTargetDecl(), Finder, Builder);
4639 }
4640 
4641 /// \brief Matches template instantiations of function, class, or static
4642 /// member variable template instantiations.
4643 ///
4644 /// Given
4645 /// \code
4646 /// template <typename T> class X {}; class A {}; X<A> x;
4647 /// \endcode
4648 /// or
4649 /// \code
4650 /// template <typename T> class X {}; class A {}; template class X<A>;
4651 /// \endcode
4652 /// cxxRecordDecl(hasName("::X"), isTemplateInstantiation())
4653 /// matches the template instantiation of X<A>.
4654 ///
4655 /// But given
4656 /// \code
4657 /// template <typename T> class X {}; class A {};
4658 /// template <> class X<A> {}; X<A> x;
4659 /// \endcode
4660 /// cxxRecordDecl(hasName("::X"), isTemplateInstantiation())
4661 /// does not match, as X<A> is an explicit template specialization.
4662 ///
4663 /// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl>
4666  CXXRecordDecl)) {
4667  return (Node.getTemplateSpecializationKind() == TSK_ImplicitInstantiation ||
4668  Node.getTemplateSpecializationKind() ==
4670 }
4671 
4672 /// \brief Matches declarations that are template instantiations or are inside
4673 /// template instantiations.
4674 ///
4675 /// Given
4676 /// \code
4677 /// template<typename T> void A(T t) { T i; }
4678 /// A(0);
4679 /// A(0U);
4680 /// \endcode
4681 /// functionDecl(isInstantiated())
4682 /// matches 'A(int) {...};' and 'A(unsigned) {...}'.
4683 AST_MATCHER_FUNCTION(internal::Matcher<Decl>, isInstantiated) {
4684  auto IsInstantiation = decl(anyOf(cxxRecordDecl(isTemplateInstantiation()),
4686  return decl(anyOf(IsInstantiation, hasAncestor(IsInstantiation)));
4687 }
4688 
4689 /// \brief Matches statements inside of a template instantiation.
4690 ///
4691 /// Given
4692 /// \code
4693 /// int j;
4694 /// template<typename T> void A(T t) { T i; j += 42;}
4695 /// A(0);
4696 /// A(0U);
4697 /// \endcode
4698 /// declStmt(isInTemplateInstantiation())
4699 /// matches 'int i;' and 'unsigned i'.
4700 /// unless(stmt(isInTemplateInstantiation()))
4701 /// will NOT match j += 42; as it's shared between the template definition and
4702 /// instantiation.
4703 AST_MATCHER_FUNCTION(internal::Matcher<Stmt>, isInTemplateInstantiation) {
4704  return stmt(
4707 }
4708 
4709 /// \brief Matches explicit template specializations of function, class, or
4710 /// static member variable template instantiations.
4711 ///
4712 /// Given
4713 /// \code
4714 /// template<typename T> void A(T t) { }
4715 /// template<> void A(int N) { }
4716 /// \endcode
4717 /// functionDecl(isExplicitTemplateSpecialization())
4718 /// matches the specialization A<int>().
4719 ///
4720 /// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl>
4721 AST_POLYMORPHIC_MATCHER(isExplicitTemplateSpecialization,
4723  CXXRecordDecl)) {
4724  return (Node.getTemplateSpecializationKind() == TSK_ExplicitSpecialization);
4725 }
4726 
4727 /// \brief Matches \c TypeLocs for which the given inner
4728 /// QualType-matcher matches.
4729 AST_MATCHER_FUNCTION_P_OVERLOAD(internal::BindableMatcher<TypeLoc>, loc,
4730  internal::Matcher<QualType>, InnerMatcher, 0) {
4731  return internal::BindableMatcher<TypeLoc>(
4732  new internal::TypeLocTypeMatcher(InnerMatcher));
4733 }
4734 
4735 /// \brief Matches type \c bool.
4736 ///
4737 /// Given
4738 /// \code
4739 /// struct S { bool func(); };
4740 /// \endcode
4741 /// functionDecl(returns(booleanType()))
4742 /// matches "bool func();"
4743 AST_MATCHER(Type, booleanType) {
4744  return Node.isBooleanType();
4745 }
4746 
4747 /// \brief Matches type \c void.
4748 ///
4749 /// Given
4750 /// \code
4751 /// struct S { void func(); };
4752 /// \endcode
4753 /// functionDecl(returns(voidType()))
4754 /// matches "void func();"
4755 AST_MATCHER(Type, voidType) {
4756  return Node.isVoidType();
4757 }
4758 
4759 template <typename NodeType>
4760 using AstTypeMatcher = internal::VariadicDynCastAllOfMatcher<Type, NodeType>;
4761 
4762 /// \brief Matches builtin Types.
4763 ///
4764 /// Given
4765 /// \code
4766 /// struct A {};
4767 /// A a;
4768 /// int b;
4769 /// float c;
4770 /// bool d;
4771 /// \endcode
4772 /// builtinType()
4773 /// matches "int b", "float c" and "bool d"
4775 
4776 /// \brief Matches all kinds of arrays.
4777 ///
4778 /// Given
4779 /// \code
4780 /// int a[] = { 2, 3 };
4781 /// int b[4];
4782 /// void f() { int c[a[0]]; }
4783 /// \endcode
4784 /// arrayType()
4785 /// matches "int a[]", "int b[4]" and "int c[a[0]]";
4787 
4788 /// \brief Matches C99 complex types.
4789 ///
4790 /// Given
4791 /// \code
4792 /// _Complex float f;
4793 /// \endcode
4794 /// complexType()
4795 /// matches "_Complex float f"
4797 
4798 /// \brief Matches any real floating-point type (float, double, long double).
4799 ///
4800 /// Given
4801 /// \code
4802 /// int i;
4803 /// float f;
4804 /// \endcode
4805 /// realFloatingPointType()
4806 /// matches "float f" but not "int i"
4807 AST_MATCHER(Type, realFloatingPointType) {
4808  return Node.isRealFloatingType();
4809 }
4810 
4811 /// \brief Matches arrays and C99 complex types that have a specific element
4812 /// type.
4813 ///
4814 /// Given
4815 /// \code
4816 /// struct A {};
4817 /// A a[7];
4818 /// int b[7];
4819 /// \endcode
4820 /// arrayType(hasElementType(builtinType()))
4821 /// matches "int b[7]"
4822 ///
4823 /// Usable as: Matcher<ArrayType>, Matcher<ComplexType>
4824 AST_TYPELOC_TRAVERSE_MATCHER_DECL(hasElementType, getElement,
4826  ComplexType));
4827 
4828 /// \brief Matches C arrays with a specified constant size.
4829 ///
4830 /// Given
4831 /// \code
4832 /// void() {
4833 /// int a[2];
4834 /// int b[] = { 2, 3 };
4835 /// int c[b[0]];
4836 /// }
4837 /// \endcode
4838 /// constantArrayType()
4839 /// matches "int a[2]"
4841 
4842 /// \brief Matches nodes that have the specified size.
4843 ///
4844 /// Given
4845 /// \code
4846 /// int a[42];
4847 /// int b[2 * 21];
4848 /// int c[41], d[43];
4849 /// char *s = "abcd";
4850 /// wchar_t *ws = L"abcd";
4851 /// char *w = "a";
4852 /// \endcode
4853 /// constantArrayType(hasSize(42))
4854 /// matches "int a[42]" and "int b[2 * 21]"
4855 /// stringLiteral(hasSize(4))
4856 /// matches "abcd", L"abcd"
4859  StringLiteral),
4860  unsigned, N) {
4861  return internal::HasSizeMatcher<NodeType>::hasSize(Node, N);
4862 }
4863 
4864 /// \brief Matches C++ arrays whose size is a value-dependent expression.
4865 ///
4866 /// Given
4867 /// \code
4868 /// template<typename T, int Size>
4869 /// class array {
4870 /// T data[Size];
4871 /// };
4872 /// \endcode
4873 /// dependentSizedArrayType
4874 /// matches "T data[Size]"
4876 
4877 /// \brief Matches C arrays with unspecified size.
4878 ///
4879 /// Given
4880 /// \code
4881 /// int a[] = { 2, 3 };
4882 /// int b[42];
4883 /// void f(int c[]) { int d[a[0]]; };
4884 /// \endcode
4885 /// incompleteArrayType()
4886 /// matches "int a[]" and "int c[]"
4888 
4889 /// \brief Matches C arrays with a specified size that is not an
4890 /// integer-constant-expression.
4891 ///
4892 /// Given
4893 /// \code
4894 /// void f() {
4895 /// int a[] = { 2, 3 }
4896 /// int b[42];
4897 /// int c[a[0]];
4898 /// }
4899 /// \endcode
4900 /// variableArrayType()
4901 /// matches "int c[a[0]]"
4903 
4904 /// \brief Matches \c VariableArrayType nodes that have a specific size
4905 /// expression.
4906 ///
4907 /// Given
4908 /// \code
4909 /// void f(int b) {
4910 /// int a[b];
4911 /// }
4912 /// \endcode
4913 /// variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
4914 /// varDecl(hasName("b")))))))
4915 /// matches "int a[b]"
4917  internal::Matcher<Expr>, InnerMatcher) {
4918  return InnerMatcher.matches(*Node.getSizeExpr(), Finder, Builder);
4919 }
4920 
4921 /// \brief Matches atomic types.
4922 ///
4923 /// Given
4924 /// \code
4925 /// _Atomic(int) i;
4926 /// \endcode
4927 /// atomicType()
4928 /// matches "_Atomic(int) i"
4930 
4931 /// \brief Matches atomic types with a specific value type.
4932 ///
4933 /// Given
4934 /// \code
4935 /// _Atomic(int) i;
4936 /// _Atomic(float) f;
4937 /// \endcode
4938 /// atomicType(hasValueType(isInteger()))
4939 /// matches "_Atomic(int) i"
4940 ///
4941 /// Usable as: Matcher<AtomicType>
4944 
4945 /// \brief Matches types nodes representing C++11 auto types.
4946 ///
4947 /// Given:
4948 /// \code
4949 /// auto n = 4;
4950 /// int v[] = { 2, 3 }
4951 /// for (auto i : v) { }
4952 /// \endcode
4953 /// autoType()
4954 /// matches "auto n" and "auto i"
4955 extern const AstTypeMatcher<AutoType> autoType;
4956 
4957 /// \brief Matches \c AutoType nodes where the deduced type is a specific type.
4958 ///
4959 /// Note: There is no \c TypeLoc for the deduced type and thus no
4960 /// \c getDeducedLoc() matcher.
4961 ///
4962 /// Given
4963 /// \code
4964 /// auto a = 1;
4965 /// auto b = 2.0;
4966 /// \endcode
4967 /// autoType(hasDeducedType(isInteger()))
4968 /// matches "auto a"
4969 ///
4970 /// Usable as: Matcher<AutoType>
4971 AST_TYPE_TRAVERSE_MATCHER(hasDeducedType, getDeducedType,
4973 
4974 /// \brief Matches \c FunctionType nodes.
4975 ///
4976 /// Given
4977 /// \code
4978 /// int (*f)(int);
4979 /// void g();
4980 /// \endcode
4981 /// functionType()
4982 /// matches "int (*f)(int)" and the type of "g".
4984 
4985 /// \brief Matches \c FunctionProtoType nodes.
4986 ///
4987 /// Given
4988 /// \code
4989 /// int (*f)(int);
4990 /// void g();
4991 /// \endcode
4992 /// functionProtoType()
4993 /// matches "int (*f)(int)" and the type of "g" in C++ mode.
4994 /// In C mode, "g" is not matched because it does not contain a prototype.
4996 
4997 /// \brief Matches \c ParenType nodes.
4998 ///
4999 /// Given
5000 /// \code
5001 /// int (*ptr_to_array)[4];
5002 /// int *array_of_ptrs[4];
5003 /// \endcode
5004 ///
5005 /// \c varDecl(hasType(pointsTo(parenType()))) matches \c ptr_to_array but not
5006 /// \c array_of_ptrs.
5008 
5009 /// \brief Matches \c ParenType nodes where the inner type is a specific type.
5010 ///
5011 /// Given
5012 /// \code
5013 /// int (*ptr_to_array)[4];
5014 /// int (*ptr_to_func)(int);
5015 /// \endcode
5016 ///
5017 /// \c varDecl(hasType(pointsTo(parenType(innerType(functionType()))))) matches
5018 /// \c ptr_to_func but not \c ptr_to_array.
5019 ///
5020 /// Usable as: Matcher<ParenType>
5021 AST_TYPE_TRAVERSE_MATCHER(innerType, getInnerType,
5023 
5024 /// \brief Matches block pointer types, i.e. types syntactically represented as
5025 /// "void (^)(int)".
5026 ///
5027 /// The \c pointee is always required to be a \c FunctionType.
5029 
5030 /// \brief Matches member pointer types.
5031 /// Given
5032 /// \code
5033 /// struct A { int i; }
5034 /// A::* ptr = A::i;
5035 /// \endcode
5036 /// memberPointerType()
5037 /// matches "A::* ptr"
5039 
5040 /// \brief Matches pointer types, but does not match Objective-C object pointer
5041 /// types.
5042 ///
5043 /// Given
5044 /// \code
5045 /// int *a;
5046 /// int &b = *a;
5047 /// int c = 5;
5048 ///
5049 /// @interface Foo
5050 /// @end
5051 /// Foo *f;
5052 /// \endcode
5053 /// pointerType()
5054 /// matches "int *a", but does not match "Foo *f".
5056 
5057 /// \brief Matches an Objective-C object pointer type, which is different from
5058 /// a pointer type, despite being syntactically similar.
5059 ///
5060 /// Given
5061 /// \code
5062 /// int *a;
5063 ///
5064 /// @interface Foo
5065 /// @end
5066 /// Foo *f;
5067 /// \endcode
5068 /// pointerType()
5069 /// matches "Foo *f", but does not match "int *a".
5071 
5072 /// \brief Matches both lvalue and rvalue reference types.
5073 ///
5074 /// Given
5075 /// \code
5076 /// int *a;
5077 /// int &b = *a;
5078 /// int &&c = 1;
5079 /// auto &d = b;
5080 /// auto &&e = c;
5081 /// auto &&f = 2;
5082 /// int g = 5;
5083 /// \endcode
5084 ///
5085 /// \c referenceType() matches the types of \c b, \c c, \c d, \c e, and \c f.
5087 
5088 /// \brief Matches lvalue reference types.
5089 ///
5090 /// Given:
5091 /// \code
5092 /// int *a;
5093 /// int &b = *a;
5094 /// int &&c = 1;
5095 /// auto &d = b;
5096 /// auto &&e = c;
5097 /// auto &&f = 2;
5098 /// int g = 5;
5099 /// \endcode
5100 ///
5101 /// \c lValueReferenceType() matches the types of \c b, \c d, and \c e. \c e is
5102 /// matched since the type is deduced as int& by reference collapsing rules.
5104 
5105 /// \brief Matches rvalue reference types.
5106 ///
5107 /// Given:
5108 /// \code
5109 /// int *a;
5110 /// int &b = *a;
5111 /// int &&c = 1;
5112 /// auto &d = b;
5113 /// auto &&e = c;
5114 /// auto &&f = 2;
5115 /// int g = 5;
5116 /// \endcode
5117 ///
5118 /// \c rValueReferenceType() matches the types of \c c and \c f. \c e is not
5119 /// matched as it is deduced to int& by reference collapsing rules.
5121 
5122 /// \brief Narrows PointerType (and similar) matchers to those where the
5123 /// \c pointee matches a given matcher.
5124 ///
5125 /// Given
5126 /// \code
5127 /// int *a;
5128 /// int const *b;
5129 /// float const *f;
5130 /// \endcode
5131 /// pointerType(pointee(isConstQualified(), isInteger()))
5132 /// matches "int const *b"
5133 ///
5134 /// Usable as: Matcher<BlockPointerType>, Matcher<MemberPointerType>,
5135 /// Matcher<PointerType>, Matcher<ReferenceType>
5137  pointee, getPointee,
5140 
5141 /// \brief Matches typedef types.
5142 ///
5143 /// Given
5144 /// \code
5145 /// typedef int X;
5146 /// \endcode
5147 /// typedefType()
5148 /// matches "typedef int X"
5150 
5151 /// \brief Matches enum types.
5152 ///
5153 /// Given
5154 /// \code
5155 /// enum C { Green };
5156 /// enum class S { Red };
5157 ///
5158 /// C c;
5159 /// S s;
5160 /// \endcode
5161 //
5162 /// \c enumType() matches the type of the variable declarations of both \c c and
5163 /// \c s.
5164 extern const AstTypeMatcher<EnumType> enumType;
5165 
5166 /// \brief Matches template specialization types.
5167 ///
5168 /// Given
5169 /// \code
5170 /// template <typename T>
5171 /// class C { };
5172 ///
5173 /// template class C<int>; // A
5174 /// C<char> var; // B
5175 /// \endcode
5176 ///
5177 /// \c templateSpecializationType() matches the type of the explicit
5178 /// instantiation in \c A and the type of the variable declaration in \c B.
5181 
5182 /// \brief Matches types nodes representing unary type transformations.
5183 ///
5184 /// Given:
5185 /// \code
5186 /// typedef __underlying_type(T) type;
5187 /// \endcode
5188 /// unaryTransformType()
5189 /// matches "__underlying_type(T)"
5191 
5192 /// \brief Matches record types (e.g. structs, classes).
5193 ///
5194 /// Given
5195 /// \code
5196 /// class C {};
5197 /// struct S {};
5198 ///
5199 /// C c;
5200 /// S s;
5201 /// \endcode
5202 ///
5203 /// \c recordType() matches the type of the variable declarations of both \c c
5204 /// and \c s.
5206 
5207 /// \brief Matches tag types (record and enum types).
5208 ///
5209 /// Given
5210 /// \code
5211 /// enum E {};
5212 /// class C {};
5213 ///
5214 /// E e;
5215 /// C c;
5216 /// \endcode
5217 ///
5218 /// \c tagType() matches the type of the variable declarations of both \c e
5219 /// and \c c.
5220 extern const AstTypeMatcher<TagType> tagType;
5221 
5222 /// \brief Matches types specified with an elaborated type keyword or with a
5223 /// qualified name.
5224 ///
5225 /// Given
5226 /// \code
5227 /// namespace N {
5228 /// namespace M {
5229 /// class D {};
5230 /// }
5231 /// }
5232 /// class C {};
5233 ///
5234 /// class C c;
5235 /// N::M::D d;
5236 /// \endcode
5237 ///
5238 /// \c elaboratedType() matches the type of the variable declarations of both
5239 /// \c c and \c d.
5241 
5242 /// \brief Matches ElaboratedTypes whose qualifier, a NestedNameSpecifier,
5243 /// matches \c InnerMatcher if the qualifier exists.
5244 ///
5245 /// Given
5246 /// \code
5247 /// namespace N {
5248 /// namespace M {
5249 /// class D {};
5250 /// }
5251 /// }
5252 /// N::M::D d;
5253 /// \endcode
5254 ///
5255 /// \c elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N"))))
5256 /// matches the type of the variable declaration of \c d.
5258  internal::Matcher<NestedNameSpecifier>, InnerMatcher) {
5259  if (const NestedNameSpecifier *Qualifier = Node.getQualifier())
5260  return InnerMatcher.matches(*Qualifier, Finder, Builder);
5261 
5262  return false;
5263 }
5264 
5265 /// \brief Matches ElaboratedTypes whose named type matches \c InnerMatcher.
5266 ///
5267 /// Given
5268 /// \code
5269 /// namespace N {
5270 /// namespace M {
5271 /// class D {};
5272 /// }
5273 /// }
5274 /// N::M::D d;
5275 /// \endcode
5276 ///
5277 /// \c elaboratedType(namesType(recordType(
5278 /// hasDeclaration(namedDecl(hasName("D")))))) matches the type of the variable
5279 /// declaration of \c d.
5280 AST_MATCHER_P(ElaboratedType, namesType, internal::Matcher<QualType>,
5281  InnerMatcher) {
5282  return InnerMatcher.matches(Node.getNamedType(), Finder, Builder);
5283 }
5284 
5285 /// \brief Matches types that represent the result of substituting a type for a
5286 /// template type parameter.
5287 ///
5288 /// Given
5289 /// \code
5290 /// template <typename T>
5291 /// void F(T t) {
5292 /// int i = 1 + t;
5293 /// }
5294 /// \endcode
5295 ///
5296 /// \c substTemplateTypeParmType() matches the type of 't' but not '1'
5299 
5300 /// \brief Matches template type parameter substitutions that have a replacement
5301 /// type that matches the provided matcher.
5302 ///
5303 /// Given
5304 /// \code
5305 /// template <typename T>
5306 /// double F(T t);
5307 /// int i;
5308 /// double j = F(i);
5309 /// \endcode
5310 ///
5311 /// \c substTemplateTypeParmType(hasReplacementType(type())) matches int
5313  hasReplacementType, getReplacementType,
5315 
5316 /// \brief Matches template type parameter types.
5317 ///
5318 /// Example matches T, but not int.
5319 /// (matcher = templateTypeParmType())
5320 /// \code
5321 /// template <typename T> void f(int i);
5322 /// \endcode
5324 
5325 /// \brief Matches injected class name types.
5326 ///
5327 /// Example matches S s, but not S<T> s.
5328 /// (matcher = parmVarDecl(hasType(injectedClassNameType())))
5329 /// \code
5330 /// template <typename T> struct S {
5331 /// void f(S s);
5332 /// void g(S<T> s);
5333 /// };
5334 /// \endcode
5336 
5337 /// \brief Matches decayed type
5338 /// Example matches i[] in declaration of f.
5339 /// (matcher = valueDecl(hasType(decayedType(hasDecayedType(pointerType())))))
5340 /// Example matches i[1].
5341 /// (matcher = expr(hasType(decayedType(hasDecayedType(pointerType())))))
5342 /// \code
5343 /// void f(int i[]) {
5344 /// i[1] = 0;
5345 /// }
5346 /// \endcode
5348 
5349 /// \brief Matches the decayed type, whos decayed type matches \c InnerMatcher
5350 AST_MATCHER_P(DecayedType, hasDecayedType, internal::Matcher<QualType>,
5351  InnerType) {
5352  return InnerType.matches(Node.getDecayedType(), Finder, Builder);
5353 }
5354 
5355 /// \brief Matches declarations whose declaration context, interpreted as a
5356 /// Decl, matches \c InnerMatcher.
5357 ///
5358 /// Given
5359 /// \code
5360 /// namespace N {
5361 /// namespace M {
5362 /// class D {};
5363 /// }
5364 /// }
5365 /// \endcode
5366 ///
5367 /// \c cxxRcordDecl(hasDeclContext(namedDecl(hasName("M")))) matches the
5368 /// declaration of \c class \c D.
5369 AST_MATCHER_P(Decl, hasDeclContext, internal::Matcher<Decl>, InnerMatcher) {
5370  const DeclContext *DC = Node.getDeclContext();
5371  if (!DC) return false;
5372  return InnerMatcher.matches(*Decl::castFromDeclContext(DC), Finder, Builder);
5373 }
5374 
5375 /// \brief Matches nested name specifiers.
5376 ///
5377 /// Given
5378 /// \code
5379 /// namespace ns {
5380 /// struct A { static void f(); };
5381 /// void A::f() {}
5382 /// void g() { A::f(); }
5383 /// }
5384 /// ns::A a;
5385 /// \endcode
5386 /// nestedNameSpecifier()
5387 /// matches "ns::" and both "A::"
5388 extern const internal::VariadicAllOfMatcher<NestedNameSpecifier>
5390 
5391 /// \brief Same as \c nestedNameSpecifier but matches \c NestedNameSpecifierLoc.
5392 extern const internal::VariadicAllOfMatcher<NestedNameSpecifierLoc>
5394 
5395 /// \brief Matches \c NestedNameSpecifierLocs for which the given inner
5396 /// NestedNameSpecifier-matcher matches.
5398  internal::BindableMatcher<NestedNameSpecifierLoc>, loc,
5399  internal::Matcher<NestedNameSpecifier>, InnerMatcher, 1) {
5400  return internal::BindableMatcher<NestedNameSpecifierLoc>(
5401  new internal::LocMatcher<NestedNameSpecifierLoc, NestedNameSpecifier>(
5402  InnerMatcher));
5403 }
5404 
5405 /// \brief Matches nested name specifiers that specify a type matching the
5406 /// given \c QualType matcher without qualifiers.
5407 ///
5408 /// Given
5409 /// \code
5410 /// struct A { struct B { struct C {}; }; };
5411 /// A::B::C c;
5412 /// \endcode
5413 /// nestedNameSpecifier(specifiesType(
5414 /// hasDeclaration(cxxRecordDecl(hasName("A")))
5415 /// ))
5416 /// matches "A::"
5418  internal::Matcher<QualType>, InnerMatcher) {
5419  if (!Node.getAsType())
5420  return false;
5421  return InnerMatcher.matches(QualType(Node.getAsType(), 0), Finder, Builder);
5422 }
5423 
5424 /// \brief Matches nested name specifier locs that specify a type matching the
5425 /// given \c TypeLoc.
5426 ///
5427 /// Given
5428 /// \code
5429 /// struct A { struct B { struct C {}; }; };
5430 /// A::B::C c;
5431 /// \endcode
5432 /// nestedNameSpecifierLoc(specifiesTypeLoc(loc(type(
5433 /// hasDeclaration(cxxRecordDecl(hasName("A")))))))
5434 /// matches "A::"
5436  internal::Matcher<TypeLoc>, InnerMatcher) {
5437  return Node && InnerMatcher.matches(Node.getTypeLoc(), Finder, Builder);
5438 }
5439 
5440 /// \brief Matches on the prefix of a \c NestedNameSpecifier.
5441 ///
5442 /// Given
5443 /// \code
5444 /// struct A { struct B { struct C {}; }; };
5445 /// A::B::C c;
5446 /// \endcode
5447 /// nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A")))) and
5448 /// matches "A::"
5450  internal::Matcher<NestedNameSpecifier>, InnerMatcher,
5451  0) {
5452  const NestedNameSpecifier *NextNode = Node.getPrefix();
5453  if (!NextNode)
5454  return false;
5455  return InnerMatcher.matches(*NextNode, Finder, Builder);
5456 }
5457 
5458 /// \brief Matches on the prefix of a \c NestedNameSpecifierLoc.
5459 ///
5460 /// Given
5461 /// \code
5462 /// struct A { struct B { struct C {}; }; };
5463 /// A::B::C c;
5464 /// \endcode
5465 /// nestedNameSpecifierLoc(hasPrefix(loc(specifiesType(asString("struct A")))))
5466 /// matches "A::"
5468  internal::Matcher<NestedNameSpecifierLoc>, InnerMatcher,
5469  1) {
5470  NestedNameSpecifierLoc NextNode = Node.getPrefix();
5471  if (!NextNode)
5472  return false;
5473  return InnerMatcher.matches(NextNode, Finder, Builder);
5474 }
5475 
5476 /// \brief Matches nested name specifiers that specify a namespace matching the
5477 /// given namespace matcher.
5478 ///
5479 /// Given
5480 /// \code
5481 /// namespace ns { struct A {}; }
5482 /// ns::A a;
5483 /// \endcode
5484 /// nestedNameSpecifier(specifiesNamespace(hasName("ns")))
5485 /// matches "ns::"
5487  internal::Matcher<NamespaceDecl>, InnerMatcher) {
5488  if (!Node.getAsNamespace())
5489  return false;
5490  return InnerMatcher.matches(*Node.getAsNamespace(), Finder, Builder);
5491 }
5492 
5493 /// \brief Overloads for the \c equalsNode matcher.
5494 /// FIXME: Implement for other node types.
5495 /// @{
5496 
5497 /// \brief Matches if a node equals another node.
5498 ///
5499 /// \c Decl has pointer identity in the AST.
5500 AST_MATCHER_P_OVERLOAD(Decl, equalsNode, const Decl*, Other, 0) {
5501  return &Node == Other;
5502 }
5503 /// \brief Matches if a node equals another node.
5504 ///
5505 /// \c Stmt has pointer identity in the AST.
5506 AST_MATCHER_P_OVERLOAD(Stmt, equalsNode, const Stmt*, Other, 1) {
5507  return &Node == Other;
5508 }
5509 /// \brief Matches if a node equals another node.
5510 ///
5511 /// \c Type has pointer identity in the AST.
5512 AST_MATCHER_P_OVERLOAD(Type, equalsNode, const Type*, Other, 2) {
5513  return &Node == Other;
5514 }
5515 
5516 /// @}
5517 
5518 /// \brief Matches each case or default statement belonging to the given switch
5519 /// statement. This matcher may produce multiple matches.
5520 ///
5521 /// Given
5522 /// \code
5523 /// switch (1) { case 1: case 2: default: switch (2) { case 3: case 4: ; } }
5524 /// \endcode
5525 /// switchStmt(forEachSwitchCase(caseStmt().bind("c"))).bind("s")
5526 /// matches four times, with "c" binding each of "case 1:", "case 2:",
5527 /// "case 3:" and "case 4:", and "s" respectively binding "switch (1)",
5528 /// "switch (1)", "switch (2)" and "switch (2)".
5529 AST_MATCHER_P(SwitchStmt, forEachSwitchCase, internal::Matcher<SwitchCase>,
5530  InnerMatcher) {
5531  BoundNodesTreeBuilder Result;
5532  // FIXME: getSwitchCaseList() does not necessarily guarantee a stable
5533  // iteration order. We should use the more general iterating matchers once
5534  // they are capable of expressing this matcher (for example, it should ignore
5535  // case statements belonging to nested switch statements).
5536  bool Matched = false;
5537  for (const SwitchCase *SC = Node.getSwitchCaseList(); SC;
5538  SC = SC->getNextSwitchCase()) {
5539  BoundNodesTreeBuilder CaseBuilder(*Builder);
5540  bool CaseMatched = InnerMatcher.matches(*SC, Finder, &CaseBuilder);
5541  if (CaseMatched) {
5542  Matched = true;
5543  Result.addMatch(CaseBuilder);
5544  }
5545  }
5546  *Builder = std::move(Result);
5547  return Matched;
5548 }
5549 
5550 /// \brief Matches each constructor initializer in a constructor definition.
5551 ///
5552 /// Given
5553 /// \code
5554 /// class A { A() : i(42), j(42) {} int i; int j; };
5555 /// \endcode
5556 /// cxxConstructorDecl(forEachConstructorInitializer(
5557 /// forField(decl().bind("x"))
5558 /// ))
5559 /// will trigger two matches, binding for 'i' and 'j' respectively.
5560 AST_MATCHER_P(CXXConstructorDecl, forEachConstructorInitializer,
5561  internal::Matcher<CXXCtorInitializer>, InnerMatcher) {
5562  BoundNodesTreeBuilder Result;
5563  bool Matched = false;
5564  for (const auto *I : Node.inits()) {
5565  BoundNodesTreeBuilder InitBuilder(*Builder);
5566  if (InnerMatcher.matches(*I, Finder, &InitBuilder)) {
5567  Matched = true;
5568  Result.addMatch(InitBuilder);
5569  }
5570  }
5571  *Builder = std::move(Result);
5572  return Matched;
5573 }
5574 
5575 /// \brief Matches constructor declarations that are copy constructors.
5576 ///
5577 /// Given
5578 /// \code
5579 /// struct S {
5580 /// S(); // #1
5581 /// S(const S &); // #2
5582 /// S(S &&); // #3
5583 /// };
5584 /// \endcode
5585 /// cxxConstructorDecl(isCopyConstructor()) will match #2, but not #1 or #3.
5586 AST_MATCHER(CXXConstructorDecl, isCopyConstructor) {
5587  return Node.isCopyConstructor();
5588 }
5589 
5590 /// \brief Matches constructor declarations that are move constructors.
5591 ///
5592 /// Given
5593 /// \code
5594 /// struct S {
5595 /// S(); // #1
5596 /// S(const S &); // #2
5597 /// S(S &&); // #3
5598 /// };
5599 /// \endcode
5600 /// cxxConstructorDecl(isMoveConstructor()) will match #3, but not #1 or #2.
5601 AST_MATCHER(CXXConstructorDecl, isMoveConstructor) {
5602  return Node.isMoveConstructor();
5603 }
5604 
5605 /// \brief Matches constructor declarations that are default constructors.
5606 ///
5607 /// Given
5608 /// \code
5609 /// struct S {
5610 /// S(); // #1
5611 /// S(const S &); // #2
5612 /// S(S &&); // #3
5613 /// };
5614 /// \endcode
5615 /// cxxConstructorDecl(isDefaultConstructor()) will match #1, but not #2 or #3.
5616 AST_MATCHER(CXXConstructorDecl, isDefaultConstructor) {
5617  return Node.isDefaultConstructor();
5618 }
5619 
5620 /// \brief Matches constructors that delegate to another constructor.
5621 ///
5622 /// Given
5623 /// \code
5624 /// struct S {
5625 /// S(); // #1
5626 /// S(int) {} // #2
5627 /// S(S &&) : S() {} // #3
5628 /// };
5629 /// S::S() : S(0) {} // #4
5630 /// \endcode
5631 /// cxxConstructorDecl(isDelegatingConstructor()) will match #3 and #4, but not
5632 /// #1 or #2.
5633 AST_MATCHER(CXXConstructorDecl, isDelegatingConstructor) {
5634  return Node.isDelegatingConstructor();
5635 }
5636 
5637 /// \brief Matches constructor and conversion declarations that are marked with
5638 /// the explicit keyword.
5639 ///
5640 /// Given
5641 /// \code
5642 /// struct S {
5643 /// S(int); // #1
5644 /// explicit S(double); // #2
5645 /// operator int(); // #3
5646 /// explicit operator bool(); // #4
5647 /// };
5648 /// \endcode
5649 /// cxxConstructorDecl(isExplicit()) will match #2, but not #1.
5650 /// cxxConversionDecl(isExplicit()) will match #4, but not #3.
5653  CXXConversionDecl)) {
5654  return Node.isExplicit();
5655 }
5656 
5657 /// \brief Matches function and namespace declarations that are marked with
5658 /// the inline keyword.
5659 ///
5660 /// Given
5661 /// \code
5662 /// inline void f();
5663 /// void g();
5664 /// namespace n {
5665 /// inline namespace m {}
5666 /// }
5667 /// \endcode
5668 /// functionDecl(isInline()) will match ::f().
5669 /// namespaceDecl(isInline()) will match n::m.
5672  FunctionDecl)) {
5673  // This is required because the spelling of the function used to determine
5674  // whether inline is specified or not differs between the polymorphic types.
5675  if (const auto *FD = dyn_cast<FunctionDecl>(&Node))
5676  return FD->isInlineSpecified();
5677  else if (const auto *NSD = dyn_cast<NamespaceDecl>(&Node))
5678  return NSD->isInline();
5679  llvm_unreachable("Not a valid polymorphic type");
5680 }
5681 
5682 /// \brief Matches anonymous namespace declarations.
5683 ///
5684 /// Given
5685 /// \code
5686 /// namespace n {
5687 /// namespace {} // #1
5688 /// }
5689 /// \endcode
5690 /// namespaceDecl(isAnonymous()) will match #1 but not ::n.
5692  return Node.isAnonymousNamespace();
5693 }
5694 
5695 /// \brief If the given case statement does not use the GNU case range
5696 /// extension, matches the constant given in the statement.
5697 ///
5698 /// Given
5699 /// \code
5700 /// switch (1) { case 1: case 1+1: case 3 ... 4: ; }
5701 /// \endcode
5702 /// caseStmt(hasCaseConstant(integerLiteral()))
5703 /// matches "case 1:"
5704 AST_MATCHER_P(CaseStmt, hasCaseConstant, internal::Matcher<Expr>,
5705  InnerMatcher) {
5706  if (Node.getRHS())
5707  return false;
5708 
5709  return InnerMatcher.matches(*Node.getLHS(), Finder, Builder);
5710 }
5711 
5712 /// \brief Matches declaration that has a given attribute.
5713 ///
5714 /// Given
5715 /// \code
5716 /// __attribute__((device)) void f() { ... }
5717 /// \endcode
5718 /// decl(hasAttr(clang::attr::CUDADevice)) matches the function declaration of
5719 /// f. If the matcher is use from clang-query, attr::Kind parameter should be
5720 /// passed as a quoted string. e.g., hasAttr("attr::CUDADevice").
5721 AST_MATCHER_P(Decl, hasAttr, attr::Kind, AttrKind) {
5722  for (const auto *Attr : Node.attrs()) {
5723  if (Attr->getKind() == AttrKind)
5724  return true;
5725  }
5726  return false;
5727 }
5728 
5729 /// \brief Matches the return value expression of a return statement
5730 ///
5731 /// Given
5732 /// \code
5733 /// return a + b;
5734 /// \endcode
5735 /// hasReturnValue(binaryOperator())
5736 /// matches 'return a + b'
5737 /// with binaryOperator()
5738 /// matching 'a + b'
5739 AST_MATCHER_P(ReturnStmt, hasReturnValue, internal::Matcher<Expr>,
5740  InnerMatcher) {
5741  if (const auto *RetValue = Node.getRetValue())
5742  return InnerMatcher.matches(*RetValue, Finder, Builder);
5743  return false;
5744 }
5745 
5746 /// \brief Matches CUDA kernel call expression.
5747 ///
5748 /// Example matches,
5749 /// \code
5750 /// kernel<<<i,j>>>();
5751 /// \endcode
5752 extern const internal::VariadicDynCastAllOfMatcher<Stmt, CUDAKernelCallExpr>
5754 
5755 /// \brief Matches expressions that resolve to a null pointer constant, such as
5756 /// GNU's __null, C++11's nullptr, or C's NULL macro.
5757 ///
5758 /// Given:
5759 /// \code
5760 /// void *v1 = NULL;
5761 /// void *v2 = nullptr;
5762 /// void *v3 = __null; // GNU extension
5763 /// char *cp = (char *)0;
5764 /// int *ip = 0;
5765 /// int i = 0;
5766 /// \endcode
5767 /// expr(nullPointerConstant())
5768 /// matches the initializer for v1, v2, v3, cp, and ip. Does not match the
5769 /// initializer for i.
5770 AST_MATCHER_FUNCTION(internal::Matcher<Expr>, nullPointerConstant) {
5771  return anyOf(
5773  integerLiteral(equals(0), hasParent(expr(hasType(pointerType())))));
5774 }
5775 
5776 /// \brief Matches declaration of the function the statement belongs to
5777 ///
5778 /// Given:
5779 /// \code
5780 /// F& operator=(const F& o) {
5781 /// std::copy_if(o.begin(), o.end(), begin(), [](V v) { return v > 0; });
5782 /// return *this;
5783 /// }
5784 /// \endcode
5785 /// returnStmt(forFunction(hasName("operator=")))
5786 /// matches 'return *this'
5787 /// but does match 'return > 0'
5788 AST_MATCHER_P(Stmt, forFunction, internal::Matcher<FunctionDecl>,
5789  InnerMatcher) {
5790  const auto &Parents = Finder->getASTContext().getParents(Node);
5791 
5793  Parents.end());
5794  while(!Stack.empty()) {
5795  const auto &CurNode = Stack.back();
5796  Stack.pop_back();
5797  if(const auto *FuncDeclNode = CurNode.get<FunctionDecl>()) {
5798  if(InnerMatcher.matches(*FuncDeclNode, Finder, Builder)) {
5799  return true;
5800  }
5801  } else if(const auto *LambdaExprNode = CurNode.get<LambdaExpr>()) {
5802  if(InnerMatcher.matches(*LambdaExprNode->getCallOperator(),
5803  Finder, Builder)) {
5804  return true;
5805  }
5806  } else {
5807  for(const auto &Parent: Finder->getASTContext().getParents(CurNode))
5808  Stack.push_back(Parent);
5809  }
5810  }
5811  return false;
5812 }
5813 
5814 /// \brief Matches a declaration that has external formal linkage.
5815 ///
5816 /// Example matches only z (matcher = varDecl(hasExternalFormalLinkage()))
5817 /// \code
5818 /// void f() {
5819 /// int x;
5820 /// static int y;
5821 /// }
5822 /// int z;
5823 /// \endcode
5824 ///
5825 /// Example matches f() because it has external formal linkage despite being
5826 /// unique to the translation unit as though it has internal likage
5827 /// (matcher = functionDecl(hasExternalFormalLinkage()))
5828 ///
5829 /// \code
5830 /// namespace {
5831 /// void f() {}
5832 /// }
5833 /// \endcode
5834 AST_MATCHER(NamedDecl, hasExternalFormalLinkage) {
5835  return Node.hasExternalFormalLinkage();
5836 }
5837 
5838 /// \brief Matches a declaration that has default arguments.
5839 ///
5840 /// Example matches y (matcher = parmVarDecl(hasDefaultArgument()))
5841 /// \code
5842 /// void x(int val) {}
5843 /// void y(int val = 0) {}
5844 /// \endcode
5845 AST_MATCHER(ParmVarDecl, hasDefaultArgument) {
5846  return Node.hasDefaultArg();
5847 }
5848 
5849 /// \brief Matches array new expressions.
5850 ///
5851 /// Given:
5852 /// \code
5853 /// MyClass *p1 = new MyClass[10];
5854 /// \endcode
5855 /// cxxNewExpr(isArray())
5856 /// matches the expression 'new MyClass[10]'.
5858  return Node.isArray();
5859 }
5860 
5861 /// \brief Matches array new expressions with a given array size.
5862 ///
5863 /// Given:
5864 /// \code
5865 /// MyClass *p1 = new MyClass[10];
5866 /// \endcode
5867 /// cxxNewExpr(hasArraySize(intgerLiteral(equals(10))))
5868 /// matches the expression 'new MyClass[10]'.
5869 AST_MATCHER_P(CXXNewExpr, hasArraySize, internal::Matcher<Expr>, InnerMatcher) {
5870  return Node.isArray() &&
5871  InnerMatcher.matches(*Node.getArraySize(), Finder, Builder);
5872 }
5873 
5874 /// \brief Matches a class declaration that is defined.
5875 ///
5876 /// Example matches x (matcher = cxxRecordDecl(hasDefinition()))
5877 /// \code
5878 /// class x {};
5879 /// class y;
5880 /// \endcode
5882  return Node.hasDefinition();
5883 }
5884 
5885 /// \brief Matches C++11 scoped enum declaration.
5886 ///
5887 /// Example matches Y (matcher = enumDecl(isScoped()))
5888 /// \code
5889 /// enum X {};
5890 /// enum class Y {};
5891 /// \endcode
5892 AST_MATCHER(EnumDecl, isScoped) {
5893  return Node.isScoped();
5894 }
5895 
5896 /// \brief Matches a function declared with a trailing return type.
5897 ///
5898 /// Example matches Y (matcher = functionDecl(hasTrailingReturn()))
5899 /// \code
5900 /// int X() {}
5901 /// auto Y() -> int {}
5902 /// \endcode
5903 AST_MATCHER(FunctionDecl, hasTrailingReturn) {
5904  if (const auto *F = Node.getType()->getAs<FunctionProtoType>())
5905  return F->hasTrailingReturn();
5906  return false;
5907 }
5908 
5909 } // namespace ast_matchers
5910 } // namespace clang
5911 
5912 #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:4790
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:1696
const internal::VariadicDynCastAllOfMatcher< Decl, TypedefDecl > typedefDecl
Matches typedef declarations.
body_iterator body_end()
Definition: Stmt.h:628
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:2289
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:844
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXUnresolvedConstructExpr > cxxUnresolvedConstructExpr
Matches unresolved constructor call expressions.
const internal::VariadicDynCastAllOfMatcher< Decl, ObjCInterfaceDecl > objcInterfaceDecl
Matches Objective-C interface declarations.
const internal::ArgumentAdaptingMatcherFunc< internal::HasMatcher > has
Matches AST nodes that have child AST nodes that match the provided matcher.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXBindTemporaryExpr > cxxBindTemporaryExpr
Matches nodes where temporaries are created.
Stmt - This represents one statement.
Definition: Stmt.h:66
internal::Matcher< Stmt > StatementMatcher
Definition: ASTMatchers.h:146
const internal::VariadicDynCastAllOfMatcher< Decl, RecordDecl > recordDecl
Matches class, struct, and union declarations.
IfStmt - This represents an if/then/else.
Definition: Stmt.h:933
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:2655
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:4407
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:1356
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2564
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:2452
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:3947
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:1515
Represents the result of substituting a type for a template type parameter.
Definition: Type.h:4262
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:3510
Represents a C99 designated initializer expression.
Definition: Expr.h:4119
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:530
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:2488
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:4760
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:3885
Represents a C++ using-declaration.
Definition: DeclCXX.h:3334
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:1207
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:4683
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:2778
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:2998
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:3616
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:2723
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:2380
const internal::VariadicDynCastAllOfMatcher< Decl, ValueDecl > valueDecl
Matches any value declaration.
const internal::VariadicDynCastAllOfMatcher< Decl, ObjCPropertyDecl > objcPropertyDecl
Matches Objective-C property declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, ObjCAtFinallyStmt > objcFinallyStmt
Matches Objective-C statements.
internal::Matcher< T > findAll(const internal::Matcher< T > &Matcher)
Matches if the node or any descendant matches.
Definition: ASTMatchers.h:2571
const internal::VariadicDynCastAllOfMatcher< Decl, FunctionTemplateDecl > functionTemplateDecl
Matches C++ function template declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXCatchStmt > cxxCatchStmt
Matches catch statements.
const internal::VariadicDynCastAllOfMatcher< Decl, ClassTemplateSpecializationDecl > classTemplateSpecializationDecl
Matches C++ class template specializations.
const internal::VariadicDynCastAllOfMatcher< Stmt, MaterializeTemporaryExpr > materializeTemporaryExpr
Matches nodes where temporaries are materialized.
NodeId Parent
Definition: ASTDiff.cpp:192
const internal::VariadicDynCastAllOfMatcher< Stmt, CaseStmt > caseStmt
Matches case statements inside switch statements.
const AstTypeMatcher< ParenType > parenType
Matches ParenType nodes.
const internal::VariadicDynCastAllOfMatcher< Decl, UsingDirectiveDecl > usingDirectiveDecl
Matches using namespace declarations.
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:595
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3274
const internal::ArgumentAdaptingMatcherFunc< internal::ForEachDescendantMatcher > forEachDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher. ...
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:688
CastKind
CastKind - The kind of operation required for a conversion.
StringRef Filename
Definition: Format.cpp:1522
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand...
Definition: Expr.h:2040
const internal::VariadicDynCastAllOfMatcher< Stmt, AsmStmt > asmStmt
Matches asm statements.
const internal::VariadicDynCastAllOfMatcher< Decl, UnresolvedUsingTypenameDecl > unresolvedUsingTypenameDecl
Matches unresolved using value declarations that involve the typename.
const internal::VariadicDynCastAllOfMatcher< Stmt, MemberExpr > memberExpr
Matches member expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, AddrLabelExpr > addrLabelExpr
Matches address of label statements (GNU extension).
const internal::VariadicDynCastAllOfMatcher< Stmt, ExplicitCastExpr > explicitCastExpr
Matches explicit cast expressions.