1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
|
//===--- ASTMatchers.h - Structural query framework -------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements matchers to be used together with the MatchFinder to
// match AST nodes.
//
// Matchers are created by generator functions, which can be combined in
// a functional in-language DSL to express queries over the C++ AST.
//
// For example, to match a class with a certain name, one would call:
// record(hasName("MyClass"))
// which returns a matcher that can be used to find all AST nodes that declare
// a class named 'MyClass'.
//
// For more complicated match expressions we're often interested in accessing
// multiple parts of the matched AST nodes once a match is found. In that case,
// use the id(...) matcher around the match expressions that match the nodes
// you want to access.
//
// For example, when we're interested in child classes of a certain class, we
// would write:
// record(hasName("MyClass"), hasChild(id("child", record())))
// When the match is found via the MatchFinder, a user provided callback will
// be called with a BoundNodes instance that contains a mapping from the
// strings that we provided for the id(...) calls to the nodes that were
// matched.
// In the given example, each time our matcher finds a match we get a callback
// where "child" is bound to the CXXRecordDecl node of the matching child
// class declaration.
//
// See ASTMatchersInternal.h for a more in-depth explanation of the
// implementation details of the matcher framework.
//
// See ASTMatchFinder.h for how to use the generated matchers to run over
// an AST.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_H
#define LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_H
#include "clang/AST/DeclTemplate.h"
#include "clang/ASTMatchers/ASTMatchersInternal.h"
#include "clang/ASTMatchers/ASTMatchersMacros.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Regex.h"
#include <iterator>
namespace clang {
namespace ast_matchers {
/// \brief Maps string IDs to AST nodes matched by parts of a matcher.
///
/// The bound nodes are generated by adding id(...) matchers into the
/// match expression around the matchers for the nodes we want to access later.
///
/// The instances of BoundNodes are created by MatchFinder when the user's
/// callbacks are executed every time a match is found.
class BoundNodes {
public:
/// \brief Returns the AST node bound to 'ID'.
/// Returns NULL if there was no node bound to 'ID' or if there is a node but
/// it cannot be converted to the specified type.
/// FIXME: We'll need one of those for every base type.
/// @{
template <typename T>
const T *getDeclAs(StringRef ID) const {
return getNodeAs<T>(DeclBindings, ID);
}
template <typename T>
const T *getStmtAs(StringRef ID) const {
return getNodeAs<T>(StmtBindings, ID);
}
/// @}
private:
/// \brief Create BoundNodes from a pre-filled map of bindings.
BoundNodes(const std::map<std::string, const Decl*> &DeclBindings,
const std::map<std::string, const Stmt*> &StmtBindings)
: DeclBindings(DeclBindings), StmtBindings(StmtBindings) {}
template <typename T, typename MapT>
const T *getNodeAs(const MapT &Bindings, StringRef ID) const {
typename MapT::const_iterator It = Bindings.find(ID);
if (It == Bindings.end()) {
return NULL;
}
return llvm::dyn_cast<T>(It->second);
}
std::map<std::string, const Decl*> DeclBindings;
std::map<std::string, const Stmt*> StmtBindings;
friend class internal::BoundNodesTree;
};
/// \brief If the provided matcher matches a node, binds the node to 'ID'.
///
/// FIXME: Add example for accessing it.
template <typename T>
internal::Matcher<T> id(const std::string &ID,
const internal::BindableMatcher<T> &InnerMatcher) {
return InnerMatcher.bind(ID);
}
/// \brief Types of matchers for the top-level classes in the AST class
/// hierarchy.
/// @{
typedef internal::Matcher<Decl> DeclarationMatcher;
typedef internal::Matcher<QualType> TypeMatcher;
typedef internal::Matcher<Stmt> StatementMatcher;
/// @}
/// \brief Matches any node.
///
/// Useful when another matcher requires a child matcher, but there's no
/// additional constraint. This will often be used with an explicit conversion
/// to a internal::Matcher<> type such as TypeMatcher.
///
/// Example: DeclarationMatcher(anything()) matches all declarations, e.g.,
/// "int* p" and "void f()" in
/// int* p;
/// void f();
inline internal::PolymorphicMatcherWithParam0<internal::TrueMatcher> anything() {
return internal::PolymorphicMatcherWithParam0<internal::TrueMatcher>();
}
/// \brief Matches declarations.
///
/// Examples matches \c X, \c C, and the friend declaration inside \c C;
/// \code
/// void X();
/// class C {
/// friend X;
/// };
/// \endcode
const internal::VariadicDynCastAllOfMatcher<Decl, Decl> decl;
/// \brief Matches a declaration of anything that could have a name.
///
/// Example matches X, S, the anonymous union type, i, and U;
/// typedef int X;
/// struct S {
/// union {
/// int i;
/// } U;
/// };
const internal::VariadicDynCastAllOfMatcher<
Decl,
NamedDecl> nameableDeclaration;
/// \brief Matches C++ class declarations.
///
/// Example matches X, Z
/// class X;
/// template<class T> class Z {};
const internal::VariadicDynCastAllOfMatcher<
Decl,
CXXRecordDecl> record;
/// \brief Matches C++ class template specializations.
///
/// Given
/// template<typename T> class A {};
/// template<> class A<double> {};
/// A<int> a;
/// classTemplateSpecialization()
/// matches the specializations \c A<int> and \c A<double>
const internal::VariadicDynCastAllOfMatcher<
Decl,
ClassTemplateSpecializationDecl> classTemplateSpecialization;
/// \brief Matches classTemplateSpecializations that have at least one
/// TemplateArgument matching the given Matcher.
///
/// Given
/// template<typename T> class A {};
/// template<> class A<double> {};
/// A<int> a;
/// classTemplateSpecialization(hasAnyTemplateArgument(
/// refersToType(asString("int"))))
/// matches the specialization \c A<int>
AST_MATCHER_P(ClassTemplateSpecializationDecl, hasAnyTemplateArgument,
internal::Matcher<TemplateArgument>, Matcher) {
const TemplateArgumentList &List = Node.getTemplateArgs();
for (unsigned i = 0; i < List.size(); ++i) {
if (Matcher.matches(List.get(i), Finder, Builder))
return true;
}
return false;
}
/// \brief Matches expressions that match InnerMatcher after any implicit casts
/// are stripped off.
///
/// Parentheses and explicit casts are not discarded.
/// Given
/// int arr[5];
/// int a = 0;
/// char b = 0;
/// const int c = a;
/// int *d = arr;
/// long e = (long) 0l;
/// The matchers
/// variable(hasInitializer(ignoringImpCasts(integerLiteral())))
/// variable(hasInitializer(ignoringImpCasts(declarationReference())))
/// would match the declarations for a, b, c, and d, but not e.
/// while
/// variable(hasInitializer(integerLiteral()))
/// variable(hasInitializer(declarationReference()))
/// only match the declarations for b, c, and d.
AST_MATCHER_P(Expr, ignoringImpCasts,
internal::Matcher<Expr>, InnerMatcher) {
return InnerMatcher.matches(*Node.IgnoreImpCasts(), Finder, Builder);
}
/// \brief Matches expressions that match InnerMatcher after parentheses and
/// casts are stripped off.
///
/// Implicit and non-C Style casts are also discarded.
/// Given
/// int a = 0;
/// char b = (0);
/// void* c = reinterpret_cast<char*>(0);
/// char d = char(0);
/// The matcher
/// variable(hasInitializer(ignoringParenCasts(integerLiteral())))
/// would match the declarations for a, b, c, and d.
/// while
/// variable(hasInitializer(integerLiteral()))
/// only match the declaration for a.
AST_MATCHER_P(Expr, ignoringParenCasts, internal::Matcher<Expr>, InnerMatcher) {
return InnerMatcher.matches(*Node.IgnoreParenCasts(), Finder, Builder);
}
/// \brief Matches expressions that match InnerMatcher after implicit casts and
/// parentheses are stripped off.
///
/// Explicit casts are not discarded.
/// Given
/// int arr[5];
/// int a = 0;
/// char b = (0);
/// const int c = a;
/// int *d = (arr);
/// long e = ((long) 0l);
/// The matchers
/// variable(hasInitializer(ignoringParenImpCasts(
/// integerLiteral())))
/// variable(hasInitializer(ignoringParenImpCasts(
/// declarationReference())))
/// would match the declarations for a, b, c, and d, but not e.
/// while
/// variable(hasInitializer(integerLiteral()))
/// variable(hasInitializer(declarationReference()))
/// would only match the declaration for a.
AST_MATCHER_P(Expr, ignoringParenImpCasts,
internal::Matcher<Expr>, InnerMatcher) {
return InnerMatcher.matches(*Node.IgnoreParenImpCasts(), Finder, Builder);
}
/// \brief Matches classTemplateSpecializations where the n'th TemplateArgument
/// matches the given Matcher.
///
/// Given
/// template<typename T, typename U> class A {};
/// A<bool, int> b;
/// A<int, bool> c;
/// classTemplateSpecialization(hasTemplateArgument(
/// 1, refersToType(asString("int"))))
/// matches the specialization \c A<bool, int>
AST_MATCHER_P2(ClassTemplateSpecializationDecl, hasTemplateArgument,
unsigned, N, internal::Matcher<TemplateArgument>, Matcher) {
const TemplateArgumentList &List = Node.getTemplateArgs();
if (List.size() <= N)
return false;
return Matcher.matches(List.get(N), Finder, Builder);
}
/// \brief Matches a TemplateArgument that refers to a certain type.
///
/// Given
/// struct X {};
/// template<typename T> struct A {};
/// A<X> a;
/// classTemplateSpecialization(hasAnyTemplateArgument(
/// refersToType(class(hasName("X")))))
/// matches the specialization \c A<X>
AST_MATCHER_P(TemplateArgument, refersToType,
internal::Matcher<QualType>, Matcher) {
if (Node.getKind() != TemplateArgument::Type)
return false;
return Matcher.matches(Node.getAsType(), Finder, Builder);
}
/// \brief Matches a TemplateArgument that refers to a certain declaration.
///
/// Given
/// template<typename T> struct A {};
/// struct B { B* next; };
/// A<&B::next> a;
/// classTemplateSpecialization(hasAnyTemplateArgument(
/// refersToDeclaration(field(hasName("next"))))
/// matches the specialization \c A<&B::next> with \c field(...) matching
/// \c B::next
AST_MATCHER_P(TemplateArgument, refersToDeclaration,
internal::Matcher<Decl>, Matcher) {
if (const Decl *Declaration = Node.getAsDecl())
return Matcher.matches(*Declaration, Finder, Builder);
return false;
}
/// \brief Matches C++ constructor declarations.
///
/// Example matches Foo::Foo() and Foo::Foo(int)
/// class Foo {
/// public:
/// Foo();
/// Foo(int);
/// int DoSomething();
/// };
const internal::VariadicDynCastAllOfMatcher<
Decl,
CXXConstructorDecl> constructor;
/// \brief Matches explicit C++ destructor declarations.
///
/// Example matches Foo::~Foo()
/// class Foo {
/// public:
/// virtual ~Foo();
/// };
const internal::VariadicDynCastAllOfMatcher<Decl, CXXDestructorDecl> destructor;
/// \brief Matches enum declarations.
///
/// Example matches X
/// enum X {
/// A, B, C
/// };
const internal::VariadicDynCastAllOfMatcher<Decl, EnumDecl> enumDecl;
/// \brief Matches enum constants.
///
/// Example matches A, B, C
/// enum X {
/// A, B, C
/// };
const internal::VariadicDynCastAllOfMatcher<
Decl,
EnumConstantDecl> enumConstant;
/// \brief Matches method declarations.
///
/// Example matches y
/// class X { void y() };
const internal::VariadicDynCastAllOfMatcher<Decl, CXXMethodDecl> method;
/// \brief Matches variable declarations.
///
/// Note: this does not match declarations of member variables, which are
/// "field" declarations in Clang parlance.
///
/// Example matches a
/// int a;
const internal::VariadicDynCastAllOfMatcher<Decl, VarDecl> variable;
/// \brief Matches field declarations.
///
/// Given
/// class X { int m; };
/// field()
/// matches 'm'.
const internal::VariadicDynCastAllOfMatcher<Decl, FieldDecl> field;
/// \brief Matches function declarations.
///
/// Example matches f
/// void f();
const internal::VariadicDynCastAllOfMatcher<Decl, FunctionDecl> function;
/// \brief Matches statements.
///
/// Given
/// { ++a; }
/// statement()
/// matches both the compound statement '{ ++a; }' and '++a'.
const internal::VariadicDynCastAllOfMatcher<Stmt, Stmt> statement;
/// \brief Matches declaration statements.
///
/// Given
/// int a;
/// declarationStatement()
/// matches 'int a'.
const internal::VariadicDynCastAllOfMatcher<
Stmt,
DeclStmt> declarationStatement;
/// \brief Matches member expressions.
///
/// Given
/// class Y {
/// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; }
/// int a; static int b;
/// };
/// memberExpression()
/// matches this->x, x, y.x, a, this->b
const internal::VariadicDynCastAllOfMatcher<
Stmt,
MemberExpr> memberExpression;
/// \brief Matches call expressions.
///
/// Example matches x.y() and y()
/// X x;
/// x.y();
/// y();
const internal::VariadicDynCastAllOfMatcher<Stmt, CallExpr> call;
/// \brief Matches member call expressions.
///
/// Example matches x.y()
/// X x;
/// x.y();
const internal::VariadicDynCastAllOfMatcher<Stmt, CXXMemberCallExpr> memberCall;
/// \brief Matches init list expressions.
///
/// Given
/// int a[] = { 1, 2 };
/// struct B { int x, y; };
/// B b = { 5, 6 };
/// initList()
/// matches "{ 1, 2 }" and "{ 5, 6 }"
const internal::VariadicDynCastAllOfMatcher<Stmt, InitListExpr> initListExpr;
/// \brief Matches using declarations.
///
/// Given
/// namespace X { int x; }
/// using X::x;
/// usingDecl()
/// matches \code using X::x \endcode
const internal::VariadicDynCastAllOfMatcher<Decl, UsingDecl> usingDecl;
/// \brief Matches constructor call expressions (including implicit ones).
///
/// Example matches string(ptr, n) and ptr within arguments of f
/// (matcher = constructorCall())
/// void f(const string &a, const string &b);
/// char *ptr;
/// int n;
/// f(string(ptr, n), ptr);
const internal::VariadicDynCastAllOfMatcher<
Stmt,
CXXConstructExpr> constructorCall;
/// \brief Matches nodes where temporaries are created.
///
/// Example matches FunctionTakesString(GetStringByValue())
/// (matcher = bindTemporaryExpression())
/// FunctionTakesString(GetStringByValue());
/// FunctionTakesStringByPointer(GetStringPointer());
const internal::VariadicDynCastAllOfMatcher<
Stmt,
CXXBindTemporaryExpr> bindTemporaryExpression;
/// \brief Matches new expressions.
///
/// Given
/// new X;
/// newExpression()
/// matches 'new X'.
const internal::VariadicDynCastAllOfMatcher<
Stmt,
CXXNewExpr> newExpression;
/// \brief Matches delete expressions.
///
/// Given
/// delete X;
/// deleteExpression()
/// matches 'delete X'.
const internal::VariadicDynCastAllOfMatcher<
Stmt,
CXXDeleteExpr> deleteExpression;
/// \brief Matches array subscript expressions.
///
/// Given
/// int i = a[1];
/// arraySubscriptExpr()
/// matches "a[1]"
const internal::VariadicDynCastAllOfMatcher<
Stmt,
ArraySubscriptExpr> arraySubscriptExpr;
/// \brief Matches the value of a default argument at the call site.
///
/// Example matches the CXXDefaultArgExpr placeholder inserted for the
/// default value of the second parameter in the call expression f(42)
/// (matcher = defaultArgument())
/// void f(int x, int y = 0);
/// f(42);
const internal::VariadicDynCastAllOfMatcher<
Stmt,
CXXDefaultArgExpr> defaultArgument;
/// \brief Matches overloaded operator calls.
///
/// Note that if an operator isn't overloaded, it won't match. Instead, use
/// binaryOperator matcher.
/// Currently it does not match operators such as new delete.
/// FIXME: figure out why these do not match?
///
/// Example matches both operator<<((o << b), c) and operator<<(o, b)
/// (matcher = overloadedOperatorCall())
/// ostream &operator<< (ostream &out, int i) { };
/// ostream &o; int b = 1, c = 1;
/// o << b << c;
const internal::VariadicDynCastAllOfMatcher<
Stmt,
CXXOperatorCallExpr> overloadedOperatorCall;
/// \brief Matches expressions.
///
/// Example matches x()
/// void f() { x(); }
const internal::VariadicDynCastAllOfMatcher<
Stmt,
Expr> expression;
/// \brief Matches expressions that refer to declarations.
///
/// Example matches x in if (x)
/// bool x;
/// if (x) {}
const internal::VariadicDynCastAllOfMatcher<
Stmt,
DeclRefExpr> declarationReference;
/// \brief Matches if statements.
///
/// Example matches 'if (x) {}'
/// if (x) {}
const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt;
/// \brief Matches for statements.
///
/// Example matches 'for (;;) {}'
/// for (;;) {}
const internal::VariadicDynCastAllOfMatcher<
Stmt, ForStmt> forStmt;
/// \brief Matches the increment statement of a for loop.
///
/// Example:
/// forStmt(hasIncrement(unaryOperator(hasOperatorName("++"))))
/// matches '++x' in
/// for (x; x < N; ++x) { }
AST_MATCHER_P(ForStmt, hasIncrement, internal::Matcher<Stmt>,
InnerMatcher) {
const Stmt *const Increment = Node.getInc();
return (Increment != NULL &&
InnerMatcher.matches(*Increment, Finder, Builder));
}
/// \brief Matches the initialization statement of a for loop.
///
/// Example:
/// forStmt(hasLoopInit(declarationStatement()))
/// matches 'int x = 0' in
/// for (int x = 0; x < N; ++x) { }
AST_MATCHER_P(ForStmt, hasLoopInit, internal::Matcher<Stmt>,
InnerMatcher) {
const Stmt *const Init = Node.getInit();
return (Init != NULL && InnerMatcher.matches(*Init, Finder, Builder));
}
/// \brief Matches while statements.
///
/// Given
/// while (true) {}
/// whileStmt()
/// matches 'while (true) {}'.
const internal::VariadicDynCastAllOfMatcher<
Stmt,
WhileStmt> whileStmt;
/// \brief Matches do statements.
///
/// Given
/// do {} while (true);
/// doStmt()
/// matches 'do {} while(true)'
const internal::VariadicDynCastAllOfMatcher<Stmt, DoStmt> doStmt;
/// \brief Matches case and default statements inside switch statements.
///
/// Given
/// switch(a) { case 42: break; default: break; }
/// switchCase()
/// matches 'case 42: break;' and 'default: break;'.
const internal::VariadicDynCastAllOfMatcher<
Stmt,
SwitchCase> switchCase;
/// \brief Matches compound statements.
///
/// Example matches '{}' and '{{}}'in 'for (;;) {{}}'
/// for (;;) {{}}
const internal::VariadicDynCastAllOfMatcher<
Stmt,
CompoundStmt> compoundStatement;
/// \brief Matches bool literals.
///
/// Example matches true
/// true
const internal::VariadicDynCastAllOfMatcher<
Expr,
CXXBoolLiteralExpr> boolLiteral;
/// \brief Matches string literals (also matches wide string literals).
///
/// Example matches "abcd", L"abcd"
/// char *s = "abcd"; wchar_t *ws = L"abcd"
const internal::VariadicDynCastAllOfMatcher<
Expr,
StringLiteral> stringLiteral;
/// \brief Matches character literals (also matches wchar_t).
///
/// Not matching Hex-encoded chars (e.g. 0x1234, which is a IntegerLiteral),
/// though.
///
/// Example matches 'a', L'a'
/// char ch = 'a'; wchar_t chw = L'a';
const internal::VariadicDynCastAllOfMatcher<
Expr,
CharacterLiteral> characterLiteral;
/// \brief Matches integer literals of all sizes / encodings.
///
/// Not matching character-encoded integers such as L'a'.
///
/// Example matches 1, 1L, 0x1, 1U
const internal::VariadicDynCastAllOfMatcher<
Expr,
IntegerLiteral> integerLiteral;
/// \brief Matches binary operator expressions.
///
/// Example matches a || b
/// !(a || b)
const internal::VariadicDynCastAllOfMatcher<
Stmt,
BinaryOperator> binaryOperator;
/// \brief Matches unary operator expressions.
///
/// Example matches !a
/// !a || b
const internal::VariadicDynCastAllOfMatcher<
Stmt,
UnaryOperator> unaryOperator;
/// \brief Matches conditional operator expressions.
///
/// Example matches a ? b : c
/// (a ? b : c) + 42
const internal::VariadicDynCastAllOfMatcher<
Stmt,
ConditionalOperator> conditionalOperator;
/// \brief Matches a reinterpret_cast expression.
///
/// Either the source expression or the destination type can be matched
/// using has(), but hasDestinationType() is more specific and can be
/// more readable.
///
/// Example matches reinterpret_cast<char*>(&p) in
/// void* p = reinterpret_cast<char*>(&p);
const internal::VariadicDynCastAllOfMatcher<
Expr,
CXXReinterpretCastExpr> reinterpretCast;
/// \brief Matches a C++ static_cast expression.
///
/// \see hasDestinationType
/// \see reinterpretCast
///
/// Example:
/// staticCast()
/// matches
/// static_cast<long>(8)
/// in
/// long eight(static_cast<long>(8));
const internal::VariadicDynCastAllOfMatcher<
Expr,
CXXStaticCastExpr> staticCast;
/// \brief Matches a dynamic_cast expression.
///
/// Example:
/// dynamicCast()
/// matches
/// dynamic_cast<D*>(&b);
/// in
/// struct B { virtual ~B() {} }; struct D : B {};
/// B b;
/// D* p = dynamic_cast<D*>(&b);
const internal::VariadicDynCastAllOfMatcher<
Expr,
CXXDynamicCastExpr> dynamicCast;
/// \brief Matches a const_cast expression.
///
/// Example: Matches const_cast<int*>(&r) in
/// int n = 42;
/// const int& r(n);
/// int* p = const_cast<int*>(&r);
const internal::VariadicDynCastAllOfMatcher<
Expr,
CXXConstCastExpr> constCast;
/// \brief Matches explicit cast expressions.
///
/// Matches any cast expression written in user code, whether it be a
/// C-style cast, a functional-style cast, or a keyword cast.
///
/// Does not match implicit conversions.
///
/// Note: the name "explicitCast" is chosen to match Clang's terminology, as
/// Clang uses the term "cast" to apply to implicit conversions as well as to
/// actual cast expressions.
///
/// \see hasDestinationType.
///
/// Example: matches all five of the casts in
/// int((int)(reinterpret_cast<int>(static_cast<int>(const_cast<int>(42)))))
/// but does not match the implicit conversion in
/// long ell = 42;
const internal::VariadicDynCastAllOfMatcher<
Expr,
ExplicitCastExpr> explicitCast;
/// \brief Matches the implicit cast nodes of Clang's AST.
///
/// This matches many different places, including function call return value
/// eliding, as well as any type conversions.
const internal::VariadicDynCastAllOfMatcher<
Expr,
ImplicitCastExpr> implicitCast;
/// \brief Matches any cast nodes of Clang's AST.
///
/// Example: castExpr() matches each of the following:
/// (int) 3;
/// const_cast<Expr *>(SubExpr);
/// char c = 0;
/// but does not match
/// int i = (0);
/// int k = 0;
const internal::VariadicDynCastAllOfMatcher<
Expr,
CastExpr> castExpr;
/// \brief Matches functional cast expressions
///
/// Example: Matches Foo(bar);
/// Foo f = bar;
/// Foo g = (Foo) bar;
/// Foo h = Foo(bar);
const internal::VariadicDynCastAllOfMatcher<
Expr,
CXXFunctionalCastExpr> functionalCast;
/// \brief Various overloads for the anyOf matcher.
/// @{
template<typename C1, typename C2>
internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C1, C2>
anyOf(const C1 &P1, const C2 &P2) {
return internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher,
C1, C2 >(P1, P2);
}
template<typename C1, typename C2, typename C3>
internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C1,
internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C2, C3> >
anyOf(const C1 &P1, const C2 &P2, const C3 &P3) {
return anyOf(P1, anyOf(P2, P3));
}
template<typename C1, typename C2, typename C3, typename C4>
internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C1,
internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C2,
internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher,
C3, C4> > >
anyOf(const C1 &P1, const C2 &P2, const C3 &P3, const C4 &P4) {
return anyOf(P1, anyOf(P2, anyOf(P3, P4)));
}
template<typename C1, typename C2, typename C3, typename C4, typename C5>
internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C1,
internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C2,
internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher, C3,
internal::PolymorphicMatcherWithParam2<internal::AnyOfMatcher,
C4, C5> > > >
anyOf(const C1& P1, const C2& P2, const C3& P3, const C4& P4, const C5& P5) {
return anyOf(P1, anyOf(P2, anyOf(P3, anyOf(P4, P5))));
}
/// @}
/// \brief Various overloads for the allOf matcher.
/// @{
template<typename C1, typename C2>
internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, C1, C2>
allOf(const C1 &P1, const C2 &P2) {
return internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher,
C1, C2>(P1, P2);
}
template<typename C1, typename C2, typename C3>
internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, C1,
internal::PolymorphicMatcherWithParam2<internal::AllOfMatcher, C2, C3> >
allOf(const C1& P1, const C2& P2, const C3& P3) {
return allOf(P1, allOf(P2, P3));
}
/// @}
/// \brief Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL)
///
/// Given
/// Foo x = bar;
/// int y = sizeof(x) + alignof(x);
/// unaryExprOrTypeTraitExpr()
/// matches \c sizeof(x) and \c alignof(x)
const internal::VariadicDynCastAllOfMatcher<
Stmt,
UnaryExprOrTypeTraitExpr> unaryExprOrTypeTraitExpr;
/// \brief Matches unary expressions that have a specific type of argument.
///
/// Given
/// int a, c; float b; int s = sizeof(a) + sizeof(b) + alignof(c);
/// unaryExprOrTypeTraitExpr(hasArgumentOfType(asString("int"))
/// matches \c sizeof(a) and \c alignof(c)
AST_MATCHER_P(UnaryExprOrTypeTraitExpr, hasArgumentOfType,
internal::Matcher<QualType>, Matcher) {
const QualType ArgumentType = Node.getTypeOfArgument();
return Matcher.matches(ArgumentType, Finder, Builder);
}
/// \brief Matches unary expressions of a certain kind.
///
/// Given
/// int x;
/// int s = sizeof(x) + alignof(x)
/// unaryExprOrTypeTraitExpr(ofKind(UETT_SizeOf))
/// matches \c sizeof(x)
AST_MATCHER_P(UnaryExprOrTypeTraitExpr, ofKind, UnaryExprOrTypeTrait, Kind) {
return Node.getKind() == Kind;
}
/// \brief Same as unaryExprOrTypeTraitExpr, but only matching
/// alignof.
inline internal::Matcher<Stmt> alignOfExpr(
const internal::Matcher<UnaryExprOrTypeTraitExpr> &Matcher) {
return internal::Matcher<Stmt>(unaryExprOrTypeTraitExpr(allOf(
ofKind(UETT_AlignOf), Matcher)));
}
/// \brief Same as unaryExprOrTypeTraitExpr, but only matching
/// sizeof.
inline internal::Matcher<Stmt> sizeOfExpr(
const internal::Matcher<UnaryExprOrTypeTraitExpr> &Matcher) {
return internal::Matcher<Stmt>(unaryExprOrTypeTraitExpr(allOf(
ofKind(UETT_SizeOf), Matcher)));
}
/// \brief Matches NamedDecl nodes that have the specified name.
///
/// Supports specifying enclosing namespaces or classes by prefixing the name
/// with '<enclosing>::'.
/// Does not match typedefs of an underlying type with the given name.
///
/// Example matches X (Name == "X")
/// class X;
///
/// Example matches X (Name is one of "::a::b::X", "a::b::X", "b::X", "X")
/// namespace a { namespace b { class X; } }
AST_MATCHER_P(NamedDecl, hasName, std::string, Name) {
assert(!Name.empty());
const std::string FullNameString = "::" + Node.getQualifiedNameAsString();
const llvm::StringRef FullName = FullNameString;
const llvm::StringRef Pattern = Name;
if (Pattern.startswith("::")) {
return FullName == Pattern;
} else {
return FullName.endswith(("::" + Pattern).str());
}
}
/// \brief Matches NamedDecl nodes whose full names partially match the
/// given RegExp.
///
/// Supports specifying enclosing namespaces or classes by
/// prefixing the name with '<enclosing>::'. Does not match typedefs
/// of an underlying type with the given name.
///
/// Example matches X (regexp == "::X")
/// class X;
///
/// Example matches X (regexp is one of "::X", "^foo::.*X", among others)
/// namespace foo { namespace bar { class X; } }
AST_MATCHER_P(NamedDecl, matchesName, std::string, RegExp) {
assert(!RegExp.empty());
std::string FullNameString = "::" + Node.getQualifiedNameAsString();
llvm::Regex RE(RegExp);
return RE.match(FullNameString);
}
/// \brief Matches overloaded operator names.
///
/// Matches overloaded operator names specified in strings without the
/// "operator" prefix, such as "<<", for OverloadedOperatorCall's.
///
/// Example matches a << b
/// (matcher == overloadedOperatorCall(hasOverloadedOperatorName("<<")))
/// a << b;
/// c && d; // assuming both operator<<
/// // and operator&& are overloaded somewhere.
AST_MATCHER_P(CXXOperatorCallExpr,
hasOverloadedOperatorName, std::string, Name) {
return getOperatorSpelling(Node.getOperator()) == Name;
}
/// \brief Matches C++ classes that are directly or indirectly derived from
/// a class matching \c Base.
///
/// Note that a class is considered to be also derived from itself.
///
/// Example matches X, Y, Z, C (Base == hasName("X"))
/// class X; // A class is considered to be derived from itself
/// class Y : public X {}; // directly derived
/// class Z : public Y {}; // indirectly derived
/// typedef X A;
/// typedef A B;
/// class C : public B {}; // derived from a typedef of X
///
/// In the following example, Bar matches isDerivedFrom(hasName("X")):
/// class Foo;
/// typedef Foo X;
/// class Bar : public Foo {}; // derived from a type that X is a typedef of
AST_MATCHER_P(CXXRecordDecl, isDerivedFrom,
internal::Matcher<NamedDecl>, Base) {
return Finder->classIsDerivedFrom(&Node, Base, Builder);
}
/// \brief Overloaded method as shortcut for \c isDerivedFrom(hasName(...)).
inline internal::Matcher<CXXRecordDecl> isDerivedFrom(StringRef BaseName) {
assert(!BaseName.empty());
return isDerivedFrom(hasName(BaseName));
}
/// \brief Matches AST nodes that have child AST nodes that match the
/// provided matcher.
///
/// Example matches X, Y (matcher = record(has(record(hasName("X")))
/// class X {}; // Matches X, because X::X is a class of name X inside X.
/// class Y { class X {}; };
/// class Z { class Y { class X {}; }; }; // Does not match Z.
///
/// ChildT must be an AST base type.
template <typename ChildT>
internal::ArgumentAdaptingMatcher<internal::HasMatcher, ChildT> has(
const internal::Matcher<ChildT> &ChildMatcher) {
return internal::ArgumentAdaptingMatcher<internal::HasMatcher,
ChildT>(ChildMatcher);
}
/// \brief Matches AST nodes that have descendant AST nodes that match the
/// provided matcher.
///
/// Example matches X, Y, Z
/// (matcher = record(hasDescendant(record(hasName("X")))))
/// class X {}; // Matches X, because X::X is a class of name X inside X.
/// class Y { class X {}; };
/// class Z { class Y { class X {}; }; };
///
/// DescendantT must be an AST base type.
template <typename DescendantT>
internal::ArgumentAdaptingMatcher<internal::HasDescendantMatcher, DescendantT>
hasDescendant(const internal::Matcher<DescendantT> &DescendantMatcher) {
return internal::ArgumentAdaptingMatcher<
internal::HasDescendantMatcher,
DescendantT>(DescendantMatcher);
}
/// \brief Matches AST nodes that have child AST nodes that match the
/// provided matcher.
///
/// Example matches X, Y (matcher = record(forEach(record(hasName("X")))
/// class X {}; // Matches X, because X::X is a class of name X inside X.
/// class Y { class X {}; };
/// class Z { class Y { class X {}; }; }; // Does not match Z.
///
/// ChildT must be an AST base type.
///
/// As opposed to 'has', 'forEach' will cause a match for each result that
/// matches instead of only on the first one.
template <typename ChildT>
internal::ArgumentAdaptingMatcher<internal::ForEachMatcher, ChildT> forEach(
const internal::Matcher<ChildT>& ChildMatcher) {
return internal::ArgumentAdaptingMatcher<
internal::ForEachMatcher,
ChildT>(ChildMatcher);
}
/// \brief Matches AST nodes that have descendant AST nodes that match the
/// provided matcher.
///
/// Example matches X, A, B, C
/// (matcher = record(forEachDescendant(record(hasName("X")))))
/// class X {}; // Matches X, because X::X is a class of name X inside X.
/// class A { class X {}; };
/// class B { class C { class X {}; }; };
///
/// DescendantT must be an AST base type.
///
/// As opposed to 'hasDescendant', 'forEachDescendant' will cause a match for
/// each result that matches instead of only on the first one.
///
/// Note: Recursively combined ForEachDescendant can cause many matches:
/// record(forEachDescendant(record(forEachDescendant(record()))))
/// will match 10 times (plus injected class name matches) on:
/// class A { class B { class C { class D { class E {}; }; }; }; };
template <typename DescendantT>
internal::ArgumentAdaptingMatcher<internal::ForEachDescendantMatcher, DescendantT>
forEachDescendant(
const internal::Matcher<DescendantT>& DescendantMatcher) {
return internal::ArgumentAdaptingMatcher<
internal::ForEachDescendantMatcher,
DescendantT>(DescendantMatcher);
}
/// \brief Matches if the provided matcher does not match.
///
/// Example matches Y (matcher = record(unless(hasName("X"))))
/// class X {};
/// class Y {};
template <typename M>
internal::PolymorphicMatcherWithParam1<internal::NotMatcher, M> unless(const M &InnerMatcher) {
return internal::PolymorphicMatcherWithParam1<
internal::NotMatcher, M>(InnerMatcher);
}
/// \brief Matches a type if the declaration of the type matches the given
/// matcher.
///
/// Usable as: Matcher<QualType>, Matcher<CallExpr>, Matcher<CXXConstructExpr>
inline internal::PolymorphicMatcherWithParam1< internal::HasDeclarationMatcher,
internal::Matcher<Decl> >
hasDeclaration(const internal::Matcher<Decl> &InnerMatcher) {
return internal::PolymorphicMatcherWithParam1<
internal::HasDeclarationMatcher,
internal::Matcher<Decl> >(InnerMatcher);
}
/// \brief Matches on the implicit object argument of a member call expression.
///
/// Example matches y.x() (matcher = call(on(hasType(record(hasName("Y"))))))
/// class Y { public: void x(); };
/// void z() { Y y; y.x(); }",
///
/// FIXME: Overload to allow directly matching types?
AST_MATCHER_P(CXXMemberCallExpr, on, internal::Matcher<Expr>,
InnerMatcher) {
const Expr *ExprNode = const_cast<CXXMemberCallExpr&>(Node)
.getImplicitObjectArgument()
->IgnoreParenImpCasts();
return (ExprNode != NULL &&
InnerMatcher.matches(*ExprNode, Finder, Builder));
}
/// \brief Matches if the call expression's callee expression matches.
///
/// Given
/// class Y { void x() { this->x(); x(); Y y; y.x(); } };
/// void f() { f(); }
/// call(callee(expression()))
/// matches this->x(), x(), y.x(), f()
/// with callee(...)
/// matching this->x, x, y.x, f respectively
///
/// Note: Callee cannot take the more general internal::Matcher<Expr>
/// because this introduces ambiguous overloads with calls to Callee taking a
/// internal::Matcher<Decl>, as the matcher hierarchy is purely
/// implemented in terms of implicit casts.
AST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>,
InnerMatcher) {
const Expr *ExprNode = Node.getCallee();
return (ExprNode != NULL &&
InnerMatcher.matches(*ExprNode, Finder, Builder));
}
/// \brief Matches if the call expression's callee's declaration matches the
/// given matcher.
///
/// Example matches y.x() (matcher = call(callee(method(hasName("x")))))
/// class Y { public: void x(); };
/// void z() { Y y; y.x();
inline internal::Matcher<CallExpr> callee(
const internal::Matcher<Decl> &InnerMatcher) {
return internal::Matcher<CallExpr>(hasDeclaration(InnerMatcher));
}
/// \brief Matches if the expression's or declaration's type matches a type
/// matcher.
///
/// Example matches x (matcher = expression(hasType(
/// hasDeclaration(record(hasName("X"))))))
/// and z (matcher = variable(hasType(
/// hasDeclaration(record(hasName("X"))))))
/// class X {};
/// void y(X &x) { x; X z; }
AST_POLYMORPHIC_MATCHER_P(hasType, internal::Matcher<QualType>,
InnerMatcher) {
TOOLING_COMPILE_ASSERT((llvm::is_base_of<Expr, NodeType>::value ||
llvm::is_base_of<ValueDecl, NodeType>::value),
instantiated_with_wrong_types);
return InnerMatcher.matches(Node.getType(), Finder, Builder);
}
/// \brief Overloaded to match the declaration of the expression's or value
/// declaration's type.
///
/// In case of a value declaration (for example a variable declaration),
/// this resolves one layer of indirection. For example, in the value
/// declaration "X x;", record(hasName("X")) matches the declaration of X,
/// while variable(hasType(record(hasName("X")))) matches the declaration
/// of x."
///
/// Example matches x (matcher = expression(hasType(record(hasName("X")))))
/// and z (matcher = variable(hasType(record(hasName("X")))))
/// class X {};
/// void y(X &x) { x; X z; }
///
/// Usable as: Matcher<Expr>, Matcher<ValueDecl>
inline internal::PolymorphicMatcherWithParam1<
internal::matcher_hasTypeMatcher,
internal::Matcher<QualType> >
hasType(const internal::Matcher<Decl> &InnerMatcher) {
return hasType(internal::Matcher<QualType>(
hasDeclaration(InnerMatcher)));
}
/// \brief Matches if the matched type is represented by the given string.
///
/// Given
/// class Y { public: void x(); };
/// void z() { Y* y; y->x(); }
/// call(on(hasType(asString("class Y *"))))
/// matches y->x()
AST_MATCHER_P(QualType, asString, std::string, Name) {
return Name == Node.getAsString();
}
/// \brief Matches if the matched type is a pointer type and the pointee type
/// matches the specified matcher.
///
/// Example matches y->x()
/// (matcher = call(on(hasType(pointsTo(record(hasName("Y")))))))
/// class Y { public: void x(); };
/// void z() { Y *y; y->x(); }
AST_MATCHER_P(
QualType, pointsTo, internal::Matcher<QualType>,
InnerMatcher) {
return (!Node.isNull() && Node->isPointerType() &&
InnerMatcher.matches(Node->getPointeeType(), Finder, Builder));
}
/// \brief Overloaded to match the pointee type's declaration.
inline internal::Matcher<QualType> pointsTo(
const internal::Matcher<Decl> &InnerMatcher) {
return pointsTo(internal::Matcher<QualType>(
hasDeclaration(InnerMatcher)));
}
/// \brief Matches if the matched type is a reference type and the referenced
/// type matches the specified matcher.
///
/// Example matches X &x and const X &y
/// (matcher = variable(hasType(references(record(hasName("X"))))))
/// class X {
/// void a(X b) {
/// X &x = b;
/// const X &y = b;
/// };
AST_MATCHER_P(QualType, references, internal::Matcher<QualType>,
InnerMatcher) {
return (!Node.isNull() && Node->isReferenceType() &&
InnerMatcher.matches(Node->getPointeeType(), Finder, Builder));
}
/// \brief Overloaded to match the referenced type's declaration.
inline internal::Matcher<QualType> references(
const internal::Matcher<Decl> &InnerMatcher) {
return references(internal::Matcher<QualType>(
hasDeclaration(InnerMatcher)));
}
AST_MATCHER_P(CXXMemberCallExpr, onImplicitObjectArgument,
internal::Matcher<Expr>, InnerMatcher) {
const Expr *ExprNode =
const_cast<CXXMemberCallExpr&>(Node).getImplicitObjectArgument();
return (ExprNode != NULL &&
InnerMatcher.matches(*ExprNode, Finder, Builder));
}
/// \brief Matches if the expression's type either matches the specified
/// matcher, or is a pointer to a type that matches the InnerMatcher.
inline internal::Matcher<CXXMemberCallExpr> thisPointerType(
const internal::Matcher<QualType> &InnerMatcher) {
return onImplicitObjectArgument(
anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher))));
}
/// \brief Overloaded to match the type's declaration.
inline internal::Matcher<CXXMemberCallExpr> thisPointerType(
const internal::Matcher<Decl> &InnerMatcher) {
return onImplicitObjectArgument(
anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher))));
}
/// \brief Matches a DeclRefExpr that refers to a declaration that matches the
/// specified matcher.
///
/// Example matches x in if(x)
/// (matcher = declarationReference(to(variable(hasName("x")))))
/// bool x;
/// if (x) {}
AST_MATCHER_P(DeclRefExpr, to, internal::Matcher<Decl>,
InnerMatcher) {
const Decl *DeclNode = Node.getDecl();
return (DeclNode != NULL &&
InnerMatcher.matches(*DeclNode, Finder, Builder));
}
/// \brief Matches a \c DeclRefExpr that refers to a declaration through a
/// specific using shadow declaration.
///
/// FIXME: This currently only works for functions. Fix.
///
/// Given
/// namespace a { void f() {} }
/// using a::f;
/// void g() {
/// f(); // Matches this ..
/// a::f(); // .. but not this.
/// }
/// declarationReference(throughUsingDeclaration(anything()))
/// matches \c f()
AST_MATCHER_P(DeclRefExpr, throughUsingDecl,
internal::Matcher<UsingShadowDecl>, Matcher) {
const NamedDecl *FoundDecl = Node.getFoundDecl();
if (const UsingShadowDecl *UsingDecl =
llvm::dyn_cast<UsingShadowDecl>(FoundDecl))
return Matcher.matches(*UsingDecl, Finder, Builder);
return false;
}
/// \brief Matches the Decl of a DeclStmt which has a single declaration.
///
/// Given
/// int a, b;
/// int c;
/// declarationStatement(hasSingleDecl(anything()))
/// matches 'int c;' but not 'int a, b;'.
AST_MATCHER_P(DeclStmt, hasSingleDecl, internal::Matcher<Decl>, InnerMatcher) {
if (Node.isSingleDecl()) {
const Decl *FoundDecl = Node.getSingleDecl();
return InnerMatcher.matches(*FoundDecl, Finder, Builder);
}
return false;
}
/// \brief Matches a variable declaration that has an initializer expression
/// that matches the given matcher.
///
/// Example matches x (matcher = variable(hasInitializer(call())))
/// bool y() { return true; }
/// bool x = y();
AST_MATCHER_P(
VarDecl, hasInitializer, internal::Matcher<Expr>,
InnerMatcher) {
const Expr *Initializer = Node.getAnyInitializer();
return (Initializer != NULL &&
InnerMatcher.matches(*Initializer, Finder, Builder));
}
/// \brief Checks that a call expression or a constructor call expression has
/// a specific number of arguments (including absent default arguments).
///
/// Example matches f(0, 0) (matcher = call(argumentCountIs(2)))
/// void f(int x, int y);
/// f(0, 0);
AST_POLYMORPHIC_MATCHER_P(argumentCountIs, unsigned, N) {
TOOLING_COMPILE_ASSERT((llvm::is_base_of<CallExpr, NodeType>::value ||
llvm::is_base_of<CXXConstructExpr,
NodeType>::value),
instantiated_with_wrong_types);
return Node.getNumArgs() == N;
}
/// \brief Matches the n'th argument of a call expression or a constructor
/// call expression.
///
/// Example matches y in x(y)
/// (matcher = call(hasArgument(0, declarationReference())))
/// void x(int) { int y; x(y); }
AST_POLYMORPHIC_MATCHER_P2(
hasArgument, unsigned, N, internal::Matcher<Expr>, InnerMatcher) {
TOOLING_COMPILE_ASSERT((llvm::is_base_of<CallExpr, NodeType>::value ||
llvm::is_base_of<CXXConstructExpr,
NodeType>::value),
instantiated_with_wrong_types);
return (N < Node.getNumArgs() &&
InnerMatcher.matches(
*Node.getArg(N)->IgnoreParenImpCasts(), Finder, Builder));
}
/// \brief Matches declaration statements that contain a specific number of
/// declarations.
///
/// Example: Given
/// int a, b;
/// int c;
/// int d = 2, e;
/// declCountIs(2)
/// matches 'int a, b;' and 'int d = 2, e;', but not 'int c;'.
AST_MATCHER_P(DeclStmt, declCountIs, unsigned, N) {
return std::distance(Node.decl_begin(), Node.decl_end()) == N;
}
/// \brief Matches the n'th declaration of a declaration statement.
///
/// Note that this does not work for global declarations because the AST
/// breaks up multiple-declaration DeclStmt's into multiple single-declaration
/// DeclStmt's.
/// Example: Given non-global declarations
/// int a, b = 0;
/// int c;
/// int d = 2, e;
/// declarationStatement(containsDeclaration(
/// 0, variable(hasInitializer(anything()))))
/// matches only 'int d = 2, e;', and
/// declarationStatement(containsDeclaration(1, variable()))
/// matches 'int a, b = 0' as well as 'int d = 2, e;'
/// but 'int c;' is not matched.
AST_MATCHER_P2(DeclStmt, containsDeclaration, unsigned, N,
internal::Matcher<Decl>, InnerMatcher) {
const unsigned NumDecls = std::distance(Node.decl_begin(), Node.decl_end());
if (N >= NumDecls)
return false;
DeclStmt::const_decl_iterator Iterator = Node.decl_begin();
std::advance(Iterator, N);
return InnerMatcher.matches(**Iterator, Finder, Builder);
}
/// \brief Matches a constructor initializer.
///
/// Given
/// struct Foo {
/// Foo() : foo_(1) { }
/// int foo_;
/// };
/// record(has(constructor(hasAnyConstructorInitializer(anything()))))
/// record matches Foo, hasAnyConstructorInitializer matches foo_(1)
AST_MATCHER_P(CXXConstructorDecl, hasAnyConstructorInitializer,
internal::Matcher<CXXCtorInitializer>, InnerMatcher) {
for (CXXConstructorDecl::init_const_iterator I = Node.init_begin();
I != Node.init_end(); ++I) {
if (InnerMatcher.matches(**I, Finder, Builder)) {
return true;
}
}
return false;
}
/// \brief Matches the field declaration of a constructor initializer.
///
/// Given
/// struct Foo {
/// Foo() : foo_(1) { }
/// int foo_;
/// };
/// record(has(constructor(hasAnyConstructorInitializer(
/// forField(hasName("foo_"))))))
/// matches Foo
/// with forField matching foo_
AST_MATCHER_P(CXXCtorInitializer, forField,
internal::Matcher<FieldDecl>, InnerMatcher) {
const FieldDecl *NodeAsDecl = Node.getMember();
return (NodeAsDecl != NULL &&
InnerMatcher.matches(*NodeAsDecl, Finder, Builder));
}
/// \brief Matches the initializer expression of a constructor initializer.
///
/// Given
/// struct Foo {
/// Foo() : foo_(1) { }
/// int foo_;
/// };
/// record(has(constructor(hasAnyConstructorInitializer(
/// withInitializer(integerLiteral(equals(1)))))))
/// matches Foo
/// with withInitializer matching (1)
AST_MATCHER_P(CXXCtorInitializer, withInitializer,
internal::Matcher<Expr>, InnerMatcher) {
const Expr* NodeAsExpr = Node.getInit();
return (NodeAsExpr != NULL &&
InnerMatcher.matches(*NodeAsExpr, Finder, Builder));
}
/// \brief Matches a contructor initializer if it is explicitly written in
/// code (as opposed to implicitly added by the compiler).
///
/// Given
/// struct Foo {
/// Foo() { }
/// Foo(int) : foo_("A") { }
/// string foo_;
/// };
/// constructor(hasAnyConstructorInitializer(isWritten()))
/// will match Foo(int), but not Foo()
AST_MATCHER(CXXCtorInitializer, isWritten) {
return Node.isWritten();
}
/// \brief Matches a constructor declaration that has been implicitly added
/// by the compiler (eg. implicit default/copy constructors).
AST_MATCHER(CXXConstructorDecl, isImplicit) {
return Node.isImplicit();
}
/// \brief Matches any argument of a call expression or a constructor call
/// expression.
///
/// Given
/// void x(int, int, int) { int y; x(1, y, 42); }
/// call(hasAnyArgument(declarationReference()))
/// matches x(1, y, 42)
/// with hasAnyArgument(...)
/// matching y
AST_POLYMORPHIC_MATCHER_P(hasAnyArgument, internal::Matcher<Expr>,
InnerMatcher) {
TOOLING_COMPILE_ASSERT((llvm::is_base_of<CallExpr, NodeType>::value ||
llvm::is_base_of<CXXConstructExpr,
NodeType>::value),
instantiated_with_wrong_types);
for (unsigned I = 0; I < Node.getNumArgs(); ++I) {
if (InnerMatcher.matches(*Node.getArg(I)->IgnoreParenImpCasts(),
Finder, Builder)) {
return true;
}
}
return false;
}
/// \brief Matches the n'th parameter of a function declaration.
///
/// Given
/// class X { void f(int x) {} };
/// method(hasParameter(0, hasType(variable())))
/// matches f(int x) {}
/// with hasParameter(...)
/// matching int x
AST_MATCHER_P2(FunctionDecl, hasParameter,
unsigned, N, internal::Matcher<ParmVarDecl>,
InnerMatcher) {
return (N < Node.getNumParams() &&
InnerMatcher.matches(
*Node.getParamDecl(N), Finder, Builder));
}
/// \brief Matches any parameter of a function declaration.
///
/// Does not match the 'this' parameter of a method.
///
/// Given
/// class X { void f(int x, int y, int z) {} };
/// method(hasAnyParameter(hasName("y")))
/// matches f(int x, int y, int z) {}
/// with hasAnyParameter(...)
/// matching int y
AST_MATCHER_P(FunctionDecl, hasAnyParameter,
internal::Matcher<ParmVarDecl>, InnerMatcher) {
for (unsigned I = 0; I < Node.getNumParams(); ++I) {
if (InnerMatcher.matches(*Node.getParamDecl(I), Finder, Builder)) {
return true;
}
}
return false;
}
/// \brief Matches the return type of a function declaration.
///
/// Given:
/// class X { int f() { return 1; } };
/// method(returns(asString("int")))
/// matches int f() { return 1; }
AST_MATCHER_P(FunctionDecl, returns, internal::Matcher<QualType>, Matcher) {
return Matcher.matches(Node.getResultType(), Finder, Builder);
}
/// \brief Matches extern "C" function declarations.
///
/// Given:
/// extern "C" void f() {}
/// extern "C" { void g() {} }
/// void h() {}
/// function(isExternC())
/// matches the declaration of f and g, but not the declaration h
AST_MATCHER(FunctionDecl, isExternC) {
return Node.isExternC();
}
/// \brief Matches the condition expression of an if statement, for loop,
/// or conditional operator.
///
/// Example matches true (matcher = hasCondition(boolLiteral(equals(true))))
/// if (true) {}
AST_POLYMORPHIC_MATCHER_P(hasCondition, internal::Matcher<Expr>,
InnerMatcher) {
TOOLING_COMPILE_ASSERT(
(llvm::is_base_of<IfStmt, NodeType>::value) ||
(llvm::is_base_of<ForStmt, NodeType>::value) ||
(llvm::is_base_of<WhileStmt, NodeType>::value) ||
(llvm::is_base_of<DoStmt, NodeType>::value) ||
(llvm::is_base_of<ConditionalOperator, NodeType>::value),
has_condition_requires_if_statement_conditional_operator_or_loop);
const Expr *const Condition = Node.getCond();
return (Condition != NULL &&
InnerMatcher.matches(*Condition, Finder, Builder));
}
/// \brief Matches the condition variable statement in an if statement.
///
/// Given
/// if (A* a = GetAPointer()) {}
/// hasConditionVariableStatment(...)
/// matches 'A* a = GetAPointer()'.
AST_MATCHER_P(IfStmt, hasConditionVariableStatement,
internal::Matcher<DeclStmt>, InnerMatcher) {
const DeclStmt* const DeclarationStatement =
Node.getConditionVariableDeclStmt();
return DeclarationStatement != NULL &&
InnerMatcher.matches(*DeclarationStatement, Finder, Builder);
}
/// \brief Matches the index expression of an array subscript expression.
///
/// Given
/// int i[5];
/// void f() { i[1] = 42; }
/// arraySubscriptExpression(hasIndex(integerLiteral()))
/// matches \c i[1] with the \c integerLiteral() matching \c 1
AST_MATCHER_P(ArraySubscriptExpr, hasIndex,
internal::Matcher<Expr>, matcher) {
if (const Expr* Expression = Node.getIdx())
return matcher.matches(*Expression, Finder, Builder);
return false;
}
/// \brief Matches the base expression of an array subscript expression.
///
/// Given
/// int i[5];
/// void f() { i[1] = 42; }
/// arraySubscriptExpression(hasBase(implicitCast(
/// hasSourceExpression(declarationReference()))))
/// matches \c i[1] with the \c declarationReference() matching \c i
AST_MATCHER_P(ArraySubscriptExpr, hasBase,
internal::Matcher<Expr>, matcher) {
if (const Expr* Expression = Node.getBase())
return matcher.matches(*Expression, Finder, Builder);
return false;
}
/// \brief Matches a 'for', 'while', or 'do while' statement that has
/// a given body.
///
/// Given
/// for (;;) {}
/// hasBody(compoundStatement())
/// matches 'for (;;) {}'
/// with compoundStatement()
/// matching '{}'
AST_POLYMORPHIC_MATCHER_P(hasBody, internal::Matcher<Stmt>,
InnerMatcher) {
TOOLING_COMPILE_ASSERT(
(llvm::is_base_of<DoStmt, NodeType>::value) ||
(llvm::is_base_of<ForStmt, NodeType>::value) ||
(llvm::is_base_of<WhileStmt, NodeType>::value),
has_body_requires_for_while_or_do_statement);
const Stmt *const Statement = Node.getBody();
return (Statement != NULL &&
InnerMatcher.matches(*Statement, Finder, Builder));
}
/// \brief Matches compound statements where at least one substatement matches
/// a given matcher.
///
/// Given
/// { {}; 1+2; }
/// hasAnySubstatement(compoundStatement())
/// matches '{ {}; 1+2; }'
/// with compoundStatement()
/// matching '{}'
AST_MATCHER_P(CompoundStmt, hasAnySubstatement,
internal::Matcher<Stmt>, InnerMatcher) {
for (CompoundStmt::const_body_iterator It = Node.body_begin();
It != Node.body_end();
++It) {
if (InnerMatcher.matches(**It, Finder, Builder)) return true;
}
return false;
}
/// \brief Checks that a compound statement contains a specific number of
/// child statements.
///
/// Example: Given
/// { for (;;) {} }
/// compoundStatement(statementCountIs(0)))
/// matches '{}'
/// but does not match the outer compound statement.
AST_MATCHER_P(CompoundStmt, statementCountIs, unsigned, N) {
return Node.size() == N;
}
/// \brief Matches literals that are equal to the given value.
///
/// Example matches true (matcher = boolLiteral(equals(true)))
/// true
///
/// Usable as: Matcher<CharacterLiteral>, Matcher<CXXBoolLiteral>,
/// Matcher<FloatingLiteral>, Matcher<IntegerLiteral>
template <typename ValueT>
internal::PolymorphicMatcherWithParam1<internal::ValueEqualsMatcher, ValueT>
equals(const ValueT &Value) {
return internal::PolymorphicMatcherWithParam1<
internal::ValueEqualsMatcher,
ValueT>(Value);
}
/// \brief Matches the operator Name of operator expressions (binary or
/// unary).
///
/// Example matches a || b (matcher = binaryOperator(hasOperatorName("||")))
/// !(a || b)
AST_POLYMORPHIC_MATCHER_P(hasOperatorName, std::string, Name) {
TOOLING_COMPILE_ASSERT(
(llvm::is_base_of<BinaryOperator, NodeType>::value) ||
(llvm::is_base_of<UnaryOperator, NodeType>::value),
has_condition_requires_if_statement_or_conditional_operator);
return Name == Node.getOpcodeStr(Node.getOpcode());
}
/// \brief Matches the left hand side of binary operator expressions.
///
/// Example matches a (matcher = binaryOperator(hasLHS()))
/// a || b
AST_MATCHER_P(BinaryOperator, hasLHS,
internal::Matcher<Expr>, InnerMatcher) {
Expr *LeftHandSide = Node.getLHS();
return (LeftHandSide != NULL &&
InnerMatcher.matches(*LeftHandSide, Finder, Builder));
}
/// \brief Matches the right hand side of binary operator expressions.
///
/// Example matches b (matcher = binaryOperator(hasRHS()))
/// a || b
AST_MATCHER_P(BinaryOperator, hasRHS,
internal::Matcher<Expr>, InnerMatcher) {
Expr *RightHandSide = Node.getRHS();
return (RightHandSide != NULL &&
InnerMatcher.matches(*RightHandSide, Finder, Builder));
}
/// \brief Matches if either the left hand side or the right hand side of a
/// binary operator matches.
inline internal::Matcher<BinaryOperator> hasEitherOperand(
const internal::Matcher<Expr> &InnerMatcher) {
return anyOf(hasLHS(InnerMatcher), hasRHS(InnerMatcher));
}
/// \brief Matches if the operand of a unary operator matches.
///
/// Example matches true (matcher = hasOperand(boolLiteral(equals(true))))
/// !true
AST_MATCHER_P(UnaryOperator, hasUnaryOperand,
internal::Matcher<Expr>, InnerMatcher) {
const Expr * const Operand = Node.getSubExpr();
return (Operand != NULL &&
InnerMatcher.matches(*Operand, Finder, Builder));
}
/// \brief Matches if the cast's source expression matches the given matcher.
///
/// Example: matches "a string" (matcher =
/// hasSourceExpression(constructorCall()))
///
/// class URL { URL(string); };
/// URL url = "a string";
AST_MATCHER_P(CastExpr, hasSourceExpression,
internal::Matcher<Expr>, InnerMatcher) {
const Expr* const SubExpression = Node.getSubExpr();
return (SubExpression != NULL &&
InnerMatcher.matches(*SubExpression, Finder, Builder));
}
/// \brief Matches casts whose destination type matches a given matcher.
///
/// (Note: Clang's AST refers to other conversions as "casts" too, and calls
/// actual casts "explicit" casts.)
AST_MATCHER_P(ExplicitCastExpr, hasDestinationType,
internal::Matcher<QualType>, InnerMatcher) {
const QualType NodeType = Node.getTypeAsWritten();
return InnerMatcher.matches(NodeType, Finder, Builder);
}
/// \brief Matches implicit casts whose destination type matches a given
/// matcher.
///
/// FIXME: Unit test this matcher
AST_MATCHER_P(ImplicitCastExpr, hasImplicitDestinationType,
internal::Matcher<QualType>, InnerMatcher) {
return InnerMatcher.matches(Node.getType(), Finder, Builder);
}
/// \brief Matches the true branch expression of a conditional operator.
///
/// Example matches a
/// condition ? a : b
AST_MATCHER_P(ConditionalOperator, hasTrueExpression,
internal::Matcher<Expr>, InnerMatcher) {
Expr *Expression = Node.getTrueExpr();
return (Expression != NULL &&
InnerMatcher.matches(*Expression, Finder, Builder));
}
/// \brief Matches the false branch expression of a conditional operator.
///
/// Example matches b
/// condition ? a : b
AST_MATCHER_P(ConditionalOperator, hasFalseExpression,
internal::Matcher<Expr>, InnerMatcher) {
Expr *Expression = Node.getFalseExpr();
return (Expression != NULL &&
InnerMatcher.matches(*Expression, Finder, Builder));
}
/// \brief Matches if a declaration has a body attached.
///
/// Example matches A, va, fa
/// class A {};
/// class B; // Doesn't match, as it has no body.
/// int va;
/// extern int vb; // Doesn't match, as it doesn't define the variable.
/// void fa() {}
/// void fb(); // Doesn't match, as it has no body.
///
/// Usable as: Matcher<TagDecl>, Matcher<VarDecl>, Matcher<FunctionDecl>
inline internal::PolymorphicMatcherWithParam0<internal::IsDefinitionMatcher>
isDefinition() {
return internal::PolymorphicMatcherWithParam0<
internal::IsDefinitionMatcher>();
}
/// \brief Matches the class declaration that the given method declaration
/// belongs to.
///
/// FIXME: Generalize this for other kinds of declarations.
/// FIXME: What other kind of declarations would we need to generalize
/// this to?
///
/// Example matches A() in the last line
/// (matcher = constructorCall(hasDeclaration(method(
/// ofClass(hasName("A"))))))
/// class A {
/// public:
/// A();
/// };
/// A a = A();
AST_MATCHER_P(CXXMethodDecl, ofClass,
internal::Matcher<CXXRecordDecl>, InnerMatcher) {
const CXXRecordDecl *Parent = Node.getParent();
return (Parent != NULL &&
InnerMatcher.matches(*Parent, Finder, Builder));
}
/// \brief Matches member expressions that are called with '->' as opposed
/// to '.'.
///
/// Member calls on the implicit this pointer match as called with '->'.
///
/// Given
/// class Y {
/// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; }
/// int a;
/// static int b;
/// };
/// memberExpression(isArrow())
/// matches this->x, x, y.x, a, this->b
inline internal::Matcher<MemberExpr> isArrow() {
return makeMatcher(new internal::IsArrowMatcher());
}
/// \brief Matches QualType nodes that are of integer type.
///
/// Given
/// void a(int);
/// void b(long);
/// void c(double);
/// function(hasAnyParameter(hasType(isInteger())))
/// matches "a(int)", "b(long)", but not "c(double)".
AST_MATCHER(QualType, isInteger) {
return Node->isIntegerType();
}
/// \brief Matches QualType nodes that are const-qualified, i.e., that
/// include "top-level" const.
///
/// Given
/// void a(int);
/// void b(int const);
/// void c(const int);
/// void d(const int*);
/// void e(int const) {};
/// function(hasAnyParameter(hasType(isConstQualified())))
/// matches "void b(int const)", "void c(const int)" and
/// "void e(int const) {}". It does not match d as there
/// is no top-level const on the parameter type "const int *".
inline internal::Matcher<QualType> isConstQualified() {
return makeMatcher(new internal::IsConstQualifiedMatcher());
}
/// \brief Matches a member expression where the member is matched by a
/// given matcher.
///
/// Given
/// struct { int first, second; } first, second;
/// int i(second.first);
/// int j(first.second);
/// memberExpression(member(hasName("first")))
/// matches second.first
/// but not first.second (because the member name there is "second").
AST_MATCHER_P(MemberExpr, member,
internal::Matcher<ValueDecl>, InnerMatcher) {
return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder);
}
/// \brief Matches a member expression where the object expression is
/// matched by a given matcher.
///
/// Given
/// struct X { int m; };
/// void f(X x) { x.m; m; }
/// memberExpression(hasObjectExpression(hasType(record(hasName("X")))))))
/// matches "x.m" and "m"
/// with hasObjectExpression(...)
/// matching "x" and the implicit object expression of "m" which has type X*.
AST_MATCHER_P(MemberExpr, hasObjectExpression,
internal::Matcher<Expr>, InnerMatcher) {
return InnerMatcher.matches(*Node.getBase(), Finder, Builder);
}
/// \brief Matches any using shadow declaration.
///
/// Given
/// namespace X { void b(); }
/// using X::b;
/// usingDecl(hasAnyUsingShadowDecl(hasName("b"))))
/// matches \code using X::b \endcode
AST_MATCHER_P(UsingDecl, hasAnyUsingShadowDecl,
internal::Matcher<UsingShadowDecl>, Matcher) {
for (UsingDecl::shadow_iterator II = Node.shadow_begin();
II != Node.shadow_end(); ++II) {
if (Matcher.matches(**II, Finder, Builder))
return true;
}
return false;
}
/// \brief Matches a using shadow declaration where the target declaration is
/// matched by the given matcher.
///
/// Given
/// namespace X { int a; void b(); }
/// using X::a;
/// using X::b;
/// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(function())))
/// matches \code using X::b \endcode
/// but not \code using X::a \endcode
AST_MATCHER_P(UsingShadowDecl, hasTargetDecl,
internal::Matcher<NamedDecl>, Matcher) {
return Matcher.matches(*Node.getTargetDecl(), Finder, Builder);
}
/// \brief Matches template instantiations of function, class, or static
/// member variable template instantiations.
///
/// Given
/// template <typename T> class X {}; class A {}; X<A> x;
/// or
/// template <typename T> class X {}; class A {}; template class X<A>;
/// record(hasName("::X"), isTemplateInstantiation())
/// matches the template instantiation of X<A>.
///
/// But given
/// template <typename T> class X {}; class A {};
/// template <> class X<A> {}; X<A> x;
/// record(hasName("::X"), isTemplateInstantiation())
/// does not match, as X<A> is an explicit template specialization.
///
/// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl>
inline internal::PolymorphicMatcherWithParam0<
internal::IsTemplateInstantiationMatcher>
isTemplateInstantiation() {
return internal::PolymorphicMatcherWithParam0<
internal::IsTemplateInstantiationMatcher>();
}
} // end namespace ast_matchers
} // end namespace clang
#endif // LLVM_CLANG_AST_MATCHERS_AST_MATCHERS_H
|