Coverage Report

Created: 2021-09-21 08:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/ASTMatchers/ASTMatchers.h
Line
Count
Source (jump to first uncovered line)
1
//===- ASTMatchers.h - Structural query framework ---------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This file implements matchers to be used together with the MatchFinder to
10
//  match AST nodes.
11
//
12
//  Matchers are created by generator functions, which can be combined in
13
//  a functional in-language DSL to express queries over the C++ AST.
14
//
15
//  For example, to match a class with a certain name, one would call:
16
//    cxxRecordDecl(hasName("MyClass"))
17
//  which returns a matcher that can be used to find all AST nodes that declare
18
//  a class named 'MyClass'.
19
//
20
//  For more complicated match expressions we're often interested in accessing
21
//  multiple parts of the matched AST nodes once a match is found. In that case,
22
//  call `.bind("name")` on match expressions that match the nodes you want to
23
//  access.
24
//
25
//  For example, when we're interested in child classes of a certain class, we
26
//  would write:
27
//    cxxRecordDecl(hasName("MyClass"), has(recordDecl().bind("child")))
28
//  When the match is found via the MatchFinder, a user provided callback will
29
//  be called with a BoundNodes instance that contains a mapping from the
30
//  strings that we provided for the `.bind()` calls to the nodes that were
31
//  matched.
32
//  In the given example, each time our matcher finds a match we get a callback
33
//  where "child" is bound to the RecordDecl node of the matching child
34
//  class declaration.
35
//
36
//  See ASTMatchersInternal.h for a more in-depth explanation of the
37
//  implementation details of the matcher framework.
38
//
39
//  See ASTMatchFinder.h for how to use the generated matchers to run over
40
//  an AST.
41
//
42
//===----------------------------------------------------------------------===//
43
44
#ifndef LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H
45
#define LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H
46
47
#include "clang/AST/ASTContext.h"
48
#include "clang/AST/ASTTypeTraits.h"
49
#include "clang/AST/Attr.h"
50
#include "clang/AST/CXXInheritance.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"
59
#include "clang/AST/LambdaCapture.h"
60
#include "clang/AST/NestedNameSpecifier.h"
61
#include "clang/AST/OpenMPClause.h"
62
#include "clang/AST/OperationKinds.h"
63
#include "clang/AST/ParentMapContext.h"
64
#include "clang/AST/Stmt.h"
65
#include "clang/AST/StmtCXX.h"
66
#include "clang/AST/StmtObjC.h"
67
#include "clang/AST/StmtOpenMP.h"
68
#include "clang/AST/TemplateBase.h"
69
#include "clang/AST/TemplateName.h"
70
#include "clang/AST/Type.h"
71
#include "clang/AST/TypeLoc.h"
72
#include "clang/ASTMatchers/ASTMatchersInternal.h"
73
#include "clang/ASTMatchers/ASTMatchersMacros.h"
74
#include "clang/Basic/AttrKinds.h"
75
#include "clang/Basic/ExceptionSpecificationType.h"
76
#include "clang/Basic/FileManager.h"
77
#include "clang/Basic/IdentifierTable.h"
78
#include "clang/Basic/LLVM.h"
79
#include "clang/Basic/SourceManager.h"
80
#include "clang/Basic/Specifiers.h"
81
#include "clang/Basic/TypeTraits.h"
82
#include "llvm/ADT/ArrayRef.h"
83
#include "llvm/ADT/SmallVector.h"
84
#include "llvm/ADT/StringRef.h"
85
#include "llvm/Support/Casting.h"
86
#include "llvm/Support/Compiler.h"
87
#include "llvm/Support/ErrorHandling.h"
88
#include "llvm/Support/Regex.h"
89
#include <cassert>
90
#include <cstddef>
91
#include <iterator>
92
#include <limits>
93
#include <string>
94
#include <utility>
95
#include <vector>
96
97
namespace clang {
98
namespace ast_matchers {
99
100
/// Maps string IDs to AST nodes matched by parts of a matcher.
101
///
102
/// The bound nodes are generated by calling \c bind("id") on the node matchers
103
/// of the nodes we want to access later.
104
///
105
/// The instances of BoundNodes are created by \c MatchFinder when the user's
106
/// callbacks are executed every time a match is found.
107
class BoundNodes {
108
public:
109
  /// Returns the AST node bound to \c ID.
110
  ///
111
  /// Returns NULL if there was no node bound to \c ID or if there is a node but
112
  /// it cannot be converted to the specified type.
113
  template <typename T>
114
10.1k
  const T *getNodeAs(StringRef ID) const {
115
10.1k
    return MyBoundNodes.getNodeAs<T>(ID);
116
10.1k
  }
clang::CallExpr const* clang::ast_matchers::BoundNodes::getNodeAs<clang::CallExpr>(llvm::StringRef) const
Line
Count
Source
114
93
  const T *getNodeAs(StringRef ID) const {
115
93
    return MyBoundNodes.getNodeAs<T>(ID);
116
93
  }
clang::Decl const* clang::ast_matchers::BoundNodes::getNodeAs<clang::Decl>(llvm::StringRef) const
Line
Count
Source
114
1.64k
  const T *getNodeAs(StringRef ID) const {
115
1.64k
    return MyBoundNodes.getNodeAs<T>(ID);
116
1.64k
  }
clang::QualType const* clang::ast_matchers::BoundNodes::getNodeAs<clang::QualType>(llvm::StringRef) const
Line
Count
Source
114
447
  const T *getNodeAs(StringRef ID) const {
115
447
    return MyBoundNodes.getNodeAs<T>(ID);
116
447
  }
clang::ParmVarDecl const* clang::ast_matchers::BoundNodes::getNodeAs<clang::ParmVarDecl>(llvm::StringRef) const
Line
Count
Source
114
80
  const T *getNodeAs(StringRef ID) const {
115
80
    return MyBoundNodes.getNodeAs<T>(ID);
116
80
  }
clang::Expr const* clang::ast_matchers::BoundNodes::getNodeAs<clang::Expr>(llvm::StringRef) const
Line
Count
Source
114
1.20k
  const T *getNodeAs(StringRef ID) const {
115
1.20k
    return MyBoundNodes.getNodeAs<T>(ID);
116
1.20k
  }
clang::ObjCMethodDecl const* clang::ast_matchers::BoundNodes::getNodeAs<clang::ObjCMethodDecl>(llvm::StringRef) const
Line
Count
Source
114
27
  const T *getNodeAs(StringRef ID) const {
115
27
    return MyBoundNodes.getNodeAs<T>(ID);
116
27
  }
clang::ObjCAutoreleasePoolStmt const* clang::ast_matchers::BoundNodes::getNodeAs<clang::ObjCAutoreleasePoolStmt>(llvm::StringRef) const
Line
Count
Source
114
43
  const T *getNodeAs(StringRef ID) const {
115
43
    return MyBoundNodes.getNodeAs<T>(ID);
116
43
  }
clang::CastExpr const* clang::ast_matchers::BoundNodes::getNodeAs<clang::CastExpr>(llvm::StringRef) const
Line
Count
Source
114
2
  const T *getNodeAs(StringRef ID) const {
115
2
    return MyBoundNodes.getNodeAs<T>(ID);
116
2
  }
clang::CXXRecordDecl const* clang::ast_matchers::BoundNodes::getNodeAs<clang::CXXRecordDecl>(llvm::StringRef) const
Line
Count
Source
114
838
  const T *getNodeAs(StringRef ID) const {
115
838
    return MyBoundNodes.getNodeAs<T>(ID);
116
838
  }
clang::Stmt const* clang::ast_matchers::BoundNodes::getNodeAs<clang::Stmt>(llvm::StringRef) const
Line
Count
Source
114
1.08k
  const T *getNodeAs(StringRef ID) const {
115
1.08k
    return MyBoundNodes.getNodeAs<T>(ID);
116
1.08k
  }
clang::ObjCMessageExpr const* clang::ast_matchers::BoundNodes::getNodeAs<clang::ObjCMessageExpr>(llvm::StringRef) const
Line
Count
Source
114
20
  const T *getNodeAs(StringRef ID) const {
115
20
    return MyBoundNodes.getNodeAs<T>(ID);
116
20
  }
clang::MemberExpr const* clang::ast_matchers::BoundNodes::getNodeAs<clang::MemberExpr>(llvm::StringRef) const
Line
Count
Source
114
19
  const T *getNodeAs(StringRef ID) const {
115
19
    return MyBoundNodes.getNodeAs<T>(ID);
116
19
  }
clang::ObjCIvarRefExpr const* clang::ast_matchers::BoundNodes::getNodeAs<clang::ObjCIvarRefExpr>(llvm::StringRef) const
Line
Count
Source
114
2
  const T *getNodeAs(StringRef ID) const {
115
2
    return MyBoundNodes.getNodeAs<T>(ID);
116
2
  }
clang::DeclRefExpr const* clang::ast_matchers::BoundNodes::getNodeAs<clang::DeclRefExpr>(llvm::StringRef) const
Line
Count
Source
114
196
  const T *getNodeAs(StringRef ID) const {
115
196
    return MyBoundNodes.getNodeAs<T>(ID);
116
196
  }
clang::IntegerLiteral const* clang::ast_matchers::BoundNodes::getNodeAs<clang::IntegerLiteral>(llvm::StringRef) const
Line
Count
Source
114
257
  const T *getNodeAs(StringRef ID) const {
115
257
    return MyBoundNodes.getNodeAs<T>(ID);
116
257
  }
clang::BinaryOperator const* clang::ast_matchers::BoundNodes::getNodeAs<clang::BinaryOperator>(llvm::StringRef) const
Line
Count
Source
114
120
  const T *getNodeAs(StringRef ID) const {
115
120
    return MyBoundNodes.getNodeAs<T>(ID);
116
120
  }
clang::VarDecl const* clang::ast_matchers::BoundNodes::getNodeAs<clang::VarDecl>(llvm::StringRef) const
Line
Count
Source
114
1.12k
  const T *getNodeAs(StringRef ID) const {
115
1.12k
    return MyBoundNodes.getNodeAs<T>(ID);
116
1.12k
  }
clang::NamedDecl const* clang::ast_matchers::BoundNodes::getNodeAs<clang::NamedDecl>(llvm::StringRef) const
Line
Count
Source
114
1.12k
  const T *getNodeAs(StringRef ID) const {
115
1.12k
    return MyBoundNodes.getNodeAs<T>(ID);
116
1.12k
  }
clang::FunctionDecl const* clang::ast_matchers::BoundNodes::getNodeAs<clang::FunctionDecl>(llvm::StringRef) const
Line
Count
Source
114
1.68k
  const T *getNodeAs(StringRef ID) const {
115
1.68k
    return MyBoundNodes.getNodeAs<T>(ID);
116
1.68k
  }
clang::IfStmt const* clang::ast_matchers::BoundNodes::getNodeAs<clang::IfStmt>(llvm::StringRef) const
Line
Count
Source
114
138
  const T *getNodeAs(StringRef ID) const {
115
138
    return MyBoundNodes.getNodeAs<T>(ID);
116
138
  }
117
118
  /// Type of mapping from binding identifiers to bound nodes. This type
119
  /// is an associative container with a key type of \c std::string and a value
120
  /// type of \c clang::DynTypedNode
121
  using IDToNodeMap = internal::BoundNodesMap::IDToNodeMap;
122
123
  /// Retrieve mapping from binding identifiers to bound nodes.
124
1.48k
  const IDToNodeMap &getMap() const {
125
1.48k
    return MyBoundNodes.getMap();
126
1.48k
  }
127
128
private:
129
  friend class internal::BoundNodesTreeBuilder;
130
131
  /// Create BoundNodes from a pre-filled map of bindings.
132
  BoundNodes(internal::BoundNodesMap &MyBoundNodes)
133
32.8k
      : MyBoundNodes(MyBoundNodes) {}
134
135
  internal::BoundNodesMap MyBoundNodes;
136
};
137
138
/// Types of matchers for the top-level classes in the AST class
139
/// hierarchy.
140
/// @{
141
using DeclarationMatcher = internal::Matcher<Decl>;
142
using StatementMatcher = internal::Matcher<Stmt>;
143
using TypeMatcher = internal::Matcher<QualType>;
144
using TypeLocMatcher = internal::Matcher<TypeLoc>;
145
using NestedNameSpecifierMatcher = internal::Matcher<NestedNameSpecifier>;
146
using NestedNameSpecifierLocMatcher = internal::Matcher<NestedNameSpecifierLoc>;
147
using CXXBaseSpecifierMatcher = internal::Matcher<CXXBaseSpecifier>;
148
using CXXCtorInitializerMatcher = internal::Matcher<CXXCtorInitializer>;
149
using TemplateArgumentMatcher = internal::Matcher<TemplateArgument>;
150
using TemplateArgumentLocMatcher = internal::Matcher<TemplateArgumentLoc>;
151
using AttrMatcher = internal::Matcher<Attr>;
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
392
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>
182
    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>
206
    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>
218
    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>
228
    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,
245
0
                        AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc)) {
246
0
  auto &SourceManager = Finder->getASTContext().getSourceManager();
247
0
  return SourceManager.isInMainFile(
248
0
      SourceManager.getExpansionLoc(Node.getBeginLoc()));
249
0
}
Unexecuted instantiation: clang::ast_matchers::internal::matcher_isExpansionInMainFileMatcher<clang::Decl>::matches(clang::Decl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Unexecuted instantiation: clang::ast_matchers::internal::matcher_isExpansionInMainFileMatcher<clang::Stmt>::matches(clang::Stmt const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Unexecuted instantiation: clang::ast_matchers::internal::matcher_isExpansionInMainFileMatcher<clang::TypeLoc>::matches(clang::TypeLoc const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
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,
266
0
                        AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc)) {
267
0
  auto &SourceManager = Finder->getASTContext().getSourceManager();
268
0
  auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getBeginLoc());
269
0
  if (ExpansionLoc.isInvalid()) {
270
0
    return false;
271
0
  }
272
0
  return SourceManager.isInSystemHeader(ExpansionLoc);
273
0
}
Unexecuted instantiation: clang::ast_matchers::internal::matcher_isExpansionInSystemHeaderMatcher<clang::Decl>::matches(clang::Decl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Unexecuted instantiation: clang::ast_matchers::internal::matcher_isExpansionInSystemHeaderMatcher<clang::Stmt>::matches(clang::Stmt const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Unexecuted instantiation: clang::ast_matchers::internal::matcher_isExpansionInSystemHeaderMatcher<clang::TypeLoc>::matches(clang::TypeLoc const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
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_REGEX(isExpansionInFileMatching,
291
                              AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt,
292
                                                              TypeLoc),
293
0
                              RegExp) {
294
0
  auto &SourceManager = Finder->getASTContext().getSourceManager();
295
0
  auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getBeginLoc());
296
0
  if (ExpansionLoc.isInvalid()) {
297
0
    return false;
298
0
  }
299
0
  auto FileEntry =
300
0
      SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc));
301
0
  if (!FileEntry) {
302
0
    return false;
303
0
  }
304
305
0
  auto Filename = FileEntry->getName();
306
0
  return RegExp->match(Filename);
307
0
}
Unexecuted instantiation: clang::ast_matchers::internal::matcher_isExpansionInFileMatching0Matcher<clang::Decl, std::__1::shared_ptr<llvm::Regex> >::matches(clang::Decl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Unexecuted instantiation: clang::ast_matchers::internal::matcher_isExpansionInFileMatching0Matcher<clang::Stmt, std::__1::shared_ptr<llvm::Regex> >::matches(clang::Stmt const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Unexecuted instantiation: clang::ast_matchers::internal::matcher_isExpansionInFileMatching0Matcher<clang::TypeLoc, std::__1::shared_ptr<llvm::Regex> >::matches(clang::TypeLoc const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
308
309
/// Matches statements that are (transitively) expanded from the named macro.
310
/// Does not match if only part of the statement is expanded from that macro or
311
/// if different parts of the statement are expanded from different
312
/// appearances of the macro.
313
AST_POLYMORPHIC_MATCHER_P(isExpandedFromMacro,
314
                          AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc),
315
68
                          std::string, MacroName) {
316
  // Verifies that the statement' beginning and ending are both expanded from
317
  // the same instance of the given macro.
318
68
  auto& Context = Finder->getASTContext();
319
68
  llvm::Optional<SourceLocation> B =
320
68
      internal::getExpansionLocOfMacro(MacroName, Node.getBeginLoc(), Context);
321
68
  if (!B) 
return false14
;
322
54
  llvm::Optional<SourceLocation> E =
323
54
      internal::getExpansionLocOfMacro(MacroName, Node.getEndLoc(), Context);
324
54
  if (!E) 
return false0
;
325
54
  return *B == *E;
326
54
}
Unexecuted instantiation: clang::ast_matchers::internal::matcher_isExpandedFromMacro0Matcher<clang::Decl, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::matches(clang::Decl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Unexecuted instantiation: clang::ast_matchers::internal::matcher_isExpandedFromMacro0Matcher<clang::Stmt, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::matches(clang::Stmt const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
clang::ast_matchers::internal::matcher_isExpandedFromMacro0Matcher<clang::TypeLoc, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::matches(clang::TypeLoc const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
315
12
                          std::string, MacroName) {
316
  // Verifies that the statement' beginning and ending are both expanded from
317
  // the same instance of the given macro.
318
12
  auto& Context = Finder->getASTContext();
319
12
  llvm::Optional<SourceLocation> B =
320
12
      internal::getExpansionLocOfMacro(MacroName, Node.getBeginLoc(), Context);
321
12
  if (!B) 
return false10
;
322
2
  llvm::Optional<SourceLocation> E =
323
2
      internal::getExpansionLocOfMacro(MacroName, Node.getEndLoc(), Context);
324
2
  if (!E) 
return false0
;
325
2
  return *B == *E;
326
2
}
clang::ast_matchers::internal::matcher_isExpandedFromMacro0Matcher<clang::CallExpr, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::matches(clang::CallExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
315
20
                          std::string, MacroName) {
316
  // Verifies that the statement' beginning and ending are both expanded from
317
  // the same instance of the given macro.
318
20
  auto& Context = Finder->getASTContext();
319
20
  llvm::Optional<SourceLocation> B =
320
20
      internal::getExpansionLocOfMacro(MacroName, Node.getBeginLoc(), Context);
321
20
  if (!B) 
return false4
;
322
16
  llvm::Optional<SourceLocation> E =
323
16
      internal::getExpansionLocOfMacro(MacroName, Node.getEndLoc(), Context);
324
16
  if (!E) 
return false0
;
325
16
  return *B == *E;
326
16
}
clang::ast_matchers::internal::matcher_isExpandedFromMacro0Matcher<clang::CXXOperatorCallExpr, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::matches(clang::CXXOperatorCallExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
315
4
                          std::string, MacroName) {
316
  // Verifies that the statement' beginning and ending are both expanded from
317
  // the same instance of the given macro.
318
4
  auto& Context = Finder->getASTContext();
319
4
  llvm::Optional<SourceLocation> B =
320
4
      internal::getExpansionLocOfMacro(MacroName, Node.getBeginLoc(), Context);
321
4
  if (!B) 
return false0
;
322
4
  llvm::Optional<SourceLocation> E =
323
4
      internal::getExpansionLocOfMacro(MacroName, Node.getEndLoc(), Context);
324
4
  if (!E) 
return false0
;
325
4
  return *B == *E;
326
4
}
clang::ast_matchers::internal::matcher_isExpandedFromMacro0Matcher<clang::CXXMemberCallExpr, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::matches(clang::CXXMemberCallExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
315
32
                          std::string, MacroName) {
316
  // Verifies that the statement' beginning and ending are both expanded from
317
  // the same instance of the given macro.
318
32
  auto& Context = Finder->getASTContext();
319
32
  llvm::Optional<SourceLocation> B =
320
32
      internal::getExpansionLocOfMacro(MacroName, Node.getBeginLoc(), Context);
321
32
  if (!B) 
return false0
;
322
32
  llvm::Optional<SourceLocation> E =
323
32
      internal::getExpansionLocOfMacro(MacroName, Node.getEndLoc(), Context);
324
32
  if (!E) 
return false0
;
325
32
  return *B == *E;
326
32
}
327
328
/// Matches declarations.
329
///
330
/// Examples matches \c X, \c C, and the friend declaration inside \c C;
331
/// \code
332
///   void X();
333
///   class C {
334
///     friend X;
335
///   };
336
/// \endcode
337
extern const internal::VariadicAllOfMatcher<Decl> decl;
338
339
/// Matches decomposition-declarations.
340
///
341
/// Examples matches the declaration node with \c foo and \c bar, but not
342
/// \c number.
343
/// (matcher = declStmt(has(decompositionDecl())))
344
///
345
/// \code
346
///   int number = 42;
347
///   auto [foo, bar] = std::make_pair{42, 42};
348
/// \endcode
349
extern const internal::VariadicDynCastAllOfMatcher<Decl, DecompositionDecl>
350
    decompositionDecl;
351
352
/// Matches binding declarations
353
/// Example matches \c foo and \c bar
354
/// (matcher = bindingDecl()
355
///
356
/// \code
357
///   auto [foo, bar] = std::make_pair{42, 42};
358
/// \endcode
359
extern const internal::VariadicDynCastAllOfMatcher<Decl, BindingDecl>
360
    bindingDecl;
361
362
/// Matches a declaration of a linkage specification.
363
///
364
/// Given
365
/// \code
366
///   extern "C" {}
367
/// \endcode
368
/// linkageSpecDecl()
369
///   matches "extern "C" {}"
370
extern const internal::VariadicDynCastAllOfMatcher<Decl, LinkageSpecDecl>
371
    linkageSpecDecl;
372
373
/// Matches a declaration of anything that could have a name.
374
///
375
/// Example matches \c X, \c S, the anonymous union type, \c i, and \c U;
376
/// \code
377
///   typedef int X;
378
///   struct S {
379
///     union {
380
///       int i;
381
///     } U;
382
///   };
383
/// \endcode
384
extern const internal::VariadicDynCastAllOfMatcher<Decl, NamedDecl> namedDecl;
385
386
/// Matches a declaration of label.
387
///
388
/// Given
389
/// \code
390
///   goto FOO;
391
///   FOO: bar();
392
/// \endcode
393
/// labelDecl()
394
///   matches 'FOO:'
395
extern const internal::VariadicDynCastAllOfMatcher<Decl, LabelDecl> labelDecl;
396
397
/// Matches a declaration of a namespace.
398
///
399
/// Given
400
/// \code
401
///   namespace {}
402
///   namespace test {}
403
/// \endcode
404
/// namespaceDecl()
405
///   matches "namespace {}" and "namespace test {}"
406
extern const internal::VariadicDynCastAllOfMatcher<Decl, NamespaceDecl>
407
    namespaceDecl;
408
409
/// Matches a declaration of a namespace alias.
410
///
411
/// Given
412
/// \code
413
///   namespace test {}
414
///   namespace alias = ::test;
415
/// \endcode
416
/// namespaceAliasDecl()
417
///   matches "namespace alias" but not "namespace test"
418
extern const internal::VariadicDynCastAllOfMatcher<Decl, NamespaceAliasDecl>
419
    namespaceAliasDecl;
420
421
/// Matches class, struct, and union declarations.
422
///
423
/// Example matches \c X, \c Z, \c U, and \c S
424
/// \code
425
///   class X;
426
///   template<class T> class Z {};
427
///   struct S {};
428
///   union U {};
429
/// \endcode
430
extern const internal::VariadicDynCastAllOfMatcher<Decl, RecordDecl> recordDecl;
431
432
/// Matches C++ class declarations.
433
///
434
/// Example matches \c X, \c Z
435
/// \code
436
///   class X;
437
///   template<class T> class Z {};
438
/// \endcode
439
extern const internal::VariadicDynCastAllOfMatcher<Decl, CXXRecordDecl>
440
    cxxRecordDecl;
441
442
/// Matches C++ class template declarations.
443
///
444
/// Example matches \c Z
445
/// \code
446
///   template<class T> class Z {};
447
/// \endcode
448
extern const internal::VariadicDynCastAllOfMatcher<Decl, ClassTemplateDecl>
449
    classTemplateDecl;
450
451
/// Matches C++ class template specializations.
452
///
453
/// Given
454
/// \code
455
///   template<typename T> class A {};
456
///   template<> class A<double> {};
457
///   A<int> a;
458
/// \endcode
459
/// classTemplateSpecializationDecl()
460
///   matches the specializations \c A<int> and \c A<double>
461
extern const internal::VariadicDynCastAllOfMatcher<
462
    Decl, ClassTemplateSpecializationDecl>
463
    classTemplateSpecializationDecl;
464
465
/// Matches C++ class template partial specializations.
466
///
467
/// Given
468
/// \code
469
///   template<class T1, class T2, int I>
470
///   class A {};
471
///
472
///   template<class T, int I>
473
///   class A<T, T*, I> {};
474
///
475
///   template<>
476
///   class A<int, int, 1> {};
477
/// \endcode
478
/// classTemplatePartialSpecializationDecl()
479
///   matches the specialization \c A<T,T*,I> but not \c A<int,int,1>
480
extern const internal::VariadicDynCastAllOfMatcher<
481
    Decl, ClassTemplatePartialSpecializationDecl>
482
    classTemplatePartialSpecializationDecl;
483
484
/// Matches declarator declarations (field, variable, function
485
/// and non-type template parameter declarations).
486
///
487
/// Given
488
/// \code
489
///   class X { int y; };
490
/// \endcode
491
/// declaratorDecl()
492
///   matches \c int y.
493
extern const internal::VariadicDynCastAllOfMatcher<Decl, DeclaratorDecl>
494
    declaratorDecl;
495
496
/// Matches parameter variable declarations.
497
///
498
/// Given
499
/// \code
500
///   void f(int x);
501
/// \endcode
502
/// parmVarDecl()
503
///   matches \c int x.
504
extern const internal::VariadicDynCastAllOfMatcher<Decl, ParmVarDecl>
505
    parmVarDecl;
506
507
/// Matches C++ access specifier declarations.
508
///
509
/// Given
510
/// \code
511
///   class C {
512
///   public:
513
///     int a;
514
///   };
515
/// \endcode
516
/// accessSpecDecl()
517
///   matches 'public:'
518
extern const internal::VariadicDynCastAllOfMatcher<Decl, AccessSpecDecl>
519
    accessSpecDecl;
520
521
/// Matches class bases.
522
///
523
/// Examples matches \c public virtual B.
524
/// \code
525
///   class B {};
526
///   class C : public virtual B {};
527
/// \endcode
528
extern const internal::VariadicAllOfMatcher<CXXBaseSpecifier> cxxBaseSpecifier;
529
530
/// Matches constructor initializers.
531
///
532
/// Examples matches \c i(42).
533
/// \code
534
///   class C {
535
///     C() : i(42) {}
536
///     int i;
537
///   };
538
/// \endcode
539
extern const internal::VariadicAllOfMatcher<CXXCtorInitializer>
540
    cxxCtorInitializer;
541
542
/// Matches template arguments.
543
///
544
/// Given
545
/// \code
546
///   template <typename T> struct C {};
547
///   C<int> c;
548
/// \endcode
549
/// templateArgument()
550
///   matches 'int' in C<int>.
551
extern const internal::VariadicAllOfMatcher<TemplateArgument> templateArgument;
552
553
/// Matches template arguments (with location info).
554
///
555
/// Given
556
/// \code
557
///   template <typename T> struct C {};
558
///   C<int> c;
559
/// \endcode
560
/// templateArgumentLoc()
561
///   matches 'int' in C<int>.
562
extern const internal::VariadicAllOfMatcher<TemplateArgumentLoc>
563
    templateArgumentLoc;
564
565
/// Matches template name.
566
///
567
/// Given
568
/// \code
569
///   template <typename T> class X { };
570
///   X<int> xi;
571
/// \endcode
572
/// templateName()
573
///   matches 'X' in X<int>.
574
extern const internal::VariadicAllOfMatcher<TemplateName> templateName;
575
576
/// Matches non-type template parameter declarations.
577
///
578
/// Given
579
/// \code
580
///   template <typename T, int N> struct C {};
581
/// \endcode
582
/// nonTypeTemplateParmDecl()
583
///   matches 'N', but not 'T'.
584
extern const internal::VariadicDynCastAllOfMatcher<Decl,
585
                                                   NonTypeTemplateParmDecl>
586
    nonTypeTemplateParmDecl;
587
588
/// Matches template type parameter declarations.
589
///
590
/// Given
591
/// \code
592
///   template <typename T, int N> struct C {};
593
/// \endcode
594
/// templateTypeParmDecl()
595
///   matches 'T', but not 'N'.
596
extern const internal::VariadicDynCastAllOfMatcher<Decl, TemplateTypeParmDecl>
597
    templateTypeParmDecl;
598
599
/// Matches template template parameter declarations.
600
///
601
/// Given
602
/// \code
603
///   template <template <typename> class Z, int N> struct C {};
604
/// \endcode
605
/// templateTypeParmDecl()
606
///   matches 'Z', but not 'N'.
607
extern const internal::VariadicDynCastAllOfMatcher<Decl,
608
                                                   TemplateTemplateParmDecl>
609
    templateTemplateParmDecl;
610
611
/// Matches public C++ declarations and C++ base specifers that specify public
612
/// inheritance.
613
///
614
/// Examples:
615
/// \code
616
///   class C {
617
///   public:    int a; // fieldDecl(isPublic()) matches 'a'
618
///   protected: int b;
619
///   private:   int c;
620
///   };
621
/// \endcode
622
///
623
/// \code
624
///   class Base {};
625
///   class Derived1 : public Base {}; // matches 'Base'
626
///   struct Derived2 : Base {}; // matches 'Base'
627
/// \endcode
628
AST_POLYMORPHIC_MATCHER(isPublic,
629
                        AST_POLYMORPHIC_SUPPORTED_TYPES(Decl,
630
102
                                                        CXXBaseSpecifier)) {
631
102
  return getAccessSpecifier(Node) == AS_public;
632
102
}
clang::ast_matchers::internal::matcher_isPublicMatcher<clang::CXXMethodDecl>::matches(clang::CXXMethodDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
630
2
                                                        CXXBaseSpecifier)) {
631
2
  return getAccessSpecifier(Node) == AS_public;
632
2
}
Unexecuted instantiation: clang::ast_matchers::internal::matcher_isPublicMatcher<clang::Decl>::matches(clang::Decl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
clang::ast_matchers::internal::matcher_isPublicMatcher<clang::CXXBaseSpecifier>::matches(clang::CXXBaseSpecifier const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
630
100
                                                        CXXBaseSpecifier)) {
631
100
  return getAccessSpecifier(Node) == AS_public;
632
100
}
633
634
/// Matches protected C++ declarations and C++ base specifers that specify
635
/// protected inheritance.
636
///
637
/// Examples:
638
/// \code
639
///   class C {
640
///   public:    int a;
641
///   protected: int b; // fieldDecl(isProtected()) matches 'b'
642
///   private:   int c;
643
///   };
644
/// \endcode
645
///
646
/// \code
647
///   class Base {};
648
///   class Derived : protected Base {}; // matches 'Base'
649
/// \endcode
650
AST_POLYMORPHIC_MATCHER(isProtected,
651
                        AST_POLYMORPHIC_SUPPORTED_TYPES(Decl,
652
60
                                                        CXXBaseSpecifier)) {
653
60
  return getAccessSpecifier(Node) == AS_protected;
654
60
}
Unexecuted instantiation: clang::ast_matchers::internal::matcher_isProtectedMatcher<clang::Decl>::matches(clang::Decl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
clang::ast_matchers::internal::matcher_isProtectedMatcher<clang::CXXBaseSpecifier>::matches(clang::CXXBaseSpecifier const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
652
60
                                                        CXXBaseSpecifier)) {
653
60
  return getAccessSpecifier(Node) == AS_protected;
654
60
}
655
656
/// Matches private C++ declarations and C++ base specifers that specify private
657
/// inheritance.
658
///
659
/// Examples:
660
/// \code
661
///   class C {
662
///   public:    int a;
663
///   protected: int b;
664
///   private:   int c; // fieldDecl(isPrivate()) matches 'c'
665
///   };
666
/// \endcode
667
///
668
/// \code
669
///   struct Base {};
670
///   struct Derived1 : private Base {}; // matches 'Base'
671
///   class Derived2 : Base {}; // matches 'Base'
672
/// \endcode
673
AST_POLYMORPHIC_MATCHER(isPrivate,
674
                        AST_POLYMORPHIC_SUPPORTED_TYPES(Decl,
675
100
                                                        CXXBaseSpecifier)) {
676
100
  return getAccessSpecifier(Node) == AS_private;
677
100
}
Unexecuted instantiation: clang::ast_matchers::internal::matcher_isPrivateMatcher<clang::Decl>::matches(clang::Decl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
clang::ast_matchers::internal::matcher_isPrivateMatcher<clang::CXXBaseSpecifier>::matches(clang::CXXBaseSpecifier const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
675
100
                                                        CXXBaseSpecifier)) {
676
100
  return getAccessSpecifier(Node) == AS_private;
677
100
}
678
679
/// Matches non-static data members that are bit-fields.
680
///
681
/// Given
682
/// \code
683
///   class C {
684
///     int a : 2;
685
///     int b;
686
///   };
687
/// \endcode
688
/// fieldDecl(isBitField())
689
///   matches 'int a;' but not 'int b;'.
690
168
AST_MATCHER(FieldDecl, isBitField) {
691
168
  return Node.isBitField();
692
168
}
693
694
/// Matches non-static data members that are bit-fields of the specified
695
/// bit width.
696
///
697
/// Given
698
/// \code
699
///   class C {
700
///     int a : 2;
701
///     int b : 4;
702
///     int c : 2;
703
///   };
704
/// \endcode
705
/// fieldDecl(hasBitWidth(2))
706
///   matches 'int a;' and 'int c;' but not 'int b;'.
707
56
AST_MATCHER_P(FieldDecl, hasBitWidth, unsigned, Width) {
708
56
  return Node.isBitField() &&
709
56
         Node.getBitWidthValue(Finder->getASTContext()) == Width;
710
56
}
711
712
/// Matches non-static data members that have an in-class initializer.
713
///
714
/// Given
715
/// \code
716
///   class C {
717
///     int a = 2;
718
///     int b = 3;
719
///     int c;
720
///   };
721
/// \endcode
722
/// fieldDecl(hasInClassInitializer(integerLiteral(equals(2))))
723
///   matches 'int a;' but not 'int b;'.
724
/// fieldDecl(hasInClassInitializer(anything()))
725
///   matches 'int a;' and 'int b;' but not 'int c;'.
726
AST_MATCHER_P(FieldDecl, hasInClassInitializer, internal::Matcher<Expr>,
727
80
              InnerMatcher) {
728
80
  const Expr *Initializer = Node.getInClassInitializer();
729
80
  return (Initializer != nullptr &&
730
80
          
InnerMatcher.matches(*Initializer, Finder, Builder)40
);
731
80
}
732
733
/// Determines whether the function is "main", which is the entry point
734
/// into an executable program.
735
263
AST_MATCHER(FunctionDecl, isMain) {
736
263
  return Node.isMain();
737
263
}
738
739
/// Matches the specialized template of a specialization declaration.
740
///
741
/// Given
742
/// \code
743
///   template<typename T> class A {}; #1
744
///   template<> class A<int> {}; #2
745
/// \endcode
746
/// classTemplateSpecializationDecl(hasSpecializedTemplate(classTemplateDecl()))
747
///   matches '#2' with classTemplateDecl() matching the class template
748
///   declaration of 'A' at #1.
749
AST_MATCHER_P(ClassTemplateSpecializationDecl, hasSpecializedTemplate,
750
10
              internal::Matcher<ClassTemplateDecl>, InnerMatcher) {
751
10
  const ClassTemplateDecl* Decl = Node.getSpecializedTemplate();
752
10
  return (Decl != nullptr &&
753
10
          InnerMatcher.matches(*Decl, Finder, Builder));
754
10
}
755
756
/// Matches an entity that has been implicitly added by the compiler (e.g.
757
/// implicit default/copy constructors).
758
AST_POLYMORPHIC_MATCHER(isImplicit,
759
4
                        AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Attr)) {
760
4
  return Node.isImplicit();
761
4
}
Unexecuted instantiation: clang::ast_matchers::internal::matcher_isImplicitMatcher<clang::Decl>::matches(clang::Decl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
clang::ast_matchers::internal::matcher_isImplicitMatcher<clang::Attr>::matches(clang::Attr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
759
4
                        AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Attr)) {
760
4
  return Node.isImplicit();
761
4
}
762
763
/// Matches classTemplateSpecializations, templateSpecializationType and
764
/// functionDecl that have at least one TemplateArgument matching the given
765
/// InnerMatcher.
766
///
767
/// Given
768
/// \code
769
///   template<typename T> class A {};
770
///   template<> class A<double> {};
771
///   A<int> a;
772
///
773
///   template<typename T> f() {};
774
///   void func() { f<int>(); };
775
/// \endcode
776
///
777
/// \endcode
778
/// classTemplateSpecializationDecl(hasAnyTemplateArgument(
779
///     refersToType(asString("int"))))
780
///   matches the specialization \c A<int>
781
///
782
/// functionDecl(hasAnyTemplateArgument(refersToType(asString("int"))))
783
///   matches the specialization \c f<int>
784
AST_POLYMORPHIC_MATCHER_P(
785
    hasAnyTemplateArgument,
786
    AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
787
                                    TemplateSpecializationType,
788
                                    FunctionDecl),
789
172
    internal::Matcher<TemplateArgument>, InnerMatcher) {
790
172
  ArrayRef<TemplateArgument> List =
791
172
      internal::getTemplateSpecializationArgs(Node);
792
172
  return matchesFirstInRange(InnerMatcher, List.begin(), List.end(), Finder,
793
172
                             Builder) != List.end();
794
172
}
clang::ast_matchers::internal::matcher_hasAnyTemplateArgument0Matcher<clang::ClassTemplateSpecializationDecl, clang::ast_matchers::internal::Matcher<clang::TemplateArgument> >::matches(clang::ClassTemplateSpecializationDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
789
160
    internal::Matcher<TemplateArgument>, InnerMatcher) {
790
160
  ArrayRef<TemplateArgument> List =
791
160
      internal::getTemplateSpecializationArgs(Node);
792
160
  return matchesFirstInRange(InnerMatcher, List.begin(), List.end(), Finder,
793
160
                             Builder) != List.end();
794
160
}
clang::ast_matchers::internal::matcher_hasAnyTemplateArgument0Matcher<clang::TemplateSpecializationType, clang::ast_matchers::internal::Matcher<clang::TemplateArgument> >::matches(clang::TemplateSpecializationType const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
789
6
    internal::Matcher<TemplateArgument>, InnerMatcher) {
790
6
  ArrayRef<TemplateArgument> List =
791
6
      internal::getTemplateSpecializationArgs(Node);
792
6
  return matchesFirstInRange(InnerMatcher, List.begin(), List.end(), Finder,
793
6
                             Builder) != List.end();
794
6
}
clang::ast_matchers::internal::matcher_hasAnyTemplateArgument0Matcher<clang::FunctionDecl, clang::ast_matchers::internal::Matcher<clang::TemplateArgument> >::matches(clang::FunctionDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
789
6
    internal::Matcher<TemplateArgument>, InnerMatcher) {
790
6
  ArrayRef<TemplateArgument> List =
791
6
      internal::getTemplateSpecializationArgs(Node);
792
6
  return matchesFirstInRange(InnerMatcher, List.begin(), List.end(), Finder,
793
6
                             Builder) != List.end();
794
6
}
795
796
/// Causes all nested matchers to be matched with the specified traversal kind.
797
///
798
/// Given
799
/// \code
800
///   void foo()
801
///   {
802
///       int i = 3.0;
803
///   }
804
/// \endcode
805
/// The matcher
806
/// \code
807
///   traverse(TK_IgnoreUnlessSpelledInSource,
808
///     varDecl(hasInitializer(floatLiteral().bind("init")))
809
///   )
810
/// \endcode
811
/// matches the variable declaration with "init" bound to the "3.0".
812
template <typename T>
813
internal::Matcher<T> traverse(TraversalKind TK,
814
491
                              const internal::Matcher<T> &InnerMatcher) {
815
491
  return internal::DynTypedMatcher::constructRestrictedWrapper(
816
491
             new internal::TraversalMatcher<T>(TK, InnerMatcher),
817
491
             InnerMatcher.getID().first)
818
491
      .template unconditionalConvertTo<T>();
819
491
}
clang::ast_matchers::internal::Matcher<clang::Decl> clang::ast_matchers::traverse<clang::Decl>(clang::TraversalKind, clang::ast_matchers::internal::Matcher<clang::Decl> const&)
Line
Count
Source
814
6
                              const internal::Matcher<T> &InnerMatcher) {
815
6
  return internal::DynTypedMatcher::constructRestrictedWrapper(
816
6
             new internal::TraversalMatcher<T>(TK, InnerMatcher),
817
6
             InnerMatcher.getID().first)
818
6
      .template unconditionalConvertTo<T>();
819
6
}
clang::ast_matchers::internal::Matcher<clang::Stmt> clang::ast_matchers::traverse<clang::Stmt>(clang::TraversalKind, clang::ast_matchers::internal::Matcher<clang::Stmt> const&)
Line
Count
Source
814
485
                              const internal::Matcher<T> &InnerMatcher) {
815
485
  return internal::DynTypedMatcher::constructRestrictedWrapper(
816
485
             new internal::TraversalMatcher<T>(TK, InnerMatcher),
817
485
             InnerMatcher.getID().first)
818
485
      .template unconditionalConvertTo<T>();
819
485
}
820
821
template <typename T>
822
internal::BindableMatcher<T>
823
894
traverse(TraversalKind TK, const internal::BindableMatcher<T> &InnerMatcher) {
824
894
  return internal::BindableMatcher<T>(
825
894
      internal::DynTypedMatcher::constructRestrictedWrapper(
826
894
          new internal::TraversalMatcher<T>(TK, InnerMatcher),
827
894
          InnerMatcher.getID().first)
828
894
          .template unconditionalConvertTo<T>());
829
894
}
830
831
template <typename... T>
832
internal::TraversalWrapper<internal::VariadicOperatorMatcher<T...>>
833
traverse(TraversalKind TK,
834
         const internal::VariadicOperatorMatcher<T...> &InnerMatcher) {
835
  return internal::TraversalWrapper<internal::VariadicOperatorMatcher<T...>>(
836
      TK, InnerMatcher);
837
}
838
839
template <template <typename ToArg, typename FromArg> class ArgumentAdapterT,
840
          typename T, typename ToTypes>
841
internal::TraversalWrapper<
842
    internal::ArgumentAdaptingMatcherFuncAdaptor<ArgumentAdapterT, T, ToTypes>>
843
traverse(TraversalKind TK, const internal::ArgumentAdaptingMatcherFuncAdaptor<
844
                               ArgumentAdapterT, T, ToTypes> &InnerMatcher) {
845
  return internal::TraversalWrapper<
846
      internal::ArgumentAdaptingMatcherFuncAdaptor<ArgumentAdapterT, T,
847
                                                   ToTypes>>(TK, InnerMatcher);
848
}
849
850
template <template <typename T, typename... P> class MatcherT, typename... P,
851
          typename ReturnTypesF>
852
internal::TraversalWrapper<
853
    internal::PolymorphicMatcher<MatcherT, ReturnTypesF, P...>>
854
traverse(TraversalKind TK,
855
         const internal::PolymorphicMatcher<MatcherT, ReturnTypesF, P...>
856
             &InnerMatcher) {
857
  return internal::TraversalWrapper<
858
      internal::PolymorphicMatcher<MatcherT, ReturnTypesF, P...>>(TK,
859
                                                                  InnerMatcher);
860
}
861
862
template <typename... T>
863
internal::Matcher<typename internal::GetClade<T...>::Type>
864
traverse(TraversalKind TK, const internal::MapAnyOfHelper<T...> &InnerMatcher) {
865
  return traverse(TK, InnerMatcher.with());
866
}
867
868
/// Matches expressions that match InnerMatcher after any implicit AST
869
/// nodes are stripped off.
870
///
871
/// Parentheses and explicit casts are not discarded.
872
/// Given
873
/// \code
874
///   class C {};
875
///   C a = C();
876
///   C b;
877
///   C c = b;
878
/// \endcode
879
/// The matchers
880
/// \code
881
///    varDecl(hasInitializer(ignoringImplicit(cxxConstructExpr())))
882
/// \endcode
883
/// would match the declarations for a, b, and c.
884
/// While
885
/// \code
886
///    varDecl(hasInitializer(cxxConstructExpr()))
887
/// \endcode
888
/// only match the declarations for b and c.
889
AST_MATCHER_P(Expr, ignoringImplicit, internal::Matcher<Expr>,
890
179
              InnerMatcher) {
891
179
  return InnerMatcher.matches(*Node.IgnoreImplicit(), Finder, Builder);
892
179
}
893
894
/// Matches expressions that match InnerMatcher after any implicit casts
895
/// are stripped off.
896
///
897
/// Parentheses and explicit casts are not discarded.
898
/// Given
899
/// \code
900
///   int arr[5];
901
///   int a = 0;
902
///   char b = 0;
903
///   const int c = a;
904
///   int *d = arr;
905
///   long e = (long) 0l;
906
/// \endcode
907
/// The matchers
908
/// \code
909
///    varDecl(hasInitializer(ignoringImpCasts(integerLiteral())))
910
///    varDecl(hasInitializer(ignoringImpCasts(declRefExpr())))
911
/// \endcode
912
/// would match the declarations for a, b, c, and d, but not e.
913
/// While
914
/// \code
915
///    varDecl(hasInitializer(integerLiteral()))
916
///    varDecl(hasInitializer(declRefExpr()))
917
/// \endcode
918
/// only match the declarations for a.
919
AST_MATCHER_P(Expr, ignoringImpCasts,
920
355
              internal::Matcher<Expr>, InnerMatcher) {
921
355
  return InnerMatcher.matches(*Node.IgnoreImpCasts(), Finder, Builder);
922
355
}
923
924
/// Matches expressions that match InnerMatcher after parentheses and
925
/// casts are stripped off.
926
///
927
/// Implicit and non-C Style casts are also discarded.
928
/// Given
929
/// \code
930
///   int a = 0;
931
///   char b = (0);
932
///   void* c = reinterpret_cast<char*>(0);
933
///   char d = char(0);
934
/// \endcode
935
/// The matcher
936
///    varDecl(hasInitializer(ignoringParenCasts(integerLiteral())))
937
/// would match the declarations for a, b, c, and d.
938
/// while
939
///    varDecl(hasInitializer(integerLiteral()))
940
/// only match the declaration for a.
941
90
AST_MATCHER_P(Expr, ignoringParenCasts, internal::Matcher<Expr>, InnerMatcher) {
942
90
  return InnerMatcher.matches(*Node.IgnoreParenCasts(), Finder, Builder);
943
90
}
944
945
/// Matches expressions that match InnerMatcher after implicit casts and
946
/// parentheses are stripped off.
947
///
948
/// Explicit casts are not discarded.
949
/// Given
950
/// \code
951
///   int arr[5];
952
///   int a = 0;
953
///   char b = (0);
954
///   const int c = a;
955
///   int *d = (arr);
956
///   long e = ((long) 0l);
957
/// \endcode
958
/// The matchers
959
///    varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral())))
960
///    varDecl(hasInitializer(ignoringParenImpCasts(declRefExpr())))
961
/// would match the declarations for a, b, c, and d, but not e.
962
/// while
963
///    varDecl(hasInitializer(integerLiteral()))
964
///    varDecl(hasInitializer(declRefExpr()))
965
/// would only match the declaration for a.
966
AST_MATCHER_P(Expr, ignoringParenImpCasts,
967
3.00k
              internal::Matcher<Expr>, InnerMatcher) {
968
3.00k
  return InnerMatcher.matches(*Node.IgnoreParenImpCasts(), Finder, Builder);
969
3.00k
}
970
971
/// Matches types that match InnerMatcher after any parens are stripped.
972
///
973
/// Given
974
/// \code
975
///   void (*fp)(void);
976
/// \endcode
977
/// The matcher
978
/// \code
979
///   varDecl(hasType(pointerType(pointee(ignoringParens(functionType())))))
980
/// \endcode
981
/// would match the declaration for fp.
982
AST_MATCHER_P_OVERLOAD(QualType, ignoringParens, internal::Matcher<QualType>,
983
70
                       InnerMatcher, 0) {
984
70
  return InnerMatcher.matches(Node.IgnoreParens(), Finder, Builder);
985
70
}
986
987
/// Overload \c ignoringParens for \c Expr.
988
///
989
/// Given
990
/// \code
991
///   const char* str = ("my-string");
992
/// \endcode
993
/// The matcher
994
/// \code
995
///   implicitCastExpr(hasSourceExpression(ignoringParens(stringLiteral())))
996
/// \endcode
997
/// would match the implicit cast resulting from the assignment.
998
AST_MATCHER_P_OVERLOAD(Expr, ignoringParens, internal::Matcher<Expr>,
999
17.2k
                       InnerMatcher, 1) {
1000
17.2k
  const Expr *E = Node.IgnoreParens();
1001
17.2k
  return InnerMatcher.matches(*E, Finder, Builder);
1002
17.2k
}
1003
1004
/// Matches expressions that are instantiation-dependent even if it is
1005
/// neither type- nor value-dependent.
1006
///
1007
/// In the following example, the expression sizeof(sizeof(T() + T()))
1008
/// is instantiation-dependent (since it involves a template parameter T),
1009
/// but is neither type- nor value-dependent, since the type of the inner
1010
/// sizeof is known (std::size_t) and therefore the size of the outer
1011
/// sizeof is known.
1012
/// \code
1013
///   template<typename T>
1014
///   void f(T x, T y) { sizeof(sizeof(T() + T()); }
1015
/// \endcode
1016
/// expr(isInstantiationDependent()) matches sizeof(sizeof(T() + T())
1017
100
AST_MATCHER(Expr, isInstantiationDependent) {
1018
100
  return Node.isInstantiationDependent();
1019
100
}
1020
1021
/// Matches expressions that are type-dependent because the template type
1022
/// is not yet instantiated.
1023
///
1024
/// For example, the expressions "x" and "x + y" are type-dependent in
1025
/// the following code, but "y" is not type-dependent:
1026
/// \code
1027
///   template<typename T>
1028
///   void add(T x, int y) {
1029
///     x + y;
1030
///   }
1031
/// \endcode
1032
/// expr(isTypeDependent()) matches x + y
1033
321
AST_MATCHER(Expr, isTypeDependent) { return Node.isTypeDependent(); }
1034
1035
/// Matches expression that are value-dependent because they contain a
1036
/// non-type template parameter.
1037
///
1038
/// For example, the array bound of "Chars" in the following example is
1039
/// value-dependent.
1040
/// \code
1041
///   template<int Size> int f() { return Size; }
1042
/// \endcode
1043
/// expr(isValueDependent()) matches return Size
1044
100
AST_MATCHER(Expr, isValueDependent) { return Node.isValueDependent(); }
1045
1046
/// Matches classTemplateSpecializations, templateSpecializationType and
1047
/// functionDecl where the n'th TemplateArgument matches the given InnerMatcher.
1048
///
1049
/// Given
1050
/// \code
1051
///   template<typename T, typename U> class A {};
1052
///   A<bool, int> b;
1053
///   A<int, bool> c;
1054
///
1055
///   template<typename T> void f() {}
1056
///   void func() { f<int>(); };
1057
/// \endcode
1058
/// classTemplateSpecializationDecl(hasTemplateArgument(
1059
///     1, refersToType(asString("int"))))
1060
///   matches the specialization \c A<bool, int>
1061
///
1062
/// functionDecl(hasTemplateArgument(0, refersToType(asString("int"))))
1063
///   matches the specialization \c f<int>
1064
AST_POLYMORPHIC_MATCHER_P2(
1065
    hasTemplateArgument,
1066
    AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
1067
                                    TemplateSpecializationType,
1068
                                    FunctionDecl),
1069
104
    unsigned, N, internal::Matcher<TemplateArgument>, InnerMatcher) {
1070
104
  ArrayRef<TemplateArgument> List =
1071
104
      internal::getTemplateSpecializationArgs(Node);
1072
104
  if (List.size() <= N)
1073
10
    return false;
1074
94
  return InnerMatcher.matches(List[N], Finder, Builder);
1075
104
}
clang::ast_matchers::internal::matcher_hasTemplateArgument0Matcher<clang::ClassTemplateSpecializationDecl, unsigned int, clang::ast_matchers::internal::Matcher<clang::TemplateArgument> >::matches(clang::ClassTemplateSpecializationDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
1069
78
    unsigned, N, internal::Matcher<TemplateArgument>, InnerMatcher) {
1070
78
  ArrayRef<TemplateArgument> List =
1071
78
      internal::getTemplateSpecializationArgs(Node);
1072
78
  if (List.size() <= N)
1073
0
    return false;
1074
78
  return InnerMatcher.matches(List[N], Finder, Builder);
1075
78
}
clang::ast_matchers::internal::matcher_hasTemplateArgument0Matcher<clang::TemplateSpecializationType, unsigned int, clang::ast_matchers::internal::Matcher<clang::TemplateArgument> >::matches(clang::TemplateSpecializationType const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
1069
4
    unsigned, N, internal::Matcher<TemplateArgument>, InnerMatcher) {
1070
4
  ArrayRef<TemplateArgument> List =
1071
4
      internal::getTemplateSpecializationArgs(Node);
1072
4
  if (List.size() <= N)
1073
0
    return false;
1074
4
  return InnerMatcher.matches(List[N], Finder, Builder);
1075
4
}
clang::ast_matchers::internal::matcher_hasTemplateArgument0Matcher<clang::FunctionDecl, unsigned int, clang::ast_matchers::internal::Matcher<clang::TemplateArgument> >::matches(clang::FunctionDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
1069
22
    unsigned, N, internal::Matcher<TemplateArgument>, InnerMatcher) {
1070
22
  ArrayRef<TemplateArgument> List =
1071
22
      internal::getTemplateSpecializationArgs(Node);
1072
22
  if (List.size() <= N)
1073
10
    return false;
1074
12
  return InnerMatcher.matches(List[N], Finder, Builder);
1075
22
}
1076
1077
/// Matches if the number of template arguments equals \p N.
1078
///
1079
/// Given
1080
/// \code
1081
///   template<typename T> struct C {};
1082
///   C<int> c;
1083
/// \endcode
1084
/// classTemplateSpecializationDecl(templateArgumentCountIs(1))
1085
///   matches C<int>.
1086
AST_POLYMORPHIC_MATCHER_P(
1087
    templateArgumentCountIs,
1088
    AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
1089
                                    TemplateSpecializationType),
1090
80
    unsigned, N) {
1091
80
  return internal::getTemplateSpecializationArgs(Node).size() == N;
1092
80
}
clang::ast_matchers::internal::matcher_templateArgumentCountIs0Matcher<clang::ClassTemplateSpecializationDecl, unsigned int>::matches(clang::ClassTemplateSpecializationDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
1090
40
    unsigned, N) {
1091
40
  return internal::getTemplateSpecializationArgs(Node).size() == N;
1092
40
}
clang::ast_matchers::internal::matcher_templateArgumentCountIs0Matcher<clang::TemplateSpecializationType, unsigned int>::matches(clang::TemplateSpecializationType const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
1090
40
    unsigned, N) {
1091
40
  return internal::getTemplateSpecializationArgs(Node).size() == N;
1092
40
}
1093
1094
/// Matches a TemplateArgument that refers to a certain type.
1095
///
1096
/// Given
1097
/// \code
1098
///   struct X {};
1099
///   template<typename T> struct A {};
1100
///   A<X> a;
1101
/// \endcode
1102
/// classTemplateSpecializationDecl(hasAnyTemplateArgument(
1103
///     refersToType(class(hasName("X")))))
1104
///   matches the specialization \c A<X>
1105
AST_MATCHER_P(TemplateArgument, refersToType,
1106
104
              internal::Matcher<QualType>, InnerMatcher) {
1107
104
  if (Node.getKind() != TemplateArgument::Type)
1108
0
    return false;
1109
104
  return InnerMatcher.matches(Node.getAsType(), Finder, Builder);
1110
104
}
1111
1112
/// Matches a TemplateArgument that refers to a certain template.
1113
///
1114
/// Given
1115
/// \code
1116
///   template<template <typename> class S> class X {};
1117
///   template<typename T> class Y {};
1118
///   X<Y> xi;
1119
/// \endcode
1120
/// classTemplateSpecializationDecl(hasAnyTemplateArgument(
1121
///     refersToTemplate(templateName())))
1122
///   matches the specialization \c X<Y>
1123
AST_MATCHER_P(TemplateArgument, refersToTemplate,
1124
2
              internal::Matcher<TemplateName>, InnerMatcher) {
1125
2
  if (Node.getKind() != TemplateArgument::Template)
1126
0
    return false;
1127
2
  return InnerMatcher.matches(Node.getAsTemplate(), Finder, Builder);
1128
2
}
1129
1130
/// Matches a canonical TemplateArgument that refers to a certain
1131
/// declaration.
1132
///
1133
/// Given
1134
/// \code
1135
///   struct B { int next; };
1136
///   template<int(B::*next_ptr)> struct A {};
1137
///   A<&B::next> a;
1138
/// \endcode
1139
/// classTemplateSpecializationDecl(hasAnyTemplateArgument(
1140
///     refersToDeclaration(fieldDecl(hasName("next")))))
1141
///   matches the specialization \c A<&B::next> with \c fieldDecl(...) matching
1142
///     \c B::next
1143
AST_MATCHER_P(TemplateArgument, refersToDeclaration,
1144
6
              internal::Matcher<Decl>, InnerMatcher) {
1145
6
  if (Node.getKind() == TemplateArgument::Declaration)
1146
2
    return InnerMatcher.matches(*Node.getAsDecl(), Finder, Builder);
1147
4
  return false;
1148
6
}
1149
1150
/// Matches a sugar TemplateArgument that refers to a certain expression.
1151
///
1152
/// Given
1153
/// \code
1154
///   struct B { int next; };
1155
///   template<int(B::*next_ptr)> struct A {};
1156
///   A<&B::next> a;
1157
/// \endcode
1158
/// templateSpecializationType(hasAnyTemplateArgument(
1159
///   isExpr(hasDescendant(declRefExpr(to(fieldDecl(hasName("next"))))))))
1160
///   matches the specialization \c A<&B::next> with \c fieldDecl(...) matching
1161
///     \c B::next
1162
2
AST_MATCHER_P(TemplateArgument, isExpr, internal::Matcher<Expr>, InnerMatcher) {
1163
2
  if (Node.getKind() == TemplateArgument::Expression)
1164
2
    return InnerMatcher.matches(*Node.getAsExpr(), Finder, Builder);
1165
0
  return false;
1166
2
}
1167
1168
/// Matches a TemplateArgument that is an integral value.
1169
///
1170
/// Given
1171
/// \code
1172
///   template<int T> struct C {};
1173
///   C<42> c;
1174
/// \endcode
1175
/// classTemplateSpecializationDecl(
1176
///   hasAnyTemplateArgument(isIntegral()))
1177
///   matches the implicit instantiation of C in C<42>
1178
///   with isIntegral() matching 42.
1179
40
AST_MATCHER(TemplateArgument, isIntegral) {
1180
40
  return Node.getKind() == TemplateArgument::Integral;
1181
40
}
1182
1183
/// Matches a TemplateArgument that refers to an integral type.
1184
///
1185
/// Given
1186
/// \code
1187
///   template<int T> struct C {};
1188
///   C<42> c;
1189
/// \endcode
1190
/// classTemplateSpecializationDecl(
1191
///   hasAnyTemplateArgument(refersToIntegralType(asString("int"))))
1192
///   matches the implicit instantiation of C in C<42>.
1193
AST_MATCHER_P(TemplateArgument, refersToIntegralType,
1194
4
              internal::Matcher<QualType>, InnerMatcher) {
1195
4
  if (Node.getKind() != TemplateArgument::Integral)
1196
0
    return false;
1197
4
  return InnerMatcher.matches(Node.getIntegralType(), Finder, Builder);
1198
4
}
1199
1200
/// Matches a TemplateArgument of integral type with a given value.
1201
///
1202
/// Note that 'Value' is a string as the template argument's value is
1203
/// an arbitrary precision integer. 'Value' must be euqal to the canonical
1204
/// representation of that integral value in base 10.
1205
///
1206
/// Given
1207
/// \code
1208
///   template<int T> struct C {};
1209
///   C<42> c;
1210
/// \endcode
1211
/// classTemplateSpecializationDecl(
1212
///   hasAnyTemplateArgument(equalsIntegralValue("42")))
1213
///   matches the implicit instantiation of C in C<42>.
1214
AST_MATCHER_P(TemplateArgument, equalsIntegralValue,
1215
80
              std::string, Value) {
1216
80
  if (Node.getKind() != TemplateArgument::Integral)
1217
0
    return false;
1218
80
  return toString(Node.getAsIntegral(), 10) == Value;
1219
80
}
1220
1221
/// Matches an Objective-C autorelease pool statement.
1222
///
1223
/// Given
1224
/// \code
1225
///   @autoreleasepool {
1226
///     int x = 0;
1227
///   }
1228
/// \endcode
1229
/// autoreleasePoolStmt(stmt()) matches the declaration of "x"
1230
/// inside the autorelease pool.
1231
extern const internal::VariadicDynCastAllOfMatcher<Stmt,
1232
       ObjCAutoreleasePoolStmt> autoreleasePoolStmt;
1233
1234
/// Matches any value declaration.
1235
///
1236
/// Example matches A, B, C and F
1237
/// \code
1238
///   enum X { A, B, C };
1239
///   void F();
1240
/// \endcode
1241
extern const internal::VariadicDynCastAllOfMatcher<Decl, ValueDecl> valueDecl;
1242
1243
/// Matches C++ constructor declarations.
1244
///
1245
/// Example matches Foo::Foo() and Foo::Foo(int)
1246
/// \code
1247
///   class Foo {
1248
///    public:
1249
///     Foo();
1250
///     Foo(int);
1251
///     int DoSomething();
1252
///   };
1253
/// \endcode
1254
extern const internal::VariadicDynCastAllOfMatcher<Decl, CXXConstructorDecl>
1255
    cxxConstructorDecl;
1256
1257
/// Matches explicit C++ destructor declarations.
1258
///
1259
/// Example matches Foo::~Foo()
1260
/// \code
1261
///   class Foo {
1262
///    public:
1263
///     virtual ~Foo();
1264
///   };
1265
/// \endcode
1266
extern const internal::VariadicDynCastAllOfMatcher<Decl, CXXDestructorDecl>
1267
    cxxDestructorDecl;
1268
1269
/// Matches enum declarations.
1270
///
1271
/// Example matches X
1272
/// \code
1273
///   enum X {
1274
///     A, B, C
1275
///   };
1276
/// \endcode
1277
extern const internal::VariadicDynCastAllOfMatcher<Decl, EnumDecl> enumDecl;
1278
1279
/// Matches enum constants.
1280
///
1281
/// Example matches A, B, C
1282
/// \code
1283
///   enum X {
1284
///     A, B, C
1285
///   };
1286
/// \endcode
1287
extern const internal::VariadicDynCastAllOfMatcher<Decl, EnumConstantDecl>
1288
    enumConstantDecl;
1289
1290
/// Matches tag declarations.
1291
///
1292
/// Example matches X, Z, U, S, E
1293
/// \code
1294
///   class X;
1295
///   template<class T> class Z {};
1296
///   struct S {};
1297
///   union U {};
1298
///   enum E {
1299
///     A, B, C
1300
///   };
1301
/// \endcode
1302
extern const internal::VariadicDynCastAllOfMatcher<Decl, TagDecl> tagDecl;
1303
1304
/// Matches method declarations.
1305
///
1306
/// Example matches y
1307
/// \code
1308
///   class X { void y(); };
1309
/// \endcode
1310
extern const internal::VariadicDynCastAllOfMatcher<Decl, CXXMethodDecl>
1311
    cxxMethodDecl;
1312
1313
/// Matches conversion operator declarations.
1314
///
1315
/// Example matches the operator.
1316
/// \code
1317
///   class X { operator int() const; };
1318
/// \endcode
1319
extern const internal::VariadicDynCastAllOfMatcher<Decl, CXXConversionDecl>
1320
    cxxConversionDecl;
1321
1322
/// Matches user-defined and implicitly generated deduction guide.
1323
///
1324
/// Example matches the deduction guide.
1325
/// \code
1326
///   template<typename T>
1327
///   class X { X(int) };
1328
///   X(int) -> X<int>;
1329
/// \endcode
1330
extern const internal::VariadicDynCastAllOfMatcher<Decl, CXXDeductionGuideDecl>
1331
    cxxDeductionGuideDecl;
1332
1333
/// Matches variable declarations.
1334
///
1335
/// Note: this does not match declarations of member variables, which are
1336
/// "field" declarations in Clang parlance.
1337
///
1338
/// Example matches a
1339
/// \code
1340
///   int a;
1341
/// \endcode
1342
extern const internal::VariadicDynCastAllOfMatcher<Decl, VarDecl> varDecl;
1343
1344
/// Matches field declarations.
1345
///
1346
/// Given
1347
/// \code
1348
///   class X { int m; };
1349
/// \endcode
1350
/// fieldDecl()
1351
///   matches 'm'.
1352
extern const internal::VariadicDynCastAllOfMatcher<Decl, FieldDecl> fieldDecl;
1353
1354
/// Matches indirect field declarations.
1355
///
1356
/// Given
1357
/// \code
1358
///   struct X { struct { int a; }; };
1359
/// \endcode
1360
/// indirectFieldDecl()
1361
///   matches 'a'.
1362
extern const internal::VariadicDynCastAllOfMatcher<Decl, IndirectFieldDecl>
1363
    indirectFieldDecl;
1364
1365
/// Matches function declarations.
1366
///
1367
/// Example matches f
1368
/// \code
1369
///   void f();
1370
/// \endcode
1371
extern const internal::VariadicDynCastAllOfMatcher<Decl, FunctionDecl>
1372
    functionDecl;
1373
1374
/// Matches C++ function template declarations.
1375
///
1376
/// Example matches f
1377
/// \code
1378
///   template<class T> void f(T t) {}
1379
/// \endcode
1380
extern const internal::VariadicDynCastAllOfMatcher<Decl, FunctionTemplateDecl>
1381
    functionTemplateDecl;
1382
1383
/// Matches friend declarations.
1384
///
1385
/// Given
1386
/// \code
1387
///   class X { friend void foo(); };
1388
/// \endcode
1389
/// friendDecl()
1390
///   matches 'friend void foo()'.
1391
extern const internal::VariadicDynCastAllOfMatcher<Decl, FriendDecl> friendDecl;
1392
1393
/// Matches statements.
1394
///
1395
/// Given
1396
/// \code
1397
///   { ++a; }
1398
/// \endcode
1399
/// stmt()
1400
///   matches both the compound statement '{ ++a; }' and '++a'.
1401
extern const internal::VariadicAllOfMatcher<Stmt> stmt;
1402
1403
/// Matches declaration statements.
1404
///
1405
/// Given
1406
/// \code
1407
///   int a;
1408
/// \endcode
1409
/// declStmt()
1410
///   matches 'int a'.
1411
extern const internal::VariadicDynCastAllOfMatcher<Stmt, DeclStmt> declStmt;
1412
1413
/// Matches member expressions.
1414
///
1415
/// Given
1416
/// \code
1417
///   class Y {
1418
///     void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; }
1419
///     int a; static int b;
1420
///   };
1421
/// \endcode
1422
/// memberExpr()
1423
///   matches this->x, x, y.x, a, this->b
1424
extern const internal::VariadicDynCastAllOfMatcher<Stmt, MemberExpr> memberExpr;
1425
1426
/// Matches unresolved member expressions.
1427
///
1428
/// Given
1429
/// \code
1430
///   struct X {
1431
///     template <class T> void f();
1432
///     void g();
1433
///   };
1434
///   template <class T> void h() { X x; x.f<T>(); x.g(); }
1435
/// \endcode
1436
/// unresolvedMemberExpr()
1437
///   matches x.f<T>
1438
extern const internal::VariadicDynCastAllOfMatcher<Stmt, UnresolvedMemberExpr>
1439
    unresolvedMemberExpr;
1440
1441
/// Matches member expressions where the actual member referenced could not be
1442
/// resolved because the base expression or the member name was dependent.
1443
///
1444
/// Given
1445
/// \code
1446
///   template <class T> void f() { T t; t.g(); }
1447
/// \endcode
1448
/// cxxDependentScopeMemberExpr()
1449
///   matches t.g
1450
extern const internal::VariadicDynCastAllOfMatcher<Stmt,
1451
                                                   CXXDependentScopeMemberExpr>
1452
    cxxDependentScopeMemberExpr;
1453
1454
/// Matches call expressions.
1455
///
1456
/// Example matches x.y() and y()
1457
/// \code
1458
///   X x;
1459
///   x.y();
1460
///   y();
1461
/// \endcode
1462
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CallExpr> callExpr;
1463
1464
/// Matches call expressions which were resolved using ADL.
1465
///
1466
/// Example matches y(x) but not y(42) or NS::y(x).
1467
/// \code
1468
///   namespace NS {
1469
///     struct X {};
1470
///     void y(X);
1471
///   }
1472
///
1473
///   void y(...);
1474
///
1475
///   void test() {
1476
///     NS::X x;
1477
///     y(x); // Matches
1478
///     NS::y(x); // Doesn't match
1479
///     y(42); // Doesn't match
1480
///     using NS::y;
1481
///     y(x); // Found by both unqualified lookup and ADL, doesn't match
1482
//    }
1483
/// \endcode
1484
200
AST_MATCHER(CallExpr, usesADL) { return Node.usesADL(); }
1485
1486
/// Matches lambda expressions.
1487
///
1488
/// Example matches [&](){return 5;}
1489
/// \code
1490
///   [&](){return 5;}
1491
/// \endcode
1492
extern const internal::VariadicDynCastAllOfMatcher<Stmt, LambdaExpr> lambdaExpr;
1493
1494
/// Matches member call expressions.
1495
///
1496
/// Example matches x.y()
1497
/// \code
1498
///   X x;
1499
///   x.y();
1500
/// \endcode
1501
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXMemberCallExpr>
1502
    cxxMemberCallExpr;
1503
1504
/// Matches ObjectiveC Message invocation expressions.
1505
///
1506
/// The innermost message send invokes the "alloc" class method on the
1507
/// NSString class, while the outermost message send invokes the
1508
/// "initWithString" instance method on the object returned from
1509
/// NSString's "alloc". This matcher should match both message sends.
1510
/// \code
1511
///   [[NSString alloc] initWithString:@"Hello"]
1512
/// \endcode
1513
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCMessageExpr>
1514
    objcMessageExpr;
1515
1516
/// Matches Objective-C interface declarations.
1517
///
1518
/// Example matches Foo
1519
/// \code
1520
///   @interface Foo
1521
///   @end
1522
/// \endcode
1523
extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCInterfaceDecl>
1524
    objcInterfaceDecl;
1525
1526
/// Matches Objective-C implementation declarations.
1527
///
1528
/// Example matches Foo
1529
/// \code
1530
///   @implementation Foo
1531
///   @end
1532
/// \endcode
1533
extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCImplementationDecl>
1534
    objcImplementationDecl;
1535
1536
/// Matches Objective-C protocol declarations.
1537
///
1538
/// Example matches FooDelegate
1539
/// \code
1540
///   @protocol FooDelegate
1541
///   @end
1542
/// \endcode
1543
extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCProtocolDecl>
1544
    objcProtocolDecl;
1545
1546
/// Matches Objective-C category declarations.
1547
///
1548
/// Example matches Foo (Additions)
1549
/// \code
1550
///   @interface Foo (Additions)
1551
///   @end
1552
/// \endcode
1553
extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCCategoryDecl>
1554
    objcCategoryDecl;
1555
1556
/// Matches Objective-C category definitions.
1557
///
1558
/// Example matches Foo (Additions)
1559
/// \code
1560
///   @implementation Foo (Additions)
1561
///   @end
1562
/// \endcode
1563
extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCCategoryImplDecl>
1564
    objcCategoryImplDecl;
1565
1566
/// Matches Objective-C method declarations.
1567
///
1568
/// Example matches both declaration and definition of -[Foo method]
1569
/// \code
1570
///   @interface Foo
1571
///   - (void)method;
1572
///   @end
1573
///
1574
///   @implementation Foo
1575
///   - (void)method {}
1576
///   @end
1577
/// \endcode
1578
extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCMethodDecl>
1579
    objcMethodDecl;
1580
1581
/// Matches block declarations.
1582
///
1583
/// Example matches the declaration of the nameless block printing an input
1584
/// integer.
1585
///
1586
/// \code
1587
///   myFunc(^(int p) {
1588
///     printf("%d", p);
1589
///   })
1590
/// \endcode
1591
extern const internal::VariadicDynCastAllOfMatcher<Decl, BlockDecl>
1592
    blockDecl;
1593
1594
/// Matches Objective-C instance variable declarations.
1595
///
1596
/// Example matches _enabled
1597
/// \code
1598
///   @implementation Foo {
1599
///     BOOL _enabled;
1600
///   }
1601
///   @end
1602
/// \endcode
1603
extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCIvarDecl>
1604
    objcIvarDecl;
1605
1606
/// Matches Objective-C property declarations.
1607
///
1608
/// Example matches enabled
1609
/// \code
1610
///   @interface Foo
1611
///   @property BOOL enabled;
1612
///   @end
1613
/// \endcode
1614
extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCPropertyDecl>
1615
    objcPropertyDecl;
1616
1617
/// Matches Objective-C \@throw statements.
1618
///
1619
/// Example matches \@throw
1620
/// \code
1621
///   @throw obj;
1622
/// \endcode
1623
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtThrowStmt>
1624
    objcThrowStmt;
1625
1626
/// Matches Objective-C @try statements.
1627
///
1628
/// Example matches @try
1629
/// \code
1630
///   @try {}
1631
///   @catch (...) {}
1632
/// \endcode
1633
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtTryStmt>
1634
    objcTryStmt;
1635
1636
/// Matches Objective-C @catch statements.
1637
///
1638
/// Example matches @catch
1639
/// \code
1640
///   @try {}
1641
///   @catch (...) {}
1642
/// \endcode
1643
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtCatchStmt>
1644
    objcCatchStmt;
1645
1646
/// Matches Objective-C @finally statements.
1647
///
1648
/// Example matches @finally
1649
/// \code
1650
///   @try {}
1651
///   @finally {}
1652
/// \endcode
1653
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtFinallyStmt>
1654
    objcFinallyStmt;
1655
1656
/// Matches expressions that introduce cleanups to be run at the end
1657
/// of the sub-expression's evaluation.
1658
///
1659
/// Example matches std::string()
1660
/// \code
1661
///   const std::string str = std::string();
1662
/// \endcode
1663
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ExprWithCleanups>
1664
    exprWithCleanups;
1665
1666
/// Matches init list expressions.
1667
///
1668
/// Given
1669
/// \code
1670
///   int a[] = { 1, 2 };
1671
///   struct B { int x, y; };
1672
///   B b = { 5, 6 };
1673
/// \endcode
1674
/// initListExpr()
1675
///   matches "{ 1, 2 }" and "{ 5, 6 }"
1676
extern const internal::VariadicDynCastAllOfMatcher<Stmt, InitListExpr>
1677
    initListExpr;
1678
1679
/// Matches the syntactic form of init list expressions
1680
/// (if expression have it).
1681
AST_MATCHER_P(InitListExpr, hasSyntacticForm,
1682
12
              internal::Matcher<Expr>, InnerMatcher) {
1683
12
  const Expr *SyntForm = Node.getSyntacticForm();
1684
12
  return (SyntForm != nullptr &&
1685
12
          
InnerMatcher.matches(*SyntForm, Finder, Builder)4
);
1686
12
}
1687
1688
/// Matches C++ initializer list expressions.
1689
///
1690
/// Given
1691
/// \code
1692
///   std::vector<int> a({ 1, 2, 3 });
1693
///   std::vector<int> b = { 4, 5 };
1694
///   int c[] = { 6, 7 };
1695
///   std::pair<int, int> d = { 8, 9 };
1696
/// \endcode
1697
/// cxxStdInitializerListExpr()
1698
///   matches "{ 1, 2, 3 }" and "{ 4, 5 }"
1699
extern const internal::VariadicDynCastAllOfMatcher<Stmt,
1700
                                                   CXXStdInitializerListExpr>
1701
    cxxStdInitializerListExpr;
1702
1703
/// Matches implicit initializers of init list expressions.
1704
///
1705
/// Given
1706
/// \code
1707
///   point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 };
1708
/// \endcode
1709
/// implicitValueInitExpr()
1710
///   matches "[0].y" (implicitly)
1711
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ImplicitValueInitExpr>
1712
    implicitValueInitExpr;
1713
1714
/// Matches paren list expressions.
1715
/// ParenListExprs don't have a predefined type and are used for late parsing.
1716
/// In the final AST, they can be met in template declarations.
1717
///
1718
/// Given
1719
/// \code
1720
///   template<typename T> class X {
1721
///     void f() {
1722
///       X x(*this);
1723
///       int a = 0, b = 1; int i = (a, b);
1724
///     }
1725
///   };
1726
/// \endcode
1727
/// parenListExpr() matches "*this" but NOT matches (a, b) because (a, b)
1728
/// has a predefined type and is a ParenExpr, not a ParenListExpr.
1729
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ParenListExpr>
1730
    parenListExpr;
1731
1732
/// Matches substitutions of non-type template parameters.
1733
///
1734
/// Given
1735
/// \code
1736
///   template <int N>
1737
///   struct A { static const int n = N; };
1738
///   struct B : public A<42> {};
1739
/// \endcode
1740
/// substNonTypeTemplateParmExpr()
1741
///   matches "N" in the right-hand side of "static const int n = N;"
1742
extern const internal::VariadicDynCastAllOfMatcher<Stmt,
1743
                                                   SubstNonTypeTemplateParmExpr>
1744
    substNonTypeTemplateParmExpr;
1745
1746
/// Matches using declarations.
1747
///
1748
/// Given
1749
/// \code
1750
///   namespace X { int x; }
1751
///   using X::x;
1752
/// \endcode
1753
/// usingDecl()
1754
///   matches \code using X::x \endcode
1755
extern const internal::VariadicDynCastAllOfMatcher<Decl, UsingDecl> usingDecl;
1756
1757
/// Matches using-enum declarations.
1758
///
1759
/// Given
1760
/// \code
1761
///   namespace X { enum x {...}; }
1762
///   using enum X::x;
1763
/// \endcode
1764
/// usingEnumDecl()
1765
///   matches \code using enum X::x \endcode
1766
extern const internal::VariadicDynCastAllOfMatcher<Decl, UsingEnumDecl>
1767
    usingEnumDecl;
1768
1769
/// Matches using namespace declarations.
1770
///
1771
/// Given
1772
/// \code
1773
///   namespace X { int x; }
1774
///   using namespace X;
1775
/// \endcode
1776
/// usingDirectiveDecl()
1777
///   matches \code using namespace X \endcode
1778
extern const internal::VariadicDynCastAllOfMatcher<Decl, UsingDirectiveDecl>
1779
    usingDirectiveDecl;
1780
1781
/// Matches reference to a name that can be looked up during parsing
1782
/// but could not be resolved to a specific declaration.
1783
///
1784
/// Given
1785
/// \code
1786
///   template<typename T>
1787
///   T foo() { T a; return a; }
1788
///   template<typename T>
1789
///   void bar() {
1790
///     foo<T>();
1791
///   }
1792
/// \endcode
1793
/// unresolvedLookupExpr()
1794
///   matches \code foo<T>() \endcode
1795
extern const internal::VariadicDynCastAllOfMatcher<Stmt, UnresolvedLookupExpr>
1796
    unresolvedLookupExpr;
1797
1798
/// Matches unresolved using value declarations.
1799
///
1800
/// Given
1801
/// \code
1802
///   template<typename X>
1803
///   class C : private X {
1804
///     using X::x;
1805
///   };
1806
/// \endcode
1807
/// unresolvedUsingValueDecl()
1808
///   matches \code using X::x \endcode
1809
extern const internal::VariadicDynCastAllOfMatcher<Decl,
1810
                                                   UnresolvedUsingValueDecl>
1811
    unresolvedUsingValueDecl;
1812
1813
/// Matches unresolved using value declarations that involve the
1814
/// typename.
1815
///
1816
/// Given
1817
/// \code
1818
///   template <typename T>
1819
///   struct Base { typedef T Foo; };
1820
///
1821
///   template<typename T>
1822
///   struct S : private Base<T> {
1823
///     using typename Base<T>::Foo;
1824
///   };
1825
/// \endcode
1826
/// unresolvedUsingTypenameDecl()
1827
///   matches \code using Base<T>::Foo \endcode
1828
extern const internal::VariadicDynCastAllOfMatcher<Decl,
1829
                                                   UnresolvedUsingTypenameDecl>
1830
    unresolvedUsingTypenameDecl;
1831
1832
/// Matches a constant expression wrapper.
1833
///
1834
/// Example matches the constant in the case statement:
1835
///     (matcher = constantExpr())
1836
/// \code
1837
///   switch (a) {
1838
///   case 37: break;
1839
///   }
1840
/// \endcode
1841
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ConstantExpr>
1842
    constantExpr;
1843
1844
/// Matches parentheses used in expressions.
1845
///
1846
/// Example matches (foo() + 1)
1847
/// \code
1848
///   int foo() { return 1; }
1849
///   int a = (foo() + 1);
1850
/// \endcode
1851
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ParenExpr> parenExpr;
1852
1853
/// Matches constructor call expressions (including implicit ones).
1854
///
1855
/// Example matches string(ptr, n) and ptr within arguments of f
1856
///     (matcher = cxxConstructExpr())
1857
/// \code
1858
///   void f(const string &a, const string &b);
1859
///   char *ptr;
1860
///   int n;
1861
///   f(string(ptr, n), ptr);
1862
/// \endcode
1863
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXConstructExpr>
1864
    cxxConstructExpr;
1865
1866
/// Matches unresolved constructor call expressions.
1867
///
1868
/// Example matches T(t) in return statement of f
1869
///     (matcher = cxxUnresolvedConstructExpr())
1870
/// \code
1871
///   template <typename T>
1872
///   void f(const T& t) { return T(t); }
1873
/// \endcode
1874
extern const internal::VariadicDynCastAllOfMatcher<Stmt,
1875
                                                   CXXUnresolvedConstructExpr>
1876
    cxxUnresolvedConstructExpr;
1877
1878
/// Matches implicit and explicit this expressions.
1879
///
1880
/// Example matches the implicit this expression in "return i".
1881
///     (matcher = cxxThisExpr())
1882
/// \code
1883
/// struct foo {
1884
///   int i;
1885
///   int f() { return i; }
1886
/// };
1887
/// \endcode
1888
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThisExpr>
1889
    cxxThisExpr;
1890
1891
/// Matches nodes where temporaries are created.
1892
///
1893
/// Example matches FunctionTakesString(GetStringByValue())
1894
///     (matcher = cxxBindTemporaryExpr())
1895
/// \code
1896
///   FunctionTakesString(GetStringByValue());
1897
///   FunctionTakesStringByPointer(GetStringPointer());
1898
/// \endcode
1899
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXBindTemporaryExpr>
1900
    cxxBindTemporaryExpr;
1901
1902
/// Matches nodes where temporaries are materialized.
1903
///
1904
/// Example: Given
1905
/// \code
1906
///   struct T {void func();};
1907
///   T f();
1908
///   void g(T);
1909
/// \endcode
1910
/// materializeTemporaryExpr() matches 'f()' in these statements
1911
/// \code
1912
///   T u(f());
1913
///   g(f());
1914
///   f().func();
1915
/// \endcode
1916
/// but does not match
1917
/// \code
1918
///   f();
1919
/// \endcode
1920
extern const internal::VariadicDynCastAllOfMatcher<Stmt,
1921
                                                   MaterializeTemporaryExpr>
1922
    materializeTemporaryExpr;
1923
1924
/// Matches new expressions.
1925
///
1926
/// Given
1927
/// \code
1928
///   new X;
1929
/// \endcode
1930
/// cxxNewExpr()
1931
///   matches 'new X'.
1932
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNewExpr> cxxNewExpr;
1933
1934
/// Matches delete expressions.
1935
///
1936
/// Given
1937
/// \code
1938
///   delete X;
1939
/// \endcode
1940
/// cxxDeleteExpr()
1941
///   matches 'delete X'.
1942
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDeleteExpr>
1943
    cxxDeleteExpr;
1944
1945
/// Matches noexcept expressions.
1946
///
1947
/// Given
1948
/// \code
1949
///   bool a() noexcept;
1950
///   bool b() noexcept(true);
1951
///   bool c() noexcept(false);
1952
///   bool d() noexcept(noexcept(a()));
1953
///   bool e = noexcept(b()) || noexcept(c());
1954
/// \endcode
1955
/// cxxNoexceptExpr()
1956
///   matches `noexcept(a())`, `noexcept(b())` and `noexcept(c())`.
1957
///   doesn't match the noexcept specifier in the declarations a, b, c or d.
1958
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNoexceptExpr>
1959
    cxxNoexceptExpr;
1960
1961
/// Matches array subscript expressions.
1962
///
1963
/// Given
1964
/// \code
1965
///   int i = a[1];
1966
/// \endcode
1967
/// arraySubscriptExpr()
1968
///   matches "a[1]"
1969
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ArraySubscriptExpr>
1970
    arraySubscriptExpr;
1971
1972
/// Matches the value of a default argument at the call site.
1973
///
1974
/// Example matches the CXXDefaultArgExpr placeholder inserted for the
1975
///     default value of the second parameter in the call expression f(42)
1976
///     (matcher = cxxDefaultArgExpr())
1977
/// \code
1978
///   void f(int x, int y = 0);
1979
///   f(42);
1980
/// \endcode
1981
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDefaultArgExpr>
1982
    cxxDefaultArgExpr;
1983
1984
/// Matches overloaded operator calls.
1985
///
1986
/// Note that if an operator isn't overloaded, it won't match. Instead, use
1987
/// binaryOperator matcher.
1988
/// Currently it does not match operators such as new delete.
1989
/// FIXME: figure out why these do not match?
1990
///
1991
/// Example matches both operator<<((o << b), c) and operator<<(o, b)
1992
///     (matcher = cxxOperatorCallExpr())
1993
/// \code
1994
///   ostream &operator<< (ostream &out, int i) { };
1995
///   ostream &o; int b = 1, c = 1;
1996
///   o << b << c;
1997
/// \endcode
1998
/// See also the binaryOperation() matcher for more-general matching of binary
1999
/// uses of this AST node.
2000
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXOperatorCallExpr>
2001
    cxxOperatorCallExpr;
2002
2003
/// Matches rewritten binary operators
2004
///
2005
/// Example matches use of "<":
2006
/// \code
2007
///   #include <compare>
2008
///   struct HasSpaceshipMem {
2009
///     int a;
2010
///     constexpr auto operator<=>(const HasSpaceshipMem&) const = default;
2011
///   };
2012
///   void compare() {
2013
///     HasSpaceshipMem hs1, hs2;
2014
///     if (hs1 < hs2)
2015
///         return;
2016
///   }
2017
/// \endcode
2018
/// See also the binaryOperation() matcher for more-general matching
2019
/// of this AST node.
2020
extern const internal::VariadicDynCastAllOfMatcher<Stmt,
2021
                                                   CXXRewrittenBinaryOperator>
2022
    cxxRewrittenBinaryOperator;
2023
2024
/// Matches expressions.
2025
///
2026
/// Example matches x()
2027
/// \code
2028
///   void f() { x(); }
2029
/// \endcode
2030
extern const internal::VariadicDynCastAllOfMatcher<Stmt, Expr> expr;
2031
2032
/// Matches expressions that refer to declarations.
2033
///
2034
/// Example matches x in if (x)
2035
/// \code
2036
///   bool x;
2037
///   if (x) {}
2038
/// \endcode
2039
extern const internal::VariadicDynCastAllOfMatcher<Stmt, DeclRefExpr>
2040
    declRefExpr;
2041
2042
/// Matches a reference to an ObjCIvar.
2043
///
2044
/// Example: matches "a" in "init" method:
2045
/// \code
2046
/// @implementation A {
2047
///   NSString *a;
2048
/// }
2049
/// - (void) init {
2050
///   a = @"hello";
2051
/// }
2052
/// \endcode
2053
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCIvarRefExpr>
2054
    objcIvarRefExpr;
2055
2056
/// Matches a reference to a block.
2057
///
2058
/// Example: matches "^{}":
2059
/// \code
2060
///   void f() { ^{}(); }
2061
/// \endcode
2062
extern const internal::VariadicDynCastAllOfMatcher<Stmt, BlockExpr> blockExpr;
2063
2064
/// Matches if statements.
2065
///
2066
/// Example matches 'if (x) {}'
2067
/// \code
2068
///   if (x) {}
2069
/// \endcode
2070
extern const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt;
2071
2072
/// Matches for statements.
2073
///
2074
/// Example matches 'for (;;) {}'
2075
/// \code
2076
///   for (;;) {}
2077
///   int i[] =  {1, 2, 3}; for (auto a : i);
2078
/// \endcode
2079
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ForStmt> forStmt;
2080
2081
/// Matches the increment statement of a for loop.
2082
///
2083
/// Example:
2084
///     forStmt(hasIncrement(unaryOperator(hasOperatorName("++"))))
2085
/// matches '++x' in
2086
/// \code
2087
///     for (x; x < N; ++x) { }
2088
/// \endcode
2089
AST_MATCHER_P(ForStmt, hasIncrement, internal::Matcher<Stmt>,
2090
163
              InnerMatcher) {
2091
163
  const Stmt *const Increment = Node.getInc();
2092
163
  return (Increment != nullptr &&
2093
163
          InnerMatcher.matches(*Increment, Finder, Builder));
2094
163
}
2095
2096
/// Matches the initialization statement of a for loop.
2097
///
2098
/// Example:
2099
///     forStmt(hasLoopInit(declStmt()))
2100
/// matches 'int x = 0' in
2101
/// \code
2102
///     for (int x = 0; x < N; ++x) { }
2103
/// \endcode
2104
AST_MATCHER_P(ForStmt, hasLoopInit, internal::Matcher<Stmt>,
2105
167
              InnerMatcher) {
2106
167
  const Stmt *const Init = Node.getInit();
2107
167
  return (Init != nullptr && 
InnerMatcher.matches(*Init, Finder, Builder)165
);
2108
167
}
2109
2110
/// Matches range-based for statements.
2111
///
2112
/// cxxForRangeStmt() matches 'for (auto a : i)'
2113
/// \code
2114
///   int i[] =  {1, 2, 3}; for (auto a : i);
2115
///   for(int j = 0; j < 5; ++j);
2116
/// \endcode
2117
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXForRangeStmt>
2118
    cxxForRangeStmt;
2119
2120
/// Matches the initialization statement of a for loop.
2121
///
2122
/// Example:
2123
///     forStmt(hasLoopVariable(anything()))
2124
/// matches 'int x' in
2125
/// \code
2126
///     for (int x : a) { }
2127
/// \endcode
2128
AST_MATCHER_P(CXXForRangeStmt, hasLoopVariable, internal::Matcher<VarDecl>,
2129
37
              InnerMatcher) {
2130
37
  const VarDecl *const Var = Node.getLoopVariable();
2131
37
  return (Var != nullptr && InnerMatcher.matches(*Var, Finder, Builder));
2132
37
}
2133
2134
/// Matches the range initialization statement of a for loop.
2135
///
2136
/// Example:
2137
///     forStmt(hasRangeInit(anything()))
2138
/// matches 'a' in
2139
/// \code
2140
///     for (int x : a) { }
2141
/// \endcode
2142
AST_MATCHER_P(CXXForRangeStmt, hasRangeInit, internal::Matcher<Expr>,
2143
41
              InnerMatcher) {
2144
41
  const Expr *const Init = Node.getRangeInit();
2145
41
  return (Init != nullptr && InnerMatcher.matches(*Init, Finder, Builder));
2146
41
}
2147
2148
/// Matches while statements.
2149
///
2150
/// Given
2151
/// \code
2152
///   while (true) {}
2153
/// \endcode
2154
/// whileStmt()
2155
///   matches 'while (true) {}'.
2156
extern const internal::VariadicDynCastAllOfMatcher<Stmt, WhileStmt> whileStmt;
2157
2158
/// Matches do statements.
2159
///
2160
/// Given
2161
/// \code
2162
///   do {} while (true);
2163
/// \endcode
2164
/// doStmt()
2165
///   matches 'do {} while(true)'
2166
extern const internal::VariadicDynCastAllOfMatcher<Stmt, DoStmt> doStmt;
2167
2168
/// Matches break statements.
2169
///
2170
/// Given
2171
/// \code
2172
///   while (true) { break; }
2173
/// \endcode
2174
/// breakStmt()
2175
///   matches 'break'
2176
extern const internal::VariadicDynCastAllOfMatcher<Stmt, BreakStmt> breakStmt;
2177
2178
/// Matches continue statements.
2179
///
2180
/// Given
2181
/// \code
2182
///   while (true) { continue; }
2183
/// \endcode
2184
/// continueStmt()
2185
///   matches 'continue'
2186
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ContinueStmt>
2187
    continueStmt;
2188
2189
/// Matches co_return statements.
2190
///
2191
/// Given
2192
/// \code
2193
///   while (true) { co_return; }
2194
/// \endcode
2195
/// coreturnStmt()
2196
///   matches 'co_return'
2197
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CoreturnStmt>
2198
    coreturnStmt;
2199
2200
/// Matches return statements.
2201
///
2202
/// Given
2203
/// \code
2204
///   return 1;
2205
/// \endcode
2206
/// returnStmt()
2207
///   matches 'return 1'
2208
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ReturnStmt> returnStmt;
2209
2210
/// Matches goto statements.
2211
///
2212
/// Given
2213
/// \code
2214
///   goto FOO;
2215
///   FOO: bar();
2216
/// \endcode
2217
/// gotoStmt()
2218
///   matches 'goto FOO'
2219
extern const internal::VariadicDynCastAllOfMatcher<Stmt, GotoStmt> gotoStmt;
2220
2221
/// Matches label statements.
2222
///
2223
/// Given
2224
/// \code
2225
///   goto FOO;
2226
///   FOO: bar();
2227
/// \endcode
2228
/// labelStmt()
2229
///   matches 'FOO:'
2230
extern const internal::VariadicDynCastAllOfMatcher<Stmt, LabelStmt> labelStmt;
2231
2232
/// Matches address of label statements (GNU extension).
2233
///
2234
/// Given
2235
/// \code
2236
///   FOO: bar();
2237
///   void *ptr = &&FOO;
2238
///   goto *bar;
2239
/// \endcode
2240
/// addrLabelExpr()
2241
///   matches '&&FOO'
2242
extern const internal::VariadicDynCastAllOfMatcher<Stmt, AddrLabelExpr>
2243
    addrLabelExpr;
2244
2245
/// Matches switch statements.
2246
///
2247
/// Given
2248
/// \code
2249
///   switch(a) { case 42: break; default: break; }
2250
/// \endcode
2251
/// switchStmt()
2252
///   matches 'switch(a)'.
2253
extern const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchStmt> switchStmt;
2254
2255
/// Matches case and default statements inside switch statements.
2256
///
2257
/// Given
2258
/// \code
2259
///   switch(a) { case 42: break; default: break; }
2260
/// \endcode
2261
/// switchCase()
2262
///   matches 'case 42:' and 'default:'.
2263
extern const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchCase> switchCase;
2264
2265
/// Matches case statements inside switch statements.
2266
///
2267
/// Given
2268
/// \code
2269
///   switch(a) { case 42: break; default: break; }
2270
/// \endcode
2271
/// caseStmt()
2272
///   matches 'case 42:'.
2273
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CaseStmt> caseStmt;
2274
2275
/// Matches default statements inside switch statements.
2276
///
2277
/// Given
2278
/// \code
2279
///   switch(a) { case 42: break; default: break; }
2280
/// \endcode
2281
/// defaultStmt()
2282
///   matches 'default:'.
2283
extern const internal::VariadicDynCastAllOfMatcher<Stmt, DefaultStmt>
2284
    defaultStmt;
2285
2286
/// Matches compound statements.
2287
///
2288
/// Example matches '{}' and '{{}}' in 'for (;;) {{}}'
2289
/// \code
2290
///   for (;;) {{}}
2291
/// \endcode
2292
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CompoundStmt>
2293
    compoundStmt;
2294
2295
/// Matches catch statements.
2296
///
2297
/// \code
2298
///   try {} catch(int i) {}
2299
/// \endcode
2300
/// cxxCatchStmt()
2301
///   matches 'catch(int i)'
2302
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXCatchStmt>
2303
    cxxCatchStmt;
2304
2305
/// Matches try statements.
2306
///
2307
/// \code
2308
///   try {} catch(int i) {}
2309
/// \endcode
2310
/// cxxTryStmt()
2311
///   matches 'try {}'
2312
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTryStmt> cxxTryStmt;
2313
2314
/// Matches throw expressions.
2315
///
2316
/// \code
2317
///   try { throw 5; } catch(int i) {}
2318
/// \endcode
2319
/// cxxThrowExpr()
2320
///   matches 'throw 5'
2321
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThrowExpr>
2322
    cxxThrowExpr;
2323
2324
/// Matches null statements.
2325
///
2326
/// \code
2327
///   foo();;
2328
/// \endcode
2329
/// nullStmt()
2330
///   matches the second ';'
2331
extern const internal::VariadicDynCastAllOfMatcher<Stmt, NullStmt> nullStmt;
2332
2333
/// Matches asm statements.
2334
///
2335
/// \code
2336
///  int i = 100;
2337
///   __asm("mov al, 2");
2338
/// \endcode
2339
/// asmStmt()
2340
///   matches '__asm("mov al, 2")'
2341
extern const internal::VariadicDynCastAllOfMatcher<Stmt, AsmStmt> asmStmt;
2342
2343
/// Matches bool literals.
2344
///
2345
/// Example matches true
2346
/// \code
2347
///   true
2348
/// \endcode
2349
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXBoolLiteralExpr>
2350
    cxxBoolLiteral;
2351
2352
/// Matches string literals (also matches wide string literals).
2353
///
2354
/// Example matches "abcd", L"abcd"
2355
/// \code
2356
///   char *s = "abcd";
2357
///   wchar_t *ws = L"abcd";
2358
/// \endcode
2359
extern const internal::VariadicDynCastAllOfMatcher<Stmt, StringLiteral>
2360
    stringLiteral;
2361
2362
/// Matches character literals (also matches wchar_t).
2363
///
2364
/// Not matching Hex-encoded chars (e.g. 0x1234, which is a IntegerLiteral),
2365
/// though.
2366
///
2367
/// Example matches 'a', L'a'
2368
/// \code
2369
///   char ch = 'a';
2370
///   wchar_t chw = L'a';
2371
/// \endcode
2372
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CharacterLiteral>
2373
    characterLiteral;
2374
2375
/// Matches integer literals of all sizes / encodings, e.g.
2376
/// 1, 1L, 0x1 and 1U.
2377
///
2378
/// Does not match character-encoded integers such as L'a'.
2379
extern const internal::VariadicDynCastAllOfMatcher<Stmt, IntegerLiteral>
2380
    integerLiteral;
2381
2382
/// Matches float literals of all sizes / encodings, e.g.
2383
/// 1.0, 1.0f, 1.0L and 1e10.
2384
///
2385
/// Does not match implicit conversions such as
2386
/// \code
2387
///   float a = 10;
2388
/// \endcode
2389
extern const internal::VariadicDynCastAllOfMatcher<Stmt, FloatingLiteral>
2390
    floatLiteral;
2391
2392
/// Matches imaginary literals, which are based on integer and floating
2393
/// point literals e.g.: 1i, 1.0i
2394
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ImaginaryLiteral>
2395
    imaginaryLiteral;
2396
2397
/// Matches fixed point literals
2398
extern const internal::VariadicDynCastAllOfMatcher<Stmt, FixedPointLiteral>
2399
    fixedPointLiteral;
2400
2401
/// Matches user defined literal operator call.
2402
///
2403
/// Example match: "foo"_suffix
2404
extern const internal::VariadicDynCastAllOfMatcher<Stmt, UserDefinedLiteral>
2405
    userDefinedLiteral;
2406
2407
/// Matches compound (i.e. non-scalar) literals
2408
///
2409
/// Example match: {1}, (1, 2)
2410
/// \code
2411
///   int array[4] = {1};
2412
///   vector int myvec = (vector int)(1, 2);
2413
/// \endcode
2414
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CompoundLiteralExpr>
2415
    compoundLiteralExpr;
2416
2417
/// Matches co_await expressions.
2418
///
2419
/// Given
2420
/// \code
2421
///   co_await 1;
2422
/// \endcode
2423
/// coawaitExpr()
2424
///   matches 'co_await 1'
2425
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CoawaitExpr>
2426
    coawaitExpr;
2427
/// Matches co_await expressions where the type of the promise is dependent
2428
extern const internal::VariadicDynCastAllOfMatcher<Stmt, DependentCoawaitExpr>
2429
    dependentCoawaitExpr;
2430
/// Matches co_yield expressions.
2431
///
2432
/// Given
2433
/// \code
2434
///   co_yield 1;
2435
/// \endcode
2436
/// coyieldExpr()
2437
///   matches 'co_yield 1'
2438
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CoyieldExpr>
2439
    coyieldExpr;
2440
2441
/// Matches nullptr literal.
2442
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNullPtrLiteralExpr>
2443
    cxxNullPtrLiteralExpr;
2444
2445
/// Matches GNU __builtin_choose_expr.
2446
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ChooseExpr>
2447
    chooseExpr;
2448
2449
/// Matches GNU __null expression.
2450
extern const internal::VariadicDynCastAllOfMatcher<Stmt, GNUNullExpr>
2451
    gnuNullExpr;
2452
2453
/// Matches C11 _Generic expression.
2454
extern const internal::VariadicDynCastAllOfMatcher<Stmt, GenericSelectionExpr>
2455
    genericSelectionExpr;
2456
2457
/// Matches atomic builtins.
2458
/// Example matches __atomic_load_n(ptr, 1)
2459
/// \code
2460
///   void foo() { int *ptr; __atomic_load_n(ptr, 1); }
2461
/// \endcode
2462
extern const internal::VariadicDynCastAllOfMatcher<Stmt, AtomicExpr> atomicExpr;
2463
2464
/// Matches statement expression (GNU extension).
2465
///
2466
/// Example match: ({ int X = 4; X; })
2467
/// \code
2468
///   int C = ({ int X = 4; X; });
2469
/// \endcode
2470
extern const internal::VariadicDynCastAllOfMatcher<Stmt, StmtExpr> stmtExpr;
2471
2472
/// Matches binary operator expressions.
2473
///
2474
/// Example matches a || b
2475
/// \code
2476
///   !(a || b)
2477
/// \endcode
2478
/// See also the binaryOperation() matcher for more-general matching.
2479
extern const internal::VariadicDynCastAllOfMatcher<Stmt, BinaryOperator>
2480
    binaryOperator;
2481
2482
/// Matches unary operator expressions.
2483
///
2484
/// Example matches !a
2485
/// \code
2486
///   !a || b
2487
/// \endcode
2488
extern const internal::VariadicDynCastAllOfMatcher<Stmt, UnaryOperator>
2489
    unaryOperator;
2490
2491
/// Matches conditional operator expressions.
2492
///
2493
/// Example matches a ? b : c
2494
/// \code
2495
///   (a ? b : c) + 42
2496
/// \endcode
2497
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ConditionalOperator>
2498
    conditionalOperator;
2499
2500
/// Matches binary conditional operator expressions (GNU extension).
2501
///
2502
/// Example matches a ?: b
2503
/// \code
2504
///   (a ?: b) + 42;
2505
/// \endcode
2506
extern const internal::VariadicDynCastAllOfMatcher<Stmt,
2507
                                                   BinaryConditionalOperator>
2508
    binaryConditionalOperator;
2509
2510
/// Matches opaque value expressions. They are used as helpers
2511
/// to reference another expressions and can be met
2512
/// in BinaryConditionalOperators, for example.
2513
///
2514
/// Example matches 'a'
2515
/// \code
2516
///   (a ?: c) + 42;
2517
/// \endcode
2518
extern const internal::VariadicDynCastAllOfMatcher<Stmt, OpaqueValueExpr>
2519
    opaqueValueExpr;
2520
2521
/// Matches a C++ static_assert declaration.
2522
///
2523
/// Example:
2524
///   staticAssertExpr()
2525
/// matches
2526
///   static_assert(sizeof(S) == sizeof(int))
2527
/// in
2528
/// \code
2529
///   struct S {
2530
///     int x;
2531
///   };
2532
///   static_assert(sizeof(S) == sizeof(int));
2533
/// \endcode
2534
extern const internal::VariadicDynCastAllOfMatcher<Decl, StaticAssertDecl>
2535
    staticAssertDecl;
2536
2537
/// Matches a reinterpret_cast expression.
2538
///
2539
/// Either the source expression or the destination type can be matched
2540
/// using has(), but hasDestinationType() is more specific and can be
2541
/// more readable.
2542
///
2543
/// Example matches reinterpret_cast<char*>(&p) in
2544
/// \code
2545
///   void* p = reinterpret_cast<char*>(&p);
2546
/// \endcode
2547
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXReinterpretCastExpr>
2548
    cxxReinterpretCastExpr;
2549
2550
/// Matches a C++ static_cast expression.
2551
///
2552
/// \see hasDestinationType
2553
/// \see reinterpretCast
2554
///
2555
/// Example:
2556
///   cxxStaticCastExpr()
2557
/// matches
2558
///   static_cast<long>(8)
2559
/// in
2560
/// \code
2561
///   long eight(static_cast<long>(8));
2562
/// \endcode
2563
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXStaticCastExpr>
2564
    cxxStaticCastExpr;
2565
2566
/// Matches a dynamic_cast expression.
2567
///
2568
/// Example:
2569
///   cxxDynamicCastExpr()
2570
/// matches
2571
///   dynamic_cast<D*>(&b);
2572
/// in
2573
/// \code
2574
///   struct B { virtual ~B() {} }; struct D : B {};
2575
///   B b;
2576
///   D* p = dynamic_cast<D*>(&b);
2577
/// \endcode
2578
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDynamicCastExpr>
2579
    cxxDynamicCastExpr;
2580
2581
/// Matches a const_cast expression.
2582
///
2583
/// Example: Matches const_cast<int*>(&r) in
2584
/// \code
2585
///   int n = 42;
2586
///   const int &r(n);
2587
///   int* p = const_cast<int*>(&r);
2588
/// \endcode
2589
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXConstCastExpr>
2590
    cxxConstCastExpr;
2591
2592
/// Matches a C-style cast expression.
2593
///
2594
/// Example: Matches (int) 2.2f in
2595
/// \code
2596
///   int i = (int) 2.2f;
2597
/// \endcode
2598
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CStyleCastExpr>
2599
    cStyleCastExpr;
2600
2601
/// Matches explicit cast expressions.
2602
///
2603
/// Matches any cast expression written in user code, whether it be a
2604
/// C-style cast, a functional-style cast, or a keyword cast.
2605
///
2606
/// Does not match implicit conversions.
2607
///
2608
/// Note: the name "explicitCast" is chosen to match Clang's terminology, as
2609
/// Clang uses the term "cast" to apply to implicit conversions as well as to
2610
/// actual cast expressions.
2611
///
2612
/// \see hasDestinationType.
2613
///
2614
/// Example: matches all five of the casts in
2615
/// \code
2616
///   int((int)(reinterpret_cast<int>(static_cast<int>(const_cast<int>(42)))))
2617
/// \endcode
2618
/// but does not match the implicit conversion in
2619
/// \code
2620
///   long ell = 42;
2621
/// \endcode
2622
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ExplicitCastExpr>
2623
    explicitCastExpr;
2624
2625
/// Matches the implicit cast nodes of Clang's AST.
2626
///
2627
/// This matches many different places, including function call return value
2628
/// eliding, as well as any type conversions.
2629
extern const internal::VariadicDynCastAllOfMatcher<Stmt, ImplicitCastExpr>
2630
    implicitCastExpr;
2631
2632
/// Matches any cast nodes of Clang's AST.
2633
///
2634
/// Example: castExpr() matches each of the following:
2635
/// \code
2636
///   (int) 3;
2637
///   const_cast<Expr *>(SubExpr);
2638
///   char c = 0;
2639
/// \endcode
2640
/// but does not match
2641
/// \code
2642
///   int i = (0);
2643
///   int k = 0;
2644
/// \endcode
2645
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CastExpr> castExpr;
2646
2647
/// Matches functional cast expressions
2648
///
2649
/// Example: Matches Foo(bar);
2650
/// \code
2651
///   Foo f = bar;
2652
///   Foo g = (Foo) bar;
2653
///   Foo h = Foo(bar);
2654
/// \endcode
2655
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXFunctionalCastExpr>
2656
    cxxFunctionalCastExpr;
2657
2658
/// Matches functional cast expressions having N != 1 arguments
2659
///
2660
/// Example: Matches Foo(bar, bar)
2661
/// \code
2662
///   Foo h = Foo(bar, bar);
2663
/// \endcode
2664
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTemporaryObjectExpr>
2665
    cxxTemporaryObjectExpr;
2666
2667
/// Matches predefined identifier expressions [C99 6.4.2.2].
2668
///
2669
/// Example: Matches __func__
2670
/// \code
2671
///   printf("%s", __func__);
2672
/// \endcode
2673
extern const internal::VariadicDynCastAllOfMatcher<Stmt, PredefinedExpr>
2674
    predefinedExpr;
2675
2676
/// Matches C99 designated initializer expressions [C99 6.7.8].
2677
///
2678
/// Example: Matches { [2].y = 1.0, [0].x = 1.0 }
2679
/// \code
2680
///   point ptarray[10] = { [2].y = 1.0, [0].x = 1.0 };
2681
/// \endcode
2682
extern const internal::VariadicDynCastAllOfMatcher<Stmt, DesignatedInitExpr>
2683
    designatedInitExpr;
2684
2685
/// Matches designated initializer expressions that contain
2686
/// a specific number of designators.
2687
///
2688
/// Example: Given
2689
/// \code
2690
///   point ptarray[10] = { [2].y = 1.0, [0].x = 1.0 };
2691
///   point ptarray2[10] = { [2].y = 1.0, [2].x = 0.0, [0].x = 1.0 };
2692
/// \endcode
2693
/// designatorCountIs(2)
2694
///   matches '{ [2].y = 1.0, [0].x = 1.0 }',
2695
///   but not '{ [2].y = 1.0, [2].x = 0.0, [0].x = 1.0 }'.
2696
60
AST_MATCHER_P(DesignatedInitExpr, designatorCountIs, unsigned, N) {
2697
60
  return Node.size() == N;
2698
60
}
2699
2700
/// Matches \c QualTypes in the clang AST.
2701
extern const internal::VariadicAllOfMatcher<QualType> qualType;
2702
2703
/// Matches \c Types in the clang AST.
2704
extern const internal::VariadicAllOfMatcher<Type> type;
2705
2706
/// Matches \c TypeLocs in the clang AST.
2707
extern const internal::VariadicAllOfMatcher<TypeLoc> typeLoc;
2708
2709
/// Matches if any of the given matchers matches.
2710
///
2711
/// Unlike \c anyOf, \c eachOf will generate a match result for each
2712
/// matching submatcher.
2713
///
2714
/// For example, in:
2715
/// \code
2716
///   class A { int a; int b; };
2717
/// \endcode
2718
/// The matcher:
2719
/// \code
2720
///   cxxRecordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2721
///                        has(fieldDecl(hasName("b")).bind("v"))))
2722
/// \endcode
2723
/// will generate two results binding "v", the first of which binds
2724
/// the field declaration of \c a, the second the field declaration of
2725
/// \c b.
2726
///
2727
/// Usable as: Any Matcher
2728
extern const internal::VariadicOperatorMatcherFunc<
2729
    2, std::numeric_limits<unsigned>::max()>
2730
    eachOf;
2731
2732
/// Matches if any of the given matchers matches.
2733
///
2734
/// Usable as: Any Matcher
2735
extern const internal::VariadicOperatorMatcherFunc<
2736
    2, std::numeric_limits<unsigned>::max()>
2737
    anyOf;
2738
2739
/// Matches if all given matchers match.
2740
///
2741
/// Usable as: Any Matcher
2742
extern const internal::VariadicOperatorMatcherFunc<
2743
    2, std::numeric_limits<unsigned>::max()>
2744
    allOf;
2745
2746
/// Matches any node regardless of the submatcher.
2747
///
2748
/// However, \c optionally will retain any bindings generated by the submatcher.
2749
/// Useful when additional information which may or may not present about a main
2750
/// matching node is desired.
2751
///
2752
/// For example, in:
2753
/// \code
2754
///   class Foo {
2755
///     int bar;
2756
///   }
2757
/// \endcode
2758
/// The matcher:
2759
/// \code
2760
///   cxxRecordDecl(
2761
///     optionally(has(
2762
///       fieldDecl(hasName("bar")).bind("var")
2763
///   ))).bind("record")
2764
/// \endcode
2765
/// will produce a result binding for both "record" and "var".
2766
/// The matcher will produce a "record" binding for even if there is no data
2767
/// member named "bar" in that class.
2768
///
2769
/// Usable as: Any Matcher
2770
extern const internal::VariadicOperatorMatcherFunc<1, 1> optionally;
2771
2772
/// Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL)
2773
///
2774
/// Given
2775
/// \code
2776
///   Foo x = bar;
2777
///   int y = sizeof(x) + alignof(x);
2778
/// \endcode
2779
/// unaryExprOrTypeTraitExpr()
2780
///   matches \c sizeof(x) and \c alignof(x)
2781
extern const internal::VariadicDynCastAllOfMatcher<Stmt,
2782
                                                   UnaryExprOrTypeTraitExpr>
2783
    unaryExprOrTypeTraitExpr;
2784
2785
/// Matches any of the \p NodeMatchers with InnerMatchers nested within
2786
///
2787
/// Given
2788
/// \code
2789
///   if (true);
2790
///   for (; true; );
2791
/// \endcode
2792
/// with the matcher
2793
/// \code
2794
///   mapAnyOf(ifStmt, forStmt).with(
2795
///     hasCondition(cxxBoolLiteralExpr(equals(true)))
2796
///     ).bind("trueCond")
2797
/// \endcode
2798
/// matches the \c if and the \c for. It is equivalent to:
2799
/// \code
2800
///   auto trueCond = hasCondition(cxxBoolLiteralExpr(equals(true)));
2801
///   anyOf(
2802
///     ifStmt(trueCond).bind("trueCond"),
2803
///     forStmt(trueCond).bind("trueCond")
2804
///     );
2805
/// \endcode
2806
///
2807
/// The with() chain-call accepts zero or more matchers which are combined
2808
/// as-if with allOf() in each of the node matchers.
2809
/// Usable as: Any Matcher
2810
template <typename T, typename... U>
2811
auto mapAnyOf(internal::VariadicDynCastAllOfMatcher<T, U> const &...) {
2812
  return internal::MapAnyOfHelper<U...>();
2813
}
2814
2815
/// Matches nodes which can be used with binary operators.
2816
///
2817
/// The code
2818
/// \code
2819
///   var1 != var2;
2820
/// \endcode
2821
/// might be represented in the clang AST as a binaryOperator, a
2822
/// cxxOperatorCallExpr or a cxxRewrittenBinaryOperator, depending on
2823
///
2824
/// * whether the types of var1 and var2 are fundamental (binaryOperator) or at
2825
///   least one is a class type (cxxOperatorCallExpr)
2826
/// * whether the code appears in a template declaration, if at least one of the
2827
///   vars is a dependent-type (binaryOperator)
2828
/// * whether the code relies on a rewritten binary operator, such as a
2829
/// spaceship operator or an inverted equality operator
2830
/// (cxxRewrittenBinaryOperator)
2831
///
2832
/// This matcher elides details in places where the matchers for the nodes are
2833
/// compatible.
2834
///
2835
/// Given
2836
/// \code
2837
///   binaryOperation(
2838
///     hasOperatorName("!="),
2839
///     hasLHS(expr().bind("lhs")),
2840
///     hasRHS(expr().bind("rhs"))
2841
///   )
2842
/// \endcode
2843
/// matches each use of "!=" in:
2844
/// \code
2845
///   struct S{
2846
///       bool operator!=(const S&) const;
2847
///   };
2848
///
2849
///   void foo()
2850
///   {
2851
///      1 != 2;
2852
///      S() != S();
2853
///   }
2854
///
2855
///   template<typename T>
2856
///   void templ()
2857
///   {
2858
///      1 != 2;
2859
///      T() != S();
2860
///   }
2861
///   struct HasOpEq
2862
///   {
2863
///       bool operator==(const HasOpEq &) const;
2864
///   };
2865
///
2866
///   void inverse()
2867
///   {
2868
///       HasOpEq s1;
2869
///       HasOpEq s2;
2870
///       if (s1 != s2)
2871
///           return;
2872
///   }
2873
///
2874
///   struct HasSpaceship
2875
///   {
2876
///       bool operator<=>(const HasOpEq &) const;
2877
///   };
2878
///
2879
///   void use_spaceship()
2880
///   {
2881
///       HasSpaceship s1;
2882
///       HasSpaceship s2;
2883
///       if (s1 != s2)
2884
///           return;
2885
///   }
2886
/// \endcode
2887
extern const internal::MapAnyOfMatcher<BinaryOperator, CXXOperatorCallExpr,
2888
                                       CXXRewrittenBinaryOperator>
2889
    binaryOperation;
2890
2891
/// Matches function calls and constructor calls
2892
///
2893
/// Because CallExpr and CXXConstructExpr do not share a common
2894
/// base class with API accessing arguments etc, AST Matchers for code
2895
/// which should match both are typically duplicated. This matcher
2896
/// removes the need for duplication.
2897
///
2898
/// Given code
2899
/// \code
2900
/// struct ConstructorTakesInt
2901
/// {
2902
///   ConstructorTakesInt(int i) {}
2903
/// };
2904
///
2905
/// void callTakesInt(int i)
2906
/// {
2907
/// }
2908
///
2909
/// void doCall()
2910
/// {
2911
///   callTakesInt(42);
2912
/// }
2913
///
2914
/// void doConstruct()
2915
/// {
2916
///   ConstructorTakesInt cti(42);
2917
/// }
2918
/// \endcode
2919
///
2920
/// The matcher
2921
/// \code
2922
/// invocation(hasArgument(0, integerLiteral(equals(42))))
2923
/// \endcode
2924
/// matches the expression in both doCall and doConstruct
2925
extern const internal::MapAnyOfMatcher<CallExpr, CXXConstructExpr> invocation;
2926
2927
/// Matches unary expressions that have a specific type of argument.
2928
///
2929
/// Given
2930
/// \code
2931
///   int a, c; float b; int s = sizeof(a) + sizeof(b) + alignof(c);
2932
/// \endcode
2933
/// unaryExprOrTypeTraitExpr(hasArgumentOfType(asString("int"))
2934
///   matches \c sizeof(a) and \c alignof(c)
2935
AST_MATCHER_P(UnaryExprOrTypeTraitExpr, hasArgumentOfType,
2936
115
              internal::Matcher<QualType>, InnerMatcher) {
2937
115
  const QualType ArgumentType = Node.getTypeOfArgument();
2938
115
  return InnerMatcher.matches(ArgumentType, Finder, Builder);
2939
115
}
2940
2941
/// Matches unary expressions of a certain kind.
2942
///
2943
/// Given
2944
/// \code
2945
///   int x;
2946
///   int s = sizeof(x) + alignof(x)
2947
/// \endcode
2948
/// unaryExprOrTypeTraitExpr(ofKind(UETT_SizeOf))
2949
///   matches \c sizeof(x)
2950
///
2951
/// If the matcher is use from clang-query, UnaryExprOrTypeTrait parameter
2952
/// should be passed as a quoted string. e.g., ofKind("UETT_SizeOf").
2953
176
AST_MATCHER_P(UnaryExprOrTypeTraitExpr, ofKind, UnaryExprOrTypeTrait, Kind) {
2954
176
  return Node.getKind() == Kind;
2955
176
}
2956
2957
/// Same as unaryExprOrTypeTraitExpr, but only matching
2958
/// alignof.
2959
inline internal::BindableMatcher<Stmt> alignOfExpr(
2960
14
    const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) {
2961
14
  return stmt(unaryExprOrTypeTraitExpr(
2962
14
      allOf(anyOf(ofKind(UETT_AlignOf), ofKind(UETT_PreferredAlignOf)),
2963
14
            InnerMatcher)));
2964
14
}
2965
2966
/// Same as unaryExprOrTypeTraitExpr, but only matching
2967
/// sizeof.
2968
inline internal::BindableMatcher<Stmt> sizeOfExpr(
2969
394
    const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) {
2970
394
  return stmt(unaryExprOrTypeTraitExpr(
2971
394
      allOf(ofKind(UETT_SizeOf), InnerMatcher)));
2972
394
}
2973
2974
/// Matches NamedDecl nodes that have the specified name.
2975
///
2976
/// Supports specifying enclosing namespaces or classes by prefixing the name
2977
/// with '<enclosing>::'.
2978
/// Does not match typedefs of an underlying type with the given name.
2979
///
2980
/// Example matches X (Name == "X")
2981
/// \code
2982
///   class X;
2983
/// \endcode
2984
///
2985
/// Example matches X (Name is one of "::a::b::X", "a::b::X", "b::X", "X")
2986
/// \code
2987
///   namespace a { namespace b { class X; } }
2988
/// \endcode
2989
20.8k
inline internal::Matcher<NamedDecl> hasName(StringRef Name) {
2990
20.8k
  return internal::Matcher<NamedDecl>(
2991
20.8k
      new internal::HasNameMatcher({std::string(Name)}));
2992
20.8k
}
2993
2994
/// Matches NamedDecl nodes that have any of the specified names.
2995
///
2996
/// This matcher is only provided as a performance optimization of hasName.
2997
/// \code
2998
///     hasAnyName(a, b, c)
2999
/// \endcode
3000
///  is equivalent to, but faster than
3001
/// \code
3002
///     anyOf(hasName(a), hasName(b), hasName(c))
3003
/// \endcode
3004
extern const internal::VariadicFunction<internal::Matcher<NamedDecl>, StringRef,
3005
                                        internal::hasAnyNameFunc>
3006
    hasAnyName;
3007
3008
/// Matches NamedDecl nodes whose fully qualified names contain
3009
/// a substring matched by the given RegExp.
3010
///
3011
/// Supports specifying enclosing namespaces or classes by
3012
/// prefixing the name with '<enclosing>::'.  Does not match typedefs
3013
/// of an underlying type with the given name.
3014
///
3015
/// Example matches X (regexp == "::X")
3016
/// \code
3017
///   class X;
3018
/// \endcode
3019
///
3020
/// Example matches X (regexp is one of "::X", "^foo::.*X", among others)
3021
/// \code
3022
///   namespace foo { namespace bar { class X; } }
3023
/// \endcode
3024
3.74k
AST_MATCHER_REGEX(NamedDecl, matchesName, RegExp) {
3025
3.74k
  std::string FullNameString = "::" + Node.getQualifiedNameAsString();
3026
3.74k
  return RegExp->match(FullNameString);
3027
3.74k
}
3028
3029
/// Matches overloaded operator names.
3030
///
3031
/// Matches overloaded operator names specified in strings without the
3032
/// "operator" prefix: e.g. "<<".
3033
///
3034
/// Given:
3035
/// \code
3036
///   class A { int operator*(); };
3037
///   const A &operator<<(const A &a, const A &b);
3038
///   A a;
3039
///   a << a;   // <-- This matches
3040
/// \endcode
3041
///
3042
/// \c cxxOperatorCallExpr(hasOverloadedOperatorName("<<"))) matches the
3043
/// specified line and
3044
/// \c cxxRecordDecl(hasMethod(hasOverloadedOperatorName("*")))
3045
/// matches the declaration of \c A.
3046
///
3047
/// Usable as: Matcher<CXXOperatorCallExpr>, Matcher<FunctionDecl>
3048
inline internal::PolymorphicMatcher<
3049
    internal::HasOverloadedOperatorNameMatcher,
3050
    AST_POLYMORPHIC_SUPPORTED_TYPES(CXXOperatorCallExpr, FunctionDecl),
3051
    std::vector<std::string>>
3052
599
hasOverloadedOperatorName(StringRef Name) {
3053
599
  return internal::PolymorphicMatcher<
3054
599
      internal::HasOverloadedOperatorNameMatcher,
3055
599
      AST_POLYMORPHIC_SUPPORTED_TYPES(CXXOperatorCallExpr, FunctionDecl),
3056
599
      std::vector<std::string>>({std::string(Name)});
3057
599
}
3058
3059
/// Matches overloaded operator names.
3060
///
3061
/// Matches overloaded operator names specified in strings without the
3062
/// "operator" prefix: e.g. "<<".
3063
///
3064
///   hasAnyOverloadedOperatorName("+", "-")
3065
/// Is equivalent to
3066
///   anyOf(hasOverloadedOperatorName("+"), hasOverloadedOperatorName("-"))
3067
extern const internal::VariadicFunction<
3068
    internal::PolymorphicMatcher<internal::HasOverloadedOperatorNameMatcher,
3069
                                 AST_POLYMORPHIC_SUPPORTED_TYPES(
3070
                                     CXXOperatorCallExpr, FunctionDecl),
3071
                                 std::vector<std::string>>,
3072
    StringRef, internal::hasAnyOverloadedOperatorNameFunc>
3073
    hasAnyOverloadedOperatorName;
3074
3075
/// Matches template-dependent, but known, member names.
3076
///
3077
/// In template declarations, dependent members are not resolved and so can
3078
/// not be matched to particular named declarations.
3079
///
3080
/// This matcher allows to match on the known name of members.
3081
///
3082
/// Given
3083
/// \code
3084
///   template <typename T>
3085
///   struct S {
3086
///       void mem();
3087
///   };
3088
///   template <typename T>
3089
///   void x() {
3090
///       S<T> s;
3091
///       s.mem();
3092
///   }
3093
/// \endcode
3094
/// \c cxxDependentScopeMemberExpr(hasMemberName("mem")) matches `s.mem()`
3095
8
AST_MATCHER_P(CXXDependentScopeMemberExpr, hasMemberName, std::string, N) {
3096
8
  return Node.getMember().getAsString() == N;
3097
8
}
3098
3099
/// Matches template-dependent, but known, member names against an already-bound
3100
/// node
3101
///
3102
/// In template declarations, dependent members are not resolved and so can
3103
/// not be matched to particular named declarations.
3104
///
3105
/// This matcher allows to match on the name of already-bound VarDecl, FieldDecl
3106
/// and CXXMethodDecl nodes.
3107
///
3108
/// Given
3109
/// \code
3110
///   template <typename T>
3111
///   struct S {
3112
///       void mem();
3113
///   };
3114
///   template <typename T>
3115
///   void x() {
3116
///       S<T> s;
3117
///       s.mem();
3118
///   }
3119
/// \endcode
3120
/// The matcher
3121
/// @code
3122
/// \c cxxDependentScopeMemberExpr(
3123
///   hasObjectExpression(declRefExpr(hasType(templateSpecializationType(
3124
///       hasDeclaration(classTemplateDecl(has(cxxRecordDecl(has(
3125
///           cxxMethodDecl(hasName("mem")).bind("templMem")
3126
///           )))))
3127
///       )))),
3128
///   memberHasSameNameAsBoundNode("templMem")
3129
///   )
3130
/// @endcode
3131
/// first matches and binds the @c mem member of the @c S template, then
3132
/// compares its name to the usage in @c s.mem() in the @c x function template
3133
AST_MATCHER_P(CXXDependentScopeMemberExpr, memberHasSameNameAsBoundNode,
3134
7
              std::string, BindingID) {
3135
7
  auto MemberName = Node.getMember().getAsString();
3136
3137
7
  return Builder->removeBindings(
3138
7
      [this, MemberName](const BoundNodesMap &Nodes) {
3139
7
        const auto &BN = Nodes.getNode(this->BindingID);
3140
7
        if (const auto *ND = BN.get<NamedDecl>()) {
3141
7
          if (!isa<FieldDecl, CXXMethodDecl, VarDecl>(ND))
3142
1
            return true;
3143
6
          return ND->getName() != MemberName;
3144
7
        }
3145
0
        return true;
3146
7
      });
3147
7
}
3148
3149
/// Matches C++ classes that are directly or indirectly derived from a class
3150
/// matching \c Base, or Objective-C classes that directly or indirectly
3151
/// subclass a class matching \c Base.
3152
///
3153
/// Note that a class is not considered to be derived from itself.
3154
///
3155
/// Example matches Y, Z, C (Base == hasName("X"))
3156
/// \code
3157
///   class X;
3158
///   class Y : public X {};  // directly derived
3159
///   class Z : public Y {};  // indirectly derived
3160
///   typedef X A;
3161
///   typedef A B;
3162
///   class C : public B {};  // derived from a typedef of X
3163
/// \endcode
3164
///
3165
/// In the following example, Bar matches isDerivedFrom(hasName("X")):
3166
/// \code
3167
///   class Foo;
3168
///   typedef Foo X;
3169
///   class Bar : public Foo {};  // derived from a type that X is a typedef of
3170
/// \endcode
3171
///
3172
/// In the following example, Bar matches isDerivedFrom(hasName("NSObject"))
3173
/// \code
3174
///   @interface NSObject @end
3175
///   @interface Bar : NSObject @end
3176
/// \endcode
3177
///
3178
/// Usable as: Matcher<CXXRecordDecl>, Matcher<ObjCInterfaceDecl>
3179
AST_POLYMORPHIC_MATCHER_P(
3180
    isDerivedFrom,
3181
    AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl),
3182
5.59k
    internal::Matcher<NamedDecl>, Base) {
3183
  // Check if the node is a C++ struct/union/class.
3184
5.59k
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3185
4.47k
    return Finder->classIsDerivedFrom(RD, Base, Builder, /*Directly=*/false);
3186
3187
  // The node must be an Objective-C class.
3188
1.12k
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3189
1.12k
  return Finder->objcClassIsDerivedFrom(InterfaceDecl, Base, Builder,
3190
1.12k
                                        /*Directly=*/false);
3191
5.59k
}
clang::ast_matchers::internal::matcher_isDerivedFrom0Matcher<clang::CXXRecordDecl, clang::ast_matchers::internal::Matcher<clang::NamedDecl> >::matches(clang::CXXRecordDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3182
4.47k
    internal::Matcher<NamedDecl>, Base) {
3183
  // Check if the node is a C++ struct/union/class.
3184
4.47k
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3185
4.47k
    return Finder->classIsDerivedFrom(RD, Base, Builder, /*Directly=*/false);
3186
3187
  // The node must be an Objective-C class.
3188
0
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3189
0
  return Finder->objcClassIsDerivedFrom(InterfaceDecl, Base, Builder,
3190
0
                                        /*Directly=*/false);
3191
4.47k
}
clang::ast_matchers::internal::matcher_isDerivedFrom0Matcher<clang::ObjCInterfaceDecl, clang::ast_matchers::internal::Matcher<clang::NamedDecl> >::matches(clang::ObjCInterfaceDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3182
1.12k
    internal::Matcher<NamedDecl>, Base) {
3183
  // Check if the node is a C++ struct/union/class.
3184
1.12k
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3185
0
    return Finder->classIsDerivedFrom(RD, Base, Builder, /*Directly=*/false);
3186
3187
  // The node must be an Objective-C class.
3188
1.12k
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3189
1.12k
  return Finder->objcClassIsDerivedFrom(InterfaceDecl, Base, Builder,
3190
1.12k
                                        /*Directly=*/false);
3191
1.12k
}
3192
3193
/// Overloaded method as shortcut for \c isDerivedFrom(hasName(...)).
3194
AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
3195
    isDerivedFrom,
3196
    AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl),
3197
4.25k
    std::string, BaseName, 1) {
3198
4.25k
  if (BaseName.empty())
3199
100
    return false;
3200
3201
4.15k
  const auto M = isDerivedFrom(hasName(BaseName));
3202
3203
4.15k
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3204
3.25k
    return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
3205
3206
896
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3207
896
  return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
3208
4.15k
}
clang::ast_matchers::internal::matcher_isDerivedFrom1Matcher<clang::CXXRecordDecl, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::matches(clang::CXXRecordDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3197
3.35k
    std::string, BaseName, 1) {
3198
3.35k
  if (BaseName.empty())
3199
100
    return false;
3200
3201
3.25k
  const auto M = isDerivedFrom(hasName(BaseName));
3202
3203
3.25k
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3204
3.25k
    return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
3205
3206
0
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3207
0
  return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
3208
3.25k
}
clang::ast_matchers::internal::matcher_isDerivedFrom1Matcher<clang::ObjCInterfaceDecl, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::matches(clang::ObjCInterfaceDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3197
896
    std::string, BaseName, 1) {
3198
896
  if (BaseName.empty())
3199
0
    return false;
3200
3201
896
  const auto M = isDerivedFrom(hasName(BaseName));
3202
3203
896
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3204
0
    return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
3205
3206
896
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3207
896
  return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
3208
896
}
3209
3210
/// Matches C++ classes that have a direct or indirect base matching \p
3211
/// BaseSpecMatcher.
3212
///
3213
/// Example:
3214
/// matcher hasAnyBase(hasType(cxxRecordDecl(hasName("SpecialBase"))))
3215
/// \code
3216
///   class Foo;
3217
///   class Bar : Foo {};
3218
///   class Baz : Bar {};
3219
///   class SpecialBase;
3220
///   class Proxy : SpecialBase {};  // matches Proxy
3221
///   class IndirectlyDerived : Proxy {};  //matches IndirectlyDerived
3222
/// \endcode
3223
///
3224
// FIXME: Refactor this and isDerivedFrom to reuse implementation.
3225
AST_MATCHER_P(CXXRecordDecl, hasAnyBase, internal::Matcher<CXXBaseSpecifier>,
3226
1.72k
              BaseSpecMatcher) {
3227
1.72k
  return internal::matchesAnyBase(Node, BaseSpecMatcher, Finder, Builder);
3228
1.72k
}
3229
3230
/// Matches C++ classes that have a direct base matching \p BaseSpecMatcher.
3231
///
3232
/// Example:
3233
/// matcher hasDirectBase(hasType(cxxRecordDecl(hasName("SpecialBase"))))
3234
/// \code
3235
///   class Foo;
3236
///   class Bar : Foo {};
3237
///   class Baz : Bar {};
3238
///   class SpecialBase;
3239
///   class Proxy : SpecialBase {};  // matches Proxy
3240
///   class IndirectlyDerived : Proxy {};  // doesn't match
3241
/// \endcode
3242
AST_MATCHER_P(CXXRecordDecl, hasDirectBase, internal::Matcher<CXXBaseSpecifier>,
3243
504
              BaseSpecMatcher) {
3244
504
  return Node.hasDefinition() &&
3245
504
         
llvm::any_of(Node.bases(), [&](const CXXBaseSpecifier &Base) 222
{
3246
170
           return BaseSpecMatcher.matches(Base, Finder, Builder);
3247
170
         });
3248
504
}
3249
3250
/// Similar to \c isDerivedFrom(), but also matches classes that directly
3251
/// match \c Base.
3252
AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
3253
    isSameOrDerivedFrom,
3254
    AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl),
3255
1.41k
    internal::Matcher<NamedDecl>, Base, 0) {
3256
1.41k
  const auto M = anyOf(Base, isDerivedFrom(Base));
3257
3258
1.41k
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3259
1.10k
    return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
3260
3261
308
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3262
308
  return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
3263
1.41k
}
clang::ast_matchers::internal::matcher_isSameOrDerivedFrom0Matcher<clang::CXXRecordDecl, clang::ast_matchers::internal::Matcher<clang::NamedDecl> >::matches(clang::CXXRecordDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3255
1.10k
    internal::Matcher<NamedDecl>, Base, 0) {
3256
1.10k
  const auto M = anyOf(Base, isDerivedFrom(Base));
3257
3258
1.10k
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3259
1.10k
    return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
3260
3261
0
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3262
0
  return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
3263
1.10k
}
clang::ast_matchers::internal::matcher_isSameOrDerivedFrom0Matcher<clang::ObjCInterfaceDecl, clang::ast_matchers::internal::Matcher<clang::NamedDecl> >::matches(clang::ObjCInterfaceDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3255
308
    internal::Matcher<NamedDecl>, Base, 0) {
3256
308
  const auto M = anyOf(Base, isDerivedFrom(Base));
3257
3258
308
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3259
0
    return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
3260
3261
308
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3262
308
  return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
3263
308
}
3264
3265
/// Overloaded method as shortcut for
3266
/// \c isSameOrDerivedFrom(hasName(...)).
3267
AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
3268
    isSameOrDerivedFrom,
3269
    AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl),
3270
1.50k
    std::string, BaseName, 1) {
3271
1.50k
  if (BaseName.empty())
3272
100
    return false;
3273
3274
1.40k
  const auto M = isSameOrDerivedFrom(hasName(BaseName));
3275
3276
1.40k
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3277
1.10k
    return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
3278
3279
308
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3280
308
  return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
3281
1.40k
}
clang::ast_matchers::internal::matcher_isSameOrDerivedFrom1Matcher<clang::CXXRecordDecl, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::matches(clang::CXXRecordDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3270
1.20k
    std::string, BaseName, 1) {
3271
1.20k
  if (BaseName.empty())
3272
100
    return false;
3273
3274
1.10k
  const auto M = isSameOrDerivedFrom(hasName(BaseName));
3275
3276
1.10k
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3277
1.10k
    return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
3278
3279
0
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3280
0
  return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
3281
1.10k
}
clang::ast_matchers::internal::matcher_isSameOrDerivedFrom1Matcher<clang::ObjCInterfaceDecl, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::matches(clang::ObjCInterfaceDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3270
308
    std::string, BaseName, 1) {
3271
308
  if (BaseName.empty())
3272
0
    return false;
3273
3274
308
  const auto M = isSameOrDerivedFrom(hasName(BaseName));
3275
3276
308
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3277
0
    return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
3278
3279
308
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3280
308
  return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
3281
308
}
3282
3283
/// Matches C++ or Objective-C classes that are directly derived from a class
3284
/// matching \c Base.
3285
///
3286
/// Note that a class is not considered to be derived from itself.
3287
///
3288
/// Example matches Y, C (Base == hasName("X"))
3289
/// \code
3290
///   class X;
3291
///   class Y : public X {};  // directly derived
3292
///   class Z : public Y {};  // indirectly derived
3293
///   typedef X A;
3294
///   typedef A B;
3295
///   class C : public B {};  // derived from a typedef of X
3296
/// \endcode
3297
///
3298
/// In the following example, Bar matches isDerivedFrom(hasName("X")):
3299
/// \code
3300
///   class Foo;
3301
///   typedef Foo X;
3302
///   class Bar : public Foo {};  // derived from a type that X is a typedef of
3303
/// \endcode
3304
AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
3305
    isDirectlyDerivedFrom,
3306
    AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl),
3307
1.26k
    internal::Matcher<NamedDecl>, Base, 0) {
3308
  // Check if the node is a C++ struct/union/class.
3309
1.26k
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3310
420
    return Finder->classIsDerivedFrom(RD, Base, Builder, /*Directly=*/true);
3311
3312
  // The node must be an Objective-C class.
3313
840
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3314
840
  return Finder->objcClassIsDerivedFrom(InterfaceDecl, Base, Builder,
3315
840
                                        /*Directly=*/true);
3316
1.26k
}
clang::ast_matchers::internal::matcher_isDirectlyDerivedFrom0Matcher<clang::CXXRecordDecl, clang::ast_matchers::internal::Matcher<clang::NamedDecl> >::matches(clang::CXXRecordDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3307
420
    internal::Matcher<NamedDecl>, Base, 0) {
3308
  // Check if the node is a C++ struct/union/class.
3309
420
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3310
420
    return Finder->classIsDerivedFrom(RD, Base, Builder, /*Directly=*/true);
3311
3312
  // The node must be an Objective-C class.
3313
0
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3314
0
  return Finder->objcClassIsDerivedFrom(InterfaceDecl, Base, Builder,
3315
0
                                        /*Directly=*/true);
3316
420
}
clang::ast_matchers::internal::matcher_isDirectlyDerivedFrom0Matcher<clang::ObjCInterfaceDecl, clang::ast_matchers::internal::Matcher<clang::NamedDecl> >::matches(clang::ObjCInterfaceDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3307
840
    internal::Matcher<NamedDecl>, Base, 0) {
3308
  // Check if the node is a C++ struct/union/class.
3309
840
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3310
0
    return Finder->classIsDerivedFrom(RD, Base, Builder, /*Directly=*/true);
3311
3312
  // The node must be an Objective-C class.
3313
840
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3314
840
  return Finder->objcClassIsDerivedFrom(InterfaceDecl, Base, Builder,
3315
840
                                        /*Directly=*/true);
3316
840
}
3317
3318
/// Overloaded method as shortcut for \c isDirectlyDerivedFrom(hasName(...)).
3319
AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
3320
    isDirectlyDerivedFrom,
3321
    AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl),
3322
1.32k
    std::string, BaseName, 1) {
3323
1.32k
  if (BaseName.empty())
3324
100
    return false;
3325
1.22k
  const auto M = isDirectlyDerivedFrom(hasName(BaseName));
3326
3327
1.22k
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3328
380
    return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
3329
3330
840
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3331
840
  return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
3332
1.22k
}
clang::ast_matchers::internal::matcher_isDirectlyDerivedFrom1Matcher<clang::CXXRecordDecl, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::matches(clang::CXXRecordDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3322
480
    std::string, BaseName, 1) {
3323
480
  if (BaseName.empty())
3324
100
    return false;
3325
380
  const auto M = isDirectlyDerivedFrom(hasName(BaseName));
3326
3327
380
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3328
380
    return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
3329
3330
0
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3331
0
  return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
3332
380
}
clang::ast_matchers::internal::matcher_isDirectlyDerivedFrom1Matcher<clang::ObjCInterfaceDecl, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::matches(clang::ObjCInterfaceDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3322
840
    std::string, BaseName, 1) {
3323
840
  if (BaseName.empty())
3324
0
    return false;
3325
840
  const auto M = isDirectlyDerivedFrom(hasName(BaseName));
3326
3327
840
  if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
3328
0
    return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
3329
3330
840
  const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
3331
840
  return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
3332
840
}
3333
/// Matches the first method of a class or struct that satisfies \c
3334
/// InnerMatcher.
3335
///
3336
/// Given:
3337
/// \code
3338
///   class A { void func(); };
3339
///   class B { void member(); };
3340
/// \endcode
3341
///
3342
/// \c cxxRecordDecl(hasMethod(hasName("func"))) matches the declaration of
3343
/// \c A but not \c B.
3344
AST_MATCHER_P(CXXRecordDecl, hasMethod, internal::Matcher<CXXMethodDecl>,
3345
224
              InnerMatcher) {
3346
224
  BoundNodesTreeBuilder Result(*Builder);
3347
224
  auto MatchIt = matchesFirstInPointerRange(InnerMatcher, Node.method_begin(),
3348
224
                                            Node.method_end(), Finder, &Result);
3349
224
  if (MatchIt == Node.method_end())
3350
143
    return false;
3351
3352
81
  if (Finder->isTraversalIgnoringImplicitNodes() && 
(*MatchIt)->isImplicit()8
)
3353
8
    return false;
3354
73
  *Builder = std::move(Result);
3355
73
  return true;
3356
81
}
3357
3358
/// Matches the generated class of lambda expressions.
3359
///
3360
/// Given:
3361
/// \code
3362
///   auto x = []{};
3363
/// \endcode
3364
///
3365
/// \c cxxRecordDecl(isLambda()) matches the implicit class declaration of
3366
/// \c decltype(x)
3367
200
AST_MATCHER(CXXRecordDecl, isLambda) {
3368
200
  return Node.isLambda();
3369
200
}
3370
3371
/// Matches AST nodes that have child AST nodes that match the
3372
/// provided matcher.
3373
///
3374
/// Example matches X, Y
3375
///   (matcher = cxxRecordDecl(has(cxxRecordDecl(hasName("X")))
3376
/// \code
3377
///   class X {};  // Matches X, because X::X is a class of name X inside X.
3378
///   class Y { class X {}; };
3379
///   class Z { class Y { class X {}; }; };  // Does not match Z.
3380
/// \endcode
3381
///
3382
/// ChildT must be an AST base type.
3383
///
3384
/// Usable as: Any Matcher
3385
/// Note that has is direct matcher, so it also matches things like implicit
3386
/// casts and paren casts. If you are matching with expr then you should
3387
/// probably consider using ignoringParenImpCasts like:
3388
/// has(ignoringParenImpCasts(expr())).
3389
extern const internal::ArgumentAdaptingMatcherFunc<internal::HasMatcher> has;
3390
3391
/// Matches AST nodes that have descendant AST nodes that match the
3392
/// provided matcher.
3393
///
3394
/// Example matches X, Y, Z
3395
///     (matcher = cxxRecordDecl(hasDescendant(cxxRecordDecl(hasName("X")))))
3396
/// \code
3397
///   class X {};  // Matches X, because X::X is a class of name X inside X.
3398
///   class Y { class X {}; };
3399
///   class Z { class Y { class X {}; }; };
3400
/// \endcode
3401
///
3402
/// DescendantT must be an AST base type.
3403
///
3404
/// Usable as: Any Matcher
3405
extern const internal::ArgumentAdaptingMatcherFunc<
3406
    internal::HasDescendantMatcher>
3407
    hasDescendant;
3408
3409
/// Matches AST nodes that have child AST nodes that match the
3410
/// provided matcher.
3411
///
3412
/// Example matches X, Y, Y::X, Z::Y, Z::Y::X
3413
///   (matcher = cxxRecordDecl(forEach(cxxRecordDecl(hasName("X")))
3414
/// \code
3415
///   class X {};
3416
///   class Y { class X {}; };  // Matches Y, because Y::X is a class of name X
3417
///                             // inside Y.
3418
///   class Z { class Y { class X {}; }; };  // Does not match Z.
3419
/// \endcode
3420
///
3421
/// ChildT must be an AST base type.
3422
///
3423
/// As opposed to 'has', 'forEach' will cause a match for each result that
3424
/// matches instead of only on the first one.
3425
///
3426
/// Usable as: Any Matcher
3427
extern const internal::ArgumentAdaptingMatcherFunc<internal::ForEachMatcher>
3428
    forEach;
3429
3430
/// Matches AST nodes that have descendant AST nodes that match the
3431
/// provided matcher.
3432
///
3433
/// Example matches X, A, A::X, B, B::C, B::C::X
3434
///   (matcher = cxxRecordDecl(forEachDescendant(cxxRecordDecl(hasName("X")))))
3435
/// \code
3436
///   class X {};
3437
///   class A { class X {}; };  // Matches A, because A::X is a class of name
3438
///                             // X inside A.
3439
///   class B { class C { class X {}; }; };
3440
/// \endcode
3441
///
3442
/// DescendantT must be an AST base type.
3443
///
3444
/// As opposed to 'hasDescendant', 'forEachDescendant' will cause a match for
3445
/// each result that matches instead of only on the first one.
3446
///
3447
/// Note: Recursively combined ForEachDescendant can cause many matches:
3448
///   cxxRecordDecl(forEachDescendant(cxxRecordDecl(
3449
///     forEachDescendant(cxxRecordDecl())
3450
///   )))
3451
/// will match 10 times (plus injected class name matches) on:
3452
/// \code
3453
///   class A { class B { class C { class D { class E {}; }; }; }; };
3454
/// \endcode
3455
///
3456
/// Usable as: Any Matcher
3457
extern const internal::ArgumentAdaptingMatcherFunc<
3458
    internal::ForEachDescendantMatcher>
3459
    forEachDescendant;
3460
3461
/// Matches if the node or any descendant matches.
3462
///
3463
/// Generates results for each match.
3464
///
3465
/// For example, in:
3466
/// \code
3467
///   class A { class B {}; class C {}; };
3468
/// \endcode
3469
/// The matcher:
3470
/// \code
3471
///   cxxRecordDecl(hasName("::A"),
3472
///                 findAll(cxxRecordDecl(isDefinition()).bind("m")))
3473
/// \endcode
3474
/// will generate results for \c A, \c B and \c C.
3475
///
3476
/// Usable as: Any Matcher
3477
template <typename T>
3478
2.25k
internal::Matcher<T> findAll(const internal::Matcher<T> &Matcher) {
3479
2.25k
  return eachOf(Matcher, forEachDescendant(Matcher));
3480
2.25k
}
clang::ast_matchers::internal::Matcher<clang::Stmt> clang::ast_matchers::findAll<clang::Stmt>(clang::ast_matchers::internal::Matcher<clang::Stmt> const&)
Line
Count
Source
3478
2.25k
internal::Matcher<T> findAll(const internal::Matcher<T> &Matcher) {
3479
2.25k
  return eachOf(Matcher, forEachDescendant(Matcher));
3480
2.25k
}
clang::ast_matchers::internal::Matcher<clang::Decl> clang::ast_matchers::findAll<clang::Decl>(clang::ast_matchers::internal::Matcher<clang::Decl> const&)
Line
Count
Source
3478
4
internal::Matcher<T> findAll(const internal::Matcher<T> &Matcher) {
3479
4
  return eachOf(Matcher, forEachDescendant(Matcher));
3480
4
}
3481
3482
/// Matches AST nodes that have a parent that matches the provided
3483
/// matcher.
3484
///
3485
/// Given
3486
/// \code
3487
/// void f() { for (;;) { int x = 42; if (true) { int x = 43; } } }
3488
/// \endcode
3489
/// \c compoundStmt(hasParent(ifStmt())) matches "{ int x = 43; }".
3490
///
3491
/// Usable as: Any Matcher
3492
extern const internal::ArgumentAdaptingMatcherFunc<
3493
    internal::HasParentMatcher,
3494
    internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr>,
3495
    internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr>>
3496
    hasParent;
3497
3498
/// Matches AST nodes that have an ancestor that matches the provided
3499
/// matcher.
3500
///
3501
/// Given
3502
/// \code
3503
/// void f() { if (true) { int x = 42; } }
3504
/// void g() { for (;;) { int x = 43; } }
3505
/// \endcode
3506
/// \c expr(integerLiteral(hasAncestor(ifStmt()))) matches \c 42, but not 43.
3507
///
3508
/// Usable as: Any Matcher
3509
extern const internal::ArgumentAdaptingMatcherFunc<
3510
    internal::HasAncestorMatcher,
3511
    internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr>,
3512
    internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr>>
3513
    hasAncestor;
3514
3515
/// Matches if the provided matcher does not match.
3516
///
3517
/// Example matches Y (matcher = cxxRecordDecl(unless(hasName("X"))))
3518
/// \code
3519
///   class X {};
3520
///   class Y {};
3521
/// \endcode
3522
///
3523
/// Usable as: Any Matcher
3524
extern const internal::VariadicOperatorMatcherFunc<1, 1> unless;
3525
3526
/// Matches a node if the declaration associated with that node
3527
/// matches the given matcher.
3528
///
3529
/// The associated declaration is:
3530
/// - for type nodes, the declaration of the underlying type
3531
/// - for CallExpr, the declaration of the callee
3532
/// - for MemberExpr, the declaration of the referenced member
3533
/// - for CXXConstructExpr, the declaration of the constructor
3534
/// - for CXXNewExpr, the declaration of the operator new
3535
/// - for ObjCIvarExpr, the declaration of the ivar
3536
///
3537
/// For type nodes, hasDeclaration will generally match the declaration of the
3538
/// sugared type. Given
3539
/// \code
3540
///   class X {};
3541
///   typedef X Y;
3542
///   Y y;
3543
/// \endcode
3544
/// in varDecl(hasType(hasDeclaration(decl()))) the decl will match the
3545
/// typedefDecl. A common use case is to match the underlying, desugared type.
3546
/// This can be achieved by using the hasUnqualifiedDesugaredType matcher:
3547
/// \code
3548
///   varDecl(hasType(hasUnqualifiedDesugaredType(
3549
///       recordType(hasDeclaration(decl())))))
3550
/// \endcode
3551
/// In this matcher, the decl will match the CXXRecordDecl of class X.
3552
///
3553
/// Usable as: Matcher<AddrLabelExpr>, Matcher<CallExpr>,
3554
///   Matcher<CXXConstructExpr>, Matcher<CXXNewExpr>, Matcher<DeclRefExpr>,
3555
///   Matcher<EnumType>, Matcher<InjectedClassNameType>, Matcher<LabelStmt>,
3556
///   Matcher<MemberExpr>, Matcher<QualType>, Matcher<RecordType>,
3557
///   Matcher<TagType>, Matcher<TemplateSpecializationType>,
3558
///   Matcher<TemplateTypeParmType>, Matcher<TypedefType>,
3559
///   Matcher<UnresolvedUsingType>
3560
inline internal::PolymorphicMatcher<
3561
    internal::HasDeclarationMatcher,
3562
    void(internal::HasDeclarationSupportedTypes), internal::Matcher<Decl>>
3563
22.2k
hasDeclaration(const internal::Matcher<Decl> &InnerMatcher) {
3564
22.2k
  return internal::PolymorphicMatcher<
3565
22.2k
      internal::HasDeclarationMatcher,
3566
22.2k
      void(internal::HasDeclarationSupportedTypes), internal::Matcher<Decl>>(
3567
22.2k
      InnerMatcher);
3568
22.2k
}
3569
3570
/// Matches a \c NamedDecl whose underlying declaration matches the given
3571
/// matcher.
3572
///
3573
/// Given
3574
/// \code
3575
///   namespace N { template<class T> void f(T t); }
3576
///   template <class T> void g() { using N::f; f(T()); }
3577
/// \endcode
3578
/// \c unresolvedLookupExpr(hasAnyDeclaration(
3579
///     namedDecl(hasUnderlyingDecl(hasName("::N::f")))))
3580
///   matches the use of \c f in \c g() .
3581
AST_MATCHER_P(NamedDecl, hasUnderlyingDecl, internal::Matcher<NamedDecl>,
3582
2
              InnerMatcher) {
3583
2
  const NamedDecl *UnderlyingDecl = Node.getUnderlyingDecl();
3584
3585
2
  return UnderlyingDecl != nullptr &&
3586
2
         InnerMatcher.matches(*UnderlyingDecl, Finder, Builder);
3587
2
}
3588
3589
/// Matches on the implicit object argument of a member call expression, after
3590
/// stripping off any parentheses or implicit casts.
3591
///
3592
/// Given
3593
/// \code
3594
///   class Y { public: void m(); };
3595
///   Y g();
3596
///   class X : public Y {};
3597
///   void z(Y y, X x) { y.m(); (g()).m(); x.m(); }
3598
/// \endcode
3599
/// cxxMemberCallExpr(on(hasType(cxxRecordDecl(hasName("Y")))))
3600
///   matches `y.m()` and `(g()).m()`.
3601
/// cxxMemberCallExpr(on(hasType(cxxRecordDecl(hasName("X")))))
3602
///   matches `x.m()`.
3603
/// cxxMemberCallExpr(on(callExpr()))
3604
///   matches `(g()).m()`.
3605
///
3606
/// FIXME: Overload to allow directly matching types?
3607
AST_MATCHER_P(CXXMemberCallExpr, on, internal::Matcher<Expr>,
3608
416
              InnerMatcher) {
3609
416
  const Expr *ExprNode = Node.getImplicitObjectArgument()
3610
416
                            ->IgnoreParenImpCasts();
3611
416
  return (ExprNode != nullptr &&
3612
416
          InnerMatcher.matches(*ExprNode, Finder, Builder));
3613
416
}
3614
3615
3616
/// Matches on the receiver of an ObjectiveC Message expression.
3617
///
3618
/// Example
3619
/// matcher = objCMessageExpr(hasReceiverType(asString("UIWebView *")));
3620
/// matches the [webView ...] message invocation.
3621
/// \code
3622
///   NSString *webViewJavaScript = ...
3623
///   UIWebView *webView = ...
3624
///   [webView stringByEvaluatingJavaScriptFromString:webViewJavascript];
3625
/// \endcode
3626
AST_MATCHER_P(ObjCMessageExpr, hasReceiverType, internal::Matcher<QualType>,
3627
11
              InnerMatcher) {
3628
11
  const QualType TypeDecl = Node.getReceiverType();
3629
11
  return InnerMatcher.matches(TypeDecl, Finder, Builder);
3630
11
}
3631
3632
/// Returns true when the Objective-C method declaration is a class method.
3633
///
3634
/// Example
3635
/// matcher = objcMethodDecl(isClassMethod())
3636
/// matches
3637
/// \code
3638
/// @interface I + (void)foo; @end
3639
/// \endcode
3640
/// but not
3641
/// \code
3642
/// @interface I - (void)bar; @end
3643
/// \endcode
3644
8
AST_MATCHER(ObjCMethodDecl, isClassMethod) {
3645
8
  return Node.isClassMethod();
3646
8
}
3647
3648
/// Returns true when the Objective-C method declaration is an instance method.
3649
///
3650
/// Example
3651
/// matcher = objcMethodDecl(isInstanceMethod())
3652
/// matches
3653
/// \code
3654
/// @interface I - (void)bar; @end
3655
/// \endcode
3656
/// but not
3657
/// \code
3658
/// @interface I + (void)foo; @end
3659
/// \endcode
3660
8
AST_MATCHER(ObjCMethodDecl, isInstanceMethod) {
3661
8
  return Node.isInstanceMethod();
3662
8
}
3663
3664
/// Returns true when the Objective-C message is sent to a class.
3665
///
3666
/// Example
3667
/// matcher = objcMessageExpr(isClassMessage())
3668
/// matches
3669
/// \code
3670
///   [NSString stringWithFormat:@"format"];
3671
/// \endcode
3672
/// but not
3673
/// \code
3674
///   NSString *x = @"hello";
3675
///   [x containsString:@"h"];
3676
/// \endcode
3677
4
AST_MATCHER(ObjCMessageExpr, isClassMessage) {
3678
4
  return Node.isClassMessage();
3679
4
}
3680
3681
/// Returns true when the Objective-C message is sent to an instance.
3682
///
3683
/// Example
3684
/// matcher = objcMessageExpr(isInstanceMessage())
3685
/// matches
3686
/// \code
3687
///   NSString *x = @"hello";
3688
///   [x containsString:@"h"];
3689
/// \endcode
3690
/// but not
3691
/// \code
3692
///   [NSString stringWithFormat:@"format"];
3693
/// \endcode
3694
4
AST_MATCHER(ObjCMessageExpr, isInstanceMessage) {
3695
4
  return Node.isInstanceMessage();
3696
4
}
3697
3698
/// Matches if the Objective-C message is sent to an instance,
3699
/// and the inner matcher matches on that instance.
3700
///
3701
/// For example the method call in
3702
/// \code
3703
///   NSString *x = @"hello";
3704
///   [x containsString:@"h"];
3705
/// \endcode
3706
/// is matched by
3707
/// objcMessageExpr(hasReceiver(declRefExpr(to(varDecl(hasName("x"))))))
3708
AST_MATCHER_P(ObjCMessageExpr, hasReceiver, internal::Matcher<Expr>,
3709
15
              InnerMatcher) {
3710
15
  const Expr *ReceiverNode = Node.getInstanceReceiver();
3711
15
  return (ReceiverNode != nullptr &&
3712
15
          InnerMatcher.matches(*ReceiverNode->IgnoreParenImpCasts(), Finder,
3713
13
                               Builder));
3714
15
}
3715
3716
/// Matches when BaseName == Selector.getAsString()
3717
///
3718
///  matcher = objCMessageExpr(hasSelector("loadHTMLString:baseURL:"));
3719
///  matches the outer message expr in the code below, but NOT the message
3720
///  invocation for self.bodyView.
3721
/// \code
3722
///     [self.bodyView loadHTMLString:html baseURL:NULL];
3723
/// \endcode
3724
53
AST_MATCHER_P(ObjCMessageExpr, hasSelector, std::string, BaseName) {
3725
53
  Selector Sel = Node.getSelector();
3726
53
  return BaseName.compare(Sel.getAsString()) == 0;
3727
53
}
3728
3729
3730
/// Matches when at least one of the supplied string equals to the
3731
/// Selector.getAsString()
3732
///
3733
///  matcher = objCMessageExpr(hasSelector("methodA:", "methodB:"));
3734
///  matches both of the expressions below:
3735
/// \code
3736
///     [myObj methodA:argA];
3737
///     [myObj methodB:argB];
3738
/// \endcode
3739
extern const internal::VariadicFunction<internal::Matcher<ObjCMessageExpr>,
3740
                                        StringRef,
3741
                                        internal::hasAnySelectorFunc>
3742
                                        hasAnySelector;
3743
3744
/// Matches ObjC selectors whose name contains
3745
/// a substring matched by the given RegExp.
3746
///  matcher = objCMessageExpr(matchesSelector("loadHTMLString\:baseURL?"));
3747
///  matches the outer message expr in the code below, but NOT the message
3748
///  invocation for self.bodyView.
3749
/// \code
3750
///     [self.bodyView loadHTMLString:html baseURL:NULL];
3751
/// \endcode
3752
12
AST_MATCHER_REGEX(ObjCMessageExpr, matchesSelector, RegExp) {
3753
12
  std::string SelectorString = Node.getSelector().getAsString();
3754
12
  return RegExp->match(SelectorString);
3755
12
}
3756
3757
/// Matches when the selector is the empty selector
3758
///
3759
/// Matches only when the selector of the objCMessageExpr is NULL. This may
3760
/// represent an error condition in the tree!
3761
2
AST_MATCHER(ObjCMessageExpr, hasNullSelector) {
3762
2
  return Node.getSelector().isNull();
3763
2
}
3764
3765
/// Matches when the selector is a Unary Selector
3766
///
3767
///  matcher = objCMessageExpr(matchesSelector(hasUnarySelector());
3768
///  matches self.bodyView in the code below, but NOT the outer message
3769
///  invocation of "loadHTMLString:baseURL:".
3770
/// \code
3771
///     [self.bodyView loadHTMLString:html baseURL:NULL];
3772
/// \endcode
3773
2
AST_MATCHER(ObjCMessageExpr, hasUnarySelector) {
3774
2
  return Node.getSelector().isUnarySelector();
3775
2
}
3776
3777
/// Matches when the selector is a keyword selector
3778
///
3779
/// objCMessageExpr(hasKeywordSelector()) matches the generated setFrame
3780
/// message expression in
3781
///
3782
/// \code
3783
///   UIWebView *webView = ...;
3784
///   CGRect bodyFrame = webView.frame;
3785
///   bodyFrame.size.height = self.bodyContentHeight;
3786
///   webView.frame = bodyFrame;
3787
///   //     ^---- matches here
3788
/// \endcode
3789
0
AST_MATCHER(ObjCMessageExpr, hasKeywordSelector) {
3790
0
  return Node.getSelector().isKeywordSelector();
3791
0
}
3792
3793
/// Matches when the selector has the specified number of arguments
3794
///
3795
///  matcher = objCMessageExpr(numSelectorArgs(0));
3796
///  matches self.bodyView in the code below
3797
///
3798
///  matcher = objCMessageExpr(numSelectorArgs(2));
3799
///  matches the invocation of "loadHTMLString:baseURL:" but not that
3800
///  of self.bodyView
3801
/// \code
3802
///     [self.bodyView loadHTMLString:html baseURL:NULL];
3803
/// \endcode
3804
2
AST_MATCHER_P(ObjCMessageExpr, numSelectorArgs, unsigned, N) {
3805
2
  return Node.getSelector().getNumArgs() == N;
3806
2
}
3807
3808
/// Matches if the call expression's callee expression matches.
3809
///
3810
/// Given
3811
/// \code
3812
///   class Y { void x() { this->x(); x(); Y y; y.x(); } };
3813
///   void f() { f(); }
3814
/// \endcode
3815
/// callExpr(callee(expr()))
3816
///   matches this->x(), x(), y.x(), f()
3817
/// with callee(...)
3818
///   matching this->x, x, y.x, f respectively
3819
///
3820
/// Note: Callee cannot take the more general internal::Matcher<Expr>
3821
/// because this introduces ambiguous overloads with calls to Callee taking a
3822
/// internal::Matcher<Decl>, as the matcher hierarchy is purely
3823
/// implemented in terms of implicit casts.
3824
AST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>,
3825
299
              InnerMatcher) {
3826
299
  const Expr *ExprNode = Node.getCallee();
3827
299
  return (ExprNode != nullptr &&
3828
299
          InnerMatcher.matches(*ExprNode, Finder, Builder));
3829
299
}
3830
3831
/// Matches if the call expression's callee's declaration matches the
3832
/// given matcher.
3833
///
3834
/// Example matches y.x() (matcher = callExpr(callee(
3835
///                                    cxxMethodDecl(hasName("x")))))
3836
/// \code
3837
///   class Y { public: void x(); };
3838
///   void z() { Y y; y.x(); }
3839
/// \endcode
3840
AST_MATCHER_P_OVERLOAD(CallExpr, callee, internal::Matcher<Decl>, InnerMatcher,
3841
1.51k
                       1) {
3842
1.51k
  return callExpr(hasDeclaration(InnerMatcher)).matches(Node, Finder, Builder);
3843
1.51k
}
3844
3845
/// Matches if the expression's or declaration's type matches a type
3846
/// matcher.
3847
///
3848
/// Example matches x (matcher = expr(hasType(cxxRecordDecl(hasName("X")))))
3849
///             and z (matcher = varDecl(hasType(cxxRecordDecl(hasName("X")))))
3850
///             and U (matcher = typedefDecl(hasType(asString("int")))
3851
///             and friend class X (matcher = friendDecl(hasType("X"))
3852
///             and public virtual X (matcher = cxxBaseSpecifier(hasType(
3853
///                                               asString("class X")))
3854
/// \code
3855
///  class X {};
3856
///  void y(X &x) { x; X z; }
3857
///  typedef int U;
3858
///  class Y { friend class X; };
3859
///  class Z : public virtual X {};
3860
/// \endcode
3861
AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
3862
    hasType,
3863
    AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, FriendDecl, TypedefNameDecl,
3864
                                    ValueDecl, CXXBaseSpecifier),
3865
7.49k
    internal::Matcher<QualType>, InnerMatcher, 0) {
3866
7.49k
  QualType QT = internal::getUnderlyingType(Node);
3867
7.49k
  if (!QT.isNull())
3868
7.47k
    return InnerMatcher.matches(QT, Finder, Builder);
3869
20
  return false;
3870
7.49k
}
clang::ast_matchers::internal::matcher_hasType0Matcher<clang::ExplicitCastExpr, clang::ast_matchers::internal::Matcher<clang::QualType> >::matches(clang::ExplicitCastExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3865
246
    internal::Matcher<QualType>, InnerMatcher, 0) {
3866
246
  QualType QT = internal::getUnderlyingType(Node);
3867
246
  if (!QT.isNull())
3868
246
    return InnerMatcher.matches(QT, Finder, Builder);
3869
0
  return false;
3870
246
}
clang::ast_matchers::internal::matcher_hasType0Matcher<clang::CStyleCastExpr, clang::ast_matchers::internal::Matcher<clang::QualType> >::matches(clang::CStyleCastExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3865
7
    internal::Matcher<QualType>, InnerMatcher, 0) {
3866
7
  QualType QT = internal::getUnderlyingType(Node);
3867
7
  if (!QT.isNull())
3868
7
    return InnerMatcher.matches(QT, Finder, Builder);
3869
0
  return false;
3870
7
}
clang::ast_matchers::internal::matcher_hasType0Matcher<clang::FieldDecl, clang::ast_matchers::internal::Matcher<clang::QualType> >::matches(clang::FieldDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3865
239
    internal::Matcher<QualType>, InnerMatcher, 0) {
3866
239
  QualType QT = internal::getUnderlyingType(Node);
3867
239
  if (!QT.isNull())
3868
239
    return InnerMatcher.matches(QT, Finder, Builder);
3869
0
  return false;
3870
239
}
clang::ast_matchers::internal::matcher_hasType0Matcher<clang::VarDecl, clang::ast_matchers::internal::Matcher<clang::QualType> >::matches(clang::VarDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3865
3.09k
    internal::Matcher<QualType>, InnerMatcher, 0) {
3866
3.09k
  QualType QT = internal::getUnderlyingType(Node);
3867
3.09k
  if (!QT.isNull())
3868
3.09k
    return InnerMatcher.matches(QT, Finder, Builder);
3869
0
  return false;
3870
3.09k
}
clang::ast_matchers::internal::matcher_hasType0Matcher<clang::ParmVarDecl, clang::ast_matchers::internal::Matcher<clang::QualType> >::matches(clang::ParmVarDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3865
493
    internal::Matcher<QualType>, InnerMatcher, 0) {
3866
493
  QualType QT = internal::getUnderlyingType(Node);
3867
493
  if (!QT.isNull())
3868
493
    return InnerMatcher.matches(QT, Finder, Builder);
3869
0
  return false;
3870
493
}
clang::ast_matchers::internal::matcher_hasType0Matcher<clang::Expr, clang::ast_matchers::internal::Matcher<clang::QualType> >::matches(clang::Expr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3865
3.24k
    internal::Matcher<QualType>, InnerMatcher, 0) {
3866
3.24k
  QualType QT = internal::getUnderlyingType(Node);
3867
3.24k
  if (!QT.isNull())
3868
3.22k
    return InnerMatcher.matches(QT, Finder, Builder);
3869
20
  return false;
3870
3.24k
}
clang::ast_matchers::internal::matcher_hasType0Matcher<clang::FriendDecl, clang::ast_matchers::internal::Matcher<clang::QualType> >::matches(clang::FriendDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3865
20
    internal::Matcher<QualType>, InnerMatcher, 0) {
3866
20
  QualType QT = internal::getUnderlyingType(Node);
3867
20
  if (!QT.isNull())
3868
20
    return InnerMatcher.matches(QT, Finder, Builder);
3869
0
  return false;
3870
20
}
clang::ast_matchers::internal::matcher_hasType0Matcher<clang::TypedefNameDecl, clang::ast_matchers::internal::Matcher<clang::QualType> >::matches(clang::TypedefNameDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3865
26
    internal::Matcher<QualType>, InnerMatcher, 0) {
3866
26
  QualType QT = internal::getUnderlyingType(Node);
3867
26
  if (!QT.isNull())
3868
26
    return InnerMatcher.matches(QT, Finder, Builder);
3869
0
  return false;
3870
26
}
clang::ast_matchers::internal::matcher_hasType0Matcher<clang::ValueDecl, clang::ast_matchers::internal::Matcher<clang::QualType> >::matches(clang::ValueDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3865
96
    internal::Matcher<QualType>, InnerMatcher, 0) {
3866
96
  QualType QT = internal::getUnderlyingType(Node);
3867
96
  if (!QT.isNull())
3868
96
    return InnerMatcher.matches(QT, Finder, Builder);
3869
0
  return false;
3870
96
}
clang::ast_matchers::internal::matcher_hasType0Matcher<clang::CXXBaseSpecifier, clang::ast_matchers::internal::Matcher<clang::QualType> >::matches(clang::CXXBaseSpecifier const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3865
4
    internal::Matcher<QualType>, InnerMatcher, 0) {
3866
4
  QualType QT = internal::getUnderlyingType(Node);
3867
4
  if (!QT.isNull())
3868
4
    return InnerMatcher.matches(QT, Finder, Builder);
3869
0
  return false;
3870
4
}
clang::ast_matchers::internal::matcher_hasType0Matcher<clang::DeclRefExpr, clang::ast_matchers::internal::Matcher<clang::QualType> >::matches(clang::DeclRefExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3865
33
    internal::Matcher<QualType>, InnerMatcher, 0) {
3866
33
  QualType QT = internal::getUnderlyingType(Node);
3867
33
  if (!QT.isNull())
3868
33
    return InnerMatcher.matches(QT, Finder, Builder);
3869
0
  return false;
3870
33
}
3871
3872
/// Overloaded to match the declaration of the expression's or value
3873
/// declaration's type.
3874
///
3875
/// In case of a value declaration (for example a variable declaration),
3876
/// this resolves one layer of indirection. For example, in the value
3877
/// declaration "X x;", cxxRecordDecl(hasName("X")) matches the declaration of
3878
/// X, while varDecl(hasType(cxxRecordDecl(hasName("X")))) matches the
3879
/// declaration of x.
3880
///
3881
/// Example matches x (matcher = expr(hasType(cxxRecordDecl(hasName("X")))))
3882
///             and z (matcher = varDecl(hasType(cxxRecordDecl(hasName("X")))))
3883
///             and friend class X (matcher = friendDecl(hasType("X"))
3884
///             and public virtual X (matcher = cxxBaseSpecifier(hasType(
3885
///                                               cxxRecordDecl(hasName("X"))))
3886
/// \code
3887
///  class X {};
3888
///  void y(X &x) { x; X z; }
3889
///  class Y { friend class X; };
3890
///  class Z : public virtual X {};
3891
/// \endcode
3892
///
3893
/// Example matches class Derived
3894
/// (matcher = cxxRecordDecl(hasAnyBase(hasType(cxxRecordDecl(hasName("Base"))))))
3895
/// \code
3896
/// class Base {};
3897
/// class Derived : Base {};
3898
/// \endcode
3899
///
3900
/// Usable as: Matcher<Expr>, Matcher<FriendDecl>, Matcher<ValueDecl>,
3901
/// Matcher<CXXBaseSpecifier>
3902
AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
3903
    hasType,
3904
    AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, FriendDecl, ValueDecl,
3905
                                    CXXBaseSpecifier),
3906
797
    internal::Matcher<Decl>, InnerMatcher, 1) {
3907
797
  QualType QT = internal::getUnderlyingType(Node);
3908
797
  if (!QT.isNull())
3909
797
    return qualType(hasDeclaration(InnerMatcher)).matches(QT, Finder, Builder);
3910
0
  return false;
3911
797
}
clang::ast_matchers::internal::matcher_hasType1Matcher<clang::VarDecl, clang::ast_matchers::internal::Matcher<clang::Decl> >::matches(clang::VarDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3906
102
    internal::Matcher<Decl>, InnerMatcher, 1) {
3907
102
  QualType QT = internal::getUnderlyingType(Node);
3908
102
  if (!QT.isNull())
3909
102
    return qualType(hasDeclaration(InnerMatcher)).matches(QT, Finder, Builder);
3910
0
  return false;
3911
102
}
clang::ast_matchers::internal::matcher_hasType1Matcher<clang::Expr, clang::ast_matchers::internal::Matcher<clang::Decl> >::matches(clang::Expr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3906
491
    internal::Matcher<Decl>, InnerMatcher, 1) {
3907
491
  QualType QT = internal::getUnderlyingType(Node);
3908
491
  if (!QT.isNull())
3909
491
    return qualType(hasDeclaration(InnerMatcher)).matches(QT, Finder, Builder);
3910
0
  return false;
3911
491
}
clang::ast_matchers::internal::matcher_hasType1Matcher<clang::FriendDecl, clang::ast_matchers::internal::Matcher<clang::Decl> >::matches(clang::FriendDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3906
20
    internal::Matcher<Decl>, InnerMatcher, 1) {
3907
20
  QualType QT = internal::getUnderlyingType(Node);
3908
20
  if (!QT.isNull())
3909
20
    return qualType(hasDeclaration(InnerMatcher)).matches(QT, Finder, Builder);
3910
0
  return false;
3911
20
}
Unexecuted instantiation: clang::ast_matchers::internal::matcher_hasType1Matcher<clang::ValueDecl, clang::ast_matchers::internal::Matcher<clang::Decl> >::matches(clang::ValueDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
clang::ast_matchers::internal::matcher_hasType1Matcher<clang::CXXBaseSpecifier, clang::ast_matchers::internal::Matcher<clang::Decl> >::matches(clang::CXXBaseSpecifier const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3906
184
    internal::Matcher<Decl>, InnerMatcher, 1) {
3907
184
  QualType QT = internal::getUnderlyingType(Node);
3908
184
  if (!QT.isNull())
3909
184
    return qualType(hasDeclaration(InnerMatcher)).matches(QT, Finder, Builder);
3910
0
  return false;
3911
184
}
3912
3913
/// Matches if the type location of a node matches the inner matcher.
3914
///
3915
/// Examples:
3916
/// \code
3917
///   int x;
3918
/// \endcode
3919
/// declaratorDecl(hasTypeLoc(loc(asString("int"))))
3920
///   matches int x
3921
///
3922
/// \code
3923
/// auto x = int(3);
3924
/// \code
3925
/// cxxTemporaryObjectExpr(hasTypeLoc(loc(asString("int"))))
3926
///   matches int(3)
3927
///
3928
/// \code
3929
/// struct Foo { Foo(int, int); };
3930
/// auto x = Foo(1, 2);
3931
/// \code
3932
/// cxxFunctionalCastExpr(hasTypeLoc(loc(asString("struct Foo"))))
3933
///   matches Foo(1, 2)
3934
///
3935
/// Usable as: Matcher<BlockDecl>, Matcher<CXXBaseSpecifier>,
3936
///   Matcher<CXXCtorInitializer>, Matcher<CXXFunctionalCastExpr>,
3937
///   Matcher<CXXNewExpr>, Matcher<CXXTemporaryObjectExpr>,
3938
///   Matcher<CXXUnresolvedConstructExpr>,
3939
///   Matcher<ClassTemplateSpecializationDecl>, Matcher<CompoundLiteralExpr>,
3940
///   Matcher<DeclaratorDecl>, Matcher<ExplicitCastExpr>,
3941
///   Matcher<ObjCPropertyDecl>, Matcher<TemplateArgumentLoc>,
3942
///   Matcher<TypedefNameDecl>
3943
AST_POLYMORPHIC_MATCHER_P(
3944
    hasTypeLoc,
3945
    AST_POLYMORPHIC_SUPPORTED_TYPES(
3946
        BlockDecl, CXXBaseSpecifier, CXXCtorInitializer, CXXFunctionalCastExpr,
3947
        CXXNewExpr, CXXTemporaryObjectExpr, CXXUnresolvedConstructExpr,
3948
        ClassTemplateSpecializationDecl, CompoundLiteralExpr, DeclaratorDecl,
3949
        ExplicitCastExpr, ObjCPropertyDecl, TemplateArgumentLoc,
3950
        TypedefNameDecl),
3951
68
    internal::Matcher<TypeLoc>, Inner) {
3952
68
  TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
3953
68
  if (source == nullptr) {
3954
    // This happens for example for implicit destructors.
3955
20
    return false;
3956
20
  }
3957
48
  return Inner.matches(source->getTypeLoc(), Finder, Builder);
3958
68
}
clang::ast_matchers::internal::matcher_hasTypeLoc0Matcher<clang::BlockDecl, clang::ast_matchers::internal::Matcher<clang::TypeLoc> >::matches(clang::BlockDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3951
2
    internal::Matcher<TypeLoc>, Inner) {
3952
2
  TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
3953
2
  if (source == nullptr) {
3954
    // This happens for example for implicit destructors.
3955
0
    return false;
3956
0
  }
3957
2
  return Inner.matches(source->getTypeLoc(), Finder, Builder);
3958
2
}
clang::ast_matchers::internal::matcher_hasTypeLoc0Matcher<clang::CXXBaseSpecifier, clang::ast_matchers::internal::Matcher<clang::TypeLoc> >::matches(clang::CXXBaseSpecifier const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3951
2
    internal::Matcher<TypeLoc>, Inner) {
3952
2
  TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
3953
2
  if (source == nullptr) {
3954
    // This happens for example for implicit destructors.
3955
0
    return false;
3956
0
  }
3957
2
  return Inner.matches(source->getTypeLoc(), Finder, Builder);
3958
2
}
clang::ast_matchers::internal::matcher_hasTypeLoc0Matcher<clang::CXXCtorInitializer, clang::ast_matchers::internal::Matcher<clang::TypeLoc> >::matches(clang::CXXCtorInitializer const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3951
2
    internal::Matcher<TypeLoc>, Inner) {
3952
2
  TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
3953
2
  if (source == nullptr) {
3954
    // This happens for example for implicit destructors.
3955
0
    return false;
3956
0
  }
3957
2
  return Inner.matches(source->getTypeLoc(), Finder, Builder);
3958
2
}
clang::ast_matchers::internal::matcher_hasTypeLoc0Matcher<clang::CXXFunctionalCastExpr, clang::ast_matchers::internal::Matcher<clang::TypeLoc> >::matches(clang::CXXFunctionalCastExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3951
3
    internal::Matcher<TypeLoc>, Inner) {
3952
3
  TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
3953
3
  if (source == nullptr) {
3954
    // This happens for example for implicit destructors.
3955
0
    return false;
3956
0
  }
3957
3
  return Inner.matches(source->getTypeLoc(), Finder, Builder);
3958
3
}
clang::ast_matchers::internal::matcher_hasTypeLoc0Matcher<clang::CXXNewExpr, clang::ast_matchers::internal::Matcher<clang::TypeLoc> >::matches(clang::CXXNewExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3951
4
    internal::Matcher<TypeLoc>, Inner) {
3952
4
  TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
3953
4
  if (source == nullptr) {
3954
    // This happens for example for implicit destructors.
3955
0
    return false;
3956
0
  }
3957
4
  return Inner.matches(source->getTypeLoc(), Finder, Builder);
3958
4
}
clang::ast_matchers::internal::matcher_hasTypeLoc0Matcher<clang::CXXTemporaryObjectExpr, clang::ast_matchers::internal::Matcher<clang::TypeLoc> >::matches(clang::CXXTemporaryObjectExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3951
3
    internal::Matcher<TypeLoc>, Inner) {
3952
3
  TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
3953
3
  if (source == nullptr) {
3954
    // This happens for example for implicit destructors.
3955
0
    return false;
3956
0
  }
3957
3
  return Inner.matches(source->getTypeLoc(), Finder, Builder);
3958
3
}
clang::ast_matchers::internal::matcher_hasTypeLoc0Matcher<clang::CXXUnresolvedConstructExpr, clang::ast_matchers::internal::Matcher<clang::TypeLoc> >::matches(clang::CXXUnresolvedConstructExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3951
2
    internal::Matcher<TypeLoc>, Inner) {
3952
2
  TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
3953
2
  if (source == nullptr) {
3954
    // This happens for example for implicit destructors.
3955
0
    return false;
3956
0
  }
3957
2
  return Inner.matches(source->getTypeLoc(), Finder, Builder);
3958
2
}
clang::ast_matchers::internal::matcher_hasTypeLoc0Matcher<clang::ClassTemplateSpecializationDecl, clang::ast_matchers::internal::Matcher<clang::TypeLoc> >::matches(clang::ClassTemplateSpecializationDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3951
2
    internal::Matcher<TypeLoc>, Inner) {
3952
2
  TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
3953
2
  if (source == nullptr) {
3954
    // This happens for example for implicit destructors.
3955
0
    return false;
3956
0
  }
3957
2
  return Inner.matches(source->getTypeLoc(), Finder, Builder);
3958
2
}
clang::ast_matchers::internal::matcher_hasTypeLoc0Matcher<clang::CompoundLiteralExpr, clang::ast_matchers::internal::Matcher<clang::TypeLoc> >::matches(clang::CompoundLiteralExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3951
2
    internal::Matcher<TypeLoc>, Inner) {
3952
2
  TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
3953
2
  if (source == nullptr) {
3954
    // This happens for example for implicit destructors.
3955
0
    return false;
3956
0
  }
3957
2
  return Inner.matches(source->getTypeLoc(), Finder, Builder);
3958
2
}
clang::ast_matchers::internal::matcher_hasTypeLoc0Matcher<clang::DeclaratorDecl, clang::ast_matchers::internal::Matcher<clang::TypeLoc> >::matches(clang::DeclaratorDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3951
26
    internal::Matcher<TypeLoc>, Inner) {
3952
26
  TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
3953
26
  if (source == nullptr) {
3954
    // This happens for example for implicit destructors.
3955
20
    return false;
3956
20
  }
3957
6
  return Inner.matches(source->getTypeLoc(), Finder, Builder);
3958
26
}
clang::ast_matchers::internal::matcher_hasTypeLoc0Matcher<clang::ExplicitCastExpr, clang::ast_matchers::internal::Matcher<clang::TypeLoc> >::matches(clang::ExplicitCastExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3951
4
    internal::Matcher<TypeLoc>, Inner) {
3952
4
  TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
3953
4
  if (source == nullptr) {
3954
    // This happens for example for implicit destructors.
3955
0
    return false;
3956
0
  }
3957
4
  return Inner.matches(source->getTypeLoc(), Finder, Builder);
3958
4
}
clang::ast_matchers::internal::matcher_hasTypeLoc0Matcher<clang::ObjCPropertyDecl, clang::ast_matchers::internal::Matcher<clang::TypeLoc> >::matches(clang::ObjCPropertyDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3951
2
    internal::Matcher<TypeLoc>, Inner) {
3952
2
  TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
3953
2
  if (source == nullptr) {
3954
    // This happens for example for implicit destructors.
3955
0
    return false;
3956
0
  }
3957
2
  return Inner.matches(source->getTypeLoc(), Finder, Builder);
3958
2
}
clang::ast_matchers::internal::matcher_hasTypeLoc0Matcher<clang::TemplateArgumentLoc, clang::ast_matchers::internal::Matcher<clang::TypeLoc> >::matches(clang::TemplateArgumentLoc const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3951
2
    internal::Matcher<TypeLoc>, Inner) {
3952
2
  TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
3953
2
  if (source == nullptr) {
3954
    // This happens for example for implicit destructors.
3955
0
    return false;
3956
0
  }
3957
2
  return Inner.matches(source->getTypeLoc(), Finder, Builder);
3958
2
}
clang::ast_matchers::internal::matcher_hasTypeLoc0Matcher<clang::TypedefNameDecl, clang::ast_matchers::internal::Matcher<clang::TypeLoc> >::matches(clang::TypedefNameDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
3951
12
    internal::Matcher<TypeLoc>, Inner) {
3952
12
  TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
3953
12
  if (source == nullptr) {
3954
    // This happens for example for implicit destructors.
3955
0
    return false;
3956
0
  }
3957
12
  return Inner.matches(source->getTypeLoc(), Finder, Builder);
3958
12
}
3959
3960
/// Matches if the matched type is represented by the given string.
3961
///
3962
/// Given
3963
/// \code
3964
///   class Y { public: void x(); };
3965
///   void z() { Y* y; y->x(); }
3966
/// \endcode
3967
/// cxxMemberCallExpr(on(hasType(asString("class Y *"))))
3968
///   matches y->x()
3969
941
AST_MATCHER_P(QualType, asString, std::string, Name) {
3970
941
  return Name == Node.getAsString();
3971
941
}
3972
3973
/// Matches if the matched type is a pointer type and the pointee type
3974
/// matches the specified matcher.
3975
///
3976
/// Example matches y->x()
3977
///   (matcher = cxxMemberCallExpr(on(hasType(pointsTo
3978
///      cxxRecordDecl(hasName("Y")))))))
3979
/// \code
3980
///   class Y { public: void x(); };
3981
///   void z() { Y *y; y->x(); }
3982
/// \endcode
3983
AST_MATCHER_P(
3984
    QualType, pointsTo, internal::Matcher<QualType>,
3985
638
    InnerMatcher) {
3986
638
  return (!Node.isNull() && Node->isAnyPointerType() &&
3987
638
          
InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)440
);
3988
638
}
3989
3990
/// Overloaded to match the pointee type's declaration.
3991
AST_MATCHER_P_OVERLOAD(QualType, pointsTo, internal::Matcher<Decl>,
3992
388
                       InnerMatcher, 1) {
3993
388
  return pointsTo(qualType(hasDeclaration(InnerMatcher)))
3994
388
      .matches(Node, Finder, Builder);
3995
388
}
3996
3997
/// Matches if the matched type matches the unqualified desugared
3998
/// type of the matched node.
3999
///
4000
/// For example, in:
4001
/// \code
4002
///   class A {};
4003
///   using B = A;
4004
/// \endcode
4005
/// The matcher type(hasUnqualifiedDesugaredType(recordType())) matches
4006
/// both B and A.
4007
AST_MATCHER_P(Type, hasUnqualifiedDesugaredType, internal::Matcher<Type>,
4008
616
              InnerMatcher) {
4009
616
  return InnerMatcher.matches(*Node.getUnqualifiedDesugaredType(), Finder,
4010
616
                              Builder);
4011
616
}
4012
4013
/// Matches if the matched type is a reference type and the referenced
4014
/// type matches the specified matcher.
4015
///
4016
/// Example matches X &x and const X &y
4017
///     (matcher = varDecl(hasType(references(cxxRecordDecl(hasName("X"))))))
4018
/// \code
4019
///   class X {
4020
///     void a(X b) {
4021
///       X &x = b;
4022
///       const X &y = b;
4023
///     }
4024
///   };
4025
/// \endcode
4026
AST_MATCHER_P(QualType, references, internal::Matcher<QualType>,
4027
325
              InnerMatcher) {
4028
325
  return (!Node.isNull() && Node->isReferenceType() &&
4029
325
          
InnerMatcher.matches(Node->getPointeeType(), Finder, Builder)105
);
4030
325
}
4031
4032
/// Matches QualTypes whose canonical type matches InnerMatcher.
4033
///
4034
/// Given:
4035
/// \code
4036
///   typedef int &int_ref;
4037
///   int a;
4038
///   int_ref b = a;
4039
/// \endcode
4040
///
4041
/// \c varDecl(hasType(qualType(referenceType()))))) will not match the
4042
/// declaration of b but \c
4043
/// varDecl(hasType(qualType(hasCanonicalType(referenceType())))))) does.
4044
AST_MATCHER_P(QualType, hasCanonicalType, internal::Matcher<QualType>,
4045
2.56k
              InnerMatcher) {
4046
2.56k
  if (Node.isNull())
4047
0
    return false;
4048
2.56k
  return InnerMatcher.matches(Node.getCanonicalType(), Finder, Builder);
4049
2.56k
}
4050
4051
/// Overloaded to match the referenced type's declaration.
4052
AST_MATCHER_P_OVERLOAD(QualType, references, internal::Matcher<Decl>,
4053
218
                       InnerMatcher, 1) {
4054
218
  return references(qualType(hasDeclaration(InnerMatcher)))
4055
218
      .matches(Node, Finder, Builder);
4056
218
}
4057
4058
/// Matches on the implicit object argument of a member call expression. Unlike
4059
/// `on`, matches the argument directly without stripping away anything.
4060
///
4061
/// Given
4062
/// \code
4063
///   class Y { public: void m(); };
4064
///   Y g();
4065
///   class X : public Y { void g(); };
4066
///   void z(Y y, X x) { y.m(); x.m(); x.g(); (g()).m(); }
4067
/// \endcode
4068
/// cxxMemberCallExpr(onImplicitObjectArgument(hasType(
4069
///     cxxRecordDecl(hasName("Y")))))
4070
///   matches `y.m()`, `x.m()` and (g()).m(), but not `x.g()`.
4071
/// cxxMemberCallExpr(on(callExpr()))
4072
///   does not match `(g()).m()`, because the parens are not ignored.
4073
///
4074
/// FIXME: Overload to allow directly matching types?
4075
AST_MATCHER_P(CXXMemberCallExpr, onImplicitObjectArgument,
4076
174
              internal::Matcher<Expr>, InnerMatcher) {
4077
174
  const Expr *ExprNode = Node.getImplicitObjectArgument();
4078
174
  return (ExprNode != nullptr &&
4079
174
          InnerMatcher.matches(*ExprNode, Finder, Builder));
4080
174
}
4081
4082
/// Matches if the type of the expression's implicit object argument either
4083
/// matches the InnerMatcher, or is a pointer to a type that matches the
4084
/// InnerMatcher.
4085
///
4086
/// Given
4087
/// \code
4088
///   class Y { public: void m(); };
4089
///   class X : public Y { void g(); };
4090
///   void z() { Y y; y.m(); Y *p; p->m(); X x; x.m(); x.g(); }
4091
/// \endcode
4092
/// cxxMemberCallExpr(thisPointerType(hasDeclaration(
4093
///     cxxRecordDecl(hasName("Y")))))
4094
///   matches `y.m()`, `p->m()` and `x.m()`.
4095
/// cxxMemberCallExpr(thisPointerType(hasDeclaration(
4096
///     cxxRecordDecl(hasName("X")))))
4097
///   matches `x.g()`.
4098
AST_MATCHER_P_OVERLOAD(CXXMemberCallExpr, thisPointerType,
4099
0
                       internal::Matcher<QualType>, InnerMatcher, 0) {
4100
0
  return onImplicitObjectArgument(
4101
0
      anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher))))
4102
0
      .matches(Node, Finder, Builder);
4103
0
}
4104
4105
/// Overloaded to match the type's declaration.
4106
AST_MATCHER_P_OVERLOAD(CXXMemberCallExpr, thisPointerType,
4107
140
                       internal::Matcher<Decl>, InnerMatcher, 1) {
4108
140
  return onImplicitObjectArgument(
4109
140
      anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher))))
4110
140
      .matches(Node, Finder, Builder);
4111
140
}
4112
4113
/// Matches a DeclRefExpr that refers to a declaration that matches the
4114
/// specified matcher.
4115
///
4116
/// Example matches x in if(x)
4117
///     (matcher = declRefExpr(to(varDecl(hasName("x")))))
4118
/// \code
4119
///   bool x;
4120
///   if (x) {}
4121
/// \endcode
4122
AST_MATCHER_P(DeclRefExpr, to, internal::Matcher<Decl>,
4123
3.26k
              InnerMatcher) {
4124
3.26k
  const Decl *DeclNode = Node.getDecl();
4125
3.26k
  return (DeclNode != nullptr &&
4126
3.26k
          InnerMatcher.matches(*DeclNode, Finder, Builder));
4127
3.26k
}
4128
4129
/// Matches a \c DeclRefExpr that refers to a declaration through a
4130
/// specific using shadow declaration.
4131
///
4132
/// Given
4133
/// \code
4134
///   namespace a { void f() {} }
4135
///   using a::f;
4136
///   void g() {
4137
///     f();     // Matches this ..
4138
///     a::f();  // .. but not this.
4139
///   }
4140
/// \endcode
4141
/// declRefExpr(throughUsingDecl(anything()))
4142
///   matches \c f()
4143
AST_MATCHER_P(DeclRefExpr, throughUsingDecl,
4144
4
              internal::Matcher<UsingShadowDecl>, InnerMatcher) {
4145
4
  const NamedDecl *FoundDecl = Node.getFoundDecl();
4146
4
  if (const UsingShadowDecl *UsingDecl = dyn_cast<UsingShadowDecl>(FoundDecl))
4147
2
    return InnerMatcher.matches(*UsingDecl, Finder, Builder);
4148
2
  return false;
4149
4
}
4150
4151
/// Matches an \c OverloadExpr if any of the declarations in the set of
4152
/// overloads matches the given matcher.
4153
///
4154
/// Given
4155
/// \code
4156
///   template <typename T> void foo(T);
4157
///   template <typename T> void bar(T);
4158
///   template <typename T> void baz(T t) {
4159
///     foo(t);
4160
///     bar(t);
4161
///   }
4162
/// \endcode
4163
/// unresolvedLookupExpr(hasAnyDeclaration(
4164
///     functionTemplateDecl(hasName("foo"))))
4165
///   matches \c foo in \c foo(t); but not \c bar in \c bar(t);
4166
AST_MATCHER_P(OverloadExpr, hasAnyDeclaration, internal::Matcher<Decl>,
4167
14
              InnerMatcher) {
4168
14
  return matchesFirstInPointerRange(InnerMatcher, Node.decls_begin(),
4169
14
                                    Node.decls_end(), Finder,
4170
14
                                    Builder) != Node.decls_end();
4171
14
}
4172
4173
/// Matches the Decl of a DeclStmt which has a single declaration.
4174
///
4175
/// Given
4176
/// \code
4177
///   int a, b;
4178
///   int c;
4179
/// \endcode
4180
/// declStmt(hasSingleDecl(anything()))
4181
///   matches 'int c;' but not 'int a, b;'.
4182
424
AST_MATCHER_P(DeclStmt, hasSingleDecl, internal::Matcher<Decl>, InnerMatcher) {
4183
424
  if (Node.isSingleDecl()) {
4184
422
    const Decl *FoundDecl = Node.getSingleDecl();
4185
422
    return InnerMatcher.matches(*FoundDecl, Finder, Builder);
4186
422
  }
4187
2
  return false;
4188
424
}
4189
4190
/// Matches a variable declaration that has an initializer expression
4191
/// that matches the given matcher.
4192
///
4193
/// Example matches x (matcher = varDecl(hasInitializer(callExpr())))
4194
/// \code
4195
///   bool y() { return true; }
4196
///   bool x = y();
4197
/// \endcode
4198
AST_MATCHER_P(
4199
    VarDecl, hasInitializer, internal::Matcher<Expr>,
4200
1.39k
    InnerMatcher) {
4201
1.39k
  const Expr *Initializer = Node.getAnyInitializer();
4202
1.39k
  return (Initializer != nullptr &&
4203
1.39k
          
InnerMatcher.matches(*Initializer, Finder, Builder)1.13k
);
4204
1.39k
}
4205
4206
/// \brief Matches a static variable with local scope.
4207
///
4208
/// Example matches y (matcher = varDecl(isStaticLocal()))
4209
/// \code
4210
/// void f() {
4211
///   int x;
4212
///   static int y;
4213
/// }
4214
/// static int z;
4215
/// \endcode
4216
112
AST_MATCHER(VarDecl, isStaticLocal) {
4217
112
  return Node.isStaticLocal();
4218
112
}
4219
4220
/// Matches a variable declaration that has function scope and is a
4221
/// non-static local variable.
4222
///
4223
/// Example matches x (matcher = varDecl(hasLocalStorage())
4224
/// \code
4225
/// void f() {
4226
///   int x;
4227
///   static int y;
4228
/// }
4229
/// int z;
4230
/// \endcode
4231
84
AST_MATCHER(VarDecl, hasLocalStorage) {
4232
84
  return Node.hasLocalStorage();
4233
84
}
4234
4235
/// Matches a variable declaration that does not have local storage.
4236
///
4237
/// Example matches y and z (matcher = varDecl(hasGlobalStorage())
4238
/// \code
4239
/// void f() {
4240
///   int x;
4241
///   static int y;
4242
/// }
4243
/// int z;
4244
/// \endcode
4245
84
AST_MATCHER(VarDecl, hasGlobalStorage) {
4246
84
  return Node.hasGlobalStorage();
4247
84
}
4248
4249
/// Matches a variable declaration that has automatic storage duration.
4250
///
4251
/// Example matches x, but not y, z, or a.
4252
/// (matcher = varDecl(hasAutomaticStorageDuration())
4253
/// \code
4254
/// void f() {
4255
///   int x;
4256
///   static int y;
4257
///   thread_local int z;
4258
/// }
4259
/// int a;
4260
/// \endcode
4261
84
AST_MATCHER(VarDecl, hasAutomaticStorageDuration) {
4262
84
  return Node.getStorageDuration() == SD_Automatic;
4263
84
}
4264
4265
/// Matches a variable declaration that has static storage duration.
4266
/// It includes the variable declared at namespace scope and those declared
4267
/// with "static" and "extern" storage class specifiers.
4268
///
4269
/// \code
4270
/// void f() {
4271
///   int x;
4272
///   static int y;
4273
///   thread_local int z;
4274
/// }
4275
/// int a;
4276
/// static int b;
4277
/// extern int c;
4278
/// varDecl(hasStaticStorageDuration())
4279
///   matches the function declaration y, a, b and c.
4280
/// \endcode
4281
144
AST_MATCHER(VarDecl, hasStaticStorageDuration) {
4282
144
  return Node.getStorageDuration() == SD_Static;
4283
144
}
4284
4285
/// Matches a variable declaration that has thread storage duration.
4286
///
4287
/// Example matches z, but not x, z, or a.
4288
/// (matcher = varDecl(hasThreadStorageDuration())
4289
/// \code
4290
/// void f() {
4291
///   int x;
4292
///   static int y;
4293
///   thread_local int z;
4294
/// }
4295
/// int a;
4296
/// \endcode
4297
92
AST_MATCHER(VarDecl, hasThreadStorageDuration) {
4298
92
  return Node.getStorageDuration() == SD_Thread;
4299
92
}
4300
4301
/// Matches a variable declaration that is an exception variable from
4302
/// a C++ catch block, or an Objective-C \@catch statement.
4303
///
4304
/// Example matches x (matcher = varDecl(isExceptionVariable())
4305
/// \code
4306
/// void f(int y) {
4307
///   try {
4308
///   } catch (int x) {
4309
///   }
4310
/// }
4311
/// \endcode
4312
40
AST_MATCHER(VarDecl, isExceptionVariable) {
4313
40
  return Node.isExceptionVariable();
4314
40
}
4315
4316
/// Checks that a call expression or a constructor call expression has
4317
/// a specific number of arguments (including absent default arguments).
4318
///
4319
/// Example matches f(0, 0) (matcher = callExpr(argumentCountIs(2)))
4320
/// \code
4321
///   void f(int x, int y);
4322
///   f(0, 0);
4323
/// \endcode
4324
AST_POLYMORPHIC_MATCHER_P(argumentCountIs,
4325
                          AST_POLYMORPHIC_SUPPORTED_TYPES(
4326
                              CallExpr, CXXConstructExpr,
4327
                              CXXUnresolvedConstructExpr, ObjCMessageExpr),
4328
210
                          unsigned, N) {
4329
210
  unsigned NumArgs = Node.getNumArgs();
4330
210
  if (!Finder->isTraversalIgnoringImplicitNodes())
4331
206
    return NumArgs == N;
4332
8
  
while (4
NumArgs) {
4333
8
    if (!isa<CXXDefaultArgExpr>(Node.getArg(NumArgs - 1)))
4334
4
      break;
4335
4
    --NumArgs;
4336
4
  }
4337
4
  return NumArgs == N;
4338
210
}
clang::ast_matchers::internal::matcher_argumentCountIs0Matcher<clang::CallExpr, unsigned int>::matches(clang::CallExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
4328
87
                          unsigned, N) {
4329
87
  unsigned NumArgs = Node.getNumArgs();
4330
87
  if (!Finder->isTraversalIgnoringImplicitNodes())
4331
83
    return NumArgs == N;
4332
8
  
while (4
NumArgs) {
4333
8
    if (!isa<CXXDefaultArgExpr>(Node.getArg(NumArgs - 1)))
4334
4
      break;
4335
4
    --NumArgs;
4336
4
  }
4337
4
  return NumArgs == N;
4338
87
}
clang::ast_matchers::internal::matcher_argumentCountIs0Matcher<clang::CXXConstructExpr, unsigned int>::matches(clang::CXXConstructExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
4328
104
                          unsigned, N) {
4329
104
  unsigned NumArgs = Node.getNumArgs();
4330
104
  if (!Finder->isTraversalIgnoringImplicitNodes())
4331
104
    return NumArgs == N;
4332
0
  while (NumArgs) {
4333
0
    if (!isa<CXXDefaultArgExpr>(Node.getArg(NumArgs - 1)))
4334
0
      break;
4335
0
    --NumArgs;
4336
0
  }
4337
0
  return NumArgs == N;
4338
104
}
clang::ast_matchers::internal::matcher_argumentCountIs0Matcher<clang::CXXUnresolvedConstructExpr, unsigned int>::matches(clang::CXXUnresolvedConstructExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
4328
4
                          unsigned, N) {
4329
4
  unsigned NumArgs = Node.getNumArgs();
4330
4
  if (!Finder->isTraversalIgnoringImplicitNodes())
4331
4
    return NumArgs == N;
4332
0
  while (NumArgs) {
4333
0
    if (!isa<CXXDefaultArgExpr>(Node.getArg(NumArgs - 1)))
4334
0
      break;
4335
0
    --NumArgs;
4336
0
  }
4337
0
  return NumArgs == N;
4338
4
}
clang::ast_matchers::internal::matcher_argumentCountIs0Matcher<clang::ObjCMessageExpr, unsigned int>::matches(clang::ObjCMessageExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
4328
2
                          unsigned, N) {
4329
2
  unsigned NumArgs = Node.getNumArgs();
4330
2
  if (!Finder->isTraversalIgnoringImplicitNodes())
4331
2
    return NumArgs == N;
4332
0
  while (NumArgs) {
4333
0
    if (!isa<CXXDefaultArgExpr>(Node.getArg(NumArgs - 1)))
4334
0
      break;
4335
0
    --NumArgs;
4336
0
  }
4337
0
  return NumArgs == N;
4338
2
}
clang::ast_matchers::internal::matcher_argumentCountIs0Matcher<clang::CXXOperatorCallExpr, unsigned int>::matches(clang::CXXOperatorCallExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
4328
13
                          unsigned, N) {
4329
13
  unsigned NumArgs = Node.getNumArgs();
4330
13
  if (!Finder->isTraversalIgnoringImplicitNodes())
4331
13
    return NumArgs == N;
4332
0
  while (NumArgs) {
4333
0
    if (!isa<CXXDefaultArgExpr>(Node.getArg(NumArgs - 1)))
4334
0
      break;
4335
0
    --NumArgs;
4336
0
  }
4337
0
  return NumArgs == N;
4338
13
}
4339
4340
/// Matches the n'th argument of a call expression or a constructor
4341
/// call expression.
4342
///
4343
/// Example matches y in x(y)
4344
///     (matcher = callExpr(hasArgument(0, declRefExpr())))
4345
/// \code
4346
///   void x(int) { int y; x(y); }
4347
/// \endcode
4348
AST_POLYMORPHIC_MATCHER_P2(hasArgument,
4349
                           AST_POLYMORPHIC_SUPPORTED_TYPES(
4350
                               CallExpr, CXXConstructExpr,
4351
                               CXXUnresolvedConstructExpr, ObjCMessageExpr),
4352
821
                           unsigned, N, internal::Matcher<Expr>, InnerMatcher) {
4353
821
  if (N >= Node.getNumArgs())
4354
22
    return false;
4355
799
  const Expr *Arg = Node.getArg(N);
4356
799
  if (Finder->isTraversalIgnoringImplicitNodes() && 
isa<CXXDefaultArgExpr>(Arg)91
)
4357
2
    return false;
4358
797
  return InnerMatcher.matches(*Arg->IgnoreParenImpCasts(), Finder, Builder);
4359
799
}
clang::ast_matchers::internal::matcher_hasArgument0Matcher<clang::CallExpr, unsigned int, clang::ast_matchers::internal::Matcher<clang::Expr> >::matches(clang::CallExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
4352
574
                           unsigned, N, internal::Matcher<Expr>, InnerMatcher) {
4353
574
  if (N >= Node.getNumArgs())
4354
2
    return false;
4355
572
  const Expr *Arg = Node.getArg(N);
4356
572
  if (Finder->isTraversalIgnoringImplicitNodes() && 
isa<CXXDefaultArgExpr>(Arg)44
)
4357
2
    return false;
4358
570
  return InnerMatcher.matches(*Arg->IgnoreParenImpCasts(), Finder, Builder);
4359
572
}
clang::ast_matchers::internal::matcher_hasArgument0Matcher<clang::CXXConstructExpr, unsigned int, clang::ast_matchers::internal::Matcher<clang::Expr> >::matches(clang::CXXConstructExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
4352
170
                           unsigned, N, internal::Matcher<Expr>, InnerMatcher) {
4353
170
  if (N >= Node.getNumArgs())
4354
20
    return false;
4355
150
  const Expr *Arg = Node.getArg(N);
4356
150
  if (Finder->isTraversalIgnoringImplicitNodes() && 
isa<CXXDefaultArgExpr>(Arg)42
)
4357
0
    return false;
4358
150
  return InnerMatcher.matches(*Arg->IgnoreParenImpCasts(), Finder, Builder);
4359
150
}
clang::ast_matchers::internal::matcher_hasArgument0Matcher<clang::CXXUnresolvedConstructExpr, unsigned int, clang::ast_matchers::internal::Matcher<clang::Expr> >::matches(clang::CXXUnresolvedConstructExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
4352
4
                           unsigned, N, internal::Matcher<Expr>, InnerMatcher) {
4353
4
  if (N >= Node.getNumArgs())
4354
0
    return false;
4355
4
  const Expr *Arg = Node.getArg(N);
4356
4
  if (Finder->isTraversalIgnoringImplicitNodes() && 
isa<CXXDefaultArgExpr>(Arg)0
)
4357
0
    return false;
4358
4
  return InnerMatcher.matches(*Arg->IgnoreParenImpCasts(), Finder, Builder);
4359
4
}
Unexecuted instantiation: clang::ast_matchers::internal::matcher_hasArgument0Matcher<clang::ObjCMessageExpr, unsigned int, clang::ast_matchers::internal::Matcher<clang::Expr> >::matches(clang::ObjCMessageExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
clang::ast_matchers::internal::matcher_hasArgument0Matcher<clang::CXXOperatorCallExpr, unsigned int, clang::ast_matchers::internal::Matcher<clang::Expr> >::matches(clang::CXXOperatorCallExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
4352
73
                           unsigned, N, internal::Matcher<Expr>, InnerMatcher) {
4353
73
  if (N >= Node.getNumArgs())
4354
0
    return false;
4355
73
  const Expr *Arg = Node.getArg(N);
4356
73
  if (Finder->isTraversalIgnoringImplicitNodes() && 
isa<CXXDefaultArgExpr>(Arg)5
)
4357
0
    return false;
4358
73
  return InnerMatcher.matches(*Arg->IgnoreParenImpCasts(), Finder, Builder);
4359
73
}
4360
4361
/// Matches the n'th item of an initializer list expression.
4362
///
4363
/// Example matches y.
4364
///     (matcher = initListExpr(hasInit(0, expr())))
4365
/// \code
4366
///   int x{y}.
4367
/// \endcode
4368
AST_MATCHER_P2(InitListExpr, hasInit, unsigned, N,
4369
32
               ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
4370
32
  return N < Node.getNumInits() &&
4371
32
          
InnerMatcher.matches(*Node.getInit(N), Finder, Builder)16
;
4372
32
}
4373
4374
/// Matches declaration statements that contain a specific number of
4375
/// declarations.
4376
///
4377
/// Example: Given
4378
/// \code
4379
///   int a, b;
4380
///   int c;
4381
///   int d = 2, e;
4382
/// \endcode
4383
/// declCountIs(2)
4384
///   matches 'int a, b;' and 'int d = 2, e;', but not 'int c;'.
4385
112
AST_MATCHER_P(DeclStmt, declCountIs, unsigned, N) {
4386
112
  return std::distance(Node.decl_begin(), Node.decl_end()) == (ptrdiff_t)N;
4387
112
}
4388
4389
/// Matches the n'th declaration of a declaration statement.
4390
///
4391
/// Note that this does not work for global declarations because the AST
4392
/// breaks up multiple-declaration DeclStmt's into multiple single-declaration
4393
/// DeclStmt's.
4394
/// Example: Given non-global declarations
4395
/// \code
4396
///   int a, b = 0;
4397
///   int c;
4398
///   int d = 2, e;
4399
/// \endcode
4400
/// declStmt(containsDeclaration(
4401
///       0, varDecl(hasInitializer(anything()))))
4402
///   matches only 'int d = 2, e;', and
4403
/// declStmt(containsDeclaration(1, varDecl()))
4404
/// \code
4405
///   matches 'int a, b = 0' as well as 'int d = 2, e;'
4406
///   but 'int c;' is not matched.
4407
/// \endcode
4408
AST_MATCHER_P2(DeclStmt, containsDeclaration, unsigned, N,
4409
9
               internal::Matcher<Decl>, InnerMatcher) {
4410
9
  const unsigned NumDecls = std::distance(Node.decl_begin(), Node.decl_end());
4411
9
  if (N >= NumDecls)
4412
2
    return false;
4413
7
  DeclStmt::const_decl_iterator Iterator = Node.decl_begin();
4414
7
  std::advance(Iterator, N);
4415
7
  return InnerMatcher.matches(**Iterator, Finder, Builder);
4416
9
}
4417
4418
/// Matches a C++ catch statement that has a catch-all handler.
4419
///
4420
/// Given
4421
/// \code
4422
///   try {
4423
///     // ...
4424
///   } catch (int) {
4425
///     // ...
4426
///   } catch (...) {
4427
///     // ...
4428
///   }
4429
/// \endcode
4430
/// cxxCatchStmt(isCatchAll()) matches catch(...) but not catch(int).
4431
60
AST_MATCHER(CXXCatchStmt, isCatchAll) {
4432
60
  return Node.getExceptionDecl() == nullptr;
4433
60
}
4434
4435
/// Matches a constructor initializer.
4436
///
4437
/// Given
4438
/// \code
4439
///   struct Foo {
4440
///     Foo() : foo_(1) { }
4441
///     int foo_;
4442
///   };
4443
/// \endcode
4444
/// cxxRecordDecl(has(cxxConstructorDecl(
4445
///   hasAnyConstructorInitializer(anything())
4446
/// )))
4447
///   record matches Foo, hasAnyConstructorInitializer matches foo_(1)
4448
AST_MATCHER_P(CXXConstructorDecl, hasAnyConstructorInitializer,
4449
128
              internal::Matcher<CXXCtorInitializer>, InnerMatcher) {
4450
128
  auto MatchIt = matchesFirstInPointerRange(InnerMatcher, Node.init_begin(),
4451
128
                                            Node.init_end(), Finder, Builder);
4452
128
  if (MatchIt == Node.init_end())
4453
96
    return false;
4454
32
  return (*MatchIt)->isWritten() || 
!Finder->isTraversalIgnoringImplicitNodes()8
;
4455
128
}
4456
4457
/// Matches the field declaration of a constructor initializer.
4458
///
4459
/// Given
4460
/// \code
4461
///   struct Foo {
4462
///     Foo() : foo_(1) { }
4463
///     int foo_;
4464
///   };
4465
/// \endcode
4466
/// cxxRecordDecl(has(cxxConstructorDecl(hasAnyConstructorInitializer(
4467
///     forField(hasName("foo_"))))))
4468
///   matches Foo
4469
/// with forField matching foo_
4470
AST_MATCHER_P(CXXCtorInitializer, forField,
4471
121
              internal::Matcher<FieldDecl>, InnerMatcher) {
4472
121
  const FieldDecl *NodeAsDecl = Node.getAnyMember();
4473
121
  return (NodeAsDecl != nullptr &&
4474
121
      
InnerMatcher.matches(*NodeAsDecl, Finder, Builder)91
);
4475
121
}
4476
4477
/// Matches the initializer expression of a constructor initializer.
4478
///
4479
/// Given
4480
/// \code
4481
///   struct Foo {
4482
///     Foo() : foo_(1) { }
4483
///     int foo_;
4484
///   };
4485
/// \endcode
4486
/// cxxRecordDecl(has(cxxConstructorDecl(hasAnyConstructorInitializer(
4487
///     withInitializer(integerLiteral(equals(1)))))))
4488
///   matches Foo
4489
/// with withInitializer matching (1)
4490
AST_MATCHER_P(CXXCtorInitializer, withInitializer,
4491
11
              internal::Matcher<Expr>, InnerMatcher) {
4492
11
  const Expr* NodeAsExpr = Node.getInit();
4493
11
  return (NodeAsExpr != nullptr &&
4494
11
      InnerMatcher.matches(*NodeAsExpr, Finder, Builder));
4495
11
}
4496
4497
/// Matches a constructor initializer if it is explicitly written in
4498
/// code (as opposed to implicitly added by the compiler).
4499
///
4500
/// Given
4501
/// \code
4502
///   struct Foo {
4503
///     Foo() { }
4504
///     Foo(int) : foo_("A") { }
4505
///     string foo_;
4506
///   };
4507
/// \endcode
4508
/// cxxConstructorDecl(hasAnyConstructorInitializer(isWritten()))
4509
///   will match Foo(int), but not Foo()
4510
18
AST_MATCHER(CXXCtorInitializer, isWritten) {
4511
18
  return Node.isWritten();
4512
18
}
4513
4514
/// Matches a constructor initializer if it is initializing a base, as
4515
/// opposed to a member.
4516
///
4517
/// Given
4518
/// \code
4519
///   struct B {};
4520
///   struct D : B {
4521
///     int I;
4522
///     D(int i) : I(i) {}
4523
///   };
4524
///   struct E : B {
4525
///     E() : B() {}
4526
///   };
4527
/// \endcode
4528
/// cxxConstructorDecl(hasAnyConstructorInitializer(isBaseInitializer()))
4529
///   will match E(), but not match D(int).
4530
16
AST_MATCHER(CXXCtorInitializer, isBaseInitializer) {
4531
16
  return Node.isBaseInitializer();
4532
16
}
4533
4534
/// Matches a constructor initializer if it is initializing a member, as
4535
/// opposed to a base.
4536
///
4537
/// Given
4538
/// \code
4539
///   struct B {};
4540
///   struct D : B {
4541
///     int I;
4542
///     D(int i) : I(i) {}
4543
///   };
4544
///   struct E : B {
4545
///     E() : B() {}
4546
///   };
4547
/// \endcode
4548
/// cxxConstructorDecl(hasAnyConstructorInitializer(isMemberInitializer()))
4549
///   will match D(int), but not match E().
4550
12
AST_MATCHER(CXXCtorInitializer, isMemberInitializer) {
4551
12
  return Node.isMemberInitializer();
4552
12
}
4553
4554
/// Matches any argument of a call expression or a constructor call
4555
/// expression, or an ObjC-message-send expression.
4556
///
4557
/// Given
4558
/// \code
4559
///   void x(int, int, int) { int y; x(1, y, 42); }
4560
/// \endcode
4561
/// callExpr(hasAnyArgument(declRefExpr()))
4562
///   matches x(1, y, 42)
4563
/// with hasAnyArgument(...)
4564
///   matching y
4565
///
4566
/// For ObjectiveC, given
4567
/// \code
4568
///   @interface I - (void) f:(int) y; @end
4569
///   void foo(I *i) { [i f:12]; }
4570
/// \endcode
4571
/// objcMessageExpr(hasAnyArgument(integerLiteral(equals(12))))
4572
///   matches [i f:12]
4573
AST_POLYMORPHIC_MATCHER_P(hasAnyArgument,
4574
                          AST_POLYMORPHIC_SUPPORTED_TYPES(
4575
                              CallExpr, CXXConstructExpr,
4576
                              CXXUnresolvedConstructExpr, ObjCMessageExpr),
4577
557
                          internal::Matcher<Expr>, InnerMatcher) {
4578
557
  for (const Expr *Arg : Node.arguments()) {
4579
508
    if (Finder->isTraversalIgnoringImplicitNodes() &&
4580
508
        
isa<CXXDefaultArgExpr>(Arg)26
)
4581
2
      break;
4582
506
    BoundNodesTreeBuilder Result(*Builder);
4583
506
    if (InnerMatcher.matches(*Arg, Finder, &Result)) {
4584
138
      *Builder = std::move(Result);
4585
138
      return true;
4586
138
    }
4587
506
  }
4588
419
  return false;
4589
557
}
clang::ast_matchers::internal::matcher_hasAnyArgument0Matcher<clang::CallExpr, clang::ast_matchers::internal::Matcher<clang::Expr> >::matches(clang::CallExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
4577
472
                          internal::Matcher<Expr>, InnerMatcher) {
4578
472
  for (const Expr *Arg : Node.arguments()) {
4579
413
    if (Finder->isTraversalIgnoringImplicitNodes() &&
4580
413
        
isa<CXXDefaultArgExpr>(Arg)16
)
4581
2
      break;
4582
411
    BoundNodesTreeBuilder Result(*Builder);
4583
411
    if (InnerMatcher.matches(*Arg, Finder, &Result)) {
4584
89
      *Builder = std::move(Result);
4585
89
      return true;
4586
89
    }
4587
411
  }
4588
383
  return false;
4589
472
}
clang::ast_matchers::internal::matcher_hasAnyArgument0Matcher<clang::ObjCMessageExpr, clang::ast_matchers::internal::Matcher<clang::Expr> >::matches(clang::ObjCMessageExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
4577
61
                          internal::Matcher<Expr>, InnerMatcher) {
4578
64
  for (const Expr *Arg : Node.arguments()) {
4579
64
    if (Finder->isTraversalIgnoringImplicitNodes() &&
4580
64
        
isa<CXXDefaultArgExpr>(Arg)0
)
4581
0
      break;
4582
64
    BoundNodesTreeBuilder Result(*Builder);
4583
64
    if (InnerMatcher.matches(*Arg, Finder, &Result)) {
4584
30
      *Builder = std::move(Result);
4585
30
      return true;
4586
30
    }
4587
64
  }
4588
31
  return false;
4589
61
}
clang::ast_matchers::internal::matcher_hasAnyArgument0Matcher<clang::CXXConstructExpr, clang::ast_matchers::internal::Matcher<clang::Expr> >::matches(clang::CXXConstructExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
4577
16
                          internal::Matcher<Expr>, InnerMatcher) {
4578
20
  for (const Expr *Arg : Node.arguments()) {
4579
20
    if (Finder->isTraversalIgnoringImplicitNodes() &&
4580
20
        
isa<CXXDefaultArgExpr>(Arg)10
)
4581
0
      break;
4582
20
    BoundNodesTreeBuilder Result(*Builder);
4583
20
    if (InnerMatcher.matches(*Arg, Finder, &Result)) {
4584
14
      *Builder = std::move(Result);
4585
14
      return true;
4586
14
    }
4587
20
  }
4588
2
  return false;
4589
16
}
clang::ast_matchers::internal::matcher_hasAnyArgument0Matcher<clang::CXXUnresolvedConstructExpr, clang::ast_matchers::internal::Matcher<clang::Expr> >::matches(clang::CXXUnresolvedConstructExpr const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
Line
Count
Source
4577
8
                          internal::Matcher<Expr>, InnerMatcher) {
4578
11
  for (const Expr *Arg : Node.arguments()) {
4579
11
    if (Finder->isTraversalIgnoringImplicitNodes() &&
4580
11
        
isa<CXXDefaultArgExpr>(Arg)0
)
4581
0
      break;
4582
11
    BoundNodesTreeBuilder Result(*Builder);
4583
11
    if (InnerMatcher.matches(*Arg, Finder, &Result)) {
4584
5
      *Builder = std::move(Result);
4585
5
      return true;
4586
5
    }
4587
11
  }
4588
3
  return false;
4589
8
}
4590
4591
/// Matches any capture of a lambda expression.
4592
///
4593
/// Given
4594
/// \code
4595
///   void foo() {
4596
///     int x;
4597
///     auto f = [x](){};
4598
///   }
4599
/// \endcode
4600
/// lambdaExpr(hasAnyCapture(anything()))
4601
///   matches [x](){};
4602
AST_MATCHER_P_OVERLOAD(LambdaExpr, hasAnyCapture, internal::Matcher<VarDecl>,
4603
10
                       InnerMatcher, 0) {
4604
10
  for (const LambdaCapture &Capture : Node.captures()) {
4605
8
    if (Capture.capturesVariable()) {
4606
6
      BoundNodesTreeBuilder Result(*Builder);
4607
6
      if (InnerMatcher.matches(*Capture.getCapturedVar(), Finder, &Result)) {
4608
4
        *Builder = std::move(Result);
4609
4
        return true;
4610
4
      }
4611
6
    }
4612
8
  }
4613
6
  return false;
4614
10
}
4615
4616
/// Matches any capture of 'this' in a lambda expression.
4617
///
4618
/// Given
4619
/// \code
4620
///   struct foo {
4621
///     void bar() {
4622
///       auto f = [this](){};
4623
///     }
4624
///   }
4625
/// \endcode
4626
/// lambdaExpr(hasAnyCapture(cxxThisExpr()))
4627
///   matches [this](){};
4628
AST_MATCHER_P_OVERLOAD(LambdaExpr, hasAnyCapture,
4629
10
                       internal::Matcher<CXXThisExpr>, InnerMatcher, 1) {
4630
10
  return llvm::any_of(Node.captures(), [](const LambdaCapture &LC) {
4631
8
    return LC.capturesThis();
4632
8
  });
4633
10
}
4634
4635
/// Matches a constructor call expression which uses list initialization.
4636
16
AST_MATCHER(CXXConstructExpr, isListInitialization) {
4637
16
  return Node.isListInitialization();
4638
16
}
4639
4640
/// Matches a constructor call expression which requires
4641
/// zero initialization.
4642
///
4643
/// Given
4644
/// \code
4645
/// void foo() {
4646
///   struct point { double x; double y; };
4647
///   point pt[2] = { { 1.0, 2.0 } };
4648
/// }
4649
/// \endcode
4650
/// initListExpr(has(cxxConstructExpr(requiresZeroInitialization()))
4651
/// will match the implicit array filler for pt[1].
4652
10
AST_MATCHER(CXXConstructExpr, requiresZeroInitialization) {
4653
10
  return Node.requiresZeroInitialization();
4654
10
}
4655
4656
/// Matches the n'th parameter of a function or an ObjC method
4657
/// declaration or a block.
4658
///
4659
/// Given
4660
/// \code
4661
///   class X { void f(int x) {} };
4662
/// \endcode
4663
/// cxxMethodDecl(hasParameter(0, hasType(varDecl())))
4664
///   matches f(int x) {}
4665
/// with hasParameter(...)
4666
///   matching int x
4667
///
4668
/// For ObjectiveC, given
4669
/// \code
4670
///   @interface I - (void) f:(int) y; @end
4671
/// \endcode
4672
//
4673
/// the matcher objcMethodDecl(hasParameter(0, hasName("y")))
4674
/// matches the declaration of method f with hasParameter
4675
/// matching y.
4676
AST_POLYMORPHIC_MATCHER_P2(hasParameter,
4677
                           AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
4678
                                                           ObjCMethodDecl,
4679
                                                           BlockDecl),
4680
                           unsigned, N, internal::Matcher<ParmVarDecl>,
4681
559
                           InnerMatcher) {
4682
559
  return (N < Node.parameters().size()
4683
559
          && 
InnerMatcher.matches(*Node.parameters()[N], Finder, Builder)496
);
4684
559
}
clang::ast_matchers::internal::matcher_hasParameter0Matcher<clang::CXXConstructorDecl, unsigned int, clang::ast_matchers::internal::Matcher<clang::ParmVarDecl> >::matches(clang::CXXConstructorDecl const&, clang::ast_matchers::internal::ASTMatchFinder*, clang::ast_matchers::internal::BoundN