Coverage Report

Created: 2020-09-19 12:23

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