Coverage Report

Created: 2020-02-25 14:32

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