blob: 7bc8421bab2ffdbecde560dd451f2783b4c94408 [file] [log] [blame]
Piotr Padlewskic6e05022016-05-17 19:22:57 +00001// unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp - AST matcher unit tests//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ASTMatchersTest.h"
11#include "clang/AST/PrettyPrinter.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Tooling/Tooling.h"
15#include "llvm/ADT/Triple.h"
16#include "llvm/Support/Host.h"
17#include "gtest/gtest.h"
18
19namespace clang {
20namespace ast_matchers {
21
22
23TEST(AllOf, AllOverloadsWork) {
24 const char Program[] =
25 "struct T { };"
26 "int f(int, T*, int, int);"
27 "void g(int x) { T t; f(x, &t, 3, 4); }";
28 EXPECT_TRUE(matches(Program,
29 callExpr(allOf(callee(functionDecl(hasName("f"))),
30 hasArgument(0, declRefExpr(to(varDecl())))))));
31 EXPECT_TRUE(matches(Program,
32 callExpr(allOf(callee(functionDecl(hasName("f"))),
33 hasArgument(0, declRefExpr(to(varDecl()))),
34 hasArgument(1, hasType(pointsTo(
35 recordDecl(hasName("T")))))))));
36 EXPECT_TRUE(matches(Program,
37 callExpr(allOf(callee(functionDecl(hasName("f"))),
38 hasArgument(0, declRefExpr(to(varDecl()))),
39 hasArgument(1, hasType(pointsTo(
40 recordDecl(hasName("T"))))),
41 hasArgument(2, integerLiteral(equals(3)))))));
42 EXPECT_TRUE(matches(Program,
43 callExpr(allOf(callee(functionDecl(hasName("f"))),
44 hasArgument(0, declRefExpr(to(varDecl()))),
45 hasArgument(1, hasType(pointsTo(
46 recordDecl(hasName("T"))))),
47 hasArgument(2, integerLiteral(equals(3))),
48 hasArgument(3, integerLiteral(equals(4)))))));
49}
50
51TEST(DeclarationMatcher, MatchHas) {
52 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
53 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
54 EXPECT_TRUE(matches("class X {};", HasClassX));
55
56 DeclarationMatcher YHasClassX =
57 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
58 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
59 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
60 EXPECT_TRUE(
61 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
62}
63
64TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
65 DeclarationMatcher Recursive =
66 recordDecl(
67 has(recordDecl(
68 has(recordDecl(hasName("X"))),
69 has(recordDecl(hasName("Y"))),
70 hasName("Z"))),
71 has(recordDecl(
72 has(recordDecl(hasName("A"))),
73 has(recordDecl(hasName("B"))),
74 hasName("C"))),
75 hasName("F"));
76
77 EXPECT_TRUE(matches(
78 "class F {"
79 " class Z {"
80 " class X {};"
81 " class Y {};"
82 " };"
83 " class C {"
84 " class A {};"
85 " class B {};"
86 " };"
87 "};", Recursive));
88
89 EXPECT_TRUE(matches(
90 "class F {"
91 " class Z {"
92 " class A {};"
93 " class X {};"
94 " class Y {};"
95 " };"
96 " class C {"
97 " class X {};"
98 " class A {};"
99 " class B {};"
100 " };"
101 "};", Recursive));
102
103 EXPECT_TRUE(matches(
104 "class O1 {"
105 " class O2 {"
106 " class F {"
107 " class Z {"
108 " class A {};"
109 " class X {};"
110 " class Y {};"
111 " };"
112 " class C {"
113 " class X {};"
114 " class A {};"
115 " class B {};"
116 " };"
117 " };"
118 " };"
119 "};", Recursive));
120}
121
122TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
123 DeclarationMatcher Recursive =
124 recordDecl(
125 anyOf(
126 has(recordDecl(
127 anyOf(
128 has(recordDecl(
129 hasName("X"))),
130 has(recordDecl(
131 hasName("Y"))),
132 hasName("Z")))),
133 has(recordDecl(
134 anyOf(
135 hasName("C"),
136 has(recordDecl(
137 hasName("A"))),
138 has(recordDecl(
139 hasName("B")))))),
140 hasName("F")));
141
142 EXPECT_TRUE(matches("class F {};", Recursive));
143 EXPECT_TRUE(matches("class Z {};", Recursive));
144 EXPECT_TRUE(matches("class C {};", Recursive));
145 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
146 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
147 EXPECT_TRUE(
148 matches("class O1 { class O2 {"
149 " class M { class N { class B {}; }; }; "
150 "}; };", Recursive));
151}
152
153TEST(DeclarationMatcher, MatchNot) {
154 DeclarationMatcher NotClassX =
155 cxxRecordDecl(
156 isDerivedFrom("Y"),
157 unless(hasName("X")));
158 EXPECT_TRUE(notMatches("", NotClassX));
159 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
160 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
161 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
162 EXPECT_TRUE(
163 notMatches("class Y {}; class Z {}; class X : public Y {};",
164 NotClassX));
165
166 DeclarationMatcher ClassXHasNotClassY =
167 recordDecl(
168 hasName("X"),
169 has(recordDecl(hasName("Z"))),
170 unless(
171 has(recordDecl(hasName("Y")))));
172 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
173 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
174 ClassXHasNotClassY));
175
176 DeclarationMatcher NamedNotRecord =
177 namedDecl(hasName("Foo"), unless(recordDecl()));
178 EXPECT_TRUE(matches("void Foo(){}", NamedNotRecord));
179 EXPECT_TRUE(notMatches("struct Foo {};", NamedNotRecord));
180}
181
182TEST(CastExpression, HasCastKind) {
183 EXPECT_TRUE(matches("char *p = 0;",
184 castExpr(hasCastKind(CK_NullToPointer))));
185 EXPECT_TRUE(notMatches("char *p = 0;",
186 castExpr(hasCastKind(CK_DerivedToBase))));
187 EXPECT_TRUE(matches("char *p = 0;",
188 implicitCastExpr(hasCastKind(CK_NullToPointer))));
189}
190
191TEST(DeclarationMatcher, HasDescendant) {
192 DeclarationMatcher ZDescendantClassX =
193 recordDecl(
194 hasDescendant(recordDecl(hasName("X"))),
195 hasName("Z"));
196 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
197 EXPECT_TRUE(
198 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
199 EXPECT_TRUE(
200 matches("class Z { class A { class Y { class X {}; }; }; };",
201 ZDescendantClassX));
202 EXPECT_TRUE(
203 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
204 ZDescendantClassX));
205 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
206
207 DeclarationMatcher ZDescendantClassXHasClassY =
208 recordDecl(
209 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
210 hasName("X"))),
211 hasName("Z"));
212 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
213 ZDescendantClassXHasClassY));
214 EXPECT_TRUE(
215 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
216 ZDescendantClassXHasClassY));
217 EXPECT_TRUE(notMatches(
218 "class Z {"
219 " class A {"
220 " class B {"
221 " class X {"
222 " class C {"
223 " class Y {};"
224 " };"
225 " };"
226 " }; "
227 " };"
228 "};", ZDescendantClassXHasClassY));
229
230 DeclarationMatcher ZDescendantClassXDescendantClassY =
231 recordDecl(
232 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
233 hasName("X"))),
234 hasName("Z"));
235 EXPECT_TRUE(
236 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
237 ZDescendantClassXDescendantClassY));
238 EXPECT_TRUE(matches(
239 "class Z {"
240 " class A {"
241 " class X {"
242 " class B {"
243 " class Y {};"
244 " };"
245 " class Y {};"
246 " };"
247 " };"
248 "};", ZDescendantClassXDescendantClassY));
249}
250
251TEST(DeclarationMatcher, HasDescendantMemoization) {
252 DeclarationMatcher CannotMemoize =
253 decl(hasDescendant(typeLoc().bind("x")), has(decl()));
254 EXPECT_TRUE(matches("void f() { int i; }", CannotMemoize));
255}
256
257TEST(DeclarationMatcher, HasDescendantMemoizationUsesRestrictKind) {
258 auto Name = hasName("i");
259 auto VD = internal::Matcher<VarDecl>(Name).dynCastTo<Decl>();
260 auto RD = internal::Matcher<RecordDecl>(Name).dynCastTo<Decl>();
261 // Matching VD first should not make a cache hit for RD.
262 EXPECT_TRUE(notMatches("void f() { int i; }",
263 decl(hasDescendant(VD), hasDescendant(RD))));
264 EXPECT_TRUE(notMatches("void f() { int i; }",
265 decl(hasDescendant(RD), hasDescendant(VD))));
266 // Not matching RD first should not make a cache hit for VD either.
267 EXPECT_TRUE(matches("void f() { int i; }",
268 decl(anyOf(hasDescendant(RD), hasDescendant(VD)))));
269}
270
271TEST(DeclarationMatcher, HasAncestorMemoization) {
272 // This triggers an hasAncestor with a TemplateArgument in the bound nodes.
273 // That node can't be memoized so we have to check for it before trying to put
274 // it on the cache.
275 DeclarationMatcher CannotMemoize = classTemplateSpecializationDecl(
276 hasAnyTemplateArgument(templateArgument().bind("targ")),
277 forEach(fieldDecl(hasAncestor(forStmt()))));
278
279 EXPECT_TRUE(notMatches("template <typename T> struct S;"
280 "template <> struct S<int>{ int i; int j; };",
281 CannotMemoize));
282}
283
284TEST(DeclarationMatcher, HasAttr) {
285 EXPECT_TRUE(matches("struct __attribute__((warn_unused)) X {};",
286 decl(hasAttr(clang::attr::WarnUnused))));
287 EXPECT_FALSE(matches("struct X {};",
288 decl(hasAttr(clang::attr::WarnUnused))));
289}
290
291
292TEST(DeclarationMatcher, MatchAnyOf) {
293 DeclarationMatcher YOrZDerivedFromX = cxxRecordDecl(
294 anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
295 EXPECT_TRUE(matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
296 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
297 EXPECT_TRUE(
298 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
299 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
300
301 DeclarationMatcher XOrYOrZOrU =
302 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
303 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
304 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
305
306 DeclarationMatcher XOrYOrZOrUOrV =
307 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
308 hasName("V")));
309 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
310 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
311 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
312 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
313 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
314 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
315
316 StatementMatcher MixedTypes = stmt(anyOf(ifStmt(), binaryOperator()));
317 EXPECT_TRUE(matches("int F() { return 1 + 2; }", MixedTypes));
318 EXPECT_TRUE(matches("int F() { if (true) return 1; }", MixedTypes));
319 EXPECT_TRUE(notMatches("int F() { return 1; }", MixedTypes));
320
321 EXPECT_TRUE(
322 matches("void f() try { } catch (int) { } catch (...) { }",
323 cxxCatchStmt(anyOf(hasDescendant(varDecl()), isCatchAll()))));
324}
325
326TEST(DeclarationMatcher, ClassIsDerived) {
327 DeclarationMatcher IsDerivedFromX = cxxRecordDecl(isDerivedFrom("X"));
328
329 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
330 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
331 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
332 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
333 EXPECT_TRUE(notMatches("", IsDerivedFromX));
334
335 DeclarationMatcher IsAX = cxxRecordDecl(isSameOrDerivedFrom("X"));
336
337 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
338 EXPECT_TRUE(matches("class X {};", IsAX));
339 EXPECT_TRUE(matches("class X;", IsAX));
340 EXPECT_TRUE(notMatches("class Y;", IsAX));
341 EXPECT_TRUE(notMatches("", IsAX));
342
343 DeclarationMatcher ZIsDerivedFromX =
344 cxxRecordDecl(hasName("Z"), isDerivedFrom("X"));
345 EXPECT_TRUE(
346 matches("class X {}; class Y : public X {}; class Z : public Y {};",
347 ZIsDerivedFromX));
348 EXPECT_TRUE(
349 matches("class X {};"
350 "template<class T> class Y : public X {};"
351 "class Z : public Y<int> {};", ZIsDerivedFromX));
352 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
353 ZIsDerivedFromX));
354 EXPECT_TRUE(
355 matches("template<class T> class X {}; "
356 "template<class T> class Z : public X<T> {};",
357 ZIsDerivedFromX));
358 EXPECT_TRUE(
359 matches("template<class T, class U=T> class X {}; "
360 "template<class T> class Z : public X<T> {};",
361 ZIsDerivedFromX));
362 EXPECT_TRUE(
363 notMatches("template<class X> class A { class Z : public X {}; };",
364 ZIsDerivedFromX));
365 EXPECT_TRUE(
366 matches("template<class X> class A { public: class Z : public X {}; }; "
367 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
368 EXPECT_TRUE(
369 matches("template <class T> class X {}; "
370 "template<class Y> class A { class Z : public X<Y> {}; };",
371 ZIsDerivedFromX));
372 EXPECT_TRUE(
373 notMatches("template<template<class T> class X> class A { "
374 " class Z : public X<int> {}; };", ZIsDerivedFromX));
375 EXPECT_TRUE(
376 matches("template<template<class T> class X> class A { "
377 " public: class Z : public X<int> {}; }; "
378 "template<class T> class X {}; void y() { A<X>::Z z; }",
379 ZIsDerivedFromX));
380 EXPECT_TRUE(
381 notMatches("template<class X> class A { class Z : public X::D {}; };",
382 ZIsDerivedFromX));
383 EXPECT_TRUE(
384 matches("template<class X> class A { public: "
385 " class Z : public X::D {}; }; "
386 "class Y { public: class X {}; typedef X D; }; "
387 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
388 EXPECT_TRUE(
389 matches("class X {}; typedef X Y; class Z : public Y {};",
390 ZIsDerivedFromX));
391 EXPECT_TRUE(
392 matches("template<class T> class Y { typedef typename T::U X; "
393 " class Z : public X {}; };", ZIsDerivedFromX));
394 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
395 ZIsDerivedFromX));
396 EXPECT_TRUE(
397 notMatches("template<class T> class X {}; "
398 "template<class T> class A { class Z : public X<T>::D {}; };",
399 ZIsDerivedFromX));
400 EXPECT_TRUE(
401 matches("template<class T> class X { public: typedef X<T> D; }; "
402 "template<class T> class A { public: "
403 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
404 ZIsDerivedFromX));
405 EXPECT_TRUE(
406 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
407 ZIsDerivedFromX));
408 EXPECT_TRUE(
409 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
410 ZIsDerivedFromX));
411 EXPECT_TRUE(
412 matches("class X {}; class Y : public X {}; "
413 "typedef Y V; typedef V W; class Z : public W {};",
414 ZIsDerivedFromX));
415 EXPECT_TRUE(
416 matches("template<class T, class U> class X {}; "
417 "template<class T> class A { class Z : public X<T, int> {}; };",
418 ZIsDerivedFromX));
419 EXPECT_TRUE(
420 notMatches("template<class X> class D { typedef X A; typedef A B; "
421 " typedef B C; class Z : public C {}; };",
422 ZIsDerivedFromX));
423 EXPECT_TRUE(
424 matches("class X {}; typedef X A; typedef A B; "
425 "class Z : public B {};", ZIsDerivedFromX));
426 EXPECT_TRUE(
427 matches("class X {}; typedef X A; typedef A B; typedef B C; "
428 "class Z : public C {};", ZIsDerivedFromX));
429 EXPECT_TRUE(
430 matches("class U {}; typedef U X; typedef X V; "
431 "class Z : public V {};", ZIsDerivedFromX));
432 EXPECT_TRUE(
433 matches("class Base {}; typedef Base X; "
434 "class Z : public Base {};", ZIsDerivedFromX));
435 EXPECT_TRUE(
436 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
437 "class Z : public Base {};", ZIsDerivedFromX));
438 EXPECT_TRUE(
439 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
440 "class Z : public Base {};", ZIsDerivedFromX));
441 EXPECT_TRUE(
442 matches("class A {}; typedef A X; typedef A Y; "
443 "class Z : public Y {};", ZIsDerivedFromX));
444 EXPECT_TRUE(
445 notMatches("template <typename T> class Z;"
446 "template <> class Z<void> {};"
447 "template <typename T> class Z : public Z<void> {};",
448 IsDerivedFromX));
449 EXPECT_TRUE(
450 matches("template <typename T> class X;"
451 "template <> class X<void> {};"
452 "template <typename T> class X : public X<void> {};",
453 IsDerivedFromX));
454 EXPECT_TRUE(matches(
455 "class X {};"
456 "template <typename T> class Z;"
457 "template <> class Z<void> {};"
458 "template <typename T> class Z : public Z<void>, public X {};",
459 ZIsDerivedFromX));
460 EXPECT_TRUE(
461 notMatches("template<int> struct X;"
462 "template<int i> struct X : public X<i-1> {};",
463 cxxRecordDecl(isDerivedFrom(recordDecl(hasName("Some"))))));
464 EXPECT_TRUE(matches(
465 "struct A {};"
466 "template<int> struct X;"
467 "template<int i> struct X : public X<i-1> {};"
468 "template<> struct X<0> : public A {};"
469 "struct B : public X<42> {};",
470 cxxRecordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A"))))));
471
472 // FIXME: Once we have better matchers for template type matching,
473 // get rid of the Variable(...) matching and match the right template
474 // declarations directly.
475 const char *RecursiveTemplateOneParameter =
476 "class Base1 {}; class Base2 {};"
477 "template <typename T> class Z;"
478 "template <> class Z<void> : public Base1 {};"
479 "template <> class Z<int> : public Base2 {};"
480 "template <> class Z<float> : public Z<void> {};"
481 "template <> class Z<double> : public Z<int> {};"
482 "template <typename T> class Z : public Z<float>, public Z<double> {};"
483 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
484 EXPECT_TRUE(matches(
485 RecursiveTemplateOneParameter,
486 varDecl(hasName("z_float"),
487 hasInitializer(hasType(cxxRecordDecl(isDerivedFrom("Base1")))))));
488 EXPECT_TRUE(notMatches(
489 RecursiveTemplateOneParameter,
490 varDecl(hasName("z_float"),
491 hasInitializer(hasType(cxxRecordDecl(isDerivedFrom("Base2")))))));
492 EXPECT_TRUE(matches(
493 RecursiveTemplateOneParameter,
494 varDecl(hasName("z_char"),
495 hasInitializer(hasType(cxxRecordDecl(isDerivedFrom("Base1"),
496 isDerivedFrom("Base2")))))));
497
498 const char *RecursiveTemplateTwoParameters =
499 "class Base1 {}; class Base2 {};"
500 "template <typename T1, typename T2> class Z;"
501 "template <typename T> class Z<void, T> : public Base1 {};"
502 "template <typename T> class Z<int, T> : public Base2 {};"
503 "template <typename T> class Z<float, T> : public Z<void, T> {};"
504 "template <typename T> class Z<double, T> : public Z<int, T> {};"
505 "template <typename T1, typename T2> class Z : "
506 " public Z<float, T2>, public Z<double, T2> {};"
507 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
508 " Z<char, void> z_char; }";
509 EXPECT_TRUE(matches(
510 RecursiveTemplateTwoParameters,
511 varDecl(hasName("z_float"),
512 hasInitializer(hasType(cxxRecordDecl(isDerivedFrom("Base1")))))));
513 EXPECT_TRUE(notMatches(
514 RecursiveTemplateTwoParameters,
515 varDecl(hasName("z_float"),
516 hasInitializer(hasType(cxxRecordDecl(isDerivedFrom("Base2")))))));
517 EXPECT_TRUE(matches(
518 RecursiveTemplateTwoParameters,
519 varDecl(hasName("z_char"),
520 hasInitializer(hasType(cxxRecordDecl(isDerivedFrom("Base1"),
521 isDerivedFrom("Base2")))))));
522 EXPECT_TRUE(matches(
523 "namespace ns { class X {}; class Y : public X {}; }",
524 cxxRecordDecl(isDerivedFrom("::ns::X"))));
525 EXPECT_TRUE(notMatches(
526 "class X {}; class Y : public X {};",
527 cxxRecordDecl(isDerivedFrom("::ns::X"))));
528
529 EXPECT_TRUE(matches(
530 "class X {}; class Y : public X {};",
531 cxxRecordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
532
533 EXPECT_TRUE(matches(
534 "template<typename T> class X {};"
535 "template<typename T> using Z = X<T>;"
536 "template <typename T> class Y : Z<T> {};",
537 cxxRecordDecl(isDerivedFrom(namedDecl(hasName("X"))))));
538}
539
Samuel Benzaquen49385c72016-06-28 14:08:56 +0000540TEST(DeclarationMatcher, IsLambda) {
541 const auto IsLambda = cxxMethodDecl(ofClass(cxxRecordDecl(isLambda())));
542 EXPECT_TRUE(matches("auto x = []{};", IsLambda));
543 EXPECT_TRUE(notMatches("struct S { void operator()() const; };", IsLambda));
544}
545
Piotr Padlewskic6e05022016-05-17 19:22:57 +0000546TEST(Matcher, BindMatchedNodes) {
547 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
548
549 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
550 ClassX, llvm::make_unique<VerifyIdIsBoundTo<CXXRecordDecl>>("x")));
551
552 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
553 ClassX, llvm::make_unique<VerifyIdIsBoundTo<CXXRecordDecl>>("other-id")));
554
555 TypeMatcher TypeAHasClassB = hasDeclaration(
556 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
557
558 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
559 TypeAHasClassB,
560 llvm::make_unique<VerifyIdIsBoundTo<Decl>>("b")));
561
562 StatementMatcher MethodX =
563 callExpr(callee(cxxMethodDecl(hasName("x")))).bind("x");
564
565 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
566 MethodX,
567 llvm::make_unique<VerifyIdIsBoundTo<CXXMemberCallExpr>>("x")));
568}
569
570TEST(Matcher, BindTheSameNameInAlternatives) {
571 StatementMatcher matcher = anyOf(
572 binaryOperator(hasOperatorName("+"),
573 hasLHS(expr().bind("x")),
574 hasRHS(integerLiteral(equals(0)))),
575 binaryOperator(hasOperatorName("+"),
576 hasLHS(integerLiteral(equals(0))),
577 hasRHS(expr().bind("x"))));
578
579 EXPECT_TRUE(matchAndVerifyResultTrue(
580 // The first branch of the matcher binds x to 0 but then fails.
581 // The second branch binds x to f() and succeeds.
582 "int f() { return 0 + f(); }",
583 matcher,
584 llvm::make_unique<VerifyIdIsBoundTo<CallExpr>>("x")));
585}
586
587TEST(Matcher, BindsIDForMemoizedResults) {
588 // Using the same matcher in two match expressions will make memoization
589 // kick in.
590 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
591 EXPECT_TRUE(matchAndVerifyResultTrue(
592 "class A { class B { class X {}; }; };",
593 DeclarationMatcher(anyOf(
594 recordDecl(hasName("A"), hasDescendant(ClassX)),
595 recordDecl(hasName("B"), hasDescendant(ClassX)))),
596 llvm::make_unique<VerifyIdIsBoundTo<Decl>>("x", 2)));
597}
598
599TEST(HasType, MatchesAsString) {
600 EXPECT_TRUE(
601 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
602 cxxMemberCallExpr(on(hasType(asString("class Y *"))))));
603 EXPECT_TRUE(
604 matches("class X { void x(int x) {} };",
605 cxxMethodDecl(hasParameter(0, hasType(asString("int"))))));
606 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
607 fieldDecl(hasType(asString("ns::A")))));
608 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
609 fieldDecl(hasType(asString("struct (anonymous namespace)::A")))));
610}
611
612TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
613 StatementMatcher OpCallAndAnd =
614 cxxOperatorCallExpr(hasOverloadedOperatorName("&&"));
615 EXPECT_TRUE(matches("class Y { }; "
616 "bool operator&&(Y x, Y y) { return true; }; "
617 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
618 StatementMatcher OpCallLessLess =
619 cxxOperatorCallExpr(hasOverloadedOperatorName("<<"));
620 EXPECT_TRUE(notMatches("class Y { }; "
621 "bool operator&&(Y x, Y y) { return true; }; "
622 "Y a; Y b; bool c = a && b;",
623 OpCallLessLess));
624 StatementMatcher OpStarCall =
625 cxxOperatorCallExpr(hasOverloadedOperatorName("*"));
626 EXPECT_TRUE(matches("class Y; int operator*(Y &); void f(Y &y) { *y; }",
627 OpStarCall));
628 DeclarationMatcher ClassWithOpStar =
629 cxxRecordDecl(hasMethod(hasOverloadedOperatorName("*")));
630 EXPECT_TRUE(matches("class Y { int operator*(); };",
631 ClassWithOpStar));
632 EXPECT_TRUE(notMatches("class Y { void myOperator(); };",
633 ClassWithOpStar)) ;
634 DeclarationMatcher AnyOpStar = functionDecl(hasOverloadedOperatorName("*"));
635 EXPECT_TRUE(matches("class Y; int operator*(Y &);", AnyOpStar));
636 EXPECT_TRUE(matches("class Y { int operator*(); };", AnyOpStar));
637}
638
639
640TEST(Matcher, NestedOverloadedOperatorCalls) {
641 EXPECT_TRUE(matchAndVerifyResultTrue(
642 "class Y { }; "
643 "Y& operator&&(Y& x, Y& y) { return x; }; "
644 "Y a; Y b; Y c; Y d = a && b && c;",
645 cxxOperatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
646 llvm::make_unique<VerifyIdIsBoundTo<CXXOperatorCallExpr>>("x", 2)));
647 EXPECT_TRUE(matches("class Y { }; "
648 "Y& operator&&(Y& x, Y& y) { return x; }; "
649 "Y a; Y b; Y c; Y d = a && b && c;",
650 cxxOperatorCallExpr(hasParent(cxxOperatorCallExpr()))));
651 EXPECT_TRUE(
652 matches("class Y { }; "
653 "Y& operator&&(Y& x, Y& y) { return x; }; "
654 "Y a; Y b; Y c; Y d = a && b && c;",
655 cxxOperatorCallExpr(hasDescendant(cxxOperatorCallExpr()))));
656}
657
658TEST(Matcher, VarDecl_Storage) {
659 auto M = varDecl(hasName("X"), hasLocalStorage());
660 EXPECT_TRUE(matches("void f() { int X; }", M));
661 EXPECT_TRUE(notMatches("int X;", M));
662 EXPECT_TRUE(notMatches("void f() { static int X; }", M));
663
664 M = varDecl(hasName("X"), hasGlobalStorage());
665 EXPECT_TRUE(notMatches("void f() { int X; }", M));
666 EXPECT_TRUE(matches("int X;", M));
667 EXPECT_TRUE(matches("void f() { static int X; }", M));
668}
669
670TEST(Matcher, VarDecl_StorageDuration) {
671 std::string T =
Haojian Wu398a8ea2016-09-27 07:53:20 +0000672 "void f() { int x; static int y; } int a;static int b;extern int c;";
Piotr Padlewskic6e05022016-05-17 19:22:57 +0000673
674 EXPECT_TRUE(matches(T, varDecl(hasName("x"), hasAutomaticStorageDuration())));
675 EXPECT_TRUE(
676 notMatches(T, varDecl(hasName("y"), hasAutomaticStorageDuration())));
677 EXPECT_TRUE(
678 notMatches(T, varDecl(hasName("a"), hasAutomaticStorageDuration())));
679
680 EXPECT_TRUE(matches(T, varDecl(hasName("y"), hasStaticStorageDuration())));
681 EXPECT_TRUE(matches(T, varDecl(hasName("a"), hasStaticStorageDuration())));
Haojian Wu398a8ea2016-09-27 07:53:20 +0000682 EXPECT_TRUE(matches(T, varDecl(hasName("b"), hasStaticStorageDuration())));
683 EXPECT_TRUE(matches(T, varDecl(hasName("c"), hasStaticStorageDuration())));
Piotr Padlewskic6e05022016-05-17 19:22:57 +0000684 EXPECT_TRUE(notMatches(T, varDecl(hasName("x"), hasStaticStorageDuration())));
685
686 // FIXME: It is really hard to test with thread_local itself because not all
687 // targets support TLS, which causes this to be an error depending on what
688 // platform the test is being run on. We do not have access to the TargetInfo
689 // object to be able to test whether the platform supports TLS or not.
690 EXPECT_TRUE(notMatches(T, varDecl(hasName("x"), hasThreadStorageDuration())));
691 EXPECT_TRUE(notMatches(T, varDecl(hasName("y"), hasThreadStorageDuration())));
692 EXPECT_TRUE(notMatches(T, varDecl(hasName("a"), hasThreadStorageDuration())));
693}
694
695TEST(Matcher, FindsVarDeclInFunctionParameter) {
696 EXPECT_TRUE(matches(
697 "void f(int i) {}",
698 varDecl(hasName("i"))));
699}
700
701TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
702 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
703 hasArgumentOfType(asString("int")))));
704 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
705 hasArgumentOfType(asString("float")))));
706 EXPECT_TRUE(matches(
707 "struct A {}; void x() { A a; int b = sizeof(a); }",
708 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
709 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
710 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
711}
712
713TEST(IsInteger, MatchesIntegers) {
714 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
715 EXPECT_TRUE(matches(
716 "long long i = 0; void f(long long) { }; void g() {f(i);}",
717 callExpr(hasArgument(0, declRefExpr(
718 to(varDecl(hasType(isInteger()))))))));
719}
720
721TEST(IsInteger, ReportsNoFalsePositives) {
722 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
723 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
724 callExpr(hasArgument(0, declRefExpr(
725 to(varDecl(hasType(isInteger()))))))));
726}
727
Clement Courbet42517592016-07-12 06:36:00 +0000728TEST(IsSignedInteger, MatchesSignedIntegers) {
729 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isSignedInteger()))));
730 EXPECT_TRUE(notMatches("unsigned i = 0;",
731 varDecl(hasType(isSignedInteger()))));
732}
733
734TEST(IsUnsignedInteger, MatchesUnsignedIntegers) {
735 EXPECT_TRUE(notMatches("int i = 0;", varDecl(hasType(isUnsignedInteger()))));
736 EXPECT_TRUE(matches("unsigned i = 0;",
737 varDecl(hasType(isUnsignedInteger()))));
738}
739
Piotr Padlewskic6e05022016-05-17 19:22:57 +0000740TEST(IsAnyPointer, MatchesPointers) {
741 EXPECT_TRUE(matches("int* i = nullptr;", varDecl(hasType(isAnyPointer()))));
742}
743
744TEST(IsAnyPointer, MatchesObjcPointer) {
745 EXPECT_TRUE(matchesObjC("@interface Foo @end Foo *f;",
746 varDecl(hasType(isAnyPointer()))));
747}
748
749TEST(IsAnyPointer, ReportsNoFalsePositives) {
750 EXPECT_TRUE(notMatches("int i = 0;", varDecl(hasType(isAnyPointer()))));
751}
752
753TEST(IsAnyCharacter, MatchesCharacters) {
754 EXPECT_TRUE(matches("char i = 0;", varDecl(hasType(isAnyCharacter()))));
755}
756
757TEST(IsAnyCharacter, ReportsNoFalsePositives) {
758 EXPECT_TRUE(notMatches("int i;", varDecl(hasType(isAnyCharacter()))));
759}
760
761TEST(IsArrow, MatchesMemberVariablesViaArrow) {
762 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
763 memberExpr(isArrow())));
764 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
765 memberExpr(isArrow())));
766 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
767 memberExpr(isArrow())));
768}
769
770TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
771 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
772 memberExpr(isArrow())));
773 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
774 memberExpr(isArrow())));
775 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
776 memberExpr(isArrow())));
777}
778
779TEST(IsArrow, MatchesMemberCallsViaArrow) {
780 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
781 memberExpr(isArrow())));
782 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
783 memberExpr(isArrow())));
784 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
785 memberExpr(isArrow())));
786}
787
788TEST(ConversionDeclaration, IsExplicit) {
789 EXPECT_TRUE(matches("struct S { explicit operator int(); };",
790 cxxConversionDecl(isExplicit())));
791 EXPECT_TRUE(notMatches("struct S { operator int(); };",
792 cxxConversionDecl(isExplicit())));
793}
794
795TEST(Matcher, ArgumentCount) {
796 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
797
798 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
799 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
800 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
801}
802
803TEST(Matcher, ParameterCount) {
804 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
805 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
806 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
807 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
808 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
809 EXPECT_TRUE(matches("void f(int i, ...) {};", Function1Arg));
810}
811
812TEST(Matcher, References) {
813 DeclarationMatcher ReferenceClassX = varDecl(
814 hasType(references(recordDecl(hasName("X")))));
815 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
816 ReferenceClassX));
817 EXPECT_TRUE(
818 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
819 // The match here is on the implicit copy constructor code for
820 // class X, not on code 'X x = y'.
821 EXPECT_TRUE(
822 matches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
823 EXPECT_TRUE(
824 notMatches("class X {}; extern X x;", ReferenceClassX));
825 EXPECT_TRUE(
826 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
827}
828
829TEST(QualType, hasLocalQualifiers) {
830 EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;",
831 varDecl(hasType(hasLocalQualifiers()))));
832 EXPECT_TRUE(matches("int *const j = nullptr;",
833 varDecl(hasType(hasLocalQualifiers()))));
834 EXPECT_TRUE(matches("int *volatile k;",
835 varDecl(hasType(hasLocalQualifiers()))));
836 EXPECT_TRUE(notMatches("int m;",
837 varDecl(hasType(hasLocalQualifiers()))));
838}
839
840TEST(IsExternC, MatchesExternCFunctionDeclarations) {
841 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
842 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
843 functionDecl(isExternC())));
844 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
845}
846
Benjamin Kramer87e6d992016-08-04 10:02:03 +0000847TEST(IsExternC, MatchesExternCVariableDeclarations) {
848 EXPECT_TRUE(matches("extern \"C\" int i;", varDecl(isExternC())));
849 EXPECT_TRUE(matches("extern \"C\" { int i; }", varDecl(isExternC())));
850 EXPECT_TRUE(notMatches("int i;", varDecl(isExternC())));
851}
852
Haojian Wub3d25462016-09-26 16:01:52 +0000853TEST(IsStaticStorageClass, MatchesStaticDeclarations) {
854 EXPECT_TRUE(
855 matches("static void f() {}", functionDecl(isStaticStorageClass())));
856 EXPECT_TRUE(matches("static int i = 1;", varDecl(isStaticStorageClass())));
857 EXPECT_TRUE(notMatches("int i = 1;", varDecl(isStaticStorageClass())));
Haojian Wu398a8ea2016-09-27 07:53:20 +0000858 EXPECT_TRUE(notMatches("extern int i;", varDecl(isStaticStorageClass())));
Haojian Wub3d25462016-09-26 16:01:52 +0000859 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isStaticStorageClass())));
860}
861
Piotr Padlewskic6e05022016-05-17 19:22:57 +0000862TEST(IsDefaulted, MatchesDefaultedFunctionDeclarations) {
863 EXPECT_TRUE(notMatches("class A { ~A(); };",
864 functionDecl(hasName("~A"), isDefaulted())));
865 EXPECT_TRUE(matches("class B { ~B() = default; };",
866 functionDecl(hasName("~B"), isDefaulted())));
867}
868
869TEST(IsDeleted, MatchesDeletedFunctionDeclarations) {
870 EXPECT_TRUE(
871 notMatches("void Func();", functionDecl(hasName("Func"), isDeleted())));
872 EXPECT_TRUE(matches("void Func() = delete;",
873 functionDecl(hasName("Func"), isDeleted())));
874}
875
876TEST(IsNoThrow, MatchesNoThrowFunctionDeclarations) {
877 EXPECT_TRUE(notMatches("void f();", functionDecl(isNoThrow())));
878 EXPECT_TRUE(notMatches("void f() throw(int);", functionDecl(isNoThrow())));
879 EXPECT_TRUE(
880 notMatches("void f() noexcept(false);", functionDecl(isNoThrow())));
881 EXPECT_TRUE(matches("void f() throw();", functionDecl(isNoThrow())));
882 EXPECT_TRUE(matches("void f() noexcept;", functionDecl(isNoThrow())));
Aaron Ballman230ad972016-06-07 17:34:45 +0000883
884 EXPECT_TRUE(notMatches("void f();", functionProtoType(isNoThrow())));
885 EXPECT_TRUE(notMatches("void f() throw(int);", functionProtoType(isNoThrow())));
886 EXPECT_TRUE(
887 notMatches("void f() noexcept(false);", functionProtoType(isNoThrow())));
888 EXPECT_TRUE(matches("void f() throw();", functionProtoType(isNoThrow())));
889 EXPECT_TRUE(matches("void f() noexcept;", functionProtoType(isNoThrow())));
Piotr Padlewskic6e05022016-05-17 19:22:57 +0000890}
891
892TEST(isConstexpr, MatchesConstexprDeclarations) {
893 EXPECT_TRUE(matches("constexpr int foo = 42;",
894 varDecl(hasName("foo"), isConstexpr())));
895 EXPECT_TRUE(matches("constexpr int bar();",
896 functionDecl(hasName("bar"), isConstexpr())));
897}
898
899TEST(TemplateArgumentCountIs, Matches) {
900 EXPECT_TRUE(
901 matches("template<typename T> struct C {}; C<int> c;",
902 classTemplateSpecializationDecl(templateArgumentCountIs(1))));
903 EXPECT_TRUE(
904 notMatches("template<typename T> struct C {}; C<int> c;",
905 classTemplateSpecializationDecl(templateArgumentCountIs(2))));
906
907 EXPECT_TRUE(matches("template<typename T> struct C {}; C<int> c;",
908 templateSpecializationType(templateArgumentCountIs(1))));
909 EXPECT_TRUE(
910 notMatches("template<typename T> struct C {}; C<int> c;",
911 templateSpecializationType(templateArgumentCountIs(2))));
912}
913
914TEST(IsIntegral, Matches) {
915 EXPECT_TRUE(matches("template<int T> struct C {}; C<42> c;",
916 classTemplateSpecializationDecl(
917 hasAnyTemplateArgument(isIntegral()))));
918 EXPECT_TRUE(notMatches("template<typename T> struct C {}; C<int> c;",
919 classTemplateSpecializationDecl(hasAnyTemplateArgument(
920 templateArgument(isIntegral())))));
921}
922
923TEST(EqualsIntegralValue, Matches) {
924 EXPECT_TRUE(matches("template<int T> struct C {}; C<42> c;",
925 classTemplateSpecializationDecl(
926 hasAnyTemplateArgument(equalsIntegralValue("42")))));
927 EXPECT_TRUE(matches("template<int T> struct C {}; C<-42> c;",
928 classTemplateSpecializationDecl(
929 hasAnyTemplateArgument(equalsIntegralValue("-42")))));
930 EXPECT_TRUE(matches("template<int T> struct C {}; C<-0042> c;",
931 classTemplateSpecializationDecl(
932 hasAnyTemplateArgument(equalsIntegralValue("-34")))));
933 EXPECT_TRUE(notMatches("template<int T> struct C {}; C<42> c;",
934 classTemplateSpecializationDecl(hasAnyTemplateArgument(
935 equalsIntegralValue("0042")))));
936}
937
938TEST(Matcher, MatchesAccessSpecDecls) {
939 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
940 EXPECT_TRUE(
941 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
942 EXPECT_TRUE(
943 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
944 EXPECT_TRUE(
945 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
946
947 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
948}
949
950TEST(Matcher, MatchesFinal) {
951 EXPECT_TRUE(matches("class X final {};", cxxRecordDecl(isFinal())));
952 EXPECT_TRUE(matches("class X { virtual void f() final; };",
953 cxxMethodDecl(isFinal())));
954 EXPECT_TRUE(notMatches("class X {};", cxxRecordDecl(isFinal())));
955 EXPECT_TRUE(
956 notMatches("class X { virtual void f(); };", cxxMethodDecl(isFinal())));
957}
958
959TEST(Matcher, MatchesVirtualMethod) {
960 EXPECT_TRUE(matches("class X { virtual int f(); };",
961 cxxMethodDecl(isVirtual(), hasName("::X::f"))));
962 EXPECT_TRUE(notMatches("class X { int f(); };", cxxMethodDecl(isVirtual())));
963}
964
965TEST(Matcher, MatchesVirtualAsWrittenMethod) {
966 EXPECT_TRUE(matches("class A { virtual int f(); };"
967 "class B : public A { int f(); };",
968 cxxMethodDecl(isVirtualAsWritten(), hasName("::A::f"))));
969 EXPECT_TRUE(
970 notMatches("class A { virtual int f(); };"
971 "class B : public A { int f(); };",
972 cxxMethodDecl(isVirtualAsWritten(), hasName("::B::f"))));
973}
974
975TEST(Matcher, MatchesPureMethod) {
976 EXPECT_TRUE(matches("class X { virtual int f() = 0; };",
977 cxxMethodDecl(isPure(), hasName("::X::f"))));
978 EXPECT_TRUE(notMatches("class X { int f(); };", cxxMethodDecl(isPure())));
979}
980
981TEST(Matcher, MatchesCopyAssignmentOperator) {
982 EXPECT_TRUE(matches("class X { X &operator=(X); };",
983 cxxMethodDecl(isCopyAssignmentOperator())));
984 EXPECT_TRUE(matches("class X { X &operator=(X &); };",
985 cxxMethodDecl(isCopyAssignmentOperator())));
986 EXPECT_TRUE(matches("class X { X &operator=(const X &); };",
987 cxxMethodDecl(isCopyAssignmentOperator())));
988 EXPECT_TRUE(matches("class X { X &operator=(volatile X &); };",
989 cxxMethodDecl(isCopyAssignmentOperator())));
990 EXPECT_TRUE(matches("class X { X &operator=(const volatile X &); };",
991 cxxMethodDecl(isCopyAssignmentOperator())));
992 EXPECT_TRUE(notMatches("class X { X &operator=(X &&); };",
993 cxxMethodDecl(isCopyAssignmentOperator())));
994}
995
996TEST(Matcher, MatchesMoveAssignmentOperator) {
997 EXPECT_TRUE(notMatches("class X { X &operator=(X); };",
998 cxxMethodDecl(isMoveAssignmentOperator())));
999 EXPECT_TRUE(matches("class X { X &operator=(X &&); };",
1000 cxxMethodDecl(isMoveAssignmentOperator())));
1001 EXPECT_TRUE(matches("class X { X &operator=(const X &&); };",
1002 cxxMethodDecl(isMoveAssignmentOperator())));
1003 EXPECT_TRUE(matches("class X { X &operator=(volatile X &&); };",
1004 cxxMethodDecl(isMoveAssignmentOperator())));
1005 EXPECT_TRUE(matches("class X { X &operator=(const volatile X &&); };",
1006 cxxMethodDecl(isMoveAssignmentOperator())));
1007 EXPECT_TRUE(notMatches("class X { X &operator=(X &); };",
1008 cxxMethodDecl(isMoveAssignmentOperator())));
1009}
1010
1011TEST(Matcher, MatchesConstMethod) {
1012 EXPECT_TRUE(
1013 matches("struct A { void foo() const; };", cxxMethodDecl(isConst())));
1014 EXPECT_TRUE(
1015 notMatches("struct A { void foo(); };", cxxMethodDecl(isConst())));
1016}
1017
1018TEST(Matcher, MatchesOverridingMethod) {
1019 EXPECT_TRUE(matches("class X { virtual int f(); }; "
1020 "class Y : public X { int f(); };",
1021 cxxMethodDecl(isOverride(), hasName("::Y::f"))));
1022 EXPECT_TRUE(notMatches("class X { virtual int f(); }; "
1023 "class Y : public X { int f(); };",
1024 cxxMethodDecl(isOverride(), hasName("::X::f"))));
1025 EXPECT_TRUE(notMatches("class X { int f(); }; "
1026 "class Y : public X { int f(); };",
1027 cxxMethodDecl(isOverride())));
1028 EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ",
1029 cxxMethodDecl(isOverride())));
1030 EXPECT_TRUE(
1031 matches("template <typename Base> struct Y : Base { void f() override;};",
1032 cxxMethodDecl(isOverride(), hasName("::Y::f"))));
1033}
1034
1035TEST(Matcher, ConstructorArgument) {
1036 StatementMatcher Constructor = cxxConstructExpr(
1037 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
1038
1039 EXPECT_TRUE(
1040 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1041 Constructor));
1042 EXPECT_TRUE(
1043 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1044 Constructor));
1045 EXPECT_TRUE(
1046 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1047 Constructor));
1048 EXPECT_TRUE(
1049 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1050 Constructor));
1051
1052 StatementMatcher WrongIndex = cxxConstructExpr(
1053 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
1054 EXPECT_TRUE(
1055 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1056 WrongIndex));
1057}
1058
1059TEST(Matcher, ConstructorArgumentCount) {
1060 StatementMatcher Constructor1Arg = cxxConstructExpr(argumentCountIs(1));
1061
1062 EXPECT_TRUE(
1063 matches("class X { public: X(int); }; void x() { X x(0); }",
1064 Constructor1Arg));
1065 EXPECT_TRUE(
1066 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1067 Constructor1Arg));
1068 EXPECT_TRUE(
1069 matches("class X { public: X(int); }; void x() { X x = 0; }",
1070 Constructor1Arg));
1071 EXPECT_TRUE(
1072 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1073 Constructor1Arg));
1074}
1075
1076TEST(Matcher, ConstructorListInitialization) {
1077 StatementMatcher ConstructorListInit =
1078 cxxConstructExpr(isListInitialization());
1079
1080 EXPECT_TRUE(
1081 matches("class X { public: X(int); }; void x() { X x{0}; }",
1082 ConstructorListInit));
1083 EXPECT_FALSE(
1084 matches("class X { public: X(int); }; void x() { X x(0); }",
1085 ConstructorListInit));
1086}
1087
1088TEST(ConstructorDeclaration, IsImplicit) {
1089 // This one doesn't match because the constructor is not added by the
1090 // compiler (it is not needed).
1091 EXPECT_TRUE(notMatches("class Foo { };",
1092 cxxConstructorDecl(isImplicit())));
1093 // The compiler added the implicit default constructor.
1094 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
1095 cxxConstructorDecl(isImplicit())));
1096 EXPECT_TRUE(matches("class Foo { Foo(){} };",
1097 cxxConstructorDecl(unless(isImplicit()))));
1098 // The compiler added an implicit assignment operator.
1099 EXPECT_TRUE(matches("struct A { int x; } a = {0}, b = a; void f() { a = b; }",
1100 cxxMethodDecl(isImplicit(), hasName("operator="))));
1101}
1102
1103TEST(ConstructorDeclaration, IsExplicit) {
1104 EXPECT_TRUE(matches("struct S { explicit S(int); };",
1105 cxxConstructorDecl(isExplicit())));
1106 EXPECT_TRUE(notMatches("struct S { S(int); };",
1107 cxxConstructorDecl(isExplicit())));
1108}
1109
1110TEST(ConstructorDeclaration, Kinds) {
Richard Smith96cd6712017-08-16 01:49:53 +00001111 EXPECT_TRUE(matches(
1112 "struct S { S(); };",
1113 cxxConstructorDecl(isDefaultConstructor(), unless(isImplicit()))));
1114 EXPECT_TRUE(notMatches(
1115 "struct S { S(); };",
1116 cxxConstructorDecl(isCopyConstructor(), unless(isImplicit()))));
1117 EXPECT_TRUE(notMatches(
1118 "struct S { S(); };",
1119 cxxConstructorDecl(isMoveConstructor(), unless(isImplicit()))));
Piotr Padlewskic6e05022016-05-17 19:22:57 +00001120
Richard Smith96cd6712017-08-16 01:49:53 +00001121 EXPECT_TRUE(notMatches(
1122 "struct S { S(const S&); };",
1123 cxxConstructorDecl(isDefaultConstructor(), unless(isImplicit()))));
1124 EXPECT_TRUE(matches(
1125 "struct S { S(const S&); };",
1126 cxxConstructorDecl(isCopyConstructor(), unless(isImplicit()))));
1127 EXPECT_TRUE(notMatches(
1128 "struct S { S(const S&); };",
1129 cxxConstructorDecl(isMoveConstructor(), unless(isImplicit()))));
Piotr Padlewskic6e05022016-05-17 19:22:57 +00001130
Richard Smith96cd6712017-08-16 01:49:53 +00001131 EXPECT_TRUE(notMatches(
1132 "struct S { S(S&&); };",
1133 cxxConstructorDecl(isDefaultConstructor(), unless(isImplicit()))));
1134 EXPECT_TRUE(notMatches(
1135 "struct S { S(S&&); };",
1136 cxxConstructorDecl(isCopyConstructor(), unless(isImplicit()))));
1137 EXPECT_TRUE(matches(
1138 "struct S { S(S&&); };",
1139 cxxConstructorDecl(isMoveConstructor(), unless(isImplicit()))));
Piotr Padlewskic6e05022016-05-17 19:22:57 +00001140}
1141
1142TEST(ConstructorDeclaration, IsUserProvided) {
1143 EXPECT_TRUE(notMatches("struct S { int X = 0; };",
1144 cxxConstructorDecl(isUserProvided())));
1145 EXPECT_TRUE(notMatches("struct S { S() = default; };",
1146 cxxConstructorDecl(isUserProvided())));
1147 EXPECT_TRUE(notMatches("struct S { S() = delete; };",
1148 cxxConstructorDecl(isUserProvided())));
1149 EXPECT_TRUE(
1150 matches("struct S { S(); };", cxxConstructorDecl(isUserProvided())));
1151 EXPECT_TRUE(matches("struct S { S(); }; S::S(){}",
1152 cxxConstructorDecl(isUserProvided())));
1153}
1154
1155TEST(ConstructorDeclaration, IsDelegatingConstructor) {
1156 EXPECT_TRUE(notMatches("struct S { S(); S(int); int X; };",
1157 cxxConstructorDecl(isDelegatingConstructor())));
1158 EXPECT_TRUE(notMatches("struct S { S(){} S(int X) : X(X) {} int X; };",
1159 cxxConstructorDecl(isDelegatingConstructor())));
1160 EXPECT_TRUE(matches(
1161 "struct S { S() : S(0) {} S(int X) : X(X) {} int X; };",
1162 cxxConstructorDecl(isDelegatingConstructor(), parameterCountIs(0))));
1163 EXPECT_TRUE(matches(
1164 "struct S { S(); S(int X); int X; }; S::S(int X) : S() {}",
1165 cxxConstructorDecl(isDelegatingConstructor(), parameterCountIs(1))));
1166}
1167
1168TEST(StringLiteral, HasSize) {
1169 StatementMatcher Literal = stringLiteral(hasSize(4));
1170 EXPECT_TRUE(matches("const char *s = \"abcd\";", Literal));
1171 // wide string
1172 EXPECT_TRUE(matches("const wchar_t *s = L\"abcd\";", Literal));
1173 // with escaped characters
1174 EXPECT_TRUE(matches("const char *s = \"\x05\x06\x07\x08\";", Literal));
1175 // no matching, too small
1176 EXPECT_TRUE(notMatches("const char *s = \"ab\";", Literal));
1177}
1178
1179TEST(Matcher, HasNameSupportsNamespaces) {
1180 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
1181 recordDecl(hasName("a::b::C"))));
1182 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
1183 recordDecl(hasName("::a::b::C"))));
1184 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
1185 recordDecl(hasName("b::C"))));
1186 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
1187 recordDecl(hasName("C"))));
1188 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
1189 recordDecl(hasName("c::b::C"))));
1190 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
1191 recordDecl(hasName("a::c::C"))));
1192 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
1193 recordDecl(hasName("a::b::A"))));
1194 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
1195 recordDecl(hasName("::C"))));
1196 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
1197 recordDecl(hasName("::b::C"))));
1198 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
1199 recordDecl(hasName("z::a::b::C"))));
1200 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
1201 recordDecl(hasName("a+b::C"))));
1202 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
1203 recordDecl(hasName("C"))));
1204}
1205
1206TEST(Matcher, HasNameSupportsOuterClasses) {
1207 EXPECT_TRUE(
1208 matches("class A { class B { class C; }; };",
1209 recordDecl(hasName("A::B::C"))));
1210 EXPECT_TRUE(
1211 matches("class A { class B { class C; }; };",
1212 recordDecl(hasName("::A::B::C"))));
1213 EXPECT_TRUE(
1214 matches("class A { class B { class C; }; };",
1215 recordDecl(hasName("B::C"))));
1216 EXPECT_TRUE(
1217 matches("class A { class B { class C; }; };",
1218 recordDecl(hasName("C"))));
1219 EXPECT_TRUE(
1220 notMatches("class A { class B { class C; }; };",
1221 recordDecl(hasName("c::B::C"))));
1222 EXPECT_TRUE(
1223 notMatches("class A { class B { class C; }; };",
1224 recordDecl(hasName("A::c::C"))));
1225 EXPECT_TRUE(
1226 notMatches("class A { class B { class C; }; };",
1227 recordDecl(hasName("A::B::A"))));
1228 EXPECT_TRUE(
1229 notMatches("class A { class B { class C; }; };",
1230 recordDecl(hasName("::C"))));
1231 EXPECT_TRUE(
1232 notMatches("class A { class B { class C; }; };",
1233 recordDecl(hasName("::B::C"))));
1234 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
1235 recordDecl(hasName("z::A::B::C"))));
1236 EXPECT_TRUE(
1237 notMatches("class A { class B { class C; }; };",
1238 recordDecl(hasName("A+B::C"))));
1239}
1240
1241TEST(Matcher, HasNameSupportsInlinedNamespaces) {
1242 std::string code = "namespace a { inline namespace b { class C; } }";
1243 EXPECT_TRUE(matches(code, recordDecl(hasName("a::b::C"))));
1244 EXPECT_TRUE(matches(code, recordDecl(hasName("a::C"))));
1245 EXPECT_TRUE(matches(code, recordDecl(hasName("::a::b::C"))));
1246 EXPECT_TRUE(matches(code, recordDecl(hasName("::a::C"))));
1247}
1248
1249TEST(Matcher, HasNameSupportsAnonymousNamespaces) {
1250 std::string code = "namespace a { namespace { class C; } }";
1251 EXPECT_TRUE(
1252 matches(code, recordDecl(hasName("a::(anonymous namespace)::C"))));
1253 EXPECT_TRUE(matches(code, recordDecl(hasName("a::C"))));
1254 EXPECT_TRUE(
1255 matches(code, recordDecl(hasName("::a::(anonymous namespace)::C"))));
1256 EXPECT_TRUE(matches(code, recordDecl(hasName("::a::C"))));
1257}
1258
1259TEST(Matcher, HasNameSupportsAnonymousOuterClasses) {
1260 EXPECT_TRUE(matches("class A { class { class C; } x; };",
1261 recordDecl(hasName("A::(anonymous class)::C"))));
1262 EXPECT_TRUE(matches("class A { class { class C; } x; };",
1263 recordDecl(hasName("::A::(anonymous class)::C"))));
1264 EXPECT_FALSE(matches("class A { class { class C; } x; };",
1265 recordDecl(hasName("::A::C"))));
1266 EXPECT_TRUE(matches("class A { struct { class C; } x; };",
1267 recordDecl(hasName("A::(anonymous struct)::C"))));
1268 EXPECT_TRUE(matches("class A { struct { class C; } x; };",
1269 recordDecl(hasName("::A::(anonymous struct)::C"))));
1270 EXPECT_FALSE(matches("class A { struct { class C; } x; };",
1271 recordDecl(hasName("::A::C"))));
1272}
1273
1274TEST(Matcher, HasNameSupportsFunctionScope) {
1275 std::string code =
1276 "namespace a { void F(int a) { struct S { int m; }; int i; } }";
1277 EXPECT_TRUE(matches(code, varDecl(hasName("i"))));
1278 EXPECT_FALSE(matches(code, varDecl(hasName("F()::i"))));
1279
1280 EXPECT_TRUE(matches(code, fieldDecl(hasName("m"))));
1281 EXPECT_TRUE(matches(code, fieldDecl(hasName("S::m"))));
1282 EXPECT_TRUE(matches(code, fieldDecl(hasName("F(int)::S::m"))));
1283 EXPECT_TRUE(matches(code, fieldDecl(hasName("a::F(int)::S::m"))));
1284 EXPECT_TRUE(matches(code, fieldDecl(hasName("::a::F(int)::S::m"))));
1285}
1286
1287TEST(Matcher, HasAnyName) {
1288 const std::string Code = "namespace a { namespace b { class C; } }";
1289
1290 EXPECT_TRUE(matches(Code, recordDecl(hasAnyName("XX", "a::b::C"))));
1291 EXPECT_TRUE(matches(Code, recordDecl(hasAnyName("a::b::C", "XX"))));
1292 EXPECT_TRUE(matches(Code, recordDecl(hasAnyName("XX::C", "a::b::C"))));
1293 EXPECT_TRUE(matches(Code, recordDecl(hasAnyName("XX", "C"))));
1294
1295 EXPECT_TRUE(notMatches(Code, recordDecl(hasAnyName("::C", "::b::C"))));
1296 EXPECT_TRUE(
1297 matches(Code, recordDecl(hasAnyName("::C", "::b::C", "::a::b::C"))));
1298
1299 std::vector<StringRef> Names = {"::C", "::b::C", "::a::b::C"};
1300 EXPECT_TRUE(matches(Code, recordDecl(hasAnyName(Names))));
1301}
1302
1303TEST(Matcher, IsDefinition) {
1304 DeclarationMatcher DefinitionOfClassA =
1305 recordDecl(hasName("A"), isDefinition());
1306 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1307 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1308
1309 DeclarationMatcher DefinitionOfVariableA =
1310 varDecl(hasName("a"), isDefinition());
1311 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1312 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1313
1314 DeclarationMatcher DefinitionOfMethodA =
1315 cxxMethodDecl(hasName("a"), isDefinition());
1316 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1317 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1318}
1319
1320TEST(Matcher, HandlesNullQualTypes) {
1321 // FIXME: Add a Type matcher so we can replace uses of this
1322 // variable with Type(True())
1323 const TypeMatcher AnyType = anything();
1324
1325 // We don't really care whether this matcher succeeds; we're testing that
1326 // it completes without crashing.
1327 EXPECT_TRUE(matches(
1328 "struct A { };"
1329 "template <typename T>"
1330 "void f(T t) {"
1331 " T local_t(t /* this becomes a null QualType in the AST */);"
1332 "}"
1333 "void g() {"
1334 " f(0);"
1335 "}",
1336 expr(hasType(TypeMatcher(
1337 anyOf(
1338 TypeMatcher(hasDeclaration(anything())),
1339 pointsTo(AnyType),
1340 references(AnyType)
1341 // Other QualType matchers should go here.
1342 ))))));
1343}
1344
1345
1346TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
1347 EXPECT_TRUE(matches("void f() { }",
1348 compoundStmt(statementCountIs(0))));
1349 EXPECT_TRUE(notMatches("void f() {}",
1350 compoundStmt(statementCountIs(1))));
1351}
1352
1353TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
1354 EXPECT_TRUE(matches("void f() { 1; }",
1355 compoundStmt(statementCountIs(1))));
1356 EXPECT_TRUE(notMatches("void f() { 1; }",
1357 compoundStmt(statementCountIs(0))));
1358 EXPECT_TRUE(notMatches("void f() { 1; }",
1359 compoundStmt(statementCountIs(2))));
1360}
1361
1362TEST(StatementCountIs, WorksWithMultipleStatements) {
1363 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
1364 compoundStmt(statementCountIs(3))));
1365}
1366
1367TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
1368 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
1369 compoundStmt(statementCountIs(1))));
1370 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
1371 compoundStmt(statementCountIs(2))));
1372 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
1373 compoundStmt(statementCountIs(3))));
1374 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
1375 compoundStmt(statementCountIs(4))));
1376}
1377
1378TEST(Member, WorksInSimplestCase) {
1379 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
1380 memberExpr(member(hasName("first")))));
1381}
1382
1383TEST(Member, DoesNotMatchTheBaseExpression) {
1384 // Don't pick out the wrong part of the member expression, this should
1385 // be checking the member (name) only.
1386 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
1387 memberExpr(member(hasName("first")))));
1388}
1389
1390TEST(Member, MatchesInMemberFunctionCall) {
1391 EXPECT_TRUE(matches("void f() {"
1392 " struct { void first() {}; } s;"
1393 " s.first();"
1394 "};",
1395 memberExpr(member(hasName("first")))));
1396}
1397
1398TEST(Member, MatchesMember) {
1399 EXPECT_TRUE(matches(
1400 "struct A { int i; }; void f() { A a; a.i = 2; }",
1401 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
1402 EXPECT_TRUE(notMatches(
1403 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
1404 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
1405}
1406
Aaron Ballman5c574342016-07-06 18:25:16 +00001407TEST(Member, BitFields) {
1408 EXPECT_TRUE(matches("class C { int a : 2; int b; };",
1409 fieldDecl(isBitField(), hasName("a"))));
1410 EXPECT_TRUE(notMatches("class C { int a : 2; int b; };",
1411 fieldDecl(isBitField(), hasName("b"))));
1412 EXPECT_TRUE(matches("class C { int a : 2; int b : 4; };",
1413 fieldDecl(isBitField(), hasBitWidth(2), hasName("a"))));
1414}
Piotr Padlewskic6e05022016-05-17 19:22:57 +00001415
Malcolm Parsons4ca3d182016-12-24 13:35:14 +00001416TEST(Member, InClassInitializer) {
1417 EXPECT_TRUE(
1418 matches("class C { int a = 2; int b; };",
1419 fieldDecl(hasInClassInitializer(integerLiteral(equals(2))),
1420 hasName("a"))));
1421 EXPECT_TRUE(
1422 notMatches("class C { int a = 2; int b; };",
1423 fieldDecl(hasInClassInitializer(anything()), hasName("b"))));
1424}
1425
Piotr Padlewskic6e05022016-05-17 19:22:57 +00001426TEST(Member, UnderstandsAccess) {
1427 EXPECT_TRUE(matches(
1428 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
1429 EXPECT_TRUE(notMatches(
1430 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
1431 EXPECT_TRUE(notMatches(
1432 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
1433
1434 EXPECT_TRUE(notMatches(
1435 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
1436 EXPECT_TRUE(notMatches(
1437 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
1438 EXPECT_TRUE(matches(
1439 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
1440
1441 EXPECT_TRUE(notMatches(
1442 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
1443 EXPECT_TRUE(matches("class A { protected: int i; };",
1444 fieldDecl(isProtected(), hasName("i"))));
1445 EXPECT_TRUE(notMatches(
1446 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
1447
1448 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
1449 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
1450 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
1451 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
1452}
1453
1454TEST(hasDynamicExceptionSpec, MatchesDynamicExceptionSpecifications) {
1455 EXPECT_TRUE(notMatches("void f();", functionDecl(hasDynamicExceptionSpec())));
1456 EXPECT_TRUE(notMatches("void g() noexcept;",
1457 functionDecl(hasDynamicExceptionSpec())));
1458 EXPECT_TRUE(notMatches("void h() noexcept(true);",
1459 functionDecl(hasDynamicExceptionSpec())));
1460 EXPECT_TRUE(notMatches("void i() noexcept(false);",
1461 functionDecl(hasDynamicExceptionSpec())));
1462 EXPECT_TRUE(
1463 matches("void j() throw();", functionDecl(hasDynamicExceptionSpec())));
1464 EXPECT_TRUE(
1465 matches("void k() throw(int);", functionDecl(hasDynamicExceptionSpec())));
1466 EXPECT_TRUE(
1467 matches("void l() throw(...);", functionDecl(hasDynamicExceptionSpec())));
Aaron Ballman230ad972016-06-07 17:34:45 +00001468
1469 EXPECT_TRUE(notMatches("void f();", functionProtoType(hasDynamicExceptionSpec())));
1470 EXPECT_TRUE(notMatches("void g() noexcept;",
1471 functionProtoType(hasDynamicExceptionSpec())));
1472 EXPECT_TRUE(notMatches("void h() noexcept(true);",
1473 functionProtoType(hasDynamicExceptionSpec())));
1474 EXPECT_TRUE(notMatches("void i() noexcept(false);",
1475 functionProtoType(hasDynamicExceptionSpec())));
1476 EXPECT_TRUE(
1477 matches("void j() throw();", functionProtoType(hasDynamicExceptionSpec())));
1478 EXPECT_TRUE(
1479 matches("void k() throw(int);", functionProtoType(hasDynamicExceptionSpec())));
1480 EXPECT_TRUE(
1481 matches("void l() throw(...);", functionProtoType(hasDynamicExceptionSpec())));
Piotr Padlewskic6e05022016-05-17 19:22:57 +00001482}
1483
1484TEST(HasObjectExpression, DoesNotMatchMember) {
1485 EXPECT_TRUE(notMatches(
1486 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
1487 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
1488}
1489
1490TEST(HasObjectExpression, MatchesBaseOfVariable) {
1491 EXPECT_TRUE(matches(
1492 "struct X { int m; }; void f(X x) { x.m; }",
1493 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
1494 EXPECT_TRUE(matches(
1495 "struct X { int m; }; void f(X* x) { x->m; }",
1496 memberExpr(hasObjectExpression(
1497 hasType(pointsTo(recordDecl(hasName("X"))))))));
1498}
1499
1500TEST(HasObjectExpression,
1501 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
1502 EXPECT_TRUE(matches(
1503 "class X {}; struct S { X m; void f() { this->m; } };",
1504 memberExpr(hasObjectExpression(
1505 hasType(pointsTo(recordDecl(hasName("S"))))))));
1506 EXPECT_TRUE(matches(
1507 "class X {}; struct S { X m; void f() { m; } };",
1508 memberExpr(hasObjectExpression(
1509 hasType(pointsTo(recordDecl(hasName("S"))))))));
1510}
1511
1512TEST(Field, DoesNotMatchNonFieldMembers) {
1513 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
1514 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
1515 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
1516 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
1517}
1518
1519TEST(Field, MatchesField) {
1520 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
1521}
1522
1523TEST(IsVolatileQualified, QualifiersMatch) {
1524 EXPECT_TRUE(matches("volatile int i = 42;",
1525 varDecl(hasType(isVolatileQualified()))));
1526 EXPECT_TRUE(notMatches("volatile int *i;",
1527 varDecl(hasType(isVolatileQualified()))));
1528 EXPECT_TRUE(matches("typedef volatile int v_int; v_int i = 42;",
1529 varDecl(hasType(isVolatileQualified()))));
1530}
1531
1532TEST(IsConstQualified, MatchesConstInt) {
1533 EXPECT_TRUE(matches("const int i = 42;",
1534 varDecl(hasType(isConstQualified()))));
1535}
1536
1537TEST(IsConstQualified, MatchesConstPointer) {
1538 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
1539 varDecl(hasType(isConstQualified()))));
1540}
1541
1542TEST(IsConstQualified, MatchesThroughTypedef) {
1543 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
1544 varDecl(hasType(isConstQualified()))));
1545 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
1546 varDecl(hasType(isConstQualified()))));
1547}
1548
1549TEST(IsConstQualified, DoesNotMatchInappropriately) {
1550 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
1551 varDecl(hasType(isConstQualified()))));
1552 EXPECT_TRUE(notMatches("int const* p;",
1553 varDecl(hasType(isConstQualified()))));
1554}
1555
1556TEST(DeclCount, DeclCountIsCorrect) {
1557 EXPECT_TRUE(matches("void f() {int i,j;}",
1558 declStmt(declCountIs(2))));
1559 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
1560 declStmt(declCountIs(3))));
1561 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
1562 declStmt(declCountIs(3))));
1563}
1564
1565
1566TEST(EachOf, TriggersForEachMatch) {
1567 EXPECT_TRUE(matchAndVerifyResultTrue(
1568 "class A { int a; int b; };",
1569 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
1570 has(fieldDecl(hasName("b")).bind("v")))),
1571 llvm::make_unique<VerifyIdIsBoundTo<FieldDecl>>("v", 2)));
1572}
1573
1574TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
1575 EXPECT_TRUE(matchAndVerifyResultTrue(
1576 "class A { int a; int c; };",
1577 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
1578 has(fieldDecl(hasName("b")).bind("v")))),
1579 llvm::make_unique<VerifyIdIsBoundTo<FieldDecl>>("v", 1)));
1580 EXPECT_TRUE(matchAndVerifyResultTrue(
1581 "class A { int c; int b; };",
1582 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
1583 has(fieldDecl(hasName("b")).bind("v")))),
1584 llvm::make_unique<VerifyIdIsBoundTo<FieldDecl>>("v", 1)));
1585 EXPECT_TRUE(notMatches(
1586 "class A { int c; int d; };",
1587 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
1588 has(fieldDecl(hasName("b")).bind("v"))))));
1589}
1590
1591TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
1592 // Make sure that we can both match the class by name (::X) and by the type
1593 // the template was instantiated with (via a field).
1594
1595 EXPECT_TRUE(matches(
1596 "template <typename T> class X {}; class A {}; X<A> x;",
1597 cxxRecordDecl(hasName("::X"), isTemplateInstantiation())));
1598
1599 EXPECT_TRUE(matches(
1600 "template <typename T> class X { T t; }; class A {}; X<A> x;",
1601 cxxRecordDecl(isTemplateInstantiation(), hasDescendant(
1602 fieldDecl(hasType(recordDecl(hasName("A"))))))));
1603}
1604
1605TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
1606 EXPECT_TRUE(matches(
1607 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
1608 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
1609 isTemplateInstantiation())));
1610}
1611
1612TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
1613 EXPECT_TRUE(matches(
1614 "template <typename T> class X { T t; }; class A {};"
1615 "template class X<A>;",
1616 cxxRecordDecl(isTemplateInstantiation(), hasDescendant(
1617 fieldDecl(hasType(recordDecl(hasName("A"))))))));
1618}
1619
1620TEST(IsTemplateInstantiation,
1621 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
1622 EXPECT_TRUE(matches(
1623 "template <typename T> class X {};"
1624 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
1625 cxxRecordDecl(hasName("::X"), isTemplateInstantiation())));
1626}
1627
1628TEST(IsTemplateInstantiation,
1629 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
1630 EXPECT_TRUE(matches(
1631 "class A {};"
1632 "class X {"
1633 " template <typename U> class Y { U u; };"
1634 " Y<A> y;"
1635 "};",
1636 cxxRecordDecl(hasName("::X::Y"), isTemplateInstantiation())));
1637}
1638
1639TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
1640 // FIXME: Figure out whether this makes sense. It doesn't affect the
1641 // normal use case as long as the uppermost instantiation always is marked
1642 // as template instantiation, but it might be confusing as a predicate.
1643 EXPECT_TRUE(matches(
1644 "class A {};"
1645 "template <typename T> class X {"
1646 " template <typename U> class Y { U u; };"
1647 " Y<T> y;"
1648 "}; X<A> x;",
1649 cxxRecordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
1650}
1651
1652TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
1653 EXPECT_TRUE(notMatches(
1654 "template <typename T> class X {}; class A {};"
1655 "template <> class X<A> {}; X<A> x;",
1656 cxxRecordDecl(hasName("::X"), isTemplateInstantiation())));
1657}
1658
1659TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
1660 EXPECT_TRUE(notMatches(
1661 "class A {}; class Y { A a; };",
1662 cxxRecordDecl(isTemplateInstantiation())));
1663}
1664
1665TEST(IsInstantiated, MatchesInstantiation) {
1666 EXPECT_TRUE(
1667 matches("template<typename T> class A { T i; }; class Y { A<int> a; };",
1668 cxxRecordDecl(isInstantiated())));
1669}
1670
1671TEST(IsInstantiated, NotMatchesDefinition) {
1672 EXPECT_TRUE(notMatches("template<typename T> class A { T i; };",
1673 cxxRecordDecl(isInstantiated())));
1674}
1675
1676TEST(IsInTemplateInstantiation, MatchesInstantiationStmt) {
1677 EXPECT_TRUE(matches("template<typename T> struct A { A() { T i; } };"
1678 "class Y { A<int> a; }; Y y;",
1679 declStmt(isInTemplateInstantiation())));
1680}
1681
1682TEST(IsInTemplateInstantiation, NotMatchesDefinitionStmt) {
1683 EXPECT_TRUE(notMatches("template<typename T> struct A { void x() { T i; } };",
1684 declStmt(isInTemplateInstantiation())));
1685}
1686
1687TEST(IsInstantiated, MatchesFunctionInstantiation) {
1688 EXPECT_TRUE(
1689 matches("template<typename T> void A(T t) { T i; } void x() { A(0); }",
1690 functionDecl(isInstantiated())));
1691}
1692
1693TEST(IsInstantiated, NotMatchesFunctionDefinition) {
1694 EXPECT_TRUE(notMatches("template<typename T> void A(T t) { T i; }",
1695 varDecl(isInstantiated())));
1696}
1697
1698TEST(IsInTemplateInstantiation, MatchesFunctionInstantiationStmt) {
1699 EXPECT_TRUE(
1700 matches("template<typename T> void A(T t) { T i; } void x() { A(0); }",
1701 declStmt(isInTemplateInstantiation())));
1702}
1703
1704TEST(IsInTemplateInstantiation, NotMatchesFunctionDefinitionStmt) {
1705 EXPECT_TRUE(notMatches("template<typename T> void A(T t) { T i; }",
1706 declStmt(isInTemplateInstantiation())));
1707}
1708
1709TEST(IsInTemplateInstantiation, Sharing) {
1710 auto Matcher = binaryOperator(unless(isInTemplateInstantiation()));
1711 // FIXME: Node sharing is an implementation detail, exposing it is ugly
1712 // and makes the matcher behave in non-obvious ways.
1713 EXPECT_TRUE(notMatches(
1714 "int j; template<typename T> void A(T t) { j += 42; } void x() { A(0); }",
1715 Matcher));
1716 EXPECT_TRUE(matches(
1717 "int j; template<typename T> void A(T t) { j += t; } void x() { A(0); }",
1718 Matcher));
1719}
1720
1721TEST(IsExplicitTemplateSpecialization,
1722 DoesNotMatchPrimaryTemplate) {
1723 EXPECT_TRUE(notMatches(
1724 "template <typename T> class X {};",
1725 cxxRecordDecl(isExplicitTemplateSpecialization())));
1726 EXPECT_TRUE(notMatches(
1727 "template <typename T> void f(T t);",
1728 functionDecl(isExplicitTemplateSpecialization())));
1729}
1730
1731TEST(IsExplicitTemplateSpecialization,
1732 DoesNotMatchExplicitTemplateInstantiations) {
1733 EXPECT_TRUE(notMatches(
1734 "template <typename T> class X {};"
1735 "template class X<int>; extern template class X<long>;",
1736 cxxRecordDecl(isExplicitTemplateSpecialization())));
1737 EXPECT_TRUE(notMatches(
1738 "template <typename T> void f(T t) {}"
1739 "template void f(int t); extern template void f(long t);",
1740 functionDecl(isExplicitTemplateSpecialization())));
1741}
1742
1743TEST(IsExplicitTemplateSpecialization,
1744 DoesNotMatchImplicitTemplateInstantiations) {
1745 EXPECT_TRUE(notMatches(
1746 "template <typename T> class X {}; X<int> x;",
1747 cxxRecordDecl(isExplicitTemplateSpecialization())));
1748 EXPECT_TRUE(notMatches(
1749 "template <typename T> void f(T t); void g() { f(10); }",
1750 functionDecl(isExplicitTemplateSpecialization())));
1751}
1752
1753TEST(IsExplicitTemplateSpecialization,
1754 MatchesExplicitTemplateSpecializations) {
1755 EXPECT_TRUE(matches(
1756 "template <typename T> class X {};"
1757 "template<> class X<int> {};",
1758 cxxRecordDecl(isExplicitTemplateSpecialization())));
1759 EXPECT_TRUE(matches(
1760 "template <typename T> void f(T t) {}"
1761 "template<> void f(int t) {}",
1762 functionDecl(isExplicitTemplateSpecialization())));
1763}
1764
1765TEST(TypeMatching, MatchesBool) {
1766 EXPECT_TRUE(matches("struct S { bool func(); };",
1767 cxxMethodDecl(returns(booleanType()))));
1768 EXPECT_TRUE(notMatches("struct S { void func(); };",
1769 cxxMethodDecl(returns(booleanType()))));
1770}
1771
1772TEST(TypeMatching, MatchesVoid) {
1773 EXPECT_TRUE(matches("struct S { void func(); };",
1774 cxxMethodDecl(returns(voidType()))));
1775}
1776
1777TEST(TypeMatching, MatchesRealFloats) {
1778 EXPECT_TRUE(matches("struct S { float func(); };",
1779 cxxMethodDecl(returns(realFloatingPointType()))));
1780 EXPECT_TRUE(notMatches("struct S { int func(); };",
1781 cxxMethodDecl(returns(realFloatingPointType()))));
1782 EXPECT_TRUE(matches("struct S { long double func(); };",
1783 cxxMethodDecl(returns(realFloatingPointType()))));
1784}
1785
1786TEST(TypeMatching, MatchesArrayTypes) {
1787 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
1788 EXPECT_TRUE(matches("int a[42];", arrayType()));
1789 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
1790
1791 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
1792 arrayType(hasElementType(builtinType()))));
1793
1794 EXPECT_TRUE(matches(
1795 "int const a[] = { 2, 3 };",
1796 qualType(arrayType(hasElementType(builtinType())))));
1797 EXPECT_TRUE(matches(
1798 "int const a[] = { 2, 3 };",
1799 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
1800 EXPECT_TRUE(matches(
1801 "typedef const int T; T x[] = { 1, 2 };",
1802 qualType(isConstQualified(), arrayType())));
1803
1804 EXPECT_TRUE(notMatches(
1805 "int a[] = { 2, 3 };",
1806 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
1807 EXPECT_TRUE(notMatches(
1808 "int a[] = { 2, 3 };",
1809 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
1810 EXPECT_TRUE(notMatches(
1811 "int const a[] = { 2, 3 };",
1812 qualType(arrayType(hasElementType(builtinType())),
1813 unless(isConstQualified()))));
1814
1815 EXPECT_TRUE(matches("int a[2];",
1816 constantArrayType(hasElementType(builtinType()))));
1817 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
1818}
1819
1820TEST(TypeMatching, DecayedType) {
1821 EXPECT_TRUE(matches("void f(int i[]);", valueDecl(hasType(decayedType(hasDecayedType(pointerType()))))));
1822 EXPECT_TRUE(notMatches("int i[7];", decayedType()));
1823}
1824
1825TEST(TypeMatching, MatchesComplexTypes) {
1826 EXPECT_TRUE(matches("_Complex float f;", complexType()));
1827 EXPECT_TRUE(matches(
1828 "_Complex float f;",
1829 complexType(hasElementType(builtinType()))));
1830 EXPECT_TRUE(notMatches(
1831 "_Complex float f;",
1832 complexType(hasElementType(isInteger()))));
1833}
1834
1835TEST(NS, Anonymous) {
1836 EXPECT_TRUE(notMatches("namespace N {}", namespaceDecl(isAnonymous())));
1837 EXPECT_TRUE(matches("namespace {}", namespaceDecl(isAnonymous())));
1838}
1839
1840TEST(EqualsBoundNodeMatcher, QualType) {
1841 EXPECT_TRUE(matches(
1842 "int i = 1;", varDecl(hasType(qualType().bind("type")),
1843 hasInitializer(ignoringParenImpCasts(
1844 hasType(qualType(equalsBoundNode("type"))))))));
1845 EXPECT_TRUE(notMatches("int i = 1.f;",
1846 varDecl(hasType(qualType().bind("type")),
1847 hasInitializer(ignoringParenImpCasts(hasType(
1848 qualType(equalsBoundNode("type"))))))));
1849}
1850
1851TEST(EqualsBoundNodeMatcher, NonMatchingTypes) {
1852 EXPECT_TRUE(notMatches(
1853 "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"),
1854 hasInitializer(ignoringParenImpCasts(
1855 hasType(qualType(equalsBoundNode("type"))))))));
1856}
1857
1858TEST(EqualsBoundNodeMatcher, Stmt) {
1859 EXPECT_TRUE(
1860 matches("void f() { if(true) {} }",
1861 stmt(allOf(ifStmt().bind("if"),
1862 hasParent(stmt(has(stmt(equalsBoundNode("if")))))))));
1863
1864 EXPECT_TRUE(notMatches(
1865 "void f() { if(true) { if (true) {} } }",
1866 stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if")))))));
1867}
1868
1869TEST(EqualsBoundNodeMatcher, Decl) {
1870 EXPECT_TRUE(matches(
1871 "class X { class Y {}; };",
1872 decl(allOf(recordDecl(hasName("::X::Y")).bind("record"),
1873 hasParent(decl(has(decl(equalsBoundNode("record")))))))));
1874
1875 EXPECT_TRUE(notMatches("class X { class Y {}; };",
1876 decl(allOf(recordDecl(hasName("::X")).bind("record"),
1877 has(decl(equalsBoundNode("record")))))));
1878}
1879
1880TEST(EqualsBoundNodeMatcher, Type) {
1881 EXPECT_TRUE(matches(
1882 "class X { int a; int b; };",
1883 recordDecl(
1884 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
1885 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
1886
1887 EXPECT_TRUE(notMatches(
1888 "class X { int a; double b; };",
1889 recordDecl(
1890 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
1891 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
1892}
1893
1894TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) {
1895 EXPECT_TRUE(matchAndVerifyResultTrue(
1896 "int f() {"
1897 " if (1) {"
1898 " int i = 9;"
1899 " }"
1900 " int j = 10;"
1901 " {"
1902 " float k = 9.0;"
1903 " }"
1904 " return 0;"
1905 "}",
1906 // Look for variable declarations within functions whose type is the same
1907 // as the function return type.
1908 functionDecl(returns(qualType().bind("type")),
1909 forEachDescendant(varDecl(hasType(
1910 qualType(equalsBoundNode("type")))).bind("decl"))),
1911 // Only i and j should match, not k.
1912 llvm::make_unique<VerifyIdIsBoundTo<VarDecl>>("decl", 2)));
1913}
1914
1915TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) {
1916 EXPECT_TRUE(matchAndVerifyResultTrue(
1917 "void f() {"
1918 " int x;"
1919 " double d;"
1920 " x = d + x - d + x;"
1921 "}",
1922 functionDecl(
1923 hasName("f"), forEachDescendant(varDecl().bind("d")),
1924 forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))),
1925 llvm::make_unique<VerifyIdIsBoundTo<VarDecl>>("d", 5)));
1926}
1927
1928TEST(EqualsBoundNodeMatcher, UnlessDescendantsOfAncestorsMatch) {
1929 EXPECT_TRUE(matchAndVerifyResultTrue(
1930 "struct StringRef { int size() const; const char* data() const; };"
1931 "void f(StringRef v) {"
1932 " v.data();"
1933 "}",
1934 cxxMemberCallExpr(
1935 callee(cxxMethodDecl(hasName("data"))),
1936 on(declRefExpr(to(
1937 varDecl(hasType(recordDecl(hasName("StringRef")))).bind("var")))),
1938 unless(hasAncestor(stmt(hasDescendant(cxxMemberCallExpr(
1939 callee(cxxMethodDecl(anyOf(hasName("size"), hasName("length")))),
1940 on(declRefExpr(to(varDecl(equalsBoundNode("var")))))))))))
1941 .bind("data"),
1942 llvm::make_unique<VerifyIdIsBoundTo<Expr>>("data", 1)));
1943
1944 EXPECT_FALSE(matches(
1945 "struct StringRef { int size() const; const char* data() const; };"
1946 "void f(StringRef v) {"
1947 " v.data();"
1948 " v.size();"
1949 "}",
1950 cxxMemberCallExpr(
1951 callee(cxxMethodDecl(hasName("data"))),
1952 on(declRefExpr(to(
1953 varDecl(hasType(recordDecl(hasName("StringRef")))).bind("var")))),
1954 unless(hasAncestor(stmt(hasDescendant(cxxMemberCallExpr(
1955 callee(cxxMethodDecl(anyOf(hasName("size"), hasName("length")))),
1956 on(declRefExpr(to(varDecl(equalsBoundNode("var")))))))))))
1957 .bind("data")));
1958}
1959
1960TEST(NullPointerConstants, Basic) {
1961 EXPECT_TRUE(matches("#define NULL ((void *)0)\n"
1962 "void *v1 = NULL;", expr(nullPointerConstant())));
1963 EXPECT_TRUE(matches("void *v2 = nullptr;", expr(nullPointerConstant())));
1964 EXPECT_TRUE(matches("void *v3 = __null;", expr(nullPointerConstant())));
1965 EXPECT_TRUE(matches("char *cp = (char *)0;", expr(nullPointerConstant())));
1966 EXPECT_TRUE(matches("int *ip = 0;", expr(nullPointerConstant())));
1967 EXPECT_TRUE(notMatches("int i = 0;", expr(nullPointerConstant())));
1968}
1969
Aaron Ballmana086b9f2016-08-17 13:10:42 +00001970TEST(HasExternalFormalLinkage, Basic) {
1971 EXPECT_TRUE(matches("int a = 0;", namedDecl(hasExternalFormalLinkage())));
1972 EXPECT_TRUE(
1973 notMatches("static int a = 0;", namedDecl(hasExternalFormalLinkage())));
1974 EXPECT_TRUE(notMatches("static void f(void) { int a = 0; }",
1975 namedDecl(hasExternalFormalLinkage())));
1976 EXPECT_TRUE(matches("void f(void) { int a = 0; }",
1977 namedDecl(hasExternalFormalLinkage())));
1978
1979 // Despite having internal semantic linkage, the anonymous namespace member
1980 // has external linkage because the member has a unique name in all
1981 // translation units.
1982 EXPECT_TRUE(matches("namespace { int a = 0; }",
1983 namedDecl(hasExternalFormalLinkage())));
1984}
1985
Piotr Padlewskic6e05022016-05-17 19:22:57 +00001986} // namespace ast_matchers
1987} // namespace clang