blob: 55e22e927a0a5151a4a3b2fbf8d98fcf902223aa [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));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000104 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
105 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek4da21662012-07-06 05:48:52 +0000106 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
107 EXPECT_TRUE(notMatches("", IsDerivedFromX));
108
Daniel Jasper76dafa72012-09-07 12:48:17 +0000109 DeclarationMatcher IsAX = recordDecl(isA("X"));
110
111 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
112 EXPECT_TRUE(matches("class X {};", IsAX));
113 EXPECT_TRUE(matches("class X;", IsAX));
114 EXPECT_TRUE(notMatches("class Y;", IsAX));
115 EXPECT_TRUE(notMatches("", IsAX));
116
Manuel Klimek4da21662012-07-06 05:48:52 +0000117 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000118 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000119 EXPECT_TRUE(
120 matches("class X {}; class Y : public X {}; class Z : public Y {};",
121 ZIsDerivedFromX));
122 EXPECT_TRUE(
123 matches("class X {};"
124 "template<class T> class Y : public X {};"
125 "class Z : public Y<int> {};", ZIsDerivedFromX));
126 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
127 ZIsDerivedFromX));
128 EXPECT_TRUE(
129 matches("template<class T> class X {}; "
130 "template<class T> class Z : public X<T> {};",
131 ZIsDerivedFromX));
132 EXPECT_TRUE(
133 matches("template<class T, class U=T> class X {}; "
134 "template<class T> class Z : public X<T> {};",
135 ZIsDerivedFromX));
136 EXPECT_TRUE(
137 notMatches("template<class X> class A { class Z : public X {}; };",
138 ZIsDerivedFromX));
139 EXPECT_TRUE(
140 matches("template<class X> class A { public: class Z : public X {}; }; "
141 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
142 EXPECT_TRUE(
143 matches("template <class T> class X {}; "
144 "template<class Y> class A { class Z : public X<Y> {}; };",
145 ZIsDerivedFromX));
146 EXPECT_TRUE(
147 notMatches("template<template<class T> class X> class A { "
148 " class Z : public X<int> {}; };", ZIsDerivedFromX));
149 EXPECT_TRUE(
150 matches("template<template<class T> class X> class A { "
151 " public: class Z : public X<int> {}; }; "
152 "template<class T> class X {}; void y() { A<X>::Z z; }",
153 ZIsDerivedFromX));
154 EXPECT_TRUE(
155 notMatches("template<class X> class A { class Z : public X::D {}; };",
156 ZIsDerivedFromX));
157 EXPECT_TRUE(
158 matches("template<class X> class A { public: "
159 " class Z : public X::D {}; }; "
160 "class Y { public: class X {}; typedef X D; }; "
161 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
162 EXPECT_TRUE(
163 matches("class X {}; typedef X Y; class Z : public Y {};",
164 ZIsDerivedFromX));
165 EXPECT_TRUE(
166 matches("template<class T> class Y { typedef typename T::U X; "
167 " class Z : public X {}; };", ZIsDerivedFromX));
168 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
169 ZIsDerivedFromX));
170 EXPECT_TRUE(
171 notMatches("template<class T> class X {}; "
172 "template<class T> class A { class Z : public X<T>::D {}; };",
173 ZIsDerivedFromX));
174 EXPECT_TRUE(
175 matches("template<class T> class X { public: typedef X<T> D; }; "
176 "template<class T> class A { public: "
177 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
178 ZIsDerivedFromX));
179 EXPECT_TRUE(
180 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
181 ZIsDerivedFromX));
182 EXPECT_TRUE(
183 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
184 ZIsDerivedFromX));
185 EXPECT_TRUE(
186 matches("class X {}; class Y : public X {}; "
187 "typedef Y V; typedef V W; class Z : public W {};",
188 ZIsDerivedFromX));
189 EXPECT_TRUE(
190 matches("template<class T, class U> class X {}; "
191 "template<class T> class A { class Z : public X<T, int> {}; };",
192 ZIsDerivedFromX));
193 EXPECT_TRUE(
194 notMatches("template<class X> class D { typedef X A; typedef A B; "
195 " typedef B C; class Z : public C {}; };",
196 ZIsDerivedFromX));
197 EXPECT_TRUE(
198 matches("class X {}; typedef X A; typedef A B; "
199 "class Z : public B {};", ZIsDerivedFromX));
200 EXPECT_TRUE(
201 matches("class X {}; typedef X A; typedef A B; typedef B C; "
202 "class Z : public C {};", ZIsDerivedFromX));
203 EXPECT_TRUE(
204 matches("class U {}; typedef U X; typedef X V; "
205 "class Z : public V {};", ZIsDerivedFromX));
206 EXPECT_TRUE(
207 matches("class Base {}; typedef Base X; "
208 "class Z : public Base {};", ZIsDerivedFromX));
209 EXPECT_TRUE(
210 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
211 "class Z : public Base {};", ZIsDerivedFromX));
212 EXPECT_TRUE(
213 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
214 "class Z : public Base {};", ZIsDerivedFromX));
215 EXPECT_TRUE(
216 matches("class A {}; typedef A X; typedef A Y; "
217 "class Z : public Y {};", ZIsDerivedFromX));
218 EXPECT_TRUE(
219 notMatches("template <typename T> class Z;"
220 "template <> class Z<void> {};"
221 "template <typename T> class Z : public Z<void> {};",
222 IsDerivedFromX));
223 EXPECT_TRUE(
224 matches("template <typename T> class X;"
225 "template <> class X<void> {};"
226 "template <typename T> class X : public X<void> {};",
227 IsDerivedFromX));
228 EXPECT_TRUE(matches(
229 "class X {};"
230 "template <typename T> class Z;"
231 "template <> class Z<void> {};"
232 "template <typename T> class Z : public Z<void>, public X {};",
233 ZIsDerivedFromX));
234
235 // FIXME: Once we have better matchers for template type matching,
236 // get rid of the Variable(...) matching and match the right template
237 // declarations directly.
238 const char *RecursiveTemplateOneParameter =
239 "class Base1 {}; class Base2 {};"
240 "template <typename T> class Z;"
241 "template <> class Z<void> : public Base1 {};"
242 "template <> class Z<int> : public Base2 {};"
243 "template <> class Z<float> : public Z<void> {};"
244 "template <> class Z<double> : public Z<int> {};"
245 "template <typename T> class Z : public Z<float>, public Z<double> {};"
246 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
247 EXPECT_TRUE(matches(
248 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000249 varDecl(hasName("z_float"),
250 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000251 EXPECT_TRUE(notMatches(
252 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000253 varDecl(hasName("z_float"),
254 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000255 EXPECT_TRUE(matches(
256 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000257 varDecl(hasName("z_char"),
258 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
259 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000260
261 const char *RecursiveTemplateTwoParameters =
262 "class Base1 {}; class Base2 {};"
263 "template <typename T1, typename T2> class Z;"
264 "template <typename T> class Z<void, T> : public Base1 {};"
265 "template <typename T> class Z<int, T> : public Base2 {};"
266 "template <typename T> class Z<float, T> : public Z<void, T> {};"
267 "template <typename T> class Z<double, T> : public Z<int, T> {};"
268 "template <typename T1, typename T2> class Z : "
269 " public Z<float, T2>, public Z<double, T2> {};"
270 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
271 " Z<char, void> z_char; }";
272 EXPECT_TRUE(matches(
273 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000274 varDecl(hasName("z_float"),
275 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000276 EXPECT_TRUE(notMatches(
277 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000278 varDecl(hasName("z_float"),
279 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000280 EXPECT_TRUE(matches(
281 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000282 varDecl(hasName("z_char"),
283 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
284 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000285 EXPECT_TRUE(matches(
286 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000287 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000288 EXPECT_TRUE(notMatches(
289 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000290 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000291
292 EXPECT_TRUE(matches(
293 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000294 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000295}
296
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000297TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000298 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000299 EXPECT_TRUE(notMatches("class X;", ClassX));
300 EXPECT_TRUE(notMatches("class X {};", ClassX));
301}
302
303TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000304 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000305 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
306 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
307}
308
309TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
310 EXPECT_TRUE(notMatches("template<typename T> class X { };"
311 "template<> class X<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
316TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
317 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
318 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000319 classTemplateDecl(hasName("X"),
320 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000321}
322
Daniel Jasper6a124492012-07-12 08:50:38 +0000323TEST(AllOf, AllOverloadsWork) {
324 const char Program[] =
325 "struct T { }; int f(int, T*); void g(int x) { T t; f(x, &t); }";
326 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000327 callExpr(allOf(callee(functionDecl(hasName("f"))),
328 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000329 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000330 callExpr(allOf(callee(functionDecl(hasName("f"))),
331 hasArgument(0, declRefExpr(to(varDecl()))),
332 hasArgument(1, hasType(pointsTo(
333 recordDecl(hasName("T")))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000334}
335
Manuel Klimek4da21662012-07-06 05:48:52 +0000336TEST(DeclarationMatcher, MatchAnyOf) {
337 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000338 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000339 EXPECT_TRUE(
340 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
341 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
342 EXPECT_TRUE(
343 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
344 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
345
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000346 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000347 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000348 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
349 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
350
Manuel Klimek4da21662012-07-06 05:48:52 +0000351 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000352 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
353 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000354 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
355 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
356 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
357 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
358 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
359 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
360}
361
362TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000363 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000364 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
365 EXPECT_TRUE(matches("class X {};", HasClassX));
366
367 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000368 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000369 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
370 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
371 EXPECT_TRUE(
372 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
373}
374
375TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
376 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000377 recordDecl(
378 has(recordDecl(
379 has(recordDecl(hasName("X"))),
380 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000381 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000382 has(recordDecl(
383 has(recordDecl(hasName("A"))),
384 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000385 hasName("C"))),
386 hasName("F"));
387
388 EXPECT_TRUE(matches(
389 "class F {"
390 " class Z {"
391 " class X {};"
392 " class Y {};"
393 " };"
394 " class C {"
395 " class A {};"
396 " class B {};"
397 " };"
398 "};", Recursive));
399
400 EXPECT_TRUE(matches(
401 "class F {"
402 " class Z {"
403 " class A {};"
404 " class X {};"
405 " class Y {};"
406 " };"
407 " class C {"
408 " class X {};"
409 " class A {};"
410 " class B {};"
411 " };"
412 "};", Recursive));
413
414 EXPECT_TRUE(matches(
415 "class O1 {"
416 " class O2 {"
417 " class F {"
418 " class Z {"
419 " class A {};"
420 " class X {};"
421 " class Y {};"
422 " };"
423 " class C {"
424 " class X {};"
425 " class A {};"
426 " class B {};"
427 " };"
428 " };"
429 " };"
430 "};", Recursive));
431}
432
433TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
434 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000435 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000436 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000437 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000438 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000439 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000440 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000441 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000442 hasName("Y"))),
443 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000444 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000445 anyOf(
446 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000447 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000448 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000449 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000450 hasName("B")))))),
451 hasName("F")));
452
453 EXPECT_TRUE(matches("class F {};", Recursive));
454 EXPECT_TRUE(matches("class Z {};", Recursive));
455 EXPECT_TRUE(matches("class C {};", Recursive));
456 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
457 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
458 EXPECT_TRUE(
459 matches("class O1 { class O2 {"
460 " class M { class N { class B {}; }; }; "
461 "}; };", Recursive));
462}
463
464TEST(DeclarationMatcher, MatchNot) {
465 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000466 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000467 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000468 unless(hasName("X")));
469 EXPECT_TRUE(notMatches("", NotClassX));
470 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
471 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
472 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
473 EXPECT_TRUE(
474 notMatches("class Y {}; class Z {}; class X : public Y {};",
475 NotClassX));
476
477 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000478 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000479 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000480 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000481 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000482 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000483 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
484 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
485 ClassXHasNotClassY));
486}
487
488TEST(DeclarationMatcher, HasDescendant) {
489 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000490 recordDecl(
491 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000492 hasName("Z"));
493 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
494 EXPECT_TRUE(
495 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
496 EXPECT_TRUE(
497 matches("class Z { class A { class Y { class X {}; }; }; };",
498 ZDescendantClassX));
499 EXPECT_TRUE(
500 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
501 ZDescendantClassX));
502 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
503
504 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000505 recordDecl(
506 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000507 hasName("X"))),
508 hasName("Z"));
509 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
510 ZDescendantClassXHasClassY));
511 EXPECT_TRUE(
512 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
513 ZDescendantClassXHasClassY));
514 EXPECT_TRUE(notMatches(
515 "class Z {"
516 " class A {"
517 " class B {"
518 " class X {"
519 " class C {"
520 " class Y {};"
521 " };"
522 " };"
523 " }; "
524 " };"
525 "};", ZDescendantClassXHasClassY));
526
527 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000528 recordDecl(
529 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
530 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000531 hasName("Z"));
532 EXPECT_TRUE(
533 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
534 ZDescendantClassXDescendantClassY));
535 EXPECT_TRUE(matches(
536 "class Z {"
537 " class A {"
538 " class X {"
539 " class B {"
540 " class Y {};"
541 " };"
542 " class Y {};"
543 " };"
544 " };"
545 "};", ZDescendantClassXDescendantClassY));
546}
547
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000548TEST(Enum, DoesNotMatchClasses) {
549 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
550}
551
552TEST(Enum, MatchesEnums) {
553 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
554}
555
556TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000557 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000558 EXPECT_TRUE(matches("enum X{ A };", Matcher));
559 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
560 EXPECT_TRUE(notMatches("enum X {};", Matcher));
561}
562
Manuel Klimek4da21662012-07-06 05:48:52 +0000563TEST(StatementMatcher, Has) {
564 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000565 expr(hasType(pointsTo(recordDecl(hasName("X")))),
566 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000567
568 EXPECT_TRUE(matches(
569 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
570 EXPECT_TRUE(notMatches(
571 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
572}
573
574TEST(StatementMatcher, HasDescendant) {
575 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000576 expr(hasType(pointsTo(recordDecl(hasName("X")))),
577 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000578
579 EXPECT_TRUE(matches(
580 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
581 HasDescendantVariableI));
582 EXPECT_TRUE(notMatches(
583 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
584 HasDescendantVariableI));
585}
586
587TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000588 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000589
590 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
591 EXPECT_TRUE(notMatches("class A {};", TypeA));
592
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000593 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000594
595 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
596 TypeDerivedFromA));
597 EXPECT_TRUE(notMatches("class A {};", TypeA));
598
599 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000600 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000601
602 EXPECT_TRUE(
603 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
604}
605
Manuel Klimek579b1202012-09-07 09:26:10 +0000606// Implements a run method that returns whether BoundNodes contains a
607// Decl bound to Id that can be dynamically cast to T.
Manuel Klimek4da21662012-07-06 05:48:52 +0000608// Optionally checks that the check succeeded a specific number of times.
609template <typename T>
610class VerifyIdIsBoundToDecl : public BoundNodesCallback {
611public:
Manuel Klimek579b1202012-09-07 09:26:10 +0000612 // Create an object that checks that a node of type \c T was bound to \c Id.
Manuel Klimek4da21662012-07-06 05:48:52 +0000613 // Does not check for a certain number of matches.
Manuel Klimek579b1202012-09-07 09:26:10 +0000614 explicit VerifyIdIsBoundToDecl(llvm::StringRef Id)
Manuel Klimek4da21662012-07-06 05:48:52 +0000615 : Id(Id), ExpectedCount(-1), Count(0) {}
616
Manuel Klimek579b1202012-09-07 09:26:10 +0000617 // Create an object that checks that a node of type \c T was bound to \c Id.
618 // Checks that there were exactly \c ExpectedCount matches.
619 VerifyIdIsBoundToDecl(llvm::StringRef Id, int ExpectedCount)
Manuel Klimek4da21662012-07-06 05:48:52 +0000620 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
621
Manuel Klimek579b1202012-09-07 09:26:10 +0000622 // Create an object that checks that a node of type \c T was bound to \c Id.
623 // Checks that there was exactly one match with the name \c ExpectedDeclName.
624 // Note that \c T must be a NamedDecl for this to work.
625 VerifyIdIsBoundToDecl(llvm::StringRef Id, llvm::StringRef ExpectedDeclName)
626 : Id(Id), ExpectedCount(1), Count(0), ExpectedDeclName(ExpectedDeclName) {}
627
Manuel Klimek4da21662012-07-06 05:48:52 +0000628 ~VerifyIdIsBoundToDecl() {
Manuel Klimek579b1202012-09-07 09:26:10 +0000629 if (ExpectedCount != -1)
Manuel Klimek4da21662012-07-06 05:48:52 +0000630 EXPECT_EQ(ExpectedCount, Count);
Manuel Klimek579b1202012-09-07 09:26:10 +0000631 if (!ExpectedDeclName.empty())
632 EXPECT_EQ(ExpectedDeclName, DeclName);
Manuel Klimek4da21662012-07-06 05:48:52 +0000633 }
634
635 virtual bool run(const BoundNodes *Nodes) {
Manuel Klimek579b1202012-09-07 09:26:10 +0000636 if (const Decl *Node = Nodes->getDeclAs<T>(Id)) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000637 ++Count;
Manuel Klimek579b1202012-09-07 09:26:10 +0000638 if (const NamedDecl *Named = llvm::dyn_cast<NamedDecl>(Node)) {
639 DeclName = Named->getNameAsString();
640 }
Manuel Klimek4da21662012-07-06 05:48:52 +0000641 return true;
642 }
643 return false;
644 }
645
646private:
647 const std::string Id;
648 const int ExpectedCount;
649 int Count;
Manuel Klimek579b1202012-09-07 09:26:10 +0000650 const std::string ExpectedDeclName;
651 std::string DeclName;
Manuel Klimek4da21662012-07-06 05:48:52 +0000652};
653template <typename T>
654class VerifyIdIsBoundToStmt : public BoundNodesCallback {
655public:
656 explicit VerifyIdIsBoundToStmt(const std::string &Id) : Id(Id) {}
657 virtual bool run(const BoundNodes *Nodes) {
658 const T *Node = Nodes->getStmtAs<T>(Id);
659 return Node != NULL;
660 }
661private:
662 const std::string Id;
663};
664
665TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000666 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000667
668 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000669 ClassX, new VerifyIdIsBoundToDecl<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000670
671 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000672 ClassX, new VerifyIdIsBoundToDecl<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000673
674 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000675 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000676
677 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
678 TypeAHasClassB,
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000679 new VerifyIdIsBoundToDecl<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000680
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000681 StatementMatcher MethodX =
682 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000683
684 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
685 MethodX,
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000686 new VerifyIdIsBoundToStmt<CXXMemberCallExpr>("x")));
687}
688
689TEST(Matcher, BindTheSameNameInAlternatives) {
690 StatementMatcher matcher = anyOf(
691 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000692 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000693 hasRHS(integerLiteral(equals(0)))),
694 binaryOperator(hasOperatorName("+"),
695 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000696 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000697
698 EXPECT_TRUE(matchAndVerifyResultTrue(
699 // The first branch of the matcher binds x to 0 but then fails.
700 // The second branch binds x to f() and succeeds.
701 "int f() { return 0 + f(); }",
702 matcher,
703 new VerifyIdIsBoundToStmt<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000704}
705
Manuel Klimek66341c52012-08-30 19:41:06 +0000706TEST(Matcher, BindsIDForMemoizedResults) {
707 // Using the same matcher in two match expressions will make memoization
708 // kick in.
709 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
710 EXPECT_TRUE(matchAndVerifyResultTrue(
711 "class A { class B { class X {}; }; };",
712 DeclarationMatcher(anyOf(
713 recordDecl(hasName("A"), hasDescendant(ClassX)),
714 recordDecl(hasName("B"), hasDescendant(ClassX)))),
715 new VerifyIdIsBoundToDecl<Decl>("x", 2)));
716}
717
Manuel Klimek4da21662012-07-06 05:48:52 +0000718TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000719 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000720 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000721 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000722 EXPECT_TRUE(
723 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000724 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000725 EXPECT_TRUE(
726 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000727 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000728}
729
730TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000731 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000732 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000733 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000734 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000735 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000736 EXPECT_TRUE(
737 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000738 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000739}
740
741TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000742 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000743 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000744 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000745 EXPECT_TRUE(
746 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000747 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000748}
749
750TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000751 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000752 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000753 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000754 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000755 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000756}
757
758TEST(Matcher, Call) {
759 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000760 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000761 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000762
763 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
764 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
765
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000766 StatementMatcher MethodOnY =
767 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000768
769 EXPECT_TRUE(
770 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
771 MethodOnY));
772 EXPECT_TRUE(
773 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
774 MethodOnY));
775 EXPECT_TRUE(
776 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
777 MethodOnY));
778 EXPECT_TRUE(
779 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
780 MethodOnY));
781 EXPECT_TRUE(
782 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
783 MethodOnY));
784
785 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000786 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000787
788 EXPECT_TRUE(
789 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
790 MethodOnYPointer));
791 EXPECT_TRUE(
792 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
793 MethodOnYPointer));
794 EXPECT_TRUE(
795 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
796 MethodOnYPointer));
797 EXPECT_TRUE(
798 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
799 MethodOnYPointer));
800 EXPECT_TRUE(
801 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
802 MethodOnYPointer));
803}
804
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000805TEST(HasType, MatchesAsString) {
806 EXPECT_TRUE(
807 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000808 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000809 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000810 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000811 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000812 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000813 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000814 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000815}
816
Manuel Klimek4da21662012-07-06 05:48:52 +0000817TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000818 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000819 // Unary operator
820 EXPECT_TRUE(matches("class Y { }; "
821 "bool operator!(Y x) { return false; }; "
822 "Y y; bool c = !y;", OpCall));
823 // No match -- special operators like "new", "delete"
824 // FIXME: operator new takes size_t, for which we need stddef.h, for which
825 // we need to figure out include paths in the test.
826 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
827 // "class Y { }; "
828 // "void *operator new(size_t size) { return 0; } "
829 // "Y *y = new Y;", OpCall));
830 EXPECT_TRUE(notMatches("class Y { }; "
831 "void operator delete(void *p) { } "
832 "void a() {Y *y = new Y; delete y;}", OpCall));
833 // Binary operator
834 EXPECT_TRUE(matches("class Y { }; "
835 "bool operator&&(Y x, Y y) { return true; }; "
836 "Y a; Y b; bool c = a && b;",
837 OpCall));
838 // No match -- normal operator, not an overloaded one.
839 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
840 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
841}
842
843TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
844 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000845 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000846 EXPECT_TRUE(matches("class Y { }; "
847 "bool operator&&(Y x, Y y) { return true; }; "
848 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
849 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000850 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000851 EXPECT_TRUE(notMatches("class Y { }; "
852 "bool operator&&(Y x, Y y) { return true; }; "
853 "Y a; Y b; bool c = a && b;",
854 OpCallLessLess));
855}
856
857TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +0000858 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000859 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000860
861 EXPECT_TRUE(
862 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
863 MethodOnY));
864 EXPECT_TRUE(
865 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
866 MethodOnY));
867 EXPECT_TRUE(
868 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
869 MethodOnY));
870 EXPECT_TRUE(
871 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
872 MethodOnY));
873 EXPECT_TRUE(
874 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
875 MethodOnY));
876
877 EXPECT_TRUE(matches(
878 "class Y {"
879 " public: virtual void x();"
880 "};"
881 "class X : public Y {"
882 " public: virtual void x();"
883 "};"
884 "void z() { X *x; x->Y::x(); }", MethodOnY));
885}
886
887TEST(Matcher, VariableUsage) {
888 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000889 declRefExpr(to(
890 varDecl(hasInitializer(
891 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000892
893 EXPECT_TRUE(matches(
894 "class Y {"
895 " public:"
896 " bool x() const;"
897 "};"
898 "void z(const Y &y) {"
899 " bool b = y.x();"
900 " if (b) {}"
901 "}", Reference));
902
903 EXPECT_TRUE(notMatches(
904 "class Y {"
905 " public:"
906 " bool x() const;"
907 "};"
908 "void z(const Y &y) {"
909 " bool b = y.x();"
910 "}", Reference));
911}
912
Daniel Jasper9bd28092012-07-30 05:03:25 +0000913TEST(Matcher, FindsVarDeclInFuncitonParameter) {
914 EXPECT_TRUE(matches(
915 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000916 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +0000917}
918
Manuel Klimek4da21662012-07-06 05:48:52 +0000919TEST(Matcher, CalledVariable) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000920 StatementMatcher CallOnVariableY = expr(
921 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000922
923 EXPECT_TRUE(matches(
924 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
925 EXPECT_TRUE(matches(
926 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
927 EXPECT_TRUE(matches(
928 "class Y { public: void x(); };"
929 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
930 EXPECT_TRUE(matches(
931 "class Y { public: void x(); };"
932 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
933 EXPECT_TRUE(notMatches(
934 "class Y { public: void x(); };"
935 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
936 CallOnVariableY));
937}
938
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000939TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
940 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
941 unaryExprOrTypeTraitExpr()));
942 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
943 alignOfExpr(anything())));
944 // FIXME: Uncomment once alignof is enabled.
945 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
946 // unaryExprOrTypeTraitExpr()));
947 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
948 // sizeOfExpr()));
949}
950
951TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
952 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
953 hasArgumentOfType(asString("int")))));
954 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
955 hasArgumentOfType(asString("float")))));
956 EXPECT_TRUE(matches(
957 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000958 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000959 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000960 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000961}
962
Manuel Klimek4da21662012-07-06 05:48:52 +0000963TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000964 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000965}
966
967TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000968 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000969}
970
971TEST(MemberExpression, MatchesVariable) {
972 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000973 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000974 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000975 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000976 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000977 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000978}
979
980TEST(MemberExpression, MatchesStaticVariable) {
981 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000982 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000983 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000984 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000985 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000986 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000987}
988
Daniel Jasper6a124492012-07-12 08:50:38 +0000989TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000990 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
991 EXPECT_TRUE(matches(
992 "long long i = 0; void f(long long) { }; void g() {f(i);}",
993 callExpr(hasArgument(0, declRefExpr(
994 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000995}
996
997TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000998 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000999 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001000 callExpr(hasArgument(0, declRefExpr(
1001 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001002}
1003
Manuel Klimek4da21662012-07-06 05:48:52 +00001004TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1005 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001006 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001007 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001008 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001009 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001010 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001011}
1012
1013TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1014 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001015 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001016 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001017 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001018 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001019 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001020}
1021
1022TEST(IsArrow, MatchesMemberCallsViaArrow) {
1023 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001024 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001025 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001026 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001027 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001028 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001029}
1030
1031TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001032 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001033
1034 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1035 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1036}
1037
1038TEST(Callee, MatchesMemberExpressions) {
1039 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001040 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001041 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001042 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001043}
1044
1045TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001046 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001047
1048 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1049 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1050
Manuel Klimeke265c872012-07-10 14:21:30 +00001051#if !defined(_MSC_VER)
1052 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001053 // Dependent contexts, but a non-dependent call.
1054 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1055 CallFunctionF));
1056 EXPECT_TRUE(
1057 matches("void f(); template <int N> struct S { void g() { f(); } };",
1058 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001059#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001060
1061 // Depedent calls don't match.
1062 EXPECT_TRUE(
1063 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1064 CallFunctionF));
1065 EXPECT_TRUE(
1066 notMatches("void f(int);"
1067 "template <typename T> struct S { void g(T t) { f(t); } };",
1068 CallFunctionF));
1069}
1070
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001071TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1072 EXPECT_TRUE(
1073 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001074 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001075}
1076
1077TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1078 EXPECT_TRUE(
1079 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001080 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001081}
1082
1083TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1084 EXPECT_TRUE(
1085 notMatches("void g(); template <typename T> void f(T t) {}"
1086 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001087 functionTemplateDecl(hasName("f"),
1088 hasDescendant(declRefExpr(to(
1089 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001090}
1091
Manuel Klimek4da21662012-07-06 05:48:52 +00001092TEST(Matcher, Argument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001093 StatementMatcher CallArgumentY = expr(callExpr(
1094 hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001095
1096 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1097 EXPECT_TRUE(
1098 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1099 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1100
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001101 StatementMatcher WrongIndex = expr(callExpr(
1102 hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001103 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1104}
1105
1106TEST(Matcher, AnyArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001107 StatementMatcher CallArgumentY = expr(callExpr(
1108 hasAnyArgument(declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001109 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1110 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1111 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1112}
1113
1114TEST(Matcher, ArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001115 StatementMatcher Call1Arg = expr(callExpr(argumentCountIs(1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00001116
1117 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1118 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1119 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1120}
1121
1122TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001123 DeclarationMatcher ReferenceClassX = varDecl(
1124 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001125 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1126 ReferenceClassX));
1127 EXPECT_TRUE(
1128 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1129 EXPECT_TRUE(
1130 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1131 EXPECT_TRUE(
1132 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1133}
1134
1135TEST(HasParameter, CallsInnerMatcher) {
1136 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001137 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001138 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001139 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001140}
1141
1142TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1143 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001144 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001145}
1146
1147TEST(HasType, MatchesParameterVariableTypesStrictly) {
1148 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001149 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001150 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001151 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001152 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001153 methodDecl(hasParameter(0,
1154 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001155 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001156 methodDecl(hasParameter(0,
1157 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001158}
1159
1160TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1161 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001162 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001163 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001164 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001165}
1166
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001167TEST(Returns, MatchesReturnTypes) {
1168 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001169 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001170 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001171 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001172 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001173 functionDecl(returns(hasDeclaration(
1174 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001175}
1176
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001177TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001178 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1179 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1180 functionDecl(isExternC())));
1181 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001182}
1183
Manuel Klimek4da21662012-07-06 05:48:52 +00001184TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1185 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001186 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001187}
1188
1189TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1190 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001191 methodDecl(hasAnyParameter(hasType(pointsTo(
1192 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001193}
1194
1195TEST(HasName, MatchesParameterVariableDeclartions) {
1196 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001197 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001198 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001199 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001200}
1201
Daniel Jasper371f9392012-08-01 08:40:24 +00001202TEST(Matcher, MatchesClassTemplateSpecialization) {
1203 EXPECT_TRUE(matches("template<typename T> struct A {};"
1204 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001205 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001206 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001207 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001208 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001209 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001210}
1211
1212TEST(Matcher, MatchesTypeTemplateArgument) {
1213 EXPECT_TRUE(matches(
1214 "template<typename T> struct B {};"
1215 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001216 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001217 asString("int"))))));
1218}
1219
1220TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1221 EXPECT_TRUE(matches(
1222 "struct B { int next; };"
1223 "template<int(B::*next_ptr)> struct A {};"
1224 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001225 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1226 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001227}
1228
1229TEST(Matcher, MatchesSpecificArgument) {
1230 EXPECT_TRUE(matches(
1231 "template<typename T, typename U> class A {};"
1232 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001233 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001234 1, refersToType(asString("int"))))));
1235 EXPECT_TRUE(notMatches(
1236 "template<typename T, typename U> class A {};"
1237 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001238 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001239 1, refersToType(asString("int"))))));
1240}
1241
Manuel Klimek4da21662012-07-06 05:48:52 +00001242TEST(Matcher, ConstructorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001243 StatementMatcher Constructor = expr(constructExpr());
Manuel Klimek4da21662012-07-06 05:48:52 +00001244
1245 EXPECT_TRUE(
1246 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1247 EXPECT_TRUE(
1248 matches("class X { public: X(); }; void x() { X x = X(); }",
1249 Constructor));
1250 EXPECT_TRUE(
1251 matches("class X { public: X(int); }; void x() { X x = 0; }",
1252 Constructor));
1253 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1254}
1255
1256TEST(Matcher, ConstructorArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001257 StatementMatcher Constructor = expr(constructExpr(
1258 hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001259
1260 EXPECT_TRUE(
1261 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1262 Constructor));
1263 EXPECT_TRUE(
1264 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1265 Constructor));
1266 EXPECT_TRUE(
1267 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1268 Constructor));
1269 EXPECT_TRUE(
1270 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1271 Constructor));
1272
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001273 StatementMatcher WrongIndex = expr(constructExpr(
1274 hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001275 EXPECT_TRUE(
1276 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1277 WrongIndex));
1278}
1279
1280TEST(Matcher, ConstructorArgumentCount) {
1281 StatementMatcher Constructor1Arg =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001282 expr(constructExpr(argumentCountIs(1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00001283
1284 EXPECT_TRUE(
1285 matches("class X { public: X(int); }; void x() { X x(0); }",
1286 Constructor1Arg));
1287 EXPECT_TRUE(
1288 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1289 Constructor1Arg));
1290 EXPECT_TRUE(
1291 matches("class X { public: X(int); }; void x() { X x = 0; }",
1292 Constructor1Arg));
1293 EXPECT_TRUE(
1294 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1295 Constructor1Arg));
1296}
1297
1298TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001299 StatementMatcher TempExpression = expr(bindTemporaryExpr());
Manuel Klimek4da21662012-07-06 05:48:52 +00001300
1301 std::string ClassString = "class string { public: string(); ~string(); }; ";
1302
1303 EXPECT_TRUE(
1304 matches(ClassString +
1305 "string GetStringByValue();"
1306 "void FunctionTakesString(string s);"
1307 "void run() { FunctionTakesString(GetStringByValue()); }",
1308 TempExpression));
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 TempExpression));
1320
1321 EXPECT_TRUE(
1322 notMatches("class no_dtor {};"
1323 "no_dtor GetObjByValue();"
1324 "void ConsumeObj(no_dtor param);"
1325 "void run() { ConsumeObj(GetObjByValue()); }",
1326 TempExpression));
1327}
1328
Sam Panzere16acd32012-08-24 22:04:44 +00001329TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1330 std::string ClassString =
1331 "class string { public: string(); int length(); }; ";
1332
1333 EXPECT_TRUE(
1334 matches(ClassString +
1335 "string GetStringByValue();"
1336 "void FunctionTakesString(string s);"
1337 "void run() { FunctionTakesString(GetStringByValue()); }",
1338 materializeTemporaryExpr()));
1339
1340 EXPECT_TRUE(
1341 notMatches(ClassString +
1342 "string* GetStringPointer(); "
1343 "void FunctionTakesStringPtr(string* s);"
1344 "void run() {"
1345 " string* s = GetStringPointer();"
1346 " FunctionTakesStringPtr(GetStringPointer());"
1347 " FunctionTakesStringPtr(s);"
1348 "}",
1349 materializeTemporaryExpr()));
1350
1351 EXPECT_TRUE(
1352 notMatches(ClassString +
1353 "string GetStringByValue();"
1354 "void run() { int k = GetStringByValue().length(); }",
1355 materializeTemporaryExpr()));
1356
1357 EXPECT_TRUE(
1358 notMatches(ClassString +
1359 "string GetStringByValue();"
1360 "void run() { GetStringByValue(); }",
1361 materializeTemporaryExpr()));
1362}
1363
Manuel Klimek4da21662012-07-06 05:48:52 +00001364TEST(ConstructorDeclaration, SimpleCase) {
1365 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001366 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001367 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001368 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001369}
1370
1371TEST(ConstructorDeclaration, IsImplicit) {
1372 // This one doesn't match because the constructor is not added by the
1373 // compiler (it is not needed).
1374 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001375 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001376 // The compiler added the implicit default constructor.
1377 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001378 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001379 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001380 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001381}
1382
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001383TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1384 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001385 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001386}
1387
1388TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001389 EXPECT_TRUE(notMatches("class Foo {};",
1390 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001391}
1392
Manuel Klimek4da21662012-07-06 05:48:52 +00001393TEST(HasAnyConstructorInitializer, SimpleCase) {
1394 EXPECT_TRUE(notMatches(
1395 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001396 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001397 EXPECT_TRUE(matches(
1398 "class Foo {"
1399 " Foo() : foo_() { }"
1400 " int foo_;"
1401 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001402 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001403}
1404
1405TEST(HasAnyConstructorInitializer, ForField) {
1406 static const char Code[] =
1407 "class Baz { };"
1408 "class Foo {"
1409 " Foo() : foo_() { }"
1410 " Baz foo_;"
1411 " Baz bar_;"
1412 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001413 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1414 forField(hasType(recordDecl(hasName("Baz"))))))));
1415 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001416 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001417 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1418 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001419}
1420
1421TEST(HasAnyConstructorInitializer, WithInitializer) {
1422 static const char Code[] =
1423 "class Foo {"
1424 " Foo() : foo_(0) { }"
1425 " int foo_;"
1426 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001427 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001428 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001429 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001430 withInitializer(integerLiteral(equals(1)))))));
1431}
1432
1433TEST(HasAnyConstructorInitializer, IsWritten) {
1434 static const char Code[] =
1435 "struct Bar { Bar(){} };"
1436 "class Foo {"
1437 " Foo() : foo_() { }"
1438 " Bar foo_;"
1439 " Bar bar_;"
1440 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001441 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001442 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001443 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001444 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001445 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001446 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1447}
1448
1449TEST(Matcher, NewExpression) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001450 StatementMatcher New = expr(newExpr());
Manuel Klimek4da21662012-07-06 05:48:52 +00001451
1452 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1453 EXPECT_TRUE(
1454 matches("class X { public: X(); }; void x() { new X(); }", New));
1455 EXPECT_TRUE(
1456 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1457 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1458}
1459
1460TEST(Matcher, NewExpressionArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001461 StatementMatcher New = expr(constructExpr(
1462 hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001463
1464 EXPECT_TRUE(
1465 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1466 New));
1467 EXPECT_TRUE(
1468 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1469 New));
1470 EXPECT_TRUE(
1471 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1472 New));
1473
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001474 StatementMatcher WrongIndex = expr(constructExpr(
1475 hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001476 EXPECT_TRUE(
1477 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1478 WrongIndex));
1479}
1480
1481TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001482 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001483
1484 EXPECT_TRUE(
1485 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1486 EXPECT_TRUE(
1487 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1488 New));
1489}
1490
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001491TEST(Matcher, DeleteExpression) {
1492 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001493 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001494}
1495
Manuel Klimek4da21662012-07-06 05:48:52 +00001496TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001497 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001498
1499 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1500 EXPECT_TRUE(
1501 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1502 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1503}
1504
1505TEST(Matcher, StringLiterals) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001506 StatementMatcher Literal = expr(stringLiteral());
Manuel Klimek4da21662012-07-06 05:48:52 +00001507 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1508 // wide string
1509 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1510 // with escaped characters
1511 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1512 // no matching -- though the data type is the same, there is no string literal
1513 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1514}
1515
1516TEST(Matcher, CharacterLiterals) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001517 StatementMatcher CharLiteral = expr(characterLiteral());
Manuel Klimek4da21662012-07-06 05:48:52 +00001518 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1519 // wide character
1520 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1521 // wide character, Hex encoded, NOT MATCHED!
1522 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1523 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1524}
1525
1526TEST(Matcher, IntegerLiterals) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001527 StatementMatcher HasIntLiteral = expr(integerLiteral());
Manuel Klimek4da21662012-07-06 05:48:52 +00001528 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1529 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1530 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1531 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1532
1533 // Non-matching cases (character literals, float and double)
1534 EXPECT_TRUE(notMatches("int i = L'a';",
1535 HasIntLiteral)); // this is actually a character
1536 // literal cast to int
1537 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1538 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1539 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1540}
1541
1542TEST(Matcher, Conditions) {
1543 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1544
1545 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1546 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1547 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1548 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1549 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1550}
1551
1552TEST(MatchBinaryOperator, HasOperatorName) {
1553 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1554
1555 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1556 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1557}
1558
1559TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1560 StatementMatcher OperatorTrueFalse =
1561 binaryOperator(hasLHS(boolLiteral(equals(true))),
1562 hasRHS(boolLiteral(equals(false))));
1563
1564 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1565 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1566 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1567}
1568
1569TEST(MatchBinaryOperator, HasEitherOperand) {
1570 StatementMatcher HasOperand =
1571 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1572
1573 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1574 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1575 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1576}
1577
1578TEST(Matcher, BinaryOperatorTypes) {
1579 // Integration test that verifies the AST provides all binary operators in
1580 // a way we expect.
1581 // FIXME: Operator ','
1582 EXPECT_TRUE(
1583 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1584 EXPECT_TRUE(
1585 matches("bool b; bool c = (b = true);",
1586 binaryOperator(hasOperatorName("="))));
1587 EXPECT_TRUE(
1588 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1589 EXPECT_TRUE(
1590 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1591 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1592 EXPECT_TRUE(
1593 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1594 EXPECT_TRUE(
1595 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1596 EXPECT_TRUE(
1597 matches("int i = 1; int j = (i <<= 2);",
1598 binaryOperator(hasOperatorName("<<="))));
1599 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1600 EXPECT_TRUE(
1601 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1602 EXPECT_TRUE(
1603 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1604 EXPECT_TRUE(
1605 matches("int i = 1; int j = (i >>= 2);",
1606 binaryOperator(hasOperatorName(">>="))));
1607 EXPECT_TRUE(
1608 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1609 EXPECT_TRUE(
1610 matches("int i = 42; int j = (i ^= 42);",
1611 binaryOperator(hasOperatorName("^="))));
1612 EXPECT_TRUE(
1613 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1614 EXPECT_TRUE(
1615 matches("int i = 42; int j = (i %= 42);",
1616 binaryOperator(hasOperatorName("%="))));
1617 EXPECT_TRUE(
1618 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1619 EXPECT_TRUE(
1620 matches("bool b = true && false;",
1621 binaryOperator(hasOperatorName("&&"))));
1622 EXPECT_TRUE(
1623 matches("bool b = true; bool c = (b &= false);",
1624 binaryOperator(hasOperatorName("&="))));
1625 EXPECT_TRUE(
1626 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1627 EXPECT_TRUE(
1628 matches("bool b = true || false;",
1629 binaryOperator(hasOperatorName("||"))));
1630 EXPECT_TRUE(
1631 matches("bool b = true; bool c = (b |= false);",
1632 binaryOperator(hasOperatorName("|="))));
1633 EXPECT_TRUE(
1634 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1635 EXPECT_TRUE(
1636 matches("int i = 42; int j = (i *= 23);",
1637 binaryOperator(hasOperatorName("*="))));
1638 EXPECT_TRUE(
1639 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1640 EXPECT_TRUE(
1641 matches("int i = 42; int j = (i /= 23);",
1642 binaryOperator(hasOperatorName("/="))));
1643 EXPECT_TRUE(
1644 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1645 EXPECT_TRUE(
1646 matches("int i = 42; int j = (i += 23);",
1647 binaryOperator(hasOperatorName("+="))));
1648 EXPECT_TRUE(
1649 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1650 EXPECT_TRUE(
1651 matches("int i = 42; int j = (i -= 23);",
1652 binaryOperator(hasOperatorName("-="))));
1653 EXPECT_TRUE(
1654 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1655 binaryOperator(hasOperatorName("->*"))));
1656 EXPECT_TRUE(
1657 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1658 binaryOperator(hasOperatorName(".*"))));
1659
1660 // Member expressions as operators are not supported in matches.
1661 EXPECT_TRUE(
1662 notMatches("struct A { void x(A *a) { a->x(this); } };",
1663 binaryOperator(hasOperatorName("->"))));
1664
1665 // Initializer assignments are not represented as operator equals.
1666 EXPECT_TRUE(
1667 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1668
1669 // Array indexing is not represented as operator.
1670 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1671
1672 // Overloaded operators do not match at all.
1673 EXPECT_TRUE(notMatches(
1674 "struct A { bool operator&&(const A &a) const { return false; } };"
1675 "void x() { A a, b; a && b; }",
1676 binaryOperator()));
1677}
1678
1679TEST(MatchUnaryOperator, HasOperatorName) {
1680 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1681
1682 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1683 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1684}
1685
1686TEST(MatchUnaryOperator, HasUnaryOperand) {
1687 StatementMatcher OperatorOnFalse =
1688 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1689
1690 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1691 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1692}
1693
1694TEST(Matcher, UnaryOperatorTypes) {
1695 // Integration test that verifies the AST provides all unary operators in
1696 // a way we expect.
1697 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1698 EXPECT_TRUE(
1699 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1700 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1701 EXPECT_TRUE(
1702 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1703 EXPECT_TRUE(
1704 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1705 EXPECT_TRUE(
1706 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1707 EXPECT_TRUE(
1708 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1709 EXPECT_TRUE(
1710 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1711 EXPECT_TRUE(
1712 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1713 EXPECT_TRUE(
1714 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1715
1716 // We don't match conversion operators.
1717 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1718
1719 // Function calls are not represented as operator.
1720 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1721
1722 // Overloaded operators do not match at all.
1723 // FIXME: We probably want to add that.
1724 EXPECT_TRUE(notMatches(
1725 "struct A { bool operator!() const { return false; } };"
1726 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1727}
1728
1729TEST(Matcher, ConditionalOperator) {
1730 StatementMatcher Conditional = conditionalOperator(
1731 hasCondition(boolLiteral(equals(true))),
1732 hasTrueExpression(boolLiteral(equals(false))));
1733
1734 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1735 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1736 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1737
1738 StatementMatcher ConditionalFalse = conditionalOperator(
1739 hasFalseExpression(boolLiteral(equals(false))));
1740
1741 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1742 EXPECT_TRUE(
1743 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1744}
1745
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001746TEST(ArraySubscriptMatchers, ArraySubscripts) {
1747 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1748 arraySubscriptExpr()));
1749 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1750 arraySubscriptExpr()));
1751}
1752
1753TEST(ArraySubscriptMatchers, ArrayIndex) {
1754 EXPECT_TRUE(matches(
1755 "int i[2]; void f() { i[1] = 1; }",
1756 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1757 EXPECT_TRUE(matches(
1758 "int i[2]; void f() { 1[i] = 1; }",
1759 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1760 EXPECT_TRUE(notMatches(
1761 "int i[2]; void f() { i[1] = 1; }",
1762 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1763}
1764
1765TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1766 EXPECT_TRUE(matches(
1767 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001768 arraySubscriptExpr(hasBase(implicitCastExpr(
1769 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001770}
1771
Manuel Klimek4da21662012-07-06 05:48:52 +00001772TEST(Matcher, HasNameSupportsNamespaces) {
1773 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001774 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001775 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001776 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001777 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001778 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001779 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001780 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001781 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001782 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001783 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001784 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001785 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001786 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001787 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001788 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001789 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001790 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001791 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001792 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001793 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001794 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001795 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001796 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001797}
1798
1799TEST(Matcher, HasNameSupportsOuterClasses) {
1800 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001801 matches("class A { class B { class C; }; };",
1802 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001803 EXPECT_TRUE(
1804 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001805 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001806 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001807 matches("class A { class B { class C; }; };",
1808 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001809 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001810 matches("class A { class B { class C; }; };",
1811 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001812 EXPECT_TRUE(
1813 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001814 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001815 EXPECT_TRUE(
1816 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001817 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001818 EXPECT_TRUE(
1819 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001820 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001821 EXPECT_TRUE(
1822 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001823 recordDecl(hasName("::C"))));
1824 EXPECT_TRUE(
1825 notMatches("class A { class B { class C; }; };",
1826 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001827 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001828 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001829 EXPECT_TRUE(
1830 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001831 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001832}
1833
1834TEST(Matcher, IsDefinition) {
1835 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001836 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001837 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1838 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1839
1840 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001841 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001842 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1843 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1844
1845 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001846 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001847 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1848 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1849}
1850
1851TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001852 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00001853 ofClass(hasName("X")))));
1854
1855 EXPECT_TRUE(
1856 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
1857 EXPECT_TRUE(
1858 matches("class X { public: X(); }; void x(int) { X x = X(); }",
1859 Constructor));
1860 EXPECT_TRUE(
1861 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
1862 Constructor));
1863}
1864
1865TEST(Matcher, VisitsTemplateInstantiations) {
1866 EXPECT_TRUE(matches(
1867 "class A { public: void x(); };"
1868 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001869 "void f() { B<A> b; b.y(); }",
1870 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001871
1872 EXPECT_TRUE(matches(
1873 "class A { public: void x(); };"
1874 "class C {"
1875 " public:"
1876 " template <typename T> class B { public: void y() { T t; t.x(); } };"
1877 "};"
1878 "void f() {"
1879 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001880 "}",
1881 recordDecl(hasName("C"),
1882 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001883}
1884
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001885TEST(Matcher, HandlesNullQualTypes) {
1886 // FIXME: Add a Type matcher so we can replace uses of this
1887 // variable with Type(True())
1888 const TypeMatcher AnyType = anything();
1889
1890 // We don't really care whether this matcher succeeds; we're testing that
1891 // it completes without crashing.
1892 EXPECT_TRUE(matches(
1893 "struct A { };"
1894 "template <typename T>"
1895 "void f(T t) {"
1896 " T local_t(t /* this becomes a null QualType in the AST */);"
1897 "}"
1898 "void g() {"
1899 " f(0);"
1900 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001901 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001902 anyOf(
1903 TypeMatcher(hasDeclaration(anything())),
1904 pointsTo(AnyType),
1905 references(AnyType)
1906 // Other QualType matchers should go here.
1907 ))))));
1908}
1909
Manuel Klimek4da21662012-07-06 05:48:52 +00001910// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001911AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00001912 // Make sure all special variables are used: node, match_finder,
1913 // bound_nodes_builder, and the parameter named 'AMatcher'.
1914 return AMatcher.matches(Node, Finder, Builder);
1915}
1916
1917TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001918 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001919
1920 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001921 HasClassB, new VerifyIdIsBoundToDecl<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001922
1923 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001924 HasClassB, new VerifyIdIsBoundToDecl<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001925
1926 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001927 HasClassB, new VerifyIdIsBoundToDecl<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001928}
1929
1930AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001931 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
1932 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
1933 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00001934 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00001935 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00001936 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00001937 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
1938 ASTMatchFinder::BK_First);
1939}
1940
1941TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001942 DeclarationMatcher HasClassB =
1943 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001944
1945 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001946 HasClassB, new VerifyIdIsBoundToDecl<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001947
1948 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001949 HasClassB, new VerifyIdIsBoundToDecl<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001950
1951 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001952 HasClassB, new VerifyIdIsBoundToDecl<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001953
1954 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001955 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001956
1957 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
1958}
1959
1960TEST(For, FindsForLoops) {
1961 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
1962 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
1963}
1964
Daniel Jasper6a124492012-07-12 08:50:38 +00001965TEST(For, ForLoopInternals) {
1966 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
1967 forStmt(hasCondition(anything()))));
1968 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
1969 forStmt(hasLoopInit(anything()))));
1970}
1971
1972TEST(For, NegativeForLoopInternals) {
1973 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001974 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001975 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
1976 forStmt(hasLoopInit(anything()))));
1977}
1978
Manuel Klimek4da21662012-07-06 05:48:52 +00001979TEST(For, ReportsNoFalsePositives) {
1980 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
1981 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
1982}
1983
1984TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001985 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
1986 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
1987 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001988}
1989
1990TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
1991 // It's not a compound statement just because there's "{}" in the source
1992 // text. This is an AST search, not grep.
1993 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001994 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001995 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001996 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001997}
1998
Daniel Jasper6a124492012-07-12 08:50:38 +00001999TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002000 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002001 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002002 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002003 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002004 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002005 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002006 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002007 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002008}
2009
2010TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2011 // The simplest case: every compound statement is in a function
2012 // definition, and the function body itself must be a compound
2013 // statement.
2014 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002015 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002016}
2017
2018TEST(HasAnySubstatement, IsNotRecursive) {
2019 // It's really "has any immediate substatement".
2020 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002021 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002022}
2023
2024TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2025 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002026 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002027}
2028
2029TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2030 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002031 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002032}
2033
2034TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2035 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002036 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002037 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002038 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002039}
2040
2041TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2042 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002043 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002044 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002045 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002046 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002047 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002048}
2049
2050TEST(StatementCountIs, WorksWithMultipleStatements) {
2051 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002052 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002053}
2054
2055TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2056 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002057 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002058 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002059 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002060 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002061 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002062 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002063 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002064}
2065
2066TEST(Member, WorksInSimplestCase) {
2067 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002068 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002069}
2070
2071TEST(Member, DoesNotMatchTheBaseExpression) {
2072 // Don't pick out the wrong part of the member expression, this should
2073 // be checking the member (name) only.
2074 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002075 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002076}
2077
2078TEST(Member, MatchesInMemberFunctionCall) {
2079 EXPECT_TRUE(matches("void f() {"
2080 " struct { void first() {}; } s;"
2081 " s.first();"
2082 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002083 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002084}
2085
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002086TEST(Member, MatchesMemberAllocationFunction) {
Dmitri Gribenko02ed37f2012-08-18 00:41:04 +00002087 EXPECT_TRUE(matches("namespace std { typedef typeof(sizeof(int)) size_t; }"
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002088 "class X { void *operator new(std::size_t); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002089 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002090
2091 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002092 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002093
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002094 EXPECT_TRUE(matches(
2095 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2096 "class X { void operator delete[](void*, std::size_t); };",
2097 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002098}
2099
Manuel Klimek4da21662012-07-06 05:48:52 +00002100TEST(HasObjectExpression, DoesNotMatchMember) {
2101 EXPECT_TRUE(notMatches(
2102 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002103 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002104}
2105
2106TEST(HasObjectExpression, MatchesBaseOfVariable) {
2107 EXPECT_TRUE(matches(
2108 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002109 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002110 EXPECT_TRUE(matches(
2111 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002112 memberExpr(hasObjectExpression(
2113 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002114}
2115
2116TEST(HasObjectExpression,
2117 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2118 EXPECT_TRUE(matches(
2119 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002120 memberExpr(hasObjectExpression(
2121 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002122 EXPECT_TRUE(matches(
2123 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002124 memberExpr(hasObjectExpression(
2125 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002126}
2127
2128TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002129 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2130 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2131 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2132 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002133}
2134
2135TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002136 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002137}
2138
2139TEST(IsConstQualified, MatchesConstInt) {
2140 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002141 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002142}
2143
2144TEST(IsConstQualified, MatchesConstPointer) {
2145 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002146 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002147}
2148
2149TEST(IsConstQualified, MatchesThroughTypedef) {
2150 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002151 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002152 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002153 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002154}
2155
2156TEST(IsConstQualified, DoesNotMatchInappropriately) {
2157 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002158 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002159 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002160 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002161}
2162
Sam Panzer089e5b32012-08-16 16:58:10 +00002163TEST(CastExpression, MatchesExplicitCasts) {
2164 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002165 expr(castExpr())));
2166 EXPECT_TRUE(matches("void *p = (void *)(&p);", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002167 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002168 expr(castExpr())));
2169 EXPECT_TRUE(matches("char c = char(0);", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002170}
2171TEST(CastExpression, MatchesImplicitCasts) {
2172 // This test creates an implicit cast from int to char.
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002173 EXPECT_TRUE(matches("char c = 0;", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002174 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002175 EXPECT_TRUE(matches("char c = 0, d = c;", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002176}
2177
2178TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002179 EXPECT_TRUE(notMatches("char c = '0';", expr(castExpr())));
2180 EXPECT_TRUE(notMatches("char c, &q = c;", expr(castExpr())));
2181 EXPECT_TRUE(notMatches("int i = (0);", expr(castExpr())));
2182 EXPECT_TRUE(notMatches("int i = 0;", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002183}
2184
Manuel Klimek4da21662012-07-06 05:48:52 +00002185TEST(ReinterpretCast, MatchesSimpleCase) {
2186 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002187 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002188}
2189
2190TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
2191 EXPECT_TRUE(notMatches("char* p = (char*)(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002192 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002193 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002194 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002195 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002196 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002197 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2198 "B b;"
2199 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002200 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002201}
2202
2203TEST(FunctionalCast, MatchesSimpleCase) {
2204 std::string foo_class = "class Foo { public: Foo(char*); };";
2205 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002206 expr(functionalCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002207}
2208
2209TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2210 std::string FooClass = "class Foo { public: Foo(char*); };";
2211 EXPECT_TRUE(
2212 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002213 expr(functionalCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002214 EXPECT_TRUE(
2215 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002216 expr(functionalCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002217}
2218
2219TEST(DynamicCast, MatchesSimpleCase) {
2220 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2221 "B b;"
2222 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002223 expr(dynamicCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002224}
2225
2226TEST(StaticCast, MatchesSimpleCase) {
2227 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002228 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002229}
2230
2231TEST(StaticCast, DoesNotMatchOtherCasts) {
2232 EXPECT_TRUE(notMatches("char* p = (char*)(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002233 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002234 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002235 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002236 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002237 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002238 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2239 "B b;"
2240 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002241 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002242}
2243
2244TEST(HasDestinationType, MatchesSimpleCase) {
2245 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002246 expr(staticCastExpr(hasDestinationType(
2247 pointsTo(TypeMatcher(anything())))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002248}
2249
Sam Panzer089e5b32012-08-16 16:58:10 +00002250TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2251 // This test creates an implicit const cast.
2252 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002253 expr(implicitCastExpr(
Sam Panzer089e5b32012-08-16 16:58:10 +00002254 hasImplicitDestinationType(isInteger())))));
2255 // This test creates an implicit array-to-pointer cast.
2256 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002257 expr(implicitCastExpr(hasImplicitDestinationType(
Sam Panzer089e5b32012-08-16 16:58:10 +00002258 pointsTo(TypeMatcher(anything())))))));
2259}
2260
2261TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2262 // This test creates an implicit cast from int to char.
2263 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002264 expr(implicitCastExpr(hasImplicitDestinationType(
Sam Panzer089e5b32012-08-16 16:58:10 +00002265 unless(anything()))))));
2266 // This test creates an implicit array-to-pointer cast.
2267 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002268 expr(implicitCastExpr(hasImplicitDestinationType(
Sam Panzer089e5b32012-08-16 16:58:10 +00002269 unless(anything()))))));
2270}
2271
2272TEST(ImplicitCast, MatchesSimpleCase) {
2273 // This test creates an implicit const cast.
2274 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002275 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002276 // This test creates an implicit cast from int to char.
2277 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002278 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002279 // This test creates an implicit array-to-pointer cast.
2280 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002281 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002282}
2283
2284TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002285 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002286 // are present, and that it ignores explicit and paren casts.
2287
2288 // These two test cases have no casts.
2289 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002290 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002291 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002292 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002293
2294 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002295 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002296 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002297 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002298
2299 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002300 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002301}
2302
2303TEST(IgnoringImpCasts, MatchesImpCasts) {
2304 // This test checks that ignoringImpCasts matches when implicit casts are
2305 // present and its inner matcher alone does not match.
2306 // Note that this test creates an implicit const cast.
2307 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002308 varDecl(hasInitializer(ignoringImpCasts(
2309 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002310 // This test creates an implict cast from int to char.
2311 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002312 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002313 integerLiteral(equals(0)))))));
2314}
2315
2316TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2317 // These tests verify that ignoringImpCasts does not match if the inner
2318 // matcher does not match.
2319 // Note that the first test creates an implicit const cast.
2320 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002321 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002322 unless(anything()))))));
2323 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002324 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002325 unless(anything()))))));
2326
2327 // These tests verify that ignoringImplictCasts does not look through explicit
2328 // casts or parentheses.
2329 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002330 varDecl(hasInitializer(ignoringImpCasts(
2331 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002332 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002333 varDecl(hasInitializer(ignoringImpCasts(
2334 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002335 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002336 varDecl(hasInitializer(ignoringImpCasts(
2337 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002338 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002339 varDecl(hasInitializer(ignoringImpCasts(
2340 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002341}
2342
2343TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2344 // This test verifies that expressions that do not have implicit casts
2345 // still match the inner matcher.
2346 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002347 varDecl(hasInitializer(ignoringImpCasts(
2348 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002349}
2350
2351TEST(IgnoringParenCasts, MatchesParenCasts) {
2352 // This test checks that ignoringParenCasts matches when parentheses and/or
2353 // casts are present and its inner matcher alone does not match.
2354 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002355 varDecl(hasInitializer(ignoringParenCasts(
2356 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002357 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002358 varDecl(hasInitializer(ignoringParenCasts(
2359 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002360
2361 // This test creates an implict cast from int to char in addition to the
2362 // parentheses.
2363 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002364 varDecl(hasInitializer(ignoringParenCasts(
2365 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002366
2367 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002368 varDecl(hasInitializer(ignoringParenCasts(
2369 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002370 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002371 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002372 integerLiteral(equals(0)))))));
2373}
2374
2375TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2376 // This test verifies that expressions that do not have any casts still match.
2377 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002378 varDecl(hasInitializer(ignoringParenCasts(
2379 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002380}
2381
2382TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2383 // These tests verify that ignoringImpCasts does not match if the inner
2384 // matcher does not match.
2385 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002386 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002387 unless(anything()))))));
2388
2389 // This test creates an implicit cast from int to char in addition to the
2390 // parentheses.
2391 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002392 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002393 unless(anything()))))));
2394
2395 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002396 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002397 unless(anything()))))));
2398}
2399
2400TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2401 // This test checks that ignoringParenAndImpCasts matches when
2402 // parentheses and/or implicit casts are present and its inner matcher alone
2403 // does not match.
2404 // Note that this test creates an implicit const cast.
2405 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002406 varDecl(hasInitializer(ignoringParenImpCasts(
2407 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002408 // This test creates an implicit cast from int to char.
2409 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002410 varDecl(hasInitializer(ignoringParenImpCasts(
2411 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002412}
2413
2414TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2415 // This test verifies that expressions that do not have parentheses or
2416 // implicit casts still match.
2417 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002418 varDecl(hasInitializer(ignoringParenImpCasts(
2419 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002420 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002421 varDecl(hasInitializer(ignoringParenImpCasts(
2422 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002423}
2424
2425TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2426 // These tests verify that ignoringParenImpCasts does not match if
2427 // the inner matcher does not match.
2428 // This test creates an implicit cast.
2429 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002430 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002431 unless(anything()))))));
2432 // These tests verify that ignoringParenAndImplictCasts does not look
2433 // through explicit casts.
2434 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002435 varDecl(hasInitializer(ignoringParenImpCasts(
2436 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002437 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002438 varDecl(hasInitializer(ignoringParenImpCasts(
2439 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002440 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002441 varDecl(hasInitializer(ignoringParenImpCasts(
2442 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002443}
2444
Manuel Klimek715c9562012-07-25 10:02:02 +00002445TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002446 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2447 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002448 expr(implicitCastExpr(
2449 hasSourceExpression(constructExpr())))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002450}
2451
Manuel Klimek715c9562012-07-25 10:02:02 +00002452TEST(HasSourceExpression, MatchesExplicitCasts) {
2453 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002454 expr(explicitCastExpr(
2455 hasSourceExpression(hasDescendant(
2456 expr(integerLiteral())))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002457}
2458
Manuel Klimek4da21662012-07-06 05:48:52 +00002459TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002460 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002461}
2462
2463TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002464 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002465}
2466
2467TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002468 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002469}
2470
2471TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002472 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002473}
2474
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002475TEST(InitListExpression, MatchesInitListExpression) {
2476 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2477 initListExpr(hasType(asString("int [2]")))));
2478 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002479 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002480}
2481
2482TEST(UsingDeclaration, MatchesUsingDeclarations) {
2483 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2484 usingDecl()));
2485}
2486
2487TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2488 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2489 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2490}
2491
2492TEST(UsingDeclaration, MatchesSpecificTarget) {
2493 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2494 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002495 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002496 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2497 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002498 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002499}
2500
2501TEST(UsingDeclaration, ThroughUsingDeclaration) {
2502 EXPECT_TRUE(matches(
2503 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002504 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002505 EXPECT_TRUE(notMatches(
2506 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002507 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002508}
2509
Sam Panzer425f41b2012-08-16 17:20:59 +00002510TEST(SingleDecl, IsSingleDecl) {
2511 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002512 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002513 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2514 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2515 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2516 SingleDeclStmt));
2517}
2518
2519TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002520 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002521
2522 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002523 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002524 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002525 declStmt(containsDeclaration(0, MatchesInit),
2526 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002527 unsigned WrongIndex = 42;
2528 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002529 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002530 MatchesInit))));
2531}
2532
2533TEST(DeclCount, DeclCountIsCorrect) {
2534 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002535 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002536 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002537 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002538 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002539 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002540}
2541
Manuel Klimek4da21662012-07-06 05:48:52 +00002542TEST(While, MatchesWhileLoops) {
2543 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2544 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2545 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2546}
2547
2548TEST(Do, MatchesDoLoops) {
2549 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2550 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2551}
2552
2553TEST(Do, DoesNotMatchWhileLoops) {
2554 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2555}
2556
2557TEST(SwitchCase, MatchesCase) {
2558 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2559 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2560 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2561 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2562}
2563
2564TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2565 EXPECT_TRUE(notMatches(
2566 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002567 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002568 EXPECT_TRUE(notMatches(
2569 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002570 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002571}
2572
2573TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2574 EXPECT_TRUE(matches(
2575 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002576 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002577}
2578
2579TEST(ForEach, BindsOneNode) {
2580 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002581 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002582 new VerifyIdIsBoundToDecl<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002583}
2584
2585TEST(ForEach, BindsMultipleNodes) {
2586 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002587 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002588 new VerifyIdIsBoundToDecl<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002589}
2590
2591TEST(ForEach, BindsRecursiveCombinations) {
2592 EXPECT_TRUE(matchAndVerifyResultTrue(
2593 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002594 recordDecl(hasName("C"),
2595 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002596 new VerifyIdIsBoundToDecl<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002597}
2598
2599TEST(ForEachDescendant, BindsOneNode) {
2600 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002601 recordDecl(hasName("C"),
2602 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002603 new VerifyIdIsBoundToDecl<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002604}
2605
2606TEST(ForEachDescendant, BindsMultipleNodes) {
2607 EXPECT_TRUE(matchAndVerifyResultTrue(
2608 "class C { class D { int x; int y; }; "
2609 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002610 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002611 new VerifyIdIsBoundToDecl<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002612}
2613
2614TEST(ForEachDescendant, BindsRecursiveCombinations) {
2615 EXPECT_TRUE(matchAndVerifyResultTrue(
2616 "class C { class D { "
2617 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002618 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2619 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002620 new VerifyIdIsBoundToDecl<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002621}
2622
2623
2624TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2625 // Make sure that we can both match the class by name (::X) and by the type
2626 // the template was instantiated with (via a field).
2627
2628 EXPECT_TRUE(matches(
2629 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002630 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002631
2632 EXPECT_TRUE(matches(
2633 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002634 recordDecl(isTemplateInstantiation(), hasDescendant(
2635 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002636}
2637
2638TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2639 EXPECT_TRUE(matches(
2640 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002641 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002642 isTemplateInstantiation())));
2643}
2644
2645TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2646 EXPECT_TRUE(matches(
2647 "template <typename T> class X { T t; }; class A {};"
2648 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002649 recordDecl(isTemplateInstantiation(), hasDescendant(
2650 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002651}
2652
2653TEST(IsTemplateInstantiation,
2654 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2655 EXPECT_TRUE(matches(
2656 "template <typename T> class X {};"
2657 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002658 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002659}
2660
2661TEST(IsTemplateInstantiation,
2662 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2663 EXPECT_TRUE(matches(
2664 "class A {};"
2665 "class X {"
2666 " template <typename U> class Y { U u; };"
2667 " Y<A> y;"
2668 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002669 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002670}
2671
2672TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2673 // FIXME: Figure out whether this makes sense. It doesn't affect the
2674 // normal use case as long as the uppermost instantiation always is marked
2675 // as template instantiation, but it might be confusing as a predicate.
2676 EXPECT_TRUE(matches(
2677 "class A {};"
2678 "template <typename T> class X {"
2679 " template <typename U> class Y { U u; };"
2680 " Y<T> y;"
2681 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002682 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002683}
2684
2685TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2686 EXPECT_TRUE(notMatches(
2687 "template <typename T> class X {}; class A {};"
2688 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002689 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002690}
2691
2692TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2693 EXPECT_TRUE(notMatches(
2694 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002695 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002696}
2697
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002698TEST(IsExplicitTemplateSpecialization,
2699 DoesNotMatchPrimaryTemplate) {
2700 EXPECT_TRUE(notMatches(
2701 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002702 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002703 EXPECT_TRUE(notMatches(
2704 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002705 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002706}
2707
2708TEST(IsExplicitTemplateSpecialization,
2709 DoesNotMatchExplicitTemplateInstantiations) {
2710 EXPECT_TRUE(notMatches(
2711 "template <typename T> class X {};"
2712 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002713 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002714 EXPECT_TRUE(notMatches(
2715 "template <typename T> void f(T t) {}"
2716 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002717 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002718}
2719
2720TEST(IsExplicitTemplateSpecialization,
2721 DoesNotMatchImplicitTemplateInstantiations) {
2722 EXPECT_TRUE(notMatches(
2723 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002724 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002725 EXPECT_TRUE(notMatches(
2726 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002727 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002728}
2729
2730TEST(IsExplicitTemplateSpecialization,
2731 MatchesExplicitTemplateSpecializations) {
2732 EXPECT_TRUE(matches(
2733 "template <typename T> class X {};"
2734 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002735 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002736 EXPECT_TRUE(matches(
2737 "template <typename T> void f(T t) {}"
2738 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002739 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002740}
2741
Manuel Klimek579b1202012-09-07 09:26:10 +00002742TEST(HasAncenstor, MatchesDeclarationAncestors) {
2743 EXPECT_TRUE(matches(
2744 "class A { class B { class C {}; }; };",
2745 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2746}
2747
2748TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2749 EXPECT_TRUE(notMatches(
2750 "class A { class B { class C {}; }; };",
2751 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2752}
2753
2754TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2755 EXPECT_TRUE(matches(
2756 "class A { class B { void f() { C c; } class C {}; }; };",
2757 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2758 hasAncestor(recordDecl(hasName("A"))))))));
2759}
2760
2761TEST(HasAncenstor, MatchesStatementAncestors) {
2762 EXPECT_TRUE(matches(
2763 "void f() { if (true) { while (false) { 42; } } }",
2764 expr(integerLiteral(equals(42), hasAncestor(ifStmt())))));
2765}
2766
2767TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2768 EXPECT_TRUE(matches(
2769 "void f() { if (true) { int x = 42; } }",
2770 expr(integerLiteral(
2771 equals(42), hasAncestor(functionDecl(hasName("f")))))));
2772}
2773
2774TEST(HasAncestor, BindsRecursiveCombinations) {
2775 EXPECT_TRUE(matchAndVerifyResultTrue(
2776 "class C { class D { class E { class F { int y; }; }; }; };",
2777 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
2778 new VerifyIdIsBoundToDecl<CXXRecordDecl>("r", 1)));
2779}
2780
2781TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
2782 EXPECT_TRUE(matchAndVerifyResultTrue(
2783 "class C { class D { class E { class F { int y; }; }; }; };",
2784 fieldDecl(hasAncestor(
2785 decl(
2786 hasDescendant(recordDecl(isDefinition(),
2787 hasAncestor(recordDecl())))
2788 ).bind("d")
2789 )),
2790 new VerifyIdIsBoundToDecl<CXXRecordDecl>("d", "E")));
2791}
2792
2793TEST(HasAncestor, MatchesInTemplateInstantiations) {
2794 EXPECT_TRUE(matches(
2795 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
2796 "A<int>::B::C a;",
2797 fieldDecl(hasType(asString("int")),
2798 hasAncestor(recordDecl(hasName("A"))))));
2799}
2800
2801TEST(HasAncestor, MatchesInImplicitCode) {
2802 EXPECT_TRUE(matches(
2803 "struct X {}; struct A { A() {} X x; };",
2804 constructorDecl(
2805 hasAnyConstructorInitializer(withInitializer(expr(
2806 hasAncestor(recordDecl(hasName("A")))))))));
2807}
2808
Manuel Klimek4da21662012-07-06 05:48:52 +00002809} // end namespace ast_matchers
2810} // end namespace clang