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