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