blob: 951bca9a301592021ffd470e6f75dfe2ea5e055a [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
688TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000689 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000690 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000691 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000692 EXPECT_TRUE(
693 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000694 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000695 EXPECT_TRUE(
696 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000697 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000698}
699
700TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
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; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000704 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000705 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000706 EXPECT_TRUE(
707 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000708 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000709}
710
711TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000712 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000713 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000714 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000715 EXPECT_TRUE(
716 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000717 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000718}
719
720TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000721 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000722 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000723 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000724 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000725 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000726}
727
728TEST(Matcher, Call) {
729 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000730 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000731 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000732
733 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
734 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
735
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000736 StatementMatcher MethodOnY =
737 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000738
739 EXPECT_TRUE(
740 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
741 MethodOnY));
742 EXPECT_TRUE(
743 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
744 MethodOnY));
745 EXPECT_TRUE(
746 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
747 MethodOnY));
748 EXPECT_TRUE(
749 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
750 MethodOnY));
751 EXPECT_TRUE(
752 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
753 MethodOnY));
754
755 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000756 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000757
758 EXPECT_TRUE(
759 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
760 MethodOnYPointer));
761 EXPECT_TRUE(
762 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
763 MethodOnYPointer));
764 EXPECT_TRUE(
765 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
766 MethodOnYPointer));
767 EXPECT_TRUE(
768 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
769 MethodOnYPointer));
770 EXPECT_TRUE(
771 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
772 MethodOnYPointer));
773}
774
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000775TEST(HasType, MatchesAsString) {
776 EXPECT_TRUE(
777 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000778 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000779 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000780 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000781 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000782 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000783 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000784 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000785}
786
Manuel Klimek4da21662012-07-06 05:48:52 +0000787TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000788 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000789 // Unary operator
790 EXPECT_TRUE(matches("class Y { }; "
791 "bool operator!(Y x) { return false; }; "
792 "Y y; bool c = !y;", OpCall));
793 // No match -- special operators like "new", "delete"
794 // FIXME: operator new takes size_t, for which we need stddef.h, for which
795 // we need to figure out include paths in the test.
796 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
797 // "class Y { }; "
798 // "void *operator new(size_t size) { return 0; } "
799 // "Y *y = new Y;", OpCall));
800 EXPECT_TRUE(notMatches("class Y { }; "
801 "void operator delete(void *p) { } "
802 "void a() {Y *y = new Y; delete y;}", OpCall));
803 // Binary operator
804 EXPECT_TRUE(matches("class Y { }; "
805 "bool operator&&(Y x, Y y) { return true; }; "
806 "Y a; Y b; bool c = a && b;",
807 OpCall));
808 // No match -- normal operator, not an overloaded one.
809 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
810 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
811}
812
813TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
814 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000815 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000816 EXPECT_TRUE(matches("class Y { }; "
817 "bool operator&&(Y x, Y y) { return true; }; "
818 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
819 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000820 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000821 EXPECT_TRUE(notMatches("class Y { }; "
822 "bool operator&&(Y x, Y y) { return true; }; "
823 "Y a; Y b; bool c = a && b;",
824 OpCallLessLess));
825}
826
827TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +0000828 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000829 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000830
831 EXPECT_TRUE(
832 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
833 MethodOnY));
834 EXPECT_TRUE(
835 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
836 MethodOnY));
837 EXPECT_TRUE(
838 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
839 MethodOnY));
840 EXPECT_TRUE(
841 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
842 MethodOnY));
843 EXPECT_TRUE(
844 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
845 MethodOnY));
846
847 EXPECT_TRUE(matches(
848 "class Y {"
849 " public: virtual void x();"
850 "};"
851 "class X : public Y {"
852 " public: virtual void x();"
853 "};"
854 "void z() { X *x; x->Y::x(); }", MethodOnY));
855}
856
857TEST(Matcher, VariableUsage) {
858 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000859 declRefExpr(to(
860 varDecl(hasInitializer(
861 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000862
863 EXPECT_TRUE(matches(
864 "class Y {"
865 " public:"
866 " bool x() const;"
867 "};"
868 "void z(const Y &y) {"
869 " bool b = y.x();"
870 " if (b) {}"
871 "}", Reference));
872
873 EXPECT_TRUE(notMatches(
874 "class Y {"
875 " public:"
876 " bool x() const;"
877 "};"
878 "void z(const Y &y) {"
879 " bool b = y.x();"
880 "}", Reference));
881}
882
Daniel Jasper9bd28092012-07-30 05:03:25 +0000883TEST(Matcher, FindsVarDeclInFuncitonParameter) {
884 EXPECT_TRUE(matches(
885 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000886 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +0000887}
888
Manuel Klimek4da21662012-07-06 05:48:52 +0000889TEST(Matcher, CalledVariable) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000890 StatementMatcher CallOnVariableY = expr(
891 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000892
893 EXPECT_TRUE(matches(
894 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
895 EXPECT_TRUE(matches(
896 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
897 EXPECT_TRUE(matches(
898 "class Y { public: void x(); };"
899 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
900 EXPECT_TRUE(matches(
901 "class Y { public: void x(); };"
902 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
903 EXPECT_TRUE(notMatches(
904 "class Y { public: void x(); };"
905 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
906 CallOnVariableY));
907}
908
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000909TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
910 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
911 unaryExprOrTypeTraitExpr()));
912 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
913 alignOfExpr(anything())));
914 // FIXME: Uncomment once alignof is enabled.
915 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
916 // unaryExprOrTypeTraitExpr()));
917 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
918 // sizeOfExpr()));
919}
920
921TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
922 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
923 hasArgumentOfType(asString("int")))));
924 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
925 hasArgumentOfType(asString("float")))));
926 EXPECT_TRUE(matches(
927 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000928 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000929 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000930 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000931}
932
Manuel Klimek4da21662012-07-06 05:48:52 +0000933TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000934 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000935}
936
937TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000938 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000939}
940
941TEST(MemberExpression, MatchesVariable) {
942 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000943 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000944 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000945 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000946 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000947 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000948}
949
950TEST(MemberExpression, MatchesStaticVariable) {
951 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000952 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000953 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000954 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000955 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000956 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000957}
958
Daniel Jasper6a124492012-07-12 08:50:38 +0000959TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000960 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
961 EXPECT_TRUE(matches(
962 "long long i = 0; void f(long long) { }; void g() {f(i);}",
963 callExpr(hasArgument(0, declRefExpr(
964 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000965}
966
967TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000968 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000969 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000970 callExpr(hasArgument(0, declRefExpr(
971 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000972}
973
Manuel Klimek4da21662012-07-06 05:48:52 +0000974TEST(IsArrow, MatchesMemberVariablesViaArrow) {
975 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000976 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +0000977 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000978 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +0000979 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000980 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +0000981}
982
983TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
984 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000985 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +0000986 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000987 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +0000988 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000989 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +0000990}
991
992TEST(IsArrow, MatchesMemberCallsViaArrow) {
993 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000994 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +0000995 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000996 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +0000997 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000998 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +0000999}
1000
1001TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001002 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001003
1004 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1005 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1006}
1007
1008TEST(Callee, MatchesMemberExpressions) {
1009 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001010 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001011 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001012 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001013}
1014
1015TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001016 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001017
1018 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1019 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1020
Manuel Klimeke265c872012-07-10 14:21:30 +00001021#if !defined(_MSC_VER)
1022 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001023 // Dependent contexts, but a non-dependent call.
1024 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1025 CallFunctionF));
1026 EXPECT_TRUE(
1027 matches("void f(); template <int N> struct S { void g() { f(); } };",
1028 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001029#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001030
1031 // Depedent calls don't match.
1032 EXPECT_TRUE(
1033 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1034 CallFunctionF));
1035 EXPECT_TRUE(
1036 notMatches("void f(int);"
1037 "template <typename T> struct S { void g(T t) { f(t); } };",
1038 CallFunctionF));
1039}
1040
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001041TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1042 EXPECT_TRUE(
1043 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001044 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001045}
1046
1047TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1048 EXPECT_TRUE(
1049 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001050 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001051}
1052
1053TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1054 EXPECT_TRUE(
1055 notMatches("void g(); template <typename T> void f(T t) {}"
1056 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001057 functionTemplateDecl(hasName("f"),
1058 hasDescendant(declRefExpr(to(
1059 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001060}
1061
Manuel Klimek4da21662012-07-06 05:48:52 +00001062TEST(Matcher, Argument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001063 StatementMatcher CallArgumentY = expr(callExpr(
1064 hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001065
1066 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1067 EXPECT_TRUE(
1068 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1069 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1070
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001071 StatementMatcher WrongIndex = expr(callExpr(
1072 hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001073 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1074}
1075
1076TEST(Matcher, AnyArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001077 StatementMatcher CallArgumentY = expr(callExpr(
1078 hasAnyArgument(declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001079 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1080 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1081 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1082}
1083
1084TEST(Matcher, ArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001085 StatementMatcher Call1Arg = expr(callExpr(argumentCountIs(1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00001086
1087 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1088 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1089 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1090}
1091
1092TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001093 DeclarationMatcher ReferenceClassX = varDecl(
1094 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001095 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1096 ReferenceClassX));
1097 EXPECT_TRUE(
1098 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1099 EXPECT_TRUE(
1100 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1101 EXPECT_TRUE(
1102 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1103}
1104
1105TEST(HasParameter, CallsInnerMatcher) {
1106 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001107 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001108 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001109 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001110}
1111
1112TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1113 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001114 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001115}
1116
1117TEST(HasType, MatchesParameterVariableTypesStrictly) {
1118 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001119 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001120 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001121 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001122 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001123 methodDecl(hasParameter(0,
1124 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001125 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001126 methodDecl(hasParameter(0,
1127 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001128}
1129
1130TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1131 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001132 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001133 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001134 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001135}
1136
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001137TEST(Returns, MatchesReturnTypes) {
1138 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001139 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001140 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001141 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001142 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001143 functionDecl(returns(hasDeclaration(
1144 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001145}
1146
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001147TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001148 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1149 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1150 functionDecl(isExternC())));
1151 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001152}
1153
Manuel Klimek4da21662012-07-06 05:48:52 +00001154TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1155 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001156 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001157}
1158
1159TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1160 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001161 methodDecl(hasAnyParameter(hasType(pointsTo(
1162 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001163}
1164
1165TEST(HasName, MatchesParameterVariableDeclartions) {
1166 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001167 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001168 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001169 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001170}
1171
Daniel Jasper371f9392012-08-01 08:40:24 +00001172TEST(Matcher, MatchesClassTemplateSpecialization) {
1173 EXPECT_TRUE(matches("template<typename T> struct A {};"
1174 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001175 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001176 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001177 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001178 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001179 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001180}
1181
1182TEST(Matcher, MatchesTypeTemplateArgument) {
1183 EXPECT_TRUE(matches(
1184 "template<typename T> struct B {};"
1185 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001186 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001187 asString("int"))))));
1188}
1189
1190TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1191 EXPECT_TRUE(matches(
1192 "struct B { int next; };"
1193 "template<int(B::*next_ptr)> struct A {};"
1194 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001195 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1196 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001197}
1198
1199TEST(Matcher, MatchesSpecificArgument) {
1200 EXPECT_TRUE(matches(
1201 "template<typename T, typename U> class A {};"
1202 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001203 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001204 1, refersToType(asString("int"))))));
1205 EXPECT_TRUE(notMatches(
1206 "template<typename T, typename U> class A {};"
1207 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001208 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001209 1, refersToType(asString("int"))))));
1210}
1211
Manuel Klimek4da21662012-07-06 05:48:52 +00001212TEST(Matcher, ConstructorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001213 StatementMatcher Constructor = expr(constructExpr());
Manuel Klimek4da21662012-07-06 05:48:52 +00001214
1215 EXPECT_TRUE(
1216 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1217 EXPECT_TRUE(
1218 matches("class X { public: X(); }; void x() { X x = X(); }",
1219 Constructor));
1220 EXPECT_TRUE(
1221 matches("class X { public: X(int); }; void x() { X x = 0; }",
1222 Constructor));
1223 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1224}
1225
1226TEST(Matcher, ConstructorArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001227 StatementMatcher Constructor = expr(constructExpr(
1228 hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001229
1230 EXPECT_TRUE(
1231 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1232 Constructor));
1233 EXPECT_TRUE(
1234 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1235 Constructor));
1236 EXPECT_TRUE(
1237 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1238 Constructor));
1239 EXPECT_TRUE(
1240 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1241 Constructor));
1242
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001243 StatementMatcher WrongIndex = expr(constructExpr(
1244 hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001245 EXPECT_TRUE(
1246 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1247 WrongIndex));
1248}
1249
1250TEST(Matcher, ConstructorArgumentCount) {
1251 StatementMatcher Constructor1Arg =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001252 expr(constructExpr(argumentCountIs(1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00001253
1254 EXPECT_TRUE(
1255 matches("class X { public: X(int); }; void x() { X x(0); }",
1256 Constructor1Arg));
1257 EXPECT_TRUE(
1258 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1259 Constructor1Arg));
1260 EXPECT_TRUE(
1261 matches("class X { public: X(int); }; void x() { X x = 0; }",
1262 Constructor1Arg));
1263 EXPECT_TRUE(
1264 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1265 Constructor1Arg));
1266}
1267
1268TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001269 StatementMatcher TempExpression = expr(bindTemporaryExpr());
Manuel Klimek4da21662012-07-06 05:48:52 +00001270
1271 std::string ClassString = "class string { public: string(); ~string(); }; ";
1272
1273 EXPECT_TRUE(
1274 matches(ClassString +
1275 "string GetStringByValue();"
1276 "void FunctionTakesString(string s);"
1277 "void run() { FunctionTakesString(GetStringByValue()); }",
1278 TempExpression));
1279
1280 EXPECT_TRUE(
1281 notMatches(ClassString +
1282 "string* GetStringPointer(); "
1283 "void FunctionTakesStringPtr(string* s);"
1284 "void run() {"
1285 " string* s = GetStringPointer();"
1286 " FunctionTakesStringPtr(GetStringPointer());"
1287 " FunctionTakesStringPtr(s);"
1288 "}",
1289 TempExpression));
1290
1291 EXPECT_TRUE(
1292 notMatches("class no_dtor {};"
1293 "no_dtor GetObjByValue();"
1294 "void ConsumeObj(no_dtor param);"
1295 "void run() { ConsumeObj(GetObjByValue()); }",
1296 TempExpression));
1297}
1298
Sam Panzere16acd32012-08-24 22:04:44 +00001299TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1300 std::string ClassString =
1301 "class string { public: string(); int length(); }; ";
1302
1303 EXPECT_TRUE(
1304 matches(ClassString +
1305 "string GetStringByValue();"
1306 "void FunctionTakesString(string s);"
1307 "void run() { FunctionTakesString(GetStringByValue()); }",
1308 materializeTemporaryExpr()));
1309
1310 EXPECT_TRUE(
1311 notMatches(ClassString +
1312 "string* GetStringPointer(); "
1313 "void FunctionTakesStringPtr(string* s);"
1314 "void run() {"
1315 " string* s = GetStringPointer();"
1316 " FunctionTakesStringPtr(GetStringPointer());"
1317 " FunctionTakesStringPtr(s);"
1318 "}",
1319 materializeTemporaryExpr()));
1320
1321 EXPECT_TRUE(
1322 notMatches(ClassString +
1323 "string GetStringByValue();"
1324 "void run() { int k = GetStringByValue().length(); }",
1325 materializeTemporaryExpr()));
1326
1327 EXPECT_TRUE(
1328 notMatches(ClassString +
1329 "string GetStringByValue();"
1330 "void run() { GetStringByValue(); }",
1331 materializeTemporaryExpr()));
1332}
1333
Manuel Klimek4da21662012-07-06 05:48:52 +00001334TEST(ConstructorDeclaration, SimpleCase) {
1335 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001336 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001337 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001338 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001339}
1340
1341TEST(ConstructorDeclaration, IsImplicit) {
1342 // This one doesn't match because the constructor is not added by the
1343 // compiler (it is not needed).
1344 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001345 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001346 // The compiler added the implicit default constructor.
1347 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001348 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001349 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001350 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001351}
1352
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001353TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1354 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001355 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001356}
1357
1358TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001359 EXPECT_TRUE(notMatches("class Foo {};",
1360 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001361}
1362
Manuel Klimek4da21662012-07-06 05:48:52 +00001363TEST(HasAnyConstructorInitializer, SimpleCase) {
1364 EXPECT_TRUE(notMatches(
1365 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001366 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001367 EXPECT_TRUE(matches(
1368 "class Foo {"
1369 " Foo() : foo_() { }"
1370 " int foo_;"
1371 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001372 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001373}
1374
1375TEST(HasAnyConstructorInitializer, ForField) {
1376 static const char Code[] =
1377 "class Baz { };"
1378 "class Foo {"
1379 " Foo() : foo_() { }"
1380 " Baz foo_;"
1381 " Baz bar_;"
1382 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001383 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1384 forField(hasType(recordDecl(hasName("Baz"))))))));
1385 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001386 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001387 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1388 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001389}
1390
1391TEST(HasAnyConstructorInitializer, WithInitializer) {
1392 static const char Code[] =
1393 "class Foo {"
1394 " Foo() : foo_(0) { }"
1395 " int foo_;"
1396 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001397 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001398 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001399 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001400 withInitializer(integerLiteral(equals(1)))))));
1401}
1402
1403TEST(HasAnyConstructorInitializer, IsWritten) {
1404 static const char Code[] =
1405 "struct Bar { Bar(){} };"
1406 "class Foo {"
1407 " Foo() : foo_() { }"
1408 " Bar foo_;"
1409 " Bar bar_;"
1410 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001411 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001412 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001413 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001414 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001415 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001416 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1417}
1418
1419TEST(Matcher, NewExpression) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001420 StatementMatcher New = expr(newExpr());
Manuel Klimek4da21662012-07-06 05:48:52 +00001421
1422 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1423 EXPECT_TRUE(
1424 matches("class X { public: X(); }; void x() { new X(); }", New));
1425 EXPECT_TRUE(
1426 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1427 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1428}
1429
1430TEST(Matcher, NewExpressionArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001431 StatementMatcher New = expr(constructExpr(
1432 hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001433
1434 EXPECT_TRUE(
1435 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1436 New));
1437 EXPECT_TRUE(
1438 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1439 New));
1440 EXPECT_TRUE(
1441 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1442 New));
1443
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001444 StatementMatcher WrongIndex = expr(constructExpr(
1445 hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001446 EXPECT_TRUE(
1447 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1448 WrongIndex));
1449}
1450
1451TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001452 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001453
1454 EXPECT_TRUE(
1455 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1456 EXPECT_TRUE(
1457 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1458 New));
1459}
1460
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001461TEST(Matcher, DeleteExpression) {
1462 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001463 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001464}
1465
Manuel Klimek4da21662012-07-06 05:48:52 +00001466TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001467 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001468
1469 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1470 EXPECT_TRUE(
1471 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1472 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1473}
1474
1475TEST(Matcher, StringLiterals) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001476 StatementMatcher Literal = expr(stringLiteral());
Manuel Klimek4da21662012-07-06 05:48:52 +00001477 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1478 // wide string
1479 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1480 // with escaped characters
1481 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1482 // no matching -- though the data type is the same, there is no string literal
1483 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1484}
1485
1486TEST(Matcher, CharacterLiterals) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001487 StatementMatcher CharLiteral = expr(characterLiteral());
Manuel Klimek4da21662012-07-06 05:48:52 +00001488 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1489 // wide character
1490 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1491 // wide character, Hex encoded, NOT MATCHED!
1492 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1493 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1494}
1495
1496TEST(Matcher, IntegerLiterals) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001497 StatementMatcher HasIntLiteral = expr(integerLiteral());
Manuel Klimek4da21662012-07-06 05:48:52 +00001498 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1499 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1500 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1501 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1502
1503 // Non-matching cases (character literals, float and double)
1504 EXPECT_TRUE(notMatches("int i = L'a';",
1505 HasIntLiteral)); // this is actually a character
1506 // literal cast to int
1507 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1508 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1509 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1510}
1511
1512TEST(Matcher, Conditions) {
1513 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1514
1515 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1516 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1517 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1518 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1519 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1520}
1521
1522TEST(MatchBinaryOperator, HasOperatorName) {
1523 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1524
1525 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1526 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1527}
1528
1529TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1530 StatementMatcher OperatorTrueFalse =
1531 binaryOperator(hasLHS(boolLiteral(equals(true))),
1532 hasRHS(boolLiteral(equals(false))));
1533
1534 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1535 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1536 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1537}
1538
1539TEST(MatchBinaryOperator, HasEitherOperand) {
1540 StatementMatcher HasOperand =
1541 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1542
1543 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1544 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1545 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1546}
1547
1548TEST(Matcher, BinaryOperatorTypes) {
1549 // Integration test that verifies the AST provides all binary operators in
1550 // a way we expect.
1551 // FIXME: Operator ','
1552 EXPECT_TRUE(
1553 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1554 EXPECT_TRUE(
1555 matches("bool b; bool c = (b = true);",
1556 binaryOperator(hasOperatorName("="))));
1557 EXPECT_TRUE(
1558 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1559 EXPECT_TRUE(
1560 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1561 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1562 EXPECT_TRUE(
1563 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1564 EXPECT_TRUE(
1565 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1566 EXPECT_TRUE(
1567 matches("int i = 1; int j = (i <<= 2);",
1568 binaryOperator(hasOperatorName("<<="))));
1569 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1570 EXPECT_TRUE(
1571 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1572 EXPECT_TRUE(
1573 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1574 EXPECT_TRUE(
1575 matches("int i = 1; int j = (i >>= 2);",
1576 binaryOperator(hasOperatorName(">>="))));
1577 EXPECT_TRUE(
1578 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1579 EXPECT_TRUE(
1580 matches("int i = 42; int j = (i ^= 42);",
1581 binaryOperator(hasOperatorName("^="))));
1582 EXPECT_TRUE(
1583 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1584 EXPECT_TRUE(
1585 matches("int i = 42; int j = (i %= 42);",
1586 binaryOperator(hasOperatorName("%="))));
1587 EXPECT_TRUE(
1588 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1589 EXPECT_TRUE(
1590 matches("bool b = true && false;",
1591 binaryOperator(hasOperatorName("&&"))));
1592 EXPECT_TRUE(
1593 matches("bool b = true; bool c = (b &= false);",
1594 binaryOperator(hasOperatorName("&="))));
1595 EXPECT_TRUE(
1596 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1597 EXPECT_TRUE(
1598 matches("bool b = true || false;",
1599 binaryOperator(hasOperatorName("||"))));
1600 EXPECT_TRUE(
1601 matches("bool b = true; bool c = (b |= false);",
1602 binaryOperator(hasOperatorName("|="))));
1603 EXPECT_TRUE(
1604 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1605 EXPECT_TRUE(
1606 matches("int i = 42; int j = (i *= 23);",
1607 binaryOperator(hasOperatorName("*="))));
1608 EXPECT_TRUE(
1609 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1610 EXPECT_TRUE(
1611 matches("int i = 42; int j = (i /= 23);",
1612 binaryOperator(hasOperatorName("/="))));
1613 EXPECT_TRUE(
1614 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1615 EXPECT_TRUE(
1616 matches("int i = 42; int j = (i += 23);",
1617 binaryOperator(hasOperatorName("+="))));
1618 EXPECT_TRUE(
1619 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1620 EXPECT_TRUE(
1621 matches("int i = 42; int j = (i -= 23);",
1622 binaryOperator(hasOperatorName("-="))));
1623 EXPECT_TRUE(
1624 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1625 binaryOperator(hasOperatorName("->*"))));
1626 EXPECT_TRUE(
1627 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1628 binaryOperator(hasOperatorName(".*"))));
1629
1630 // Member expressions as operators are not supported in matches.
1631 EXPECT_TRUE(
1632 notMatches("struct A { void x(A *a) { a->x(this); } };",
1633 binaryOperator(hasOperatorName("->"))));
1634
1635 // Initializer assignments are not represented as operator equals.
1636 EXPECT_TRUE(
1637 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1638
1639 // Array indexing is not represented as operator.
1640 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1641
1642 // Overloaded operators do not match at all.
1643 EXPECT_TRUE(notMatches(
1644 "struct A { bool operator&&(const A &a) const { return false; } };"
1645 "void x() { A a, b; a && b; }",
1646 binaryOperator()));
1647}
1648
1649TEST(MatchUnaryOperator, HasOperatorName) {
1650 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1651
1652 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1653 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1654}
1655
1656TEST(MatchUnaryOperator, HasUnaryOperand) {
1657 StatementMatcher OperatorOnFalse =
1658 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1659
1660 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1661 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1662}
1663
1664TEST(Matcher, UnaryOperatorTypes) {
1665 // Integration test that verifies the AST provides all unary operators in
1666 // a way we expect.
1667 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1668 EXPECT_TRUE(
1669 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1670 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1671 EXPECT_TRUE(
1672 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1673 EXPECT_TRUE(
1674 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1675 EXPECT_TRUE(
1676 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1677 EXPECT_TRUE(
1678 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1679 EXPECT_TRUE(
1680 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1681 EXPECT_TRUE(
1682 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1683 EXPECT_TRUE(
1684 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1685
1686 // We don't match conversion operators.
1687 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1688
1689 // Function calls are not represented as operator.
1690 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1691
1692 // Overloaded operators do not match at all.
1693 // FIXME: We probably want to add that.
1694 EXPECT_TRUE(notMatches(
1695 "struct A { bool operator!() const { return false; } };"
1696 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1697}
1698
1699TEST(Matcher, ConditionalOperator) {
1700 StatementMatcher Conditional = conditionalOperator(
1701 hasCondition(boolLiteral(equals(true))),
1702 hasTrueExpression(boolLiteral(equals(false))));
1703
1704 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1705 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1706 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1707
1708 StatementMatcher ConditionalFalse = conditionalOperator(
1709 hasFalseExpression(boolLiteral(equals(false))));
1710
1711 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1712 EXPECT_TRUE(
1713 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1714}
1715
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001716TEST(ArraySubscriptMatchers, ArraySubscripts) {
1717 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1718 arraySubscriptExpr()));
1719 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1720 arraySubscriptExpr()));
1721}
1722
1723TEST(ArraySubscriptMatchers, ArrayIndex) {
1724 EXPECT_TRUE(matches(
1725 "int i[2]; void f() { i[1] = 1; }",
1726 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1727 EXPECT_TRUE(matches(
1728 "int i[2]; void f() { 1[i] = 1; }",
1729 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1730 EXPECT_TRUE(notMatches(
1731 "int i[2]; void f() { i[1] = 1; }",
1732 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1733}
1734
1735TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1736 EXPECT_TRUE(matches(
1737 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001738 arraySubscriptExpr(hasBase(implicitCastExpr(
1739 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001740}
1741
Manuel Klimek4da21662012-07-06 05:48:52 +00001742TEST(Matcher, HasNameSupportsNamespaces) {
1743 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001744 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001745 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001746 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001747 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001748 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001749 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001750 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001751 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001752 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001753 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001754 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001755 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001756 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001757 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001758 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001759 EXPECT_TRUE(notMatches("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(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001762 recordDecl(hasName("z::a::b::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("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001765 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001766 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001767}
1768
1769TEST(Matcher, HasNameSupportsOuterClasses) {
1770 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001771 matches("class A { class B { class C; }; };",
1772 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001773 EXPECT_TRUE(
1774 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001775 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001776 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001777 matches("class A { class B { class C; }; };",
1778 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001779 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001780 matches("class A { class B { class C; }; };",
1781 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001782 EXPECT_TRUE(
1783 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001784 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001785 EXPECT_TRUE(
1786 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001787 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001788 EXPECT_TRUE(
1789 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001790 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001791 EXPECT_TRUE(
1792 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001793 recordDecl(hasName("::C"))));
1794 EXPECT_TRUE(
1795 notMatches("class A { class B { class C; }; };",
1796 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001797 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001798 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001799 EXPECT_TRUE(
1800 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001801 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001802}
1803
1804TEST(Matcher, IsDefinition) {
1805 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001806 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001807 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1808 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1809
1810 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001811 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001812 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1813 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1814
1815 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001816 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001817 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1818 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1819}
1820
1821TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001822 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00001823 ofClass(hasName("X")))));
1824
1825 EXPECT_TRUE(
1826 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
1827 EXPECT_TRUE(
1828 matches("class X { public: X(); }; void x(int) { X x = X(); }",
1829 Constructor));
1830 EXPECT_TRUE(
1831 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
1832 Constructor));
1833}
1834
1835TEST(Matcher, VisitsTemplateInstantiations) {
1836 EXPECT_TRUE(matches(
1837 "class A { public: void x(); };"
1838 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001839 "void f() { B<A> b; b.y(); }",
1840 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001841
1842 EXPECT_TRUE(matches(
1843 "class A { public: void x(); };"
1844 "class C {"
1845 " public:"
1846 " template <typename T> class B { public: void y() { T t; t.x(); } };"
1847 "};"
1848 "void f() {"
1849 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001850 "}",
1851 recordDecl(hasName("C"),
1852 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001853}
1854
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001855TEST(Matcher, HandlesNullQualTypes) {
1856 // FIXME: Add a Type matcher so we can replace uses of this
1857 // variable with Type(True())
1858 const TypeMatcher AnyType = anything();
1859
1860 // We don't really care whether this matcher succeeds; we're testing that
1861 // it completes without crashing.
1862 EXPECT_TRUE(matches(
1863 "struct A { };"
1864 "template <typename T>"
1865 "void f(T t) {"
1866 " T local_t(t /* this becomes a null QualType in the AST */);"
1867 "}"
1868 "void g() {"
1869 " f(0);"
1870 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001871 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001872 anyOf(
1873 TypeMatcher(hasDeclaration(anything())),
1874 pointsTo(AnyType),
1875 references(AnyType)
1876 // Other QualType matchers should go here.
1877 ))))));
1878}
1879
Manuel Klimek4da21662012-07-06 05:48:52 +00001880// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001881AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00001882 // Make sure all special variables are used: node, match_finder,
1883 // bound_nodes_builder, and the parameter named 'AMatcher'.
1884 return AMatcher.matches(Node, Finder, Builder);
1885}
1886
1887TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001888 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001889
1890 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001891 HasClassB, new VerifyIdIsBoundToDecl<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001892
1893 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001894 HasClassB, new VerifyIdIsBoundToDecl<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001895
1896 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001897 HasClassB, new VerifyIdIsBoundToDecl<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001898}
1899
1900AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001901 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
1902 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
1903 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00001904 assert_node_type_is_accessible);
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001905 internal::TypedBaseMatcher<Decl> ChildMatcher(AMatcher);
Manuel Klimek4da21662012-07-06 05:48:52 +00001906 return Finder->matchesChildOf(
1907 Node, ChildMatcher, Builder,
1908 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
1909 ASTMatchFinder::BK_First);
1910}
1911
1912TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001913 DeclarationMatcher HasClassB =
1914 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001915
1916 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001917 HasClassB, new VerifyIdIsBoundToDecl<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001918
1919 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001920 HasClassB, new VerifyIdIsBoundToDecl<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001921
1922 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001923 HasClassB, new VerifyIdIsBoundToDecl<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001924
1925 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001926 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001927
1928 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
1929}
1930
1931TEST(For, FindsForLoops) {
1932 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
1933 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
1934}
1935
Daniel Jasper6a124492012-07-12 08:50:38 +00001936TEST(For, ForLoopInternals) {
1937 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
1938 forStmt(hasCondition(anything()))));
1939 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
1940 forStmt(hasLoopInit(anything()))));
1941}
1942
1943TEST(For, NegativeForLoopInternals) {
1944 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001945 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001946 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
1947 forStmt(hasLoopInit(anything()))));
1948}
1949
Manuel Klimek4da21662012-07-06 05:48:52 +00001950TEST(For, ReportsNoFalsePositives) {
1951 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
1952 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
1953}
1954
1955TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001956 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
1957 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
1958 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001959}
1960
1961TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
1962 // It's not a compound statement just because there's "{}" in the source
1963 // text. This is an AST search, not grep.
1964 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001965 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001966 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001967 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001968}
1969
Daniel Jasper6a124492012-07-12 08:50:38 +00001970TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00001971 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001972 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001973 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001974 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001975 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001976 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001977 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001978 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001979}
1980
1981TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
1982 // The simplest case: every compound statement is in a function
1983 // definition, and the function body itself must be a compound
1984 // statement.
1985 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001986 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001987}
1988
1989TEST(HasAnySubstatement, IsNotRecursive) {
1990 // It's really "has any immediate substatement".
1991 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001992 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001993}
1994
1995TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
1996 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001997 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001998}
1999
2000TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2001 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002002 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002003}
2004
2005TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2006 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002007 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002008 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002009 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002010}
2011
2012TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2013 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002014 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002015 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002016 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002017 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002018 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002019}
2020
2021TEST(StatementCountIs, WorksWithMultipleStatements) {
2022 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002023 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002024}
2025
2026TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2027 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002028 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002029 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002030 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002031 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002032 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002033 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002034 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002035}
2036
2037TEST(Member, WorksInSimplestCase) {
2038 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002039 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002040}
2041
2042TEST(Member, DoesNotMatchTheBaseExpression) {
2043 // Don't pick out the wrong part of the member expression, this should
2044 // be checking the member (name) only.
2045 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002046 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002047}
2048
2049TEST(Member, MatchesInMemberFunctionCall) {
2050 EXPECT_TRUE(matches("void f() {"
2051 " struct { void first() {}; } s;"
2052 " s.first();"
2053 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002054 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002055}
2056
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002057TEST(Member, MatchesMemberAllocationFunction) {
Dmitri Gribenko02ed37f2012-08-18 00:41:04 +00002058 EXPECT_TRUE(matches("namespace std { typedef typeof(sizeof(int)) size_t; }"
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002059 "class X { void *operator new(std::size_t); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002060 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002061
2062 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002063 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002064
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002065 EXPECT_TRUE(matches(
2066 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2067 "class X { void operator delete[](void*, std::size_t); };",
2068 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002069}
2070
Manuel Klimek4da21662012-07-06 05:48:52 +00002071TEST(HasObjectExpression, DoesNotMatchMember) {
2072 EXPECT_TRUE(notMatches(
2073 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002074 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002075}
2076
2077TEST(HasObjectExpression, MatchesBaseOfVariable) {
2078 EXPECT_TRUE(matches(
2079 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002080 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002081 EXPECT_TRUE(matches(
2082 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002083 memberExpr(hasObjectExpression(
2084 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002085}
2086
2087TEST(HasObjectExpression,
2088 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2089 EXPECT_TRUE(matches(
2090 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002091 memberExpr(hasObjectExpression(
2092 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002093 EXPECT_TRUE(matches(
2094 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002095 memberExpr(hasObjectExpression(
2096 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002097}
2098
2099TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002100 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2101 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2102 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2103 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002104}
2105
2106TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002107 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002108}
2109
2110TEST(IsConstQualified, MatchesConstInt) {
2111 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002112 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002113}
2114
2115TEST(IsConstQualified, MatchesConstPointer) {
2116 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002117 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002118}
2119
2120TEST(IsConstQualified, MatchesThroughTypedef) {
2121 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002122 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002123 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002124 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002125}
2126
2127TEST(IsConstQualified, DoesNotMatchInappropriately) {
2128 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002129 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002130 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002131 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002132}
2133
Sam Panzer089e5b32012-08-16 16:58:10 +00002134TEST(CastExpression, MatchesExplicitCasts) {
2135 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002136 expr(castExpr())));
2137 EXPECT_TRUE(matches("void *p = (void *)(&p);", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002138 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002139 expr(castExpr())));
2140 EXPECT_TRUE(matches("char c = char(0);", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002141}
2142TEST(CastExpression, MatchesImplicitCasts) {
2143 // This test creates an implicit cast from int to char.
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002144 EXPECT_TRUE(matches("char c = 0;", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002145 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002146 EXPECT_TRUE(matches("char c = 0, d = c;", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002147}
2148
2149TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002150 EXPECT_TRUE(notMatches("char c = '0';", expr(castExpr())));
2151 EXPECT_TRUE(notMatches("char c, &q = c;", expr(castExpr())));
2152 EXPECT_TRUE(notMatches("int i = (0);", expr(castExpr())));
2153 EXPECT_TRUE(notMatches("int i = 0;", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002154}
2155
Manuel Klimek4da21662012-07-06 05:48:52 +00002156TEST(ReinterpretCast, MatchesSimpleCase) {
2157 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002158 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002159}
2160
2161TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
2162 EXPECT_TRUE(notMatches("char* p = (char*)(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002163 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002164 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002165 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002166 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002167 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002168 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2169 "B b;"
2170 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002171 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002172}
2173
2174TEST(FunctionalCast, MatchesSimpleCase) {
2175 std::string foo_class = "class Foo { public: Foo(char*); };";
2176 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002177 expr(functionalCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002178}
2179
2180TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2181 std::string FooClass = "class Foo { public: Foo(char*); };";
2182 EXPECT_TRUE(
2183 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002184 expr(functionalCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002185 EXPECT_TRUE(
2186 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002187 expr(functionalCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002188}
2189
2190TEST(DynamicCast, MatchesSimpleCase) {
2191 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2192 "B b;"
2193 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002194 expr(dynamicCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002195}
2196
2197TEST(StaticCast, MatchesSimpleCase) {
2198 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002199 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002200}
2201
2202TEST(StaticCast, DoesNotMatchOtherCasts) {
2203 EXPECT_TRUE(notMatches("char* p = (char*)(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002204 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002205 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002206 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002207 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002208 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002209 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2210 "B b;"
2211 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002212 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002213}
2214
2215TEST(HasDestinationType, MatchesSimpleCase) {
2216 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002217 expr(staticCastExpr(hasDestinationType(
2218 pointsTo(TypeMatcher(anything())))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002219}
2220
Sam Panzer089e5b32012-08-16 16:58:10 +00002221TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2222 // This test creates an implicit const cast.
2223 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002224 expr(implicitCastExpr(
Sam Panzer089e5b32012-08-16 16:58:10 +00002225 hasImplicitDestinationType(isInteger())))));
2226 // This test creates an implicit array-to-pointer cast.
2227 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002228 expr(implicitCastExpr(hasImplicitDestinationType(
Sam Panzer089e5b32012-08-16 16:58:10 +00002229 pointsTo(TypeMatcher(anything())))))));
2230}
2231
2232TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2233 // This test creates an implicit cast from int to char.
2234 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002235 expr(implicitCastExpr(hasImplicitDestinationType(
Sam Panzer089e5b32012-08-16 16:58:10 +00002236 unless(anything()))))));
2237 // This test creates an implicit array-to-pointer cast.
2238 EXPECT_TRUE(notMatches("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 unless(anything()))))));
2241}
2242
2243TEST(ImplicitCast, MatchesSimpleCase) {
2244 // This test creates an implicit const cast.
2245 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002246 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002247 // This test creates an implicit cast from int to char.
2248 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002249 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002250 // This test creates an implicit array-to-pointer cast.
2251 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002252 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002253}
2254
2255TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002256 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002257 // are present, and that it ignores explicit and paren casts.
2258
2259 // These two test cases have no casts.
2260 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002261 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002262 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002263 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002264
2265 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002266 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002267 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002268 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002269
2270 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002271 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002272}
2273
2274TEST(IgnoringImpCasts, MatchesImpCasts) {
2275 // This test checks that ignoringImpCasts matches when implicit casts are
2276 // present and its inner matcher alone does not match.
2277 // Note that this test creates an implicit const cast.
2278 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002279 varDecl(hasInitializer(ignoringImpCasts(
2280 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002281 // This test creates an implict cast from int to char.
2282 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002283 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002284 integerLiteral(equals(0)))))));
2285}
2286
2287TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2288 // These tests verify that ignoringImpCasts does not match if the inner
2289 // matcher does not match.
2290 // Note that the first test creates an implicit const cast.
2291 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002292 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002293 unless(anything()))))));
2294 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002295 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002296 unless(anything()))))));
2297
2298 // These tests verify that ignoringImplictCasts does not look through explicit
2299 // casts or parentheses.
2300 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002301 varDecl(hasInitializer(ignoringImpCasts(
2302 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002303 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002304 varDecl(hasInitializer(ignoringImpCasts(
2305 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002306 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002307 varDecl(hasInitializer(ignoringImpCasts(
2308 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002309 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002310 varDecl(hasInitializer(ignoringImpCasts(
2311 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002312}
2313
2314TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2315 // This test verifies that expressions that do not have implicit casts
2316 // still match the inner matcher.
2317 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002318 varDecl(hasInitializer(ignoringImpCasts(
2319 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002320}
2321
2322TEST(IgnoringParenCasts, MatchesParenCasts) {
2323 // This test checks that ignoringParenCasts matches when parentheses and/or
2324 // casts are present and its inner matcher alone does not match.
2325 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002326 varDecl(hasInitializer(ignoringParenCasts(
2327 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002328 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002329 varDecl(hasInitializer(ignoringParenCasts(
2330 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002331
2332 // This test creates an implict cast from int to char in addition to the
2333 // parentheses.
2334 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002335 varDecl(hasInitializer(ignoringParenCasts(
2336 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002337
2338 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002339 varDecl(hasInitializer(ignoringParenCasts(
2340 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002341 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002342 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002343 integerLiteral(equals(0)))))));
2344}
2345
2346TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2347 // This test verifies that expressions that do not have any casts still match.
2348 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002349 varDecl(hasInitializer(ignoringParenCasts(
2350 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002351}
2352
2353TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2354 // These tests verify that ignoringImpCasts does not match if the inner
2355 // matcher does not match.
2356 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002357 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002358 unless(anything()))))));
2359
2360 // This test creates an implicit cast from int to char in addition to the
2361 // parentheses.
2362 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002363 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002364 unless(anything()))))));
2365
2366 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002367 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002368 unless(anything()))))));
2369}
2370
2371TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2372 // This test checks that ignoringParenAndImpCasts matches when
2373 // parentheses and/or implicit casts are present and its inner matcher alone
2374 // does not match.
2375 // Note that this test creates an implicit const cast.
2376 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002377 varDecl(hasInitializer(ignoringParenImpCasts(
2378 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002379 // This test creates an implicit cast from int to char.
2380 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002381 varDecl(hasInitializer(ignoringParenImpCasts(
2382 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002383}
2384
2385TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2386 // This test verifies that expressions that do not have parentheses or
2387 // implicit casts still match.
2388 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002389 varDecl(hasInitializer(ignoringParenImpCasts(
2390 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002391 EXPECT_TRUE(matches("int 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, DoesNotMatchIncorrectly) {
2397 // These tests verify that ignoringParenImpCasts does not match if
2398 // the inner matcher does not match.
2399 // This test creates an implicit cast.
2400 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002401 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002402 unless(anything()))))));
2403 // These tests verify that ignoringParenAndImplictCasts does not look
2404 // through explicit casts.
2405 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002406 varDecl(hasInitializer(ignoringParenImpCasts(
2407 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002408 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002409 varDecl(hasInitializer(ignoringParenImpCasts(
2410 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002411 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002412 varDecl(hasInitializer(ignoringParenImpCasts(
2413 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002414}
2415
Manuel Klimek715c9562012-07-25 10:02:02 +00002416TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002417 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2418 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002419 expr(implicitCastExpr(
2420 hasSourceExpression(constructExpr())))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002421}
2422
Manuel Klimek715c9562012-07-25 10:02:02 +00002423TEST(HasSourceExpression, MatchesExplicitCasts) {
2424 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002425 expr(explicitCastExpr(
2426 hasSourceExpression(hasDescendant(
2427 expr(integerLiteral())))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002428}
2429
Manuel Klimek4da21662012-07-06 05:48:52 +00002430TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002431 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002432}
2433
2434TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002435 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002436}
2437
2438TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002439 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002440}
2441
2442TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002443 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002444}
2445
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002446TEST(InitListExpression, MatchesInitListExpression) {
2447 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2448 initListExpr(hasType(asString("int [2]")))));
2449 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002450 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002451}
2452
2453TEST(UsingDeclaration, MatchesUsingDeclarations) {
2454 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2455 usingDecl()));
2456}
2457
2458TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2459 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2460 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2461}
2462
2463TEST(UsingDeclaration, MatchesSpecificTarget) {
2464 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2465 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002466 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002467 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2468 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002469 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002470}
2471
2472TEST(UsingDeclaration, ThroughUsingDeclaration) {
2473 EXPECT_TRUE(matches(
2474 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002475 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002476 EXPECT_TRUE(notMatches(
2477 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002478 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002479}
2480
Sam Panzer425f41b2012-08-16 17:20:59 +00002481TEST(SingleDecl, IsSingleDecl) {
2482 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002483 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002484 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2485 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2486 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2487 SingleDeclStmt));
2488}
2489
2490TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002491 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002492
2493 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002494 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002495 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002496 declStmt(containsDeclaration(0, MatchesInit),
2497 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002498 unsigned WrongIndex = 42;
2499 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002500 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002501 MatchesInit))));
2502}
2503
2504TEST(DeclCount, DeclCountIsCorrect) {
2505 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002506 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002507 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002508 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002509 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002510 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002511}
2512
Manuel Klimek4da21662012-07-06 05:48:52 +00002513TEST(While, MatchesWhileLoops) {
2514 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2515 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2516 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2517}
2518
2519TEST(Do, MatchesDoLoops) {
2520 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2521 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2522}
2523
2524TEST(Do, DoesNotMatchWhileLoops) {
2525 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2526}
2527
2528TEST(SwitchCase, MatchesCase) {
2529 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2530 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2531 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2532 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2533}
2534
2535TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2536 EXPECT_TRUE(notMatches(
2537 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002538 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002539 EXPECT_TRUE(notMatches(
2540 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002541 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002542}
2543
2544TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2545 EXPECT_TRUE(matches(
2546 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002547 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002548}
2549
2550TEST(ForEach, BindsOneNode) {
2551 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002552 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002553 new VerifyIdIsBoundToDecl<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002554}
2555
2556TEST(ForEach, BindsMultipleNodes) {
2557 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002558 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002559 new VerifyIdIsBoundToDecl<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002560}
2561
2562TEST(ForEach, BindsRecursiveCombinations) {
2563 EXPECT_TRUE(matchAndVerifyResultTrue(
2564 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002565 recordDecl(hasName("C"),
2566 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002567 new VerifyIdIsBoundToDecl<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002568}
2569
2570TEST(ForEachDescendant, BindsOneNode) {
2571 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002572 recordDecl(hasName("C"),
2573 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002574 new VerifyIdIsBoundToDecl<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002575}
2576
2577TEST(ForEachDescendant, BindsMultipleNodes) {
2578 EXPECT_TRUE(matchAndVerifyResultTrue(
2579 "class C { class D { int x; int y; }; "
2580 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002581 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002582 new VerifyIdIsBoundToDecl<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002583}
2584
2585TEST(ForEachDescendant, BindsRecursiveCombinations) {
2586 EXPECT_TRUE(matchAndVerifyResultTrue(
2587 "class C { class D { "
2588 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002589 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2590 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002591 new VerifyIdIsBoundToDecl<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002592}
2593
2594
2595TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2596 // Make sure that we can both match the class by name (::X) and by the type
2597 // the template was instantiated with (via a field).
2598
2599 EXPECT_TRUE(matches(
2600 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002601 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002602
2603 EXPECT_TRUE(matches(
2604 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002605 recordDecl(isTemplateInstantiation(), hasDescendant(
2606 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002607}
2608
2609TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2610 EXPECT_TRUE(matches(
2611 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002612 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002613 isTemplateInstantiation())));
2614}
2615
2616TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2617 EXPECT_TRUE(matches(
2618 "template <typename T> class X { T t; }; class A {};"
2619 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002620 recordDecl(isTemplateInstantiation(), hasDescendant(
2621 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002622}
2623
2624TEST(IsTemplateInstantiation,
2625 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2626 EXPECT_TRUE(matches(
2627 "template <typename T> class X {};"
2628 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002629 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002630}
2631
2632TEST(IsTemplateInstantiation,
2633 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2634 EXPECT_TRUE(matches(
2635 "class A {};"
2636 "class X {"
2637 " template <typename U> class Y { U u; };"
2638 " Y<A> y;"
2639 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002640 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002641}
2642
2643TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2644 // FIXME: Figure out whether this makes sense. It doesn't affect the
2645 // normal use case as long as the uppermost instantiation always is marked
2646 // as template instantiation, but it might be confusing as a predicate.
2647 EXPECT_TRUE(matches(
2648 "class A {};"
2649 "template <typename T> class X {"
2650 " template <typename U> class Y { U u; };"
2651 " Y<T> y;"
2652 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002653 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002654}
2655
2656TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2657 EXPECT_TRUE(notMatches(
2658 "template <typename T> class X {}; class A {};"
2659 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002660 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002661}
2662
2663TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2664 EXPECT_TRUE(notMatches(
2665 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002666 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002667}
2668
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002669TEST(IsExplicitTemplateSpecialization,
2670 DoesNotMatchPrimaryTemplate) {
2671 EXPECT_TRUE(notMatches(
2672 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002673 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002674 EXPECT_TRUE(notMatches(
2675 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002676 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002677}
2678
2679TEST(IsExplicitTemplateSpecialization,
2680 DoesNotMatchExplicitTemplateInstantiations) {
2681 EXPECT_TRUE(notMatches(
2682 "template <typename T> class X {};"
2683 "template class X<int>; extern template class X<long>;",
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) {}"
2687 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002688 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002689}
2690
2691TEST(IsExplicitTemplateSpecialization,
2692 DoesNotMatchImplicitTemplateInstantiations) {
2693 EXPECT_TRUE(notMatches(
2694 "template <typename T> class X {}; X<int> x;",
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); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002698 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002699}
2700
2701TEST(IsExplicitTemplateSpecialization,
2702 MatchesExplicitTemplateSpecializations) {
2703 EXPECT_TRUE(matches(
2704 "template <typename T> class X {};"
2705 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002706 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002707 EXPECT_TRUE(matches(
2708 "template <typename T> void f(T t) {}"
2709 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002710 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002711}
2712
Manuel Klimek4da21662012-07-06 05:48:52 +00002713} // end namespace ast_matchers
2714} // end namespace clang