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