blob: 86e949fef4f99b3e619e20e8e8196659272ffd2f [file] [log] [blame]
Manuel Klimek4da21662012-07-06 05:48:52 +00001//===- unittest/Tooling/ASTMatchersTest.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/ASTMatchers/ASTMatchers.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Tooling/Tooling.h"
14#include "gtest/gtest.h"
15
16namespace clang {
17namespace ast_matchers {
18
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000019#if GTEST_HAS_DEATH_TEST
Manuel Klimek4da21662012-07-06 05:48:52 +000020TEST(HasNameDeathTest, DiesOnEmptyName) {
21 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000022 DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000023 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
24 }, "");
25}
26
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000027TEST(HasNameDeathTest, DiesOnEmptyPattern) {
28 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000029 DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000030 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
31 }, "");
32}
33
Manuel Klimek4da21662012-07-06 05:48:52 +000034TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
35 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000036 DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000037 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
38 }, "");
39}
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000040#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000041
Manuel Klimek715c9562012-07-25 10:02:02 +000042TEST(Decl, MatchesDeclarations) {
43 EXPECT_TRUE(notMatches("", decl(usingDecl())));
44 EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
45 decl(usingDecl())));
46}
47
Manuel Klimek4da21662012-07-06 05:48:52 +000048TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000049 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +000050 EXPECT_TRUE(matches("typedef int X;", NamedX));
51 EXPECT_TRUE(matches("int X;", NamedX));
52 EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
53 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
54 EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
55 EXPECT_TRUE(matches("namespace X { }", NamedX));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000056 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek4da21662012-07-06 05:48:52 +000057
58 EXPECT_TRUE(notMatches("#define X 1", NamedX));
59}
60
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000061TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000062 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000063 EXPECT_TRUE(matches("typedef int Xa;", NamedX));
64 EXPECT_TRUE(matches("int Xb;", NamedX));
65 EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
66 EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
67 EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
68 EXPECT_TRUE(matches("namespace Xij { }", NamedX));
69 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
70
71 EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
72
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000073 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000074 EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
75 EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
76
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000077 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000078 EXPECT_TRUE(matches("int abc;", Abc));
79 EXPECT_TRUE(matches("int aFOObBARc;", Abc));
80 EXPECT_TRUE(notMatches("int cab;", Abc));
81 EXPECT_TRUE(matches("int cabc;", Abc));
82}
83
Manuel Klimek4da21662012-07-06 05:48:52 +000084TEST(DeclarationMatcher, MatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000085 DeclarationMatcher ClassMatcher(recordDecl());
Manuel Klimeke265c872012-07-10 14:21:30 +000086#if !defined(_MSC_VER)
Manuel Klimek4da21662012-07-06 05:48:52 +000087 EXPECT_FALSE(matches("", ClassMatcher));
Manuel Klimeke265c872012-07-10 14:21:30 +000088#else
89 // Matches class type_info.
90 EXPECT_TRUE(matches("", ClassMatcher));
91#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000092
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000093 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +000094 EXPECT_TRUE(matches("class X;", ClassX));
95 EXPECT_TRUE(matches("class X {};", ClassX));
96 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
97 EXPECT_TRUE(notMatches("", ClassX));
98}
99
100TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000101 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000102
103 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
104 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
105 EXPECT_TRUE(matches("class X {};", IsDerivedFromX));
106 EXPECT_TRUE(matches("class X;", IsDerivedFromX));
107 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
108 EXPECT_TRUE(notMatches("", IsDerivedFromX));
109
110 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000111 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000112 EXPECT_TRUE(
113 matches("class X {}; class Y : public X {}; class Z : public Y {};",
114 ZIsDerivedFromX));
115 EXPECT_TRUE(
116 matches("class X {};"
117 "template<class T> class Y : public X {};"
118 "class Z : public Y<int> {};", ZIsDerivedFromX));
119 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
120 ZIsDerivedFromX));
121 EXPECT_TRUE(
122 matches("template<class T> class X {}; "
123 "template<class T> class Z : public X<T> {};",
124 ZIsDerivedFromX));
125 EXPECT_TRUE(
126 matches("template<class T, class U=T> class X {}; "
127 "template<class T> class Z : public X<T> {};",
128 ZIsDerivedFromX));
129 EXPECT_TRUE(
130 notMatches("template<class X> class A { class Z : public X {}; };",
131 ZIsDerivedFromX));
132 EXPECT_TRUE(
133 matches("template<class X> class A { public: class Z : public X {}; }; "
134 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
135 EXPECT_TRUE(
136 matches("template <class T> class X {}; "
137 "template<class Y> class A { class Z : public X<Y> {}; };",
138 ZIsDerivedFromX));
139 EXPECT_TRUE(
140 notMatches("template<template<class T> class X> class A { "
141 " class Z : public X<int> {}; };", ZIsDerivedFromX));
142 EXPECT_TRUE(
143 matches("template<template<class T> class X> class A { "
144 " public: class Z : public X<int> {}; }; "
145 "template<class T> class X {}; void y() { A<X>::Z z; }",
146 ZIsDerivedFromX));
147 EXPECT_TRUE(
148 notMatches("template<class X> class A { class Z : public X::D {}; };",
149 ZIsDerivedFromX));
150 EXPECT_TRUE(
151 matches("template<class X> class A { public: "
152 " class Z : public X::D {}; }; "
153 "class Y { public: class X {}; typedef X D; }; "
154 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
155 EXPECT_TRUE(
156 matches("class X {}; typedef X Y; class Z : public Y {};",
157 ZIsDerivedFromX));
158 EXPECT_TRUE(
159 matches("template<class T> class Y { typedef typename T::U X; "
160 " class Z : public X {}; };", ZIsDerivedFromX));
161 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
162 ZIsDerivedFromX));
163 EXPECT_TRUE(
164 notMatches("template<class T> class X {}; "
165 "template<class T> class A { class Z : public X<T>::D {}; };",
166 ZIsDerivedFromX));
167 EXPECT_TRUE(
168 matches("template<class T> class X { public: typedef X<T> D; }; "
169 "template<class T> class A { public: "
170 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
171 ZIsDerivedFromX));
172 EXPECT_TRUE(
173 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
174 ZIsDerivedFromX));
175 EXPECT_TRUE(
176 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
177 ZIsDerivedFromX));
178 EXPECT_TRUE(
179 matches("class X {}; class Y : public X {}; "
180 "typedef Y V; typedef V W; class Z : public W {};",
181 ZIsDerivedFromX));
182 EXPECT_TRUE(
183 matches("template<class T, class U> class X {}; "
184 "template<class T> class A { class Z : public X<T, int> {}; };",
185 ZIsDerivedFromX));
186 EXPECT_TRUE(
187 notMatches("template<class X> class D { typedef X A; typedef A B; "
188 " typedef B C; class Z : public C {}; };",
189 ZIsDerivedFromX));
190 EXPECT_TRUE(
191 matches("class X {}; typedef X A; typedef A B; "
192 "class Z : public B {};", ZIsDerivedFromX));
193 EXPECT_TRUE(
194 matches("class X {}; typedef X A; typedef A B; typedef B C; "
195 "class Z : public C {};", ZIsDerivedFromX));
196 EXPECT_TRUE(
197 matches("class U {}; typedef U X; typedef X V; "
198 "class Z : public V {};", ZIsDerivedFromX));
199 EXPECT_TRUE(
200 matches("class Base {}; typedef Base X; "
201 "class Z : public Base {};", ZIsDerivedFromX));
202 EXPECT_TRUE(
203 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
204 "class Z : public Base {};", ZIsDerivedFromX));
205 EXPECT_TRUE(
206 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
207 "class Z : public Base {};", ZIsDerivedFromX));
208 EXPECT_TRUE(
209 matches("class A {}; typedef A X; typedef A Y; "
210 "class Z : public Y {};", ZIsDerivedFromX));
211 EXPECT_TRUE(
212 notMatches("template <typename T> class Z;"
213 "template <> class Z<void> {};"
214 "template <typename T> class Z : public Z<void> {};",
215 IsDerivedFromX));
216 EXPECT_TRUE(
217 matches("template <typename T> class X;"
218 "template <> class X<void> {};"
219 "template <typename T> class X : public X<void> {};",
220 IsDerivedFromX));
221 EXPECT_TRUE(matches(
222 "class X {};"
223 "template <typename T> class Z;"
224 "template <> class Z<void> {};"
225 "template <typename T> class Z : public Z<void>, public X {};",
226 ZIsDerivedFromX));
227
228 // FIXME: Once we have better matchers for template type matching,
229 // get rid of the Variable(...) matching and match the right template
230 // declarations directly.
231 const char *RecursiveTemplateOneParameter =
232 "class Base1 {}; class Base2 {};"
233 "template <typename T> class Z;"
234 "template <> class Z<void> : public Base1 {};"
235 "template <> class Z<int> : public Base2 {};"
236 "template <> class Z<float> : public Z<void> {};"
237 "template <> class Z<double> : public Z<int> {};"
238 "template <typename T> class Z : public Z<float>, public Z<double> {};"
239 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
240 EXPECT_TRUE(matches(
241 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000242 varDecl(hasName("z_float"),
243 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000244 EXPECT_TRUE(notMatches(
245 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000246 varDecl(hasName("z_float"),
247 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000248 EXPECT_TRUE(matches(
249 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000250 varDecl(hasName("z_char"),
251 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
252 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000253
254 const char *RecursiveTemplateTwoParameters =
255 "class Base1 {}; class Base2 {};"
256 "template <typename T1, typename T2> class Z;"
257 "template <typename T> class Z<void, T> : public Base1 {};"
258 "template <typename T> class Z<int, T> : public Base2 {};"
259 "template <typename T> class Z<float, T> : public Z<void, T> {};"
260 "template <typename T> class Z<double, T> : public Z<int, T> {};"
261 "template <typename T1, typename T2> class Z : "
262 " public Z<float, T2>, public Z<double, T2> {};"
263 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
264 " Z<char, void> z_char; }";
265 EXPECT_TRUE(matches(
266 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000267 varDecl(hasName("z_float"),
268 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000269 EXPECT_TRUE(notMatches(
270 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000271 varDecl(hasName("z_float"),
272 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000273 EXPECT_TRUE(matches(
274 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000275 varDecl(hasName("z_char"),
276 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
277 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000278 EXPECT_TRUE(matches(
279 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000280 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000281 EXPECT_TRUE(notMatches(
282 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000283 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000284
285 EXPECT_TRUE(matches(
286 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000287 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000288}
289
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000290TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000291 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000292 EXPECT_TRUE(notMatches("class X;", ClassX));
293 EXPECT_TRUE(notMatches("class X {};", ClassX));
294}
295
296TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000297 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000298 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
299 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
300}
301
302TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
303 EXPECT_TRUE(notMatches("template<typename T> class X { };"
304 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000305 classTemplateDecl(hasName("X"),
306 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000307}
308
309TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
310 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
311 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000312 classTemplateDecl(hasName("X"),
313 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000314}
315
Daniel Jasper6a124492012-07-12 08:50:38 +0000316TEST(AllOf, AllOverloadsWork) {
317 const char Program[] =
318 "struct T { }; int f(int, T*); void g(int x) { T t; f(x, &t); }";
319 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000320 callExpr(allOf(callee(functionDecl(hasName("f"))),
321 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000322 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000323 callExpr(allOf(callee(functionDecl(hasName("f"))),
324 hasArgument(0, declRefExpr(to(varDecl()))),
325 hasArgument(1, hasType(pointsTo(
326 recordDecl(hasName("T")))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000327}
328
Manuel Klimek4da21662012-07-06 05:48:52 +0000329TEST(DeclarationMatcher, MatchAnyOf) {
330 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000331 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000332 EXPECT_TRUE(
333 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
334 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
335 EXPECT_TRUE(
336 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
337 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
338
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000339 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000340 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000341 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
342 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
343
Manuel Klimek4da21662012-07-06 05:48:52 +0000344 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000345 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
346 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000347 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
348 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
349 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
350 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
351 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
352 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
353}
354
355TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000356 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000357 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
358 EXPECT_TRUE(matches("class X {};", HasClassX));
359
360 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000361 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000362 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
363 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
364 EXPECT_TRUE(
365 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
366}
367
368TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
369 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000370 recordDecl(
371 has(recordDecl(
372 has(recordDecl(hasName("X"))),
373 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000374 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000375 has(recordDecl(
376 has(recordDecl(hasName("A"))),
377 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000378 hasName("C"))),
379 hasName("F"));
380
381 EXPECT_TRUE(matches(
382 "class F {"
383 " class Z {"
384 " class X {};"
385 " class Y {};"
386 " };"
387 " class C {"
388 " class A {};"
389 " class B {};"
390 " };"
391 "};", Recursive));
392
393 EXPECT_TRUE(matches(
394 "class F {"
395 " class Z {"
396 " class A {};"
397 " class X {};"
398 " class Y {};"
399 " };"
400 " class C {"
401 " class X {};"
402 " class A {};"
403 " class B {};"
404 " };"
405 "};", Recursive));
406
407 EXPECT_TRUE(matches(
408 "class O1 {"
409 " class O2 {"
410 " class F {"
411 " class Z {"
412 " class A {};"
413 " class X {};"
414 " class Y {};"
415 " };"
416 " class C {"
417 " class X {};"
418 " class A {};"
419 " class B {};"
420 " };"
421 " };"
422 " };"
423 "};", Recursive));
424}
425
426TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
427 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000428 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000429 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000430 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000431 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000432 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000433 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000434 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000435 hasName("Y"))),
436 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000437 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000438 anyOf(
439 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000440 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000441 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000442 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000443 hasName("B")))))),
444 hasName("F")));
445
446 EXPECT_TRUE(matches("class F {};", Recursive));
447 EXPECT_TRUE(matches("class Z {};", Recursive));
448 EXPECT_TRUE(matches("class C {};", Recursive));
449 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
450 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
451 EXPECT_TRUE(
452 matches("class O1 { class O2 {"
453 " class M { class N { class B {}; }; }; "
454 "}; };", Recursive));
455}
456
457TEST(DeclarationMatcher, MatchNot) {
458 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000459 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000460 isDerivedFrom("Y"),
461 unless(hasName("Y")),
462 unless(hasName("X")));
463 EXPECT_TRUE(notMatches("", NotClassX));
464 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
465 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
466 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
467 EXPECT_TRUE(
468 notMatches("class Y {}; class Z {}; class X : public Y {};",
469 NotClassX));
470
471 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000472 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000473 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000474 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000475 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000476 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000477 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
478 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
479 ClassXHasNotClassY));
480}
481
482TEST(DeclarationMatcher, HasDescendant) {
483 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000484 recordDecl(
485 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000486 hasName("Z"));
487 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
488 EXPECT_TRUE(
489 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
490 EXPECT_TRUE(
491 matches("class Z { class A { class Y { class X {}; }; }; };",
492 ZDescendantClassX));
493 EXPECT_TRUE(
494 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
495 ZDescendantClassX));
496 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
497
498 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000499 recordDecl(
500 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000501 hasName("X"))),
502 hasName("Z"));
503 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
504 ZDescendantClassXHasClassY));
505 EXPECT_TRUE(
506 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
507 ZDescendantClassXHasClassY));
508 EXPECT_TRUE(notMatches(
509 "class Z {"
510 " class A {"
511 " class B {"
512 " class X {"
513 " class C {"
514 " class Y {};"
515 " };"
516 " };"
517 " }; "
518 " };"
519 "};", ZDescendantClassXHasClassY));
520
521 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000522 recordDecl(
523 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
524 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000525 hasName("Z"));
526 EXPECT_TRUE(
527 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
528 ZDescendantClassXDescendantClassY));
529 EXPECT_TRUE(matches(
530 "class Z {"
531 " class A {"
532 " class X {"
533 " class B {"
534 " class Y {};"
535 " };"
536 " class Y {};"
537 " };"
538 " };"
539 "};", ZDescendantClassXDescendantClassY));
540}
541
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000542TEST(Enum, DoesNotMatchClasses) {
543 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
544}
545
546TEST(Enum, MatchesEnums) {
547 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
548}
549
550TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000551 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000552 EXPECT_TRUE(matches("enum X{ A };", Matcher));
553 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
554 EXPECT_TRUE(notMatches("enum X {};", Matcher));
555}
556
Manuel Klimek4da21662012-07-06 05:48:52 +0000557TEST(StatementMatcher, Has) {
558 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000559 expr(hasType(pointsTo(recordDecl(hasName("X")))),
560 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000561
562 EXPECT_TRUE(matches(
563 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
564 EXPECT_TRUE(notMatches(
565 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
566}
567
568TEST(StatementMatcher, HasDescendant) {
569 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000570 expr(hasType(pointsTo(recordDecl(hasName("X")))),
571 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000572
573 EXPECT_TRUE(matches(
574 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
575 HasDescendantVariableI));
576 EXPECT_TRUE(notMatches(
577 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
578 HasDescendantVariableI));
579}
580
581TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000582 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000583
584 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
585 EXPECT_TRUE(notMatches("class A {};", TypeA));
586
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000587 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000588
589 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
590 TypeDerivedFromA));
591 EXPECT_TRUE(notMatches("class A {};", TypeA));
592
593 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000594 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000595
596 EXPECT_TRUE(
597 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
598}
599
600// Returns from Run whether 'bound_nodes' contain a Decl bound to 'Id', which
601// can be dynamically casted to T.
602// Optionally checks that the check succeeded a specific number of times.
603template <typename T>
604class VerifyIdIsBoundToDecl : public BoundNodesCallback {
605public:
606 // Create an object that checks that a node of type 'T' was bound to 'Id'.
607 // Does not check for a certain number of matches.
608 explicit VerifyIdIsBoundToDecl(const std::string& Id)
609 : Id(Id), ExpectedCount(-1), Count(0) {}
610
611 // Create an object that checks that a node of type 'T' was bound to 'Id'.
612 // Checks that there were exactly 'ExpectedCount' matches.
613 explicit VerifyIdIsBoundToDecl(const std::string& Id, int ExpectedCount)
614 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
615
616 ~VerifyIdIsBoundToDecl() {
617 if (ExpectedCount != -1) {
618 EXPECT_EQ(ExpectedCount, Count);
619 }
620 }
621
622 virtual bool run(const BoundNodes *Nodes) {
623 if (Nodes->getDeclAs<T>(Id) != NULL) {
624 ++Count;
625 return true;
626 }
627 return false;
628 }
629
630private:
631 const std::string Id;
632 const int ExpectedCount;
633 int Count;
634};
635template <typename T>
636class VerifyIdIsBoundToStmt : public BoundNodesCallback {
637public:
638 explicit VerifyIdIsBoundToStmt(const std::string &Id) : Id(Id) {}
639 virtual bool run(const BoundNodes *Nodes) {
640 const T *Node = Nodes->getStmtAs<T>(Id);
641 return Node != NULL;
642 }
643private:
644 const std::string Id;
645};
646
647TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000648 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000649
650 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000651 ClassX, new VerifyIdIsBoundToDecl<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000652
653 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000654 ClassX, new VerifyIdIsBoundToDecl<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000655
656 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000657 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000658
659 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
660 TypeAHasClassB,
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000661 new VerifyIdIsBoundToDecl<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000662
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000663 StatementMatcher MethodX =
664 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000665
666 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
667 MethodX,
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000668 new VerifyIdIsBoundToStmt<CXXMemberCallExpr>("x")));
669}
670
671TEST(Matcher, BindTheSameNameInAlternatives) {
672 StatementMatcher matcher = anyOf(
673 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000674 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000675 hasRHS(integerLiteral(equals(0)))),
676 binaryOperator(hasOperatorName("+"),
677 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000678 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000679
680 EXPECT_TRUE(matchAndVerifyResultTrue(
681 // The first branch of the matcher binds x to 0 but then fails.
682 // The second branch binds x to f() and succeeds.
683 "int f() { return 0 + f(); }",
684 matcher,
685 new VerifyIdIsBoundToStmt<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000686}
687
Manuel Klimek66341c52012-08-30 19:41:06 +0000688TEST(Matcher, BindsIDForMemoizedResults) {
689 // Using the same matcher in two match expressions will make memoization
690 // kick in.
691 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
692 EXPECT_TRUE(matchAndVerifyResultTrue(
693 "class A { class B { class X {}; }; };",
694 DeclarationMatcher(anyOf(
695 recordDecl(hasName("A"), hasDescendant(ClassX)),
696 recordDecl(hasName("B"), hasDescendant(ClassX)))),
697 new VerifyIdIsBoundToDecl<Decl>("x", 2)));
698}
699
Manuel Klimek4da21662012-07-06 05:48:52 +0000700TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000701 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000702 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000703 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000704 EXPECT_TRUE(
705 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000706 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000707 EXPECT_TRUE(
708 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000709 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000710}
711
712TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000713 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000714 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000715 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000716 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000717 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000718 EXPECT_TRUE(
719 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000720 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000721}
722
723TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000724 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000725 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000726 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000727 EXPECT_TRUE(
728 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000729 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000730}
731
732TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000733 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000734 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000735 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000736 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000737 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000738}
739
740TEST(Matcher, Call) {
741 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000742 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000743 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000744
745 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
746 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
747
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000748 StatementMatcher MethodOnY =
749 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000750
751 EXPECT_TRUE(
752 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
753 MethodOnY));
754 EXPECT_TRUE(
755 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
756 MethodOnY));
757 EXPECT_TRUE(
758 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
759 MethodOnY));
760 EXPECT_TRUE(
761 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
762 MethodOnY));
763 EXPECT_TRUE(
764 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
765 MethodOnY));
766
767 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000768 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000769
770 EXPECT_TRUE(
771 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
772 MethodOnYPointer));
773 EXPECT_TRUE(
774 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
775 MethodOnYPointer));
776 EXPECT_TRUE(
777 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
778 MethodOnYPointer));
779 EXPECT_TRUE(
780 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
781 MethodOnYPointer));
782 EXPECT_TRUE(
783 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
784 MethodOnYPointer));
785}
786
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000787TEST(HasType, MatchesAsString) {
788 EXPECT_TRUE(
789 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000790 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000791 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000792 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000793 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000794 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000795 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000796 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000797}
798
Manuel Klimek4da21662012-07-06 05:48:52 +0000799TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000800 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000801 // Unary operator
802 EXPECT_TRUE(matches("class Y { }; "
803 "bool operator!(Y x) { return false; }; "
804 "Y y; bool c = !y;", OpCall));
805 // No match -- special operators like "new", "delete"
806 // FIXME: operator new takes size_t, for which we need stddef.h, for which
807 // we need to figure out include paths in the test.
808 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
809 // "class Y { }; "
810 // "void *operator new(size_t size) { return 0; } "
811 // "Y *y = new Y;", OpCall));
812 EXPECT_TRUE(notMatches("class Y { }; "
813 "void operator delete(void *p) { } "
814 "void a() {Y *y = new Y; delete y;}", OpCall));
815 // Binary operator
816 EXPECT_TRUE(matches("class Y { }; "
817 "bool operator&&(Y x, Y y) { return true; }; "
818 "Y a; Y b; bool c = a && b;",
819 OpCall));
820 // No match -- normal operator, not an overloaded one.
821 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
822 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
823}
824
825TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
826 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000827 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000828 EXPECT_TRUE(matches("class Y { }; "
829 "bool operator&&(Y x, Y y) { return true; }; "
830 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
831 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000832 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000833 EXPECT_TRUE(notMatches("class Y { }; "
834 "bool operator&&(Y x, Y y) { return true; }; "
835 "Y a; Y b; bool c = a && b;",
836 OpCallLessLess));
837}
838
839TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +0000840 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000841 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000842
843 EXPECT_TRUE(
844 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
845 MethodOnY));
846 EXPECT_TRUE(
847 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
848 MethodOnY));
849 EXPECT_TRUE(
850 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
851 MethodOnY));
852 EXPECT_TRUE(
853 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
854 MethodOnY));
855 EXPECT_TRUE(
856 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
857 MethodOnY));
858
859 EXPECT_TRUE(matches(
860 "class Y {"
861 " public: virtual void x();"
862 "};"
863 "class X : public Y {"
864 " public: virtual void x();"
865 "};"
866 "void z() { X *x; x->Y::x(); }", MethodOnY));
867}
868
869TEST(Matcher, VariableUsage) {
870 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000871 declRefExpr(to(
872 varDecl(hasInitializer(
873 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000874
875 EXPECT_TRUE(matches(
876 "class Y {"
877 " public:"
878 " bool x() const;"
879 "};"
880 "void z(const Y &y) {"
881 " bool b = y.x();"
882 " if (b) {}"
883 "}", Reference));
884
885 EXPECT_TRUE(notMatches(
886 "class Y {"
887 " public:"
888 " bool x() const;"
889 "};"
890 "void z(const Y &y) {"
891 " bool b = y.x();"
892 "}", Reference));
893}
894
Daniel Jasper9bd28092012-07-30 05:03:25 +0000895TEST(Matcher, FindsVarDeclInFuncitonParameter) {
896 EXPECT_TRUE(matches(
897 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000898 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +0000899}
900
Manuel Klimek4da21662012-07-06 05:48:52 +0000901TEST(Matcher, CalledVariable) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000902 StatementMatcher CallOnVariableY = expr(
903 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000904
905 EXPECT_TRUE(matches(
906 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
907 EXPECT_TRUE(matches(
908 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
909 EXPECT_TRUE(matches(
910 "class Y { public: void x(); };"
911 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
912 EXPECT_TRUE(matches(
913 "class Y { public: void x(); };"
914 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
915 EXPECT_TRUE(notMatches(
916 "class Y { public: void x(); };"
917 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
918 CallOnVariableY));
919}
920
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000921TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
922 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
923 unaryExprOrTypeTraitExpr()));
924 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
925 alignOfExpr(anything())));
926 // FIXME: Uncomment once alignof is enabled.
927 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
928 // unaryExprOrTypeTraitExpr()));
929 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
930 // sizeOfExpr()));
931}
932
933TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
934 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
935 hasArgumentOfType(asString("int")))));
936 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
937 hasArgumentOfType(asString("float")))));
938 EXPECT_TRUE(matches(
939 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000940 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000941 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000942 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000943}
944
Manuel Klimek4da21662012-07-06 05:48:52 +0000945TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000946 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000947}
948
949TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000950 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000951}
952
953TEST(MemberExpression, MatchesVariable) {
954 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000955 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000956 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000957 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000958 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000959 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000960}
961
962TEST(MemberExpression, MatchesStaticVariable) {
963 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000964 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000965 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000966 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000967 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000968 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000969}
970
Daniel Jasper6a124492012-07-12 08:50:38 +0000971TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000972 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
973 EXPECT_TRUE(matches(
974 "long long i = 0; void f(long long) { }; void g() {f(i);}",
975 callExpr(hasArgument(0, declRefExpr(
976 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000977}
978
979TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000980 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000981 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000982 callExpr(hasArgument(0, declRefExpr(
983 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000984}
985
Manuel Klimek4da21662012-07-06 05:48:52 +0000986TEST(IsArrow, MatchesMemberVariablesViaArrow) {
987 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000988 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +0000989 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000990 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +0000991 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000992 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +0000993}
994
995TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
996 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000997 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +0000998 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000999 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001000 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001001 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001002}
1003
1004TEST(IsArrow, MatchesMemberCallsViaArrow) {
1005 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001006 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001007 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001008 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001009 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001010 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001011}
1012
1013TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001014 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001015
1016 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1017 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1018}
1019
1020TEST(Callee, MatchesMemberExpressions) {
1021 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001022 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001023 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001024 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001025}
1026
1027TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001028 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001029
1030 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1031 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1032
Manuel Klimeke265c872012-07-10 14:21:30 +00001033#if !defined(_MSC_VER)
1034 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001035 // Dependent contexts, but a non-dependent call.
1036 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1037 CallFunctionF));
1038 EXPECT_TRUE(
1039 matches("void f(); template <int N> struct S { void g() { f(); } };",
1040 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001041#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001042
1043 // Depedent calls don't match.
1044 EXPECT_TRUE(
1045 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1046 CallFunctionF));
1047 EXPECT_TRUE(
1048 notMatches("void f(int);"
1049 "template <typename T> struct S { void g(T t) { f(t); } };",
1050 CallFunctionF));
1051}
1052
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001053TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1054 EXPECT_TRUE(
1055 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001056 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001057}
1058
1059TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1060 EXPECT_TRUE(
1061 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001062 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001063}
1064
1065TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1066 EXPECT_TRUE(
1067 notMatches("void g(); template <typename T> void f(T t) {}"
1068 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001069 functionTemplateDecl(hasName("f"),
1070 hasDescendant(declRefExpr(to(
1071 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001072}
1073
Manuel Klimek4da21662012-07-06 05:48:52 +00001074TEST(Matcher, Argument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001075 StatementMatcher CallArgumentY = expr(callExpr(
1076 hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001077
1078 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1079 EXPECT_TRUE(
1080 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1081 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1082
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001083 StatementMatcher WrongIndex = expr(callExpr(
1084 hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001085 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1086}
1087
1088TEST(Matcher, AnyArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001089 StatementMatcher CallArgumentY = expr(callExpr(
1090 hasAnyArgument(declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001091 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1092 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1093 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1094}
1095
1096TEST(Matcher, ArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001097 StatementMatcher Call1Arg = expr(callExpr(argumentCountIs(1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00001098
1099 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1100 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1101 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1102}
1103
1104TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001105 DeclarationMatcher ReferenceClassX = varDecl(
1106 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001107 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1108 ReferenceClassX));
1109 EXPECT_TRUE(
1110 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1111 EXPECT_TRUE(
1112 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1113 EXPECT_TRUE(
1114 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1115}
1116
1117TEST(HasParameter, CallsInnerMatcher) {
1118 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001119 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001120 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001121 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001122}
1123
1124TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1125 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001126 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001127}
1128
1129TEST(HasType, MatchesParameterVariableTypesStrictly) {
1130 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001131 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001132 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001133 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001134 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001135 methodDecl(hasParameter(0,
1136 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001137 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001138 methodDecl(hasParameter(0,
1139 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001140}
1141
1142TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1143 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001144 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001145 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001146 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001147}
1148
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001149TEST(Returns, MatchesReturnTypes) {
1150 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001151 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001152 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001153 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001154 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001155 functionDecl(returns(hasDeclaration(
1156 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001157}
1158
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001159TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001160 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1161 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1162 functionDecl(isExternC())));
1163 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001164}
1165
Manuel Klimek4da21662012-07-06 05:48:52 +00001166TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1167 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001168 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001169}
1170
1171TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1172 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001173 methodDecl(hasAnyParameter(hasType(pointsTo(
1174 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001175}
1176
1177TEST(HasName, MatchesParameterVariableDeclartions) {
1178 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001179 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001180 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001181 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001182}
1183
Daniel Jasper371f9392012-08-01 08:40:24 +00001184TEST(Matcher, MatchesClassTemplateSpecialization) {
1185 EXPECT_TRUE(matches("template<typename T> struct A {};"
1186 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001187 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001188 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001189 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001190 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001191 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001192}
1193
1194TEST(Matcher, MatchesTypeTemplateArgument) {
1195 EXPECT_TRUE(matches(
1196 "template<typename T> struct B {};"
1197 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001198 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001199 asString("int"))))));
1200}
1201
1202TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1203 EXPECT_TRUE(matches(
1204 "struct B { int next; };"
1205 "template<int(B::*next_ptr)> struct A {};"
1206 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001207 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1208 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001209}
1210
1211TEST(Matcher, MatchesSpecificArgument) {
1212 EXPECT_TRUE(matches(
1213 "template<typename T, typename U> class A {};"
1214 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001215 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001216 1, refersToType(asString("int"))))));
1217 EXPECT_TRUE(notMatches(
1218 "template<typename T, typename U> class A {};"
1219 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001220 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001221 1, refersToType(asString("int"))))));
1222}
1223
Manuel Klimek4da21662012-07-06 05:48:52 +00001224TEST(Matcher, ConstructorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001225 StatementMatcher Constructor = expr(constructExpr());
Manuel Klimek4da21662012-07-06 05:48:52 +00001226
1227 EXPECT_TRUE(
1228 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1229 EXPECT_TRUE(
1230 matches("class X { public: X(); }; void x() { X x = X(); }",
1231 Constructor));
1232 EXPECT_TRUE(
1233 matches("class X { public: X(int); }; void x() { X x = 0; }",
1234 Constructor));
1235 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1236}
1237
1238TEST(Matcher, ConstructorArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001239 StatementMatcher Constructor = expr(constructExpr(
1240 hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001241
1242 EXPECT_TRUE(
1243 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1244 Constructor));
1245 EXPECT_TRUE(
1246 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1247 Constructor));
1248 EXPECT_TRUE(
1249 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1250 Constructor));
1251 EXPECT_TRUE(
1252 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1253 Constructor));
1254
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001255 StatementMatcher WrongIndex = expr(constructExpr(
1256 hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001257 EXPECT_TRUE(
1258 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1259 WrongIndex));
1260}
1261
1262TEST(Matcher, ConstructorArgumentCount) {
1263 StatementMatcher Constructor1Arg =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001264 expr(constructExpr(argumentCountIs(1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00001265
1266 EXPECT_TRUE(
1267 matches("class X { public: X(int); }; void x() { X x(0); }",
1268 Constructor1Arg));
1269 EXPECT_TRUE(
1270 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1271 Constructor1Arg));
1272 EXPECT_TRUE(
1273 matches("class X { public: X(int); }; void x() { X x = 0; }",
1274 Constructor1Arg));
1275 EXPECT_TRUE(
1276 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1277 Constructor1Arg));
1278}
1279
1280TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001281 StatementMatcher TempExpression = expr(bindTemporaryExpr());
Manuel Klimek4da21662012-07-06 05:48:52 +00001282
1283 std::string ClassString = "class string { public: string(); ~string(); }; ";
1284
1285 EXPECT_TRUE(
1286 matches(ClassString +
1287 "string GetStringByValue();"
1288 "void FunctionTakesString(string s);"
1289 "void run() { FunctionTakesString(GetStringByValue()); }",
1290 TempExpression));
1291
1292 EXPECT_TRUE(
1293 notMatches(ClassString +
1294 "string* GetStringPointer(); "
1295 "void FunctionTakesStringPtr(string* s);"
1296 "void run() {"
1297 " string* s = GetStringPointer();"
1298 " FunctionTakesStringPtr(GetStringPointer());"
1299 " FunctionTakesStringPtr(s);"
1300 "}",
1301 TempExpression));
1302
1303 EXPECT_TRUE(
1304 notMatches("class no_dtor {};"
1305 "no_dtor GetObjByValue();"
1306 "void ConsumeObj(no_dtor param);"
1307 "void run() { ConsumeObj(GetObjByValue()); }",
1308 TempExpression));
1309}
1310
Sam Panzere16acd32012-08-24 22:04:44 +00001311TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1312 std::string ClassString =
1313 "class string { public: string(); int length(); }; ";
1314
1315 EXPECT_TRUE(
1316 matches(ClassString +
1317 "string GetStringByValue();"
1318 "void FunctionTakesString(string s);"
1319 "void run() { FunctionTakesString(GetStringByValue()); }",
1320 materializeTemporaryExpr()));
1321
1322 EXPECT_TRUE(
1323 notMatches(ClassString +
1324 "string* GetStringPointer(); "
1325 "void FunctionTakesStringPtr(string* s);"
1326 "void run() {"
1327 " string* s = GetStringPointer();"
1328 " FunctionTakesStringPtr(GetStringPointer());"
1329 " FunctionTakesStringPtr(s);"
1330 "}",
1331 materializeTemporaryExpr()));
1332
1333 EXPECT_TRUE(
1334 notMatches(ClassString +
1335 "string GetStringByValue();"
1336 "void run() { int k = GetStringByValue().length(); }",
1337 materializeTemporaryExpr()));
1338
1339 EXPECT_TRUE(
1340 notMatches(ClassString +
1341 "string GetStringByValue();"
1342 "void run() { GetStringByValue(); }",
1343 materializeTemporaryExpr()));
1344}
1345
Manuel Klimek4da21662012-07-06 05:48:52 +00001346TEST(ConstructorDeclaration, SimpleCase) {
1347 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001348 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001349 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001350 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001351}
1352
1353TEST(ConstructorDeclaration, IsImplicit) {
1354 // This one doesn't match because the constructor is not added by the
1355 // compiler (it is not needed).
1356 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001357 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001358 // The compiler added the implicit default constructor.
1359 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001360 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001361 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001362 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001363}
1364
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001365TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1366 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001367 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001368}
1369
1370TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001371 EXPECT_TRUE(notMatches("class Foo {};",
1372 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001373}
1374
Manuel Klimek4da21662012-07-06 05:48:52 +00001375TEST(HasAnyConstructorInitializer, SimpleCase) {
1376 EXPECT_TRUE(notMatches(
1377 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001378 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001379 EXPECT_TRUE(matches(
1380 "class Foo {"
1381 " Foo() : foo_() { }"
1382 " int foo_;"
1383 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001384 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001385}
1386
1387TEST(HasAnyConstructorInitializer, ForField) {
1388 static const char Code[] =
1389 "class Baz { };"
1390 "class Foo {"
1391 " Foo() : foo_() { }"
1392 " Baz foo_;"
1393 " Baz bar_;"
1394 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001395 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1396 forField(hasType(recordDecl(hasName("Baz"))))))));
1397 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001398 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001399 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1400 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001401}
1402
1403TEST(HasAnyConstructorInitializer, WithInitializer) {
1404 static const char Code[] =
1405 "class Foo {"
1406 " Foo() : foo_(0) { }"
1407 " int foo_;"
1408 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001409 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001410 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001411 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001412 withInitializer(integerLiteral(equals(1)))))));
1413}
1414
1415TEST(HasAnyConstructorInitializer, IsWritten) {
1416 static const char Code[] =
1417 "struct Bar { Bar(){} };"
1418 "class Foo {"
1419 " Foo() : foo_() { }"
1420 " Bar foo_;"
1421 " Bar bar_;"
1422 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001423 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001424 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001425 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001426 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001427 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001428 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1429}
1430
1431TEST(Matcher, NewExpression) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001432 StatementMatcher New = expr(newExpr());
Manuel Klimek4da21662012-07-06 05:48:52 +00001433
1434 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1435 EXPECT_TRUE(
1436 matches("class X { public: X(); }; void x() { new X(); }", New));
1437 EXPECT_TRUE(
1438 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1439 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1440}
1441
1442TEST(Matcher, NewExpressionArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001443 StatementMatcher New = expr(constructExpr(
1444 hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001445
1446 EXPECT_TRUE(
1447 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1448 New));
1449 EXPECT_TRUE(
1450 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1451 New));
1452 EXPECT_TRUE(
1453 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1454 New));
1455
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001456 StatementMatcher WrongIndex = expr(constructExpr(
1457 hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001458 EXPECT_TRUE(
1459 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1460 WrongIndex));
1461}
1462
1463TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001464 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001465
1466 EXPECT_TRUE(
1467 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1468 EXPECT_TRUE(
1469 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1470 New));
1471}
1472
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001473TEST(Matcher, DeleteExpression) {
1474 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001475 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001476}
1477
Manuel Klimek4da21662012-07-06 05:48:52 +00001478TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001479 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001480
1481 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1482 EXPECT_TRUE(
1483 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1484 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1485}
1486
1487TEST(Matcher, StringLiterals) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001488 StatementMatcher Literal = expr(stringLiteral());
Manuel Klimek4da21662012-07-06 05:48:52 +00001489 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1490 // wide string
1491 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1492 // with escaped characters
1493 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1494 // no matching -- though the data type is the same, there is no string literal
1495 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1496}
1497
1498TEST(Matcher, CharacterLiterals) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001499 StatementMatcher CharLiteral = expr(characterLiteral());
Manuel Klimek4da21662012-07-06 05:48:52 +00001500 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1501 // wide character
1502 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1503 // wide character, Hex encoded, NOT MATCHED!
1504 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1505 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1506}
1507
1508TEST(Matcher, IntegerLiterals) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001509 StatementMatcher HasIntLiteral = expr(integerLiteral());
Manuel Klimek4da21662012-07-06 05:48:52 +00001510 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1511 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1512 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1513 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1514
1515 // Non-matching cases (character literals, float and double)
1516 EXPECT_TRUE(notMatches("int i = L'a';",
1517 HasIntLiteral)); // this is actually a character
1518 // literal cast to int
1519 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1520 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1521 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1522}
1523
1524TEST(Matcher, Conditions) {
1525 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1526
1527 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1528 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1529 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1530 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1531 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1532}
1533
1534TEST(MatchBinaryOperator, HasOperatorName) {
1535 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1536
1537 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1538 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1539}
1540
1541TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1542 StatementMatcher OperatorTrueFalse =
1543 binaryOperator(hasLHS(boolLiteral(equals(true))),
1544 hasRHS(boolLiteral(equals(false))));
1545
1546 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1547 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1548 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1549}
1550
1551TEST(MatchBinaryOperator, HasEitherOperand) {
1552 StatementMatcher HasOperand =
1553 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1554
1555 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1556 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1557 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1558}
1559
1560TEST(Matcher, BinaryOperatorTypes) {
1561 // Integration test that verifies the AST provides all binary operators in
1562 // a way we expect.
1563 // FIXME: Operator ','
1564 EXPECT_TRUE(
1565 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1566 EXPECT_TRUE(
1567 matches("bool b; bool c = (b = true);",
1568 binaryOperator(hasOperatorName("="))));
1569 EXPECT_TRUE(
1570 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1571 EXPECT_TRUE(
1572 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1573 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1574 EXPECT_TRUE(
1575 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1576 EXPECT_TRUE(
1577 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1578 EXPECT_TRUE(
1579 matches("int i = 1; int j = (i <<= 2);",
1580 binaryOperator(hasOperatorName("<<="))));
1581 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1582 EXPECT_TRUE(
1583 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1584 EXPECT_TRUE(
1585 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1586 EXPECT_TRUE(
1587 matches("int i = 1; int j = (i >>= 2);",
1588 binaryOperator(hasOperatorName(">>="))));
1589 EXPECT_TRUE(
1590 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1591 EXPECT_TRUE(
1592 matches("int i = 42; int j = (i ^= 42);",
1593 binaryOperator(hasOperatorName("^="))));
1594 EXPECT_TRUE(
1595 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1596 EXPECT_TRUE(
1597 matches("int i = 42; int j = (i %= 42);",
1598 binaryOperator(hasOperatorName("%="))));
1599 EXPECT_TRUE(
1600 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1601 EXPECT_TRUE(
1602 matches("bool b = true && false;",
1603 binaryOperator(hasOperatorName("&&"))));
1604 EXPECT_TRUE(
1605 matches("bool b = true; bool c = (b &= false);",
1606 binaryOperator(hasOperatorName("&="))));
1607 EXPECT_TRUE(
1608 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1609 EXPECT_TRUE(
1610 matches("bool b = true || false;",
1611 binaryOperator(hasOperatorName("||"))));
1612 EXPECT_TRUE(
1613 matches("bool b = true; bool c = (b |= false);",
1614 binaryOperator(hasOperatorName("|="))));
1615 EXPECT_TRUE(
1616 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1617 EXPECT_TRUE(
1618 matches("int i = 42; int j = (i *= 23);",
1619 binaryOperator(hasOperatorName("*="))));
1620 EXPECT_TRUE(
1621 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1622 EXPECT_TRUE(
1623 matches("int i = 42; int j = (i /= 23);",
1624 binaryOperator(hasOperatorName("/="))));
1625 EXPECT_TRUE(
1626 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1627 EXPECT_TRUE(
1628 matches("int i = 42; int j = (i += 23);",
1629 binaryOperator(hasOperatorName("+="))));
1630 EXPECT_TRUE(
1631 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1632 EXPECT_TRUE(
1633 matches("int i = 42; int j = (i -= 23);",
1634 binaryOperator(hasOperatorName("-="))));
1635 EXPECT_TRUE(
1636 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1637 binaryOperator(hasOperatorName("->*"))));
1638 EXPECT_TRUE(
1639 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1640 binaryOperator(hasOperatorName(".*"))));
1641
1642 // Member expressions as operators are not supported in matches.
1643 EXPECT_TRUE(
1644 notMatches("struct A { void x(A *a) { a->x(this); } };",
1645 binaryOperator(hasOperatorName("->"))));
1646
1647 // Initializer assignments are not represented as operator equals.
1648 EXPECT_TRUE(
1649 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1650
1651 // Array indexing is not represented as operator.
1652 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1653
1654 // Overloaded operators do not match at all.
1655 EXPECT_TRUE(notMatches(
1656 "struct A { bool operator&&(const A &a) const { return false; } };"
1657 "void x() { A a, b; a && b; }",
1658 binaryOperator()));
1659}
1660
1661TEST(MatchUnaryOperator, HasOperatorName) {
1662 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1663
1664 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1665 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1666}
1667
1668TEST(MatchUnaryOperator, HasUnaryOperand) {
1669 StatementMatcher OperatorOnFalse =
1670 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1671
1672 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1673 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1674}
1675
1676TEST(Matcher, UnaryOperatorTypes) {
1677 // Integration test that verifies the AST provides all unary operators in
1678 // a way we expect.
1679 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1680 EXPECT_TRUE(
1681 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1682 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1683 EXPECT_TRUE(
1684 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1685 EXPECT_TRUE(
1686 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1687 EXPECT_TRUE(
1688 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1689 EXPECT_TRUE(
1690 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1691 EXPECT_TRUE(
1692 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1693 EXPECT_TRUE(
1694 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1695 EXPECT_TRUE(
1696 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1697
1698 // We don't match conversion operators.
1699 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1700
1701 // Function calls are not represented as operator.
1702 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1703
1704 // Overloaded operators do not match at all.
1705 // FIXME: We probably want to add that.
1706 EXPECT_TRUE(notMatches(
1707 "struct A { bool operator!() const { return false; } };"
1708 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1709}
1710
1711TEST(Matcher, ConditionalOperator) {
1712 StatementMatcher Conditional = conditionalOperator(
1713 hasCondition(boolLiteral(equals(true))),
1714 hasTrueExpression(boolLiteral(equals(false))));
1715
1716 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1717 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1718 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1719
1720 StatementMatcher ConditionalFalse = conditionalOperator(
1721 hasFalseExpression(boolLiteral(equals(false))));
1722
1723 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1724 EXPECT_TRUE(
1725 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1726}
1727
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001728TEST(ArraySubscriptMatchers, ArraySubscripts) {
1729 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1730 arraySubscriptExpr()));
1731 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1732 arraySubscriptExpr()));
1733}
1734
1735TEST(ArraySubscriptMatchers, ArrayIndex) {
1736 EXPECT_TRUE(matches(
1737 "int i[2]; void f() { i[1] = 1; }",
1738 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1739 EXPECT_TRUE(matches(
1740 "int i[2]; void f() { 1[i] = 1; }",
1741 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1742 EXPECT_TRUE(notMatches(
1743 "int i[2]; void f() { i[1] = 1; }",
1744 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1745}
1746
1747TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1748 EXPECT_TRUE(matches(
1749 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001750 arraySubscriptExpr(hasBase(implicitCastExpr(
1751 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001752}
1753
Manuel Klimek4da21662012-07-06 05:48:52 +00001754TEST(Matcher, HasNameSupportsNamespaces) {
1755 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001756 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001757 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001758 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001759 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001760 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001761 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001762 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001763 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001764 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001765 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001766 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001767 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001768 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001769 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001770 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001771 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001772 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001773 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001774 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001775 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001776 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001777 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001778 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001779}
1780
1781TEST(Matcher, HasNameSupportsOuterClasses) {
1782 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001783 matches("class A { class B { class C; }; };",
1784 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001785 EXPECT_TRUE(
1786 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001787 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001788 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001789 matches("class A { class B { class C; }; };",
1790 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001791 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001792 matches("class A { class B { class C; }; };",
1793 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001794 EXPECT_TRUE(
1795 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001796 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001797 EXPECT_TRUE(
1798 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001799 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001800 EXPECT_TRUE(
1801 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001802 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001803 EXPECT_TRUE(
1804 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001805 recordDecl(hasName("::C"))));
1806 EXPECT_TRUE(
1807 notMatches("class A { class B { class C; }; };",
1808 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001809 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001810 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001811 EXPECT_TRUE(
1812 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001813 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001814}
1815
1816TEST(Matcher, IsDefinition) {
1817 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001818 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001819 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1820 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1821
1822 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001823 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001824 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1825 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1826
1827 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001828 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001829 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1830 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1831}
1832
1833TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001834 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00001835 ofClass(hasName("X")))));
1836
1837 EXPECT_TRUE(
1838 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
1839 EXPECT_TRUE(
1840 matches("class X { public: X(); }; void x(int) { X x = X(); }",
1841 Constructor));
1842 EXPECT_TRUE(
1843 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
1844 Constructor));
1845}
1846
1847TEST(Matcher, VisitsTemplateInstantiations) {
1848 EXPECT_TRUE(matches(
1849 "class A { public: void x(); };"
1850 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001851 "void f() { B<A> b; b.y(); }",
1852 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001853
1854 EXPECT_TRUE(matches(
1855 "class A { public: void x(); };"
1856 "class C {"
1857 " public:"
1858 " template <typename T> class B { public: void y() { T t; t.x(); } };"
1859 "};"
1860 "void f() {"
1861 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001862 "}",
1863 recordDecl(hasName("C"),
1864 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001865}
1866
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001867TEST(Matcher, HandlesNullQualTypes) {
1868 // FIXME: Add a Type matcher so we can replace uses of this
1869 // variable with Type(True())
1870 const TypeMatcher AnyType = anything();
1871
1872 // We don't really care whether this matcher succeeds; we're testing that
1873 // it completes without crashing.
1874 EXPECT_TRUE(matches(
1875 "struct A { };"
1876 "template <typename T>"
1877 "void f(T t) {"
1878 " T local_t(t /* this becomes a null QualType in the AST */);"
1879 "}"
1880 "void g() {"
1881 " f(0);"
1882 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001883 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001884 anyOf(
1885 TypeMatcher(hasDeclaration(anything())),
1886 pointsTo(AnyType),
1887 references(AnyType)
1888 // Other QualType matchers should go here.
1889 ))))));
1890}
1891
Manuel Klimek4da21662012-07-06 05:48:52 +00001892// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001893AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00001894 // Make sure all special variables are used: node, match_finder,
1895 // bound_nodes_builder, and the parameter named 'AMatcher'.
1896 return AMatcher.matches(Node, Finder, Builder);
1897}
1898
1899TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001900 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001901
1902 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001903 HasClassB, new VerifyIdIsBoundToDecl<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001904
1905 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001906 HasClassB, new VerifyIdIsBoundToDecl<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001907
1908 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001909 HasClassB, new VerifyIdIsBoundToDecl<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001910}
1911
1912AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001913 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
1914 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
1915 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00001916 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00001917 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00001918 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00001919 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
1920 ASTMatchFinder::BK_First);
1921}
1922
1923TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001924 DeclarationMatcher HasClassB =
1925 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001926
1927 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001928 HasClassB, new VerifyIdIsBoundToDecl<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001929
1930 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001931 HasClassB, new VerifyIdIsBoundToDecl<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001932
1933 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001934 HasClassB, new VerifyIdIsBoundToDecl<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001935
1936 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001937 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001938
1939 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
1940}
1941
1942TEST(For, FindsForLoops) {
1943 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
1944 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
1945}
1946
Daniel Jasper6a124492012-07-12 08:50:38 +00001947TEST(For, ForLoopInternals) {
1948 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
1949 forStmt(hasCondition(anything()))));
1950 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
1951 forStmt(hasLoopInit(anything()))));
1952}
1953
1954TEST(For, NegativeForLoopInternals) {
1955 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001956 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001957 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
1958 forStmt(hasLoopInit(anything()))));
1959}
1960
Manuel Klimek4da21662012-07-06 05:48:52 +00001961TEST(For, ReportsNoFalsePositives) {
1962 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
1963 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
1964}
1965
1966TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001967 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
1968 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
1969 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001970}
1971
1972TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
1973 // It's not a compound statement just because there's "{}" in the source
1974 // text. This is an AST search, not grep.
1975 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001976 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001977 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001978 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001979}
1980
Daniel Jasper6a124492012-07-12 08:50:38 +00001981TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00001982 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001983 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001984 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001985 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001986 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001987 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001988 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001989 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001990}
1991
1992TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
1993 // The simplest case: every compound statement is in a function
1994 // definition, and the function body itself must be a compound
1995 // statement.
1996 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001997 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001998}
1999
2000TEST(HasAnySubstatement, IsNotRecursive) {
2001 // It's really "has any immediate substatement".
2002 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002003 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002004}
2005
2006TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2007 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002008 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002009}
2010
2011TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2012 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002013 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002014}
2015
2016TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2017 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002018 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002019 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002020 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002021}
2022
2023TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2024 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002025 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002026 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002027 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002028 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002029 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002030}
2031
2032TEST(StatementCountIs, WorksWithMultipleStatements) {
2033 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002034 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002035}
2036
2037TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2038 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002039 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002040 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002041 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002042 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002043 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002044 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002045 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002046}
2047
2048TEST(Member, WorksInSimplestCase) {
2049 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002050 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002051}
2052
2053TEST(Member, DoesNotMatchTheBaseExpression) {
2054 // Don't pick out the wrong part of the member expression, this should
2055 // be checking the member (name) only.
2056 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002057 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002058}
2059
2060TEST(Member, MatchesInMemberFunctionCall) {
2061 EXPECT_TRUE(matches("void f() {"
2062 " struct { void first() {}; } s;"
2063 " s.first();"
2064 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002065 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002066}
2067
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002068TEST(Member, MatchesMemberAllocationFunction) {
Dmitri Gribenko02ed37f2012-08-18 00:41:04 +00002069 EXPECT_TRUE(matches("namespace std { typedef typeof(sizeof(int)) size_t; }"
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002070 "class X { void *operator new(std::size_t); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002071 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002072
2073 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002074 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002075
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002076 EXPECT_TRUE(matches(
2077 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2078 "class X { void operator delete[](void*, std::size_t); };",
2079 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002080}
2081
Manuel Klimek4da21662012-07-06 05:48:52 +00002082TEST(HasObjectExpression, DoesNotMatchMember) {
2083 EXPECT_TRUE(notMatches(
2084 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002085 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002086}
2087
2088TEST(HasObjectExpression, MatchesBaseOfVariable) {
2089 EXPECT_TRUE(matches(
2090 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002091 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002092 EXPECT_TRUE(matches(
2093 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002094 memberExpr(hasObjectExpression(
2095 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002096}
2097
2098TEST(HasObjectExpression,
2099 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2100 EXPECT_TRUE(matches(
2101 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002102 memberExpr(hasObjectExpression(
2103 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002104 EXPECT_TRUE(matches(
2105 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002106 memberExpr(hasObjectExpression(
2107 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002108}
2109
2110TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002111 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2112 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2113 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2114 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002115}
2116
2117TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002118 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002119}
2120
2121TEST(IsConstQualified, MatchesConstInt) {
2122 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002123 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002124}
2125
2126TEST(IsConstQualified, MatchesConstPointer) {
2127 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002128 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002129}
2130
2131TEST(IsConstQualified, MatchesThroughTypedef) {
2132 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002133 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002134 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002135 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002136}
2137
2138TEST(IsConstQualified, DoesNotMatchInappropriately) {
2139 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002140 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002141 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002142 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002143}
2144
Sam Panzer089e5b32012-08-16 16:58:10 +00002145TEST(CastExpression, MatchesExplicitCasts) {
2146 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002147 expr(castExpr())));
2148 EXPECT_TRUE(matches("void *p = (void *)(&p);", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002149 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002150 expr(castExpr())));
2151 EXPECT_TRUE(matches("char c = char(0);", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002152}
2153TEST(CastExpression, MatchesImplicitCasts) {
2154 // This test creates an implicit cast from int to char.
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002155 EXPECT_TRUE(matches("char c = 0;", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002156 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002157 EXPECT_TRUE(matches("char c = 0, d = c;", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002158}
2159
2160TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002161 EXPECT_TRUE(notMatches("char c = '0';", expr(castExpr())));
2162 EXPECT_TRUE(notMatches("char c, &q = c;", expr(castExpr())));
2163 EXPECT_TRUE(notMatches("int i = (0);", expr(castExpr())));
2164 EXPECT_TRUE(notMatches("int i = 0;", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002165}
2166
Manuel Klimek4da21662012-07-06 05:48:52 +00002167TEST(ReinterpretCast, MatchesSimpleCase) {
2168 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002169 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002170}
2171
2172TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
2173 EXPECT_TRUE(notMatches("char* p = (char*)(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002174 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002175 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002176 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002177 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002178 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002179 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2180 "B b;"
2181 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002182 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002183}
2184
2185TEST(FunctionalCast, MatchesSimpleCase) {
2186 std::string foo_class = "class Foo { public: Foo(char*); };";
2187 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002188 expr(functionalCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002189}
2190
2191TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2192 std::string FooClass = "class Foo { public: Foo(char*); };";
2193 EXPECT_TRUE(
2194 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002195 expr(functionalCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002196 EXPECT_TRUE(
2197 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002198 expr(functionalCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002199}
2200
2201TEST(DynamicCast, MatchesSimpleCase) {
2202 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2203 "B b;"
2204 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002205 expr(dynamicCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002206}
2207
2208TEST(StaticCast, MatchesSimpleCase) {
2209 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002210 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002211}
2212
2213TEST(StaticCast, DoesNotMatchOtherCasts) {
2214 EXPECT_TRUE(notMatches("char* p = (char*)(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002215 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002216 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002217 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002218 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002219 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002220 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2221 "B b;"
2222 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002223 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002224}
2225
2226TEST(HasDestinationType, MatchesSimpleCase) {
2227 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002228 expr(staticCastExpr(hasDestinationType(
2229 pointsTo(TypeMatcher(anything())))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002230}
2231
Sam Panzer089e5b32012-08-16 16:58:10 +00002232TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2233 // This test creates an implicit const cast.
2234 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002235 expr(implicitCastExpr(
Sam Panzer089e5b32012-08-16 16:58:10 +00002236 hasImplicitDestinationType(isInteger())))));
2237 // This test creates an implicit array-to-pointer cast.
2238 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002239 expr(implicitCastExpr(hasImplicitDestinationType(
Sam Panzer089e5b32012-08-16 16:58:10 +00002240 pointsTo(TypeMatcher(anything())))))));
2241}
2242
2243TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2244 // This test creates an implicit cast from int to char.
2245 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002246 expr(implicitCastExpr(hasImplicitDestinationType(
Sam Panzer089e5b32012-08-16 16:58:10 +00002247 unless(anything()))))));
2248 // This test creates an implicit array-to-pointer cast.
2249 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002250 expr(implicitCastExpr(hasImplicitDestinationType(
Sam Panzer089e5b32012-08-16 16:58:10 +00002251 unless(anything()))))));
2252}
2253
2254TEST(ImplicitCast, MatchesSimpleCase) {
2255 // This test creates an implicit const cast.
2256 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002257 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002258 // This test creates an implicit cast from int to char.
2259 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002260 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002261 // This test creates an implicit array-to-pointer cast.
2262 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002263 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002264}
2265
2266TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002267 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002268 // are present, and that it ignores explicit and paren casts.
2269
2270 // These two test cases have no casts.
2271 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002272 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002273 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002274 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002275
2276 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002277 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002278 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002279 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002280
2281 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002282 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002283}
2284
2285TEST(IgnoringImpCasts, MatchesImpCasts) {
2286 // This test checks that ignoringImpCasts matches when implicit casts are
2287 // present and its inner matcher alone does not match.
2288 // Note that this test creates an implicit const cast.
2289 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002290 varDecl(hasInitializer(ignoringImpCasts(
2291 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002292 // This test creates an implict cast from int to char.
2293 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002294 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002295 integerLiteral(equals(0)))))));
2296}
2297
2298TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2299 // These tests verify that ignoringImpCasts does not match if the inner
2300 // matcher does not match.
2301 // Note that the first test creates an implicit const cast.
2302 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002303 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002304 unless(anything()))))));
2305 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002306 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002307 unless(anything()))))));
2308
2309 // These tests verify that ignoringImplictCasts does not look through explicit
2310 // casts or parentheses.
2311 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002312 varDecl(hasInitializer(ignoringImpCasts(
2313 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002314 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002315 varDecl(hasInitializer(ignoringImpCasts(
2316 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002317 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002318 varDecl(hasInitializer(ignoringImpCasts(
2319 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002320 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002321 varDecl(hasInitializer(ignoringImpCasts(
2322 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002323}
2324
2325TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2326 // This test verifies that expressions that do not have implicit casts
2327 // still match the inner matcher.
2328 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002329 varDecl(hasInitializer(ignoringImpCasts(
2330 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002331}
2332
2333TEST(IgnoringParenCasts, MatchesParenCasts) {
2334 // This test checks that ignoringParenCasts matches when parentheses and/or
2335 // casts are present and its inner matcher alone does not match.
2336 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002337 varDecl(hasInitializer(ignoringParenCasts(
2338 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002339 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002340 varDecl(hasInitializer(ignoringParenCasts(
2341 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002342
2343 // This test creates an implict cast from int to char in addition to the
2344 // parentheses.
2345 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002346 varDecl(hasInitializer(ignoringParenCasts(
2347 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002348
2349 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002350 varDecl(hasInitializer(ignoringParenCasts(
2351 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002352 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002353 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002354 integerLiteral(equals(0)))))));
2355}
2356
2357TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2358 // This test verifies that expressions that do not have any casts still match.
2359 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002360 varDecl(hasInitializer(ignoringParenCasts(
2361 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002362}
2363
2364TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2365 // These tests verify that ignoringImpCasts does not match if the inner
2366 // matcher does not match.
2367 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002368 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002369 unless(anything()))))));
2370
2371 // This test creates an implicit cast from int to char in addition to the
2372 // parentheses.
2373 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002374 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002375 unless(anything()))))));
2376
2377 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002378 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002379 unless(anything()))))));
2380}
2381
2382TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2383 // This test checks that ignoringParenAndImpCasts matches when
2384 // parentheses and/or implicit casts are present and its inner matcher alone
2385 // does not match.
2386 // Note that this test creates an implicit const cast.
2387 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002388 varDecl(hasInitializer(ignoringParenImpCasts(
2389 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002390 // This test creates an implicit cast from int to char.
2391 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002392 varDecl(hasInitializer(ignoringParenImpCasts(
2393 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002394}
2395
2396TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2397 // This test verifies that expressions that do not have parentheses or
2398 // implicit casts still match.
2399 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002400 varDecl(hasInitializer(ignoringParenImpCasts(
2401 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002402 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002403 varDecl(hasInitializer(ignoringParenImpCasts(
2404 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002405}
2406
2407TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2408 // These tests verify that ignoringParenImpCasts does not match if
2409 // the inner matcher does not match.
2410 // This test creates an implicit cast.
2411 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002412 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002413 unless(anything()))))));
2414 // These tests verify that ignoringParenAndImplictCasts does not look
2415 // through explicit casts.
2416 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002417 varDecl(hasInitializer(ignoringParenImpCasts(
2418 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002419 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002420 varDecl(hasInitializer(ignoringParenImpCasts(
2421 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002422 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002423 varDecl(hasInitializer(ignoringParenImpCasts(
2424 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002425}
2426
Manuel Klimek715c9562012-07-25 10:02:02 +00002427TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002428 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2429 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002430 expr(implicitCastExpr(
2431 hasSourceExpression(constructExpr())))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002432}
2433
Manuel Klimek715c9562012-07-25 10:02:02 +00002434TEST(HasSourceExpression, MatchesExplicitCasts) {
2435 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002436 expr(explicitCastExpr(
2437 hasSourceExpression(hasDescendant(
2438 expr(integerLiteral())))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002439}
2440
Manuel Klimek4da21662012-07-06 05:48:52 +00002441TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002442 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002443}
2444
2445TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002446 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002447}
2448
2449TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002450 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002451}
2452
2453TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002454 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002455}
2456
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002457TEST(InitListExpression, MatchesInitListExpression) {
2458 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2459 initListExpr(hasType(asString("int [2]")))));
2460 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002461 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002462}
2463
2464TEST(UsingDeclaration, MatchesUsingDeclarations) {
2465 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2466 usingDecl()));
2467}
2468
2469TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2470 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2471 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2472}
2473
2474TEST(UsingDeclaration, MatchesSpecificTarget) {
2475 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2476 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002477 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002478 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2479 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002480 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002481}
2482
2483TEST(UsingDeclaration, ThroughUsingDeclaration) {
2484 EXPECT_TRUE(matches(
2485 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002486 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002487 EXPECT_TRUE(notMatches(
2488 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002489 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002490}
2491
Sam Panzer425f41b2012-08-16 17:20:59 +00002492TEST(SingleDecl, IsSingleDecl) {
2493 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002494 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002495 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2496 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2497 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2498 SingleDeclStmt));
2499}
2500
2501TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002502 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002503
2504 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002505 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002506 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002507 declStmt(containsDeclaration(0, MatchesInit),
2508 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002509 unsigned WrongIndex = 42;
2510 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002511 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002512 MatchesInit))));
2513}
2514
2515TEST(DeclCount, DeclCountIsCorrect) {
2516 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002517 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002518 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002519 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002520 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002521 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002522}
2523
Manuel Klimek4da21662012-07-06 05:48:52 +00002524TEST(While, MatchesWhileLoops) {
2525 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2526 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2527 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2528}
2529
2530TEST(Do, MatchesDoLoops) {
2531 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2532 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2533}
2534
2535TEST(Do, DoesNotMatchWhileLoops) {
2536 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2537}
2538
2539TEST(SwitchCase, MatchesCase) {
2540 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2541 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2542 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2543 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2544}
2545
2546TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2547 EXPECT_TRUE(notMatches(
2548 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002549 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002550 EXPECT_TRUE(notMatches(
2551 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002552 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002553}
2554
2555TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2556 EXPECT_TRUE(matches(
2557 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002558 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002559}
2560
2561TEST(ForEach, BindsOneNode) {
2562 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002563 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002564 new VerifyIdIsBoundToDecl<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002565}
2566
2567TEST(ForEach, BindsMultipleNodes) {
2568 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002569 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002570 new VerifyIdIsBoundToDecl<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002571}
2572
2573TEST(ForEach, BindsRecursiveCombinations) {
2574 EXPECT_TRUE(matchAndVerifyResultTrue(
2575 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002576 recordDecl(hasName("C"),
2577 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002578 new VerifyIdIsBoundToDecl<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002579}
2580
2581TEST(ForEachDescendant, BindsOneNode) {
2582 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002583 recordDecl(hasName("C"),
2584 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002585 new VerifyIdIsBoundToDecl<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002586}
2587
2588TEST(ForEachDescendant, BindsMultipleNodes) {
2589 EXPECT_TRUE(matchAndVerifyResultTrue(
2590 "class C { class D { int x; int y; }; "
2591 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002592 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002593 new VerifyIdIsBoundToDecl<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002594}
2595
2596TEST(ForEachDescendant, BindsRecursiveCombinations) {
2597 EXPECT_TRUE(matchAndVerifyResultTrue(
2598 "class C { class D { "
2599 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002600 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2601 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002602 new VerifyIdIsBoundToDecl<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002603}
2604
2605
2606TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2607 // Make sure that we can both match the class by name (::X) and by the type
2608 // the template was instantiated with (via a field).
2609
2610 EXPECT_TRUE(matches(
2611 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002612 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002613
2614 EXPECT_TRUE(matches(
2615 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002616 recordDecl(isTemplateInstantiation(), hasDescendant(
2617 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002618}
2619
2620TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2621 EXPECT_TRUE(matches(
2622 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002623 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002624 isTemplateInstantiation())));
2625}
2626
2627TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2628 EXPECT_TRUE(matches(
2629 "template <typename T> class X { T t; }; class A {};"
2630 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002631 recordDecl(isTemplateInstantiation(), hasDescendant(
2632 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002633}
2634
2635TEST(IsTemplateInstantiation,
2636 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2637 EXPECT_TRUE(matches(
2638 "template <typename T> class X {};"
2639 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002640 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002641}
2642
2643TEST(IsTemplateInstantiation,
2644 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2645 EXPECT_TRUE(matches(
2646 "class A {};"
2647 "class X {"
2648 " template <typename U> class Y { U u; };"
2649 " Y<A> y;"
2650 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002651 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002652}
2653
2654TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2655 // FIXME: Figure out whether this makes sense. It doesn't affect the
2656 // normal use case as long as the uppermost instantiation always is marked
2657 // as template instantiation, but it might be confusing as a predicate.
2658 EXPECT_TRUE(matches(
2659 "class A {};"
2660 "template <typename T> class X {"
2661 " template <typename U> class Y { U u; };"
2662 " Y<T> y;"
2663 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002664 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002665}
2666
2667TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2668 EXPECT_TRUE(notMatches(
2669 "template <typename T> class X {}; class A {};"
2670 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002671 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002672}
2673
2674TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2675 EXPECT_TRUE(notMatches(
2676 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002677 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002678}
2679
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002680TEST(IsExplicitTemplateSpecialization,
2681 DoesNotMatchPrimaryTemplate) {
2682 EXPECT_TRUE(notMatches(
2683 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002684 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002685 EXPECT_TRUE(notMatches(
2686 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002687 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002688}
2689
2690TEST(IsExplicitTemplateSpecialization,
2691 DoesNotMatchExplicitTemplateInstantiations) {
2692 EXPECT_TRUE(notMatches(
2693 "template <typename T> class X {};"
2694 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002695 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002696 EXPECT_TRUE(notMatches(
2697 "template <typename T> void f(T t) {}"
2698 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002699 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002700}
2701
2702TEST(IsExplicitTemplateSpecialization,
2703 DoesNotMatchImplicitTemplateInstantiations) {
2704 EXPECT_TRUE(notMatches(
2705 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002706 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002707 EXPECT_TRUE(notMatches(
2708 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002709 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002710}
2711
2712TEST(IsExplicitTemplateSpecialization,
2713 MatchesExplicitTemplateSpecializations) {
2714 EXPECT_TRUE(matches(
2715 "template <typename T> class X {};"
2716 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002717 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002718 EXPECT_TRUE(matches(
2719 "template <typename T> void f(T t) {}"
2720 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002721 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002722}
2723
Manuel Klimek4da21662012-07-06 05:48:52 +00002724} // end namespace ast_matchers
2725} // end namespace clang