blob: c6e161da930cc63ef7d1eea2be628a4d193f4169 [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 Jasper63d88722012-09-12 21:14:15 +0000109 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000110
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>
Daniel Jaspera7564432012-09-13 13:11:25 +0000610class VerifyIdIsBoundTo : public BoundNodesCallback {
Manuel Klimek4da21662012-07-06 05:48:52 +0000611public:
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.
Daniel Jaspera7564432012-09-13 13:11:25 +0000614 explicit VerifyIdIsBoundTo(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.
Daniel Jaspera7564432012-09-13 13:11:25 +0000619 VerifyIdIsBoundTo(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.
Daniel Jaspera7564432012-09-13 13:11:25 +0000623 // Checks that there was exactly one match with the name \c ExpectedName.
Manuel Klimek579b1202012-09-07 09:26:10 +0000624 // Note that \c T must be a NamedDecl for this to work.
Daniel Jaspera7564432012-09-13 13:11:25 +0000625 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName)
626 : Id(Id), ExpectedCount(1), Count(0), ExpectedName(ExpectedName) {}
Manuel Klimek579b1202012-09-07 09:26:10 +0000627
Daniel Jaspera7564432012-09-13 13:11:25 +0000628 ~VerifyIdIsBoundTo() {
Manuel Klimek579b1202012-09-07 09:26:10 +0000629 if (ExpectedCount != -1)
Manuel Klimek4da21662012-07-06 05:48:52 +0000630 EXPECT_EQ(ExpectedCount, Count);
Daniel Jaspera7564432012-09-13 13:11:25 +0000631 if (!ExpectedName.empty())
632 EXPECT_EQ(ExpectedName, Name);
Manuel Klimek4da21662012-07-06 05:48:52 +0000633 }
634
635 virtual bool run(const BoundNodes *Nodes) {
Daniel Jaspera7564432012-09-13 13:11:25 +0000636 if (Nodes->getNodeAs<T>(Id)) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000637 ++Count;
Daniel Jaspera7564432012-09-13 13:11:25 +0000638 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
639 Name = Named->getNameAsString();
640 } else if (const NestedNameSpecifier *NNS =
641 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
642 llvm::raw_string_ostream OS(Name);
643 NNS->print(OS, PrintingPolicy(LangOptions()));
Manuel Klimek579b1202012-09-07 09:26:10 +0000644 }
Manuel Klimek4da21662012-07-06 05:48:52 +0000645 return true;
646 }
647 return false;
648 }
649
650private:
651 const std::string Id;
652 const int ExpectedCount;
653 int Count;
Daniel Jaspera7564432012-09-13 13:11:25 +0000654 const std::string ExpectedName;
655 std::string Name;
Manuel Klimek4da21662012-07-06 05:48:52 +0000656};
657
658TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000659 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000660
661 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000662 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000663
664 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000665 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000666
667 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000668 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000669
670 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
671 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000672 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000673
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000674 StatementMatcher MethodX =
675 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000676
677 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
678 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000679 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000680}
681
682TEST(Matcher, BindTheSameNameInAlternatives) {
683 StatementMatcher matcher = anyOf(
684 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000685 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000686 hasRHS(integerLiteral(equals(0)))),
687 binaryOperator(hasOperatorName("+"),
688 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000689 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000690
691 EXPECT_TRUE(matchAndVerifyResultTrue(
692 // The first branch of the matcher binds x to 0 but then fails.
693 // The second branch binds x to f() and succeeds.
694 "int f() { return 0 + f(); }",
695 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000696 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000697}
698
Manuel Klimek66341c52012-08-30 19:41:06 +0000699TEST(Matcher, BindsIDForMemoizedResults) {
700 // Using the same matcher in two match expressions will make memoization
701 // kick in.
702 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
703 EXPECT_TRUE(matchAndVerifyResultTrue(
704 "class A { class B { class X {}; }; };",
705 DeclarationMatcher(anyOf(
706 recordDecl(hasName("A"), hasDescendant(ClassX)),
707 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000708 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000709}
710
Manuel Klimek4da21662012-07-06 05:48:52 +0000711TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000712 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000713 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000714 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000715 EXPECT_TRUE(
716 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000717 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000718 EXPECT_TRUE(
719 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000720 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000721}
722
723TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000724 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000725 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000726 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000727 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000728 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000729 EXPECT_TRUE(
730 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000731 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000732}
733
734TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000735 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000736 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000737 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000738 EXPECT_TRUE(
739 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000740 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000741}
742
743TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000744 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000745 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000746 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000747 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000748 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000749}
750
751TEST(Matcher, Call) {
752 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000753 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000754 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000755
756 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
757 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
758
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000759 StatementMatcher MethodOnY =
760 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000761
762 EXPECT_TRUE(
763 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
764 MethodOnY));
765 EXPECT_TRUE(
766 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
767 MethodOnY));
768 EXPECT_TRUE(
769 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
770 MethodOnY));
771 EXPECT_TRUE(
772 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
773 MethodOnY));
774 EXPECT_TRUE(
775 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
776 MethodOnY));
777
778 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000779 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000780
781 EXPECT_TRUE(
782 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
783 MethodOnYPointer));
784 EXPECT_TRUE(
785 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
786 MethodOnYPointer));
787 EXPECT_TRUE(
788 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
789 MethodOnYPointer));
790 EXPECT_TRUE(
791 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
792 MethodOnYPointer));
793 EXPECT_TRUE(
794 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
795 MethodOnYPointer));
796}
797
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000798TEST(HasType, MatchesAsString) {
799 EXPECT_TRUE(
800 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000801 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000802 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000803 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000804 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000805 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000806 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000807 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000808}
809
Manuel Klimek4da21662012-07-06 05:48:52 +0000810TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000811 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000812 // Unary operator
813 EXPECT_TRUE(matches("class Y { }; "
814 "bool operator!(Y x) { return false; }; "
815 "Y y; bool c = !y;", OpCall));
816 // No match -- special operators like "new", "delete"
817 // FIXME: operator new takes size_t, for which we need stddef.h, for which
818 // we need to figure out include paths in the test.
819 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
820 // "class Y { }; "
821 // "void *operator new(size_t size) { return 0; } "
822 // "Y *y = new Y;", OpCall));
823 EXPECT_TRUE(notMatches("class Y { }; "
824 "void operator delete(void *p) { } "
825 "void a() {Y *y = new Y; delete y;}", OpCall));
826 // Binary operator
827 EXPECT_TRUE(matches("class Y { }; "
828 "bool operator&&(Y x, Y y) { return true; }; "
829 "Y a; Y b; bool c = a && b;",
830 OpCall));
831 // No match -- normal operator, not an overloaded one.
832 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
833 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
834}
835
836TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
837 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000838 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000839 EXPECT_TRUE(matches("class Y { }; "
840 "bool operator&&(Y x, Y y) { return true; }; "
841 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
842 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000843 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000844 EXPECT_TRUE(notMatches("class Y { }; "
845 "bool operator&&(Y x, Y y) { return true; }; "
846 "Y a; Y b; bool c = a && b;",
847 OpCallLessLess));
848}
849
850TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +0000851 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000852 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000853
854 EXPECT_TRUE(
855 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
856 MethodOnY));
857 EXPECT_TRUE(
858 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
859 MethodOnY));
860 EXPECT_TRUE(
861 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
862 MethodOnY));
863 EXPECT_TRUE(
864 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
865 MethodOnY));
866 EXPECT_TRUE(
867 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
868 MethodOnY));
869
870 EXPECT_TRUE(matches(
871 "class Y {"
872 " public: virtual void x();"
873 "};"
874 "class X : public Y {"
875 " public: virtual void x();"
876 "};"
877 "void z() { X *x; x->Y::x(); }", MethodOnY));
878}
879
880TEST(Matcher, VariableUsage) {
881 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000882 declRefExpr(to(
883 varDecl(hasInitializer(
884 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000885
886 EXPECT_TRUE(matches(
887 "class Y {"
888 " public:"
889 " bool x() const;"
890 "};"
891 "void z(const Y &y) {"
892 " bool b = y.x();"
893 " if (b) {}"
894 "}", Reference));
895
896 EXPECT_TRUE(notMatches(
897 "class Y {"
898 " public:"
899 " bool x() const;"
900 "};"
901 "void z(const Y &y) {"
902 " bool b = y.x();"
903 "}", Reference));
904}
905
Daniel Jasper9bd28092012-07-30 05:03:25 +0000906TEST(Matcher, FindsVarDeclInFuncitonParameter) {
907 EXPECT_TRUE(matches(
908 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000909 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +0000910}
911
Manuel Klimek4da21662012-07-06 05:48:52 +0000912TEST(Matcher, CalledVariable) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000913 StatementMatcher CallOnVariableY = expr(
914 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000915
916 EXPECT_TRUE(matches(
917 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
918 EXPECT_TRUE(matches(
919 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
920 EXPECT_TRUE(matches(
921 "class Y { public: void x(); };"
922 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
923 EXPECT_TRUE(matches(
924 "class Y { public: void x(); };"
925 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
926 EXPECT_TRUE(notMatches(
927 "class Y { public: void x(); };"
928 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
929 CallOnVariableY));
930}
931
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000932TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
933 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
934 unaryExprOrTypeTraitExpr()));
935 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
936 alignOfExpr(anything())));
937 // FIXME: Uncomment once alignof is enabled.
938 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
939 // unaryExprOrTypeTraitExpr()));
940 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
941 // sizeOfExpr()));
942}
943
944TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
945 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
946 hasArgumentOfType(asString("int")))));
947 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
948 hasArgumentOfType(asString("float")))));
949 EXPECT_TRUE(matches(
950 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000951 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000952 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000953 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000954}
955
Manuel Klimek4da21662012-07-06 05:48:52 +0000956TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000957 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000958}
959
960TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000961 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000962}
963
964TEST(MemberExpression, MatchesVariable) {
965 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000966 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000967 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000968 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000969 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000970 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000971}
972
973TEST(MemberExpression, MatchesStaticVariable) {
974 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000975 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000976 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000977 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000978 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000979 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000980}
981
Daniel Jasper6a124492012-07-12 08:50:38 +0000982TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000983 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
984 EXPECT_TRUE(matches(
985 "long long i = 0; void f(long long) { }; void g() {f(i);}",
986 callExpr(hasArgument(0, declRefExpr(
987 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000988}
989
990TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000991 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000992 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000993 callExpr(hasArgument(0, declRefExpr(
994 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000995}
996
Manuel Klimek4da21662012-07-06 05:48:52 +0000997TEST(IsArrow, MatchesMemberVariablesViaArrow) {
998 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000999 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001000 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001001 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001002 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001003 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001004}
1005
1006TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1007 EXPECT_TRUE(matches("class Y { void x() { this->y; } static 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() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001010 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001011 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001012 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001013}
1014
1015TEST(IsArrow, MatchesMemberCallsViaArrow) {
1016 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001017 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001018 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001019 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001020 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001021 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001022}
1023
1024TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001025 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001026
1027 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1028 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1029}
1030
1031TEST(Callee, MatchesMemberExpressions) {
1032 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001033 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001034 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001035 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001036}
1037
1038TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001039 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001040
1041 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1042 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1043
Manuel Klimeke265c872012-07-10 14:21:30 +00001044#if !defined(_MSC_VER)
1045 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001046 // Dependent contexts, but a non-dependent call.
1047 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1048 CallFunctionF));
1049 EXPECT_TRUE(
1050 matches("void f(); template <int N> struct S { void g() { f(); } };",
1051 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001052#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001053
1054 // Depedent calls don't match.
1055 EXPECT_TRUE(
1056 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1057 CallFunctionF));
1058 EXPECT_TRUE(
1059 notMatches("void f(int);"
1060 "template <typename T> struct S { void g(T t) { f(t); } };",
1061 CallFunctionF));
1062}
1063
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001064TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1065 EXPECT_TRUE(
1066 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001067 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001068}
1069
1070TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1071 EXPECT_TRUE(
1072 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001073 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001074}
1075
1076TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1077 EXPECT_TRUE(
1078 notMatches("void g(); template <typename T> void f(T t) {}"
1079 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001080 functionTemplateDecl(hasName("f"),
1081 hasDescendant(declRefExpr(to(
1082 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001083}
1084
Manuel Klimek4da21662012-07-06 05:48:52 +00001085TEST(Matcher, Argument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001086 StatementMatcher CallArgumentY = expr(callExpr(
1087 hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001088
1089 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1090 EXPECT_TRUE(
1091 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1092 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1093
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001094 StatementMatcher WrongIndex = expr(callExpr(
1095 hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001096 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1097}
1098
1099TEST(Matcher, AnyArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001100 StatementMatcher CallArgumentY = expr(callExpr(
1101 hasAnyArgument(declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001102 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1103 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1104 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1105}
1106
1107TEST(Matcher, ArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001108 StatementMatcher Call1Arg = expr(callExpr(argumentCountIs(1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00001109
1110 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1111 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1112 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1113}
1114
1115TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001116 DeclarationMatcher ReferenceClassX = varDecl(
1117 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001118 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1119 ReferenceClassX));
1120 EXPECT_TRUE(
1121 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1122 EXPECT_TRUE(
1123 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1124 EXPECT_TRUE(
1125 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1126}
1127
1128TEST(HasParameter, CallsInnerMatcher) {
1129 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001130 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001131 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001132 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001133}
1134
1135TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1136 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001137 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001138}
1139
1140TEST(HasType, MatchesParameterVariableTypesStrictly) {
1141 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001142 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001143 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001144 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001145 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001146 methodDecl(hasParameter(0,
1147 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001148 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001149 methodDecl(hasParameter(0,
1150 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001151}
1152
1153TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1154 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001155 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001156 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001157 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001158}
1159
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001160TEST(Returns, MatchesReturnTypes) {
1161 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001162 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001163 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001164 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001165 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001166 functionDecl(returns(hasDeclaration(
1167 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001168}
1169
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001170TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001171 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1172 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1173 functionDecl(isExternC())));
1174 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001175}
1176
Manuel Klimek4da21662012-07-06 05:48:52 +00001177TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1178 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001179 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001180}
1181
1182TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1183 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001184 methodDecl(hasAnyParameter(hasType(pointsTo(
1185 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001186}
1187
1188TEST(HasName, MatchesParameterVariableDeclartions) {
1189 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001190 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001191 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001192 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001193}
1194
Daniel Jasper371f9392012-08-01 08:40:24 +00001195TEST(Matcher, MatchesClassTemplateSpecialization) {
1196 EXPECT_TRUE(matches("template<typename T> struct A {};"
1197 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001198 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001199 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001200 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001201 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001202 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001203}
1204
1205TEST(Matcher, MatchesTypeTemplateArgument) {
1206 EXPECT_TRUE(matches(
1207 "template<typename T> struct B {};"
1208 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001209 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001210 asString("int"))))));
1211}
1212
1213TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1214 EXPECT_TRUE(matches(
1215 "struct B { int next; };"
1216 "template<int(B::*next_ptr)> struct A {};"
1217 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001218 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1219 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001220}
1221
1222TEST(Matcher, MatchesSpecificArgument) {
1223 EXPECT_TRUE(matches(
1224 "template<typename T, typename U> class A {};"
1225 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001226 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001227 1, refersToType(asString("int"))))));
1228 EXPECT_TRUE(notMatches(
1229 "template<typename T, typename U> class A {};"
1230 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001231 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001232 1, refersToType(asString("int"))))));
1233}
1234
Manuel Klimek4da21662012-07-06 05:48:52 +00001235TEST(Matcher, ConstructorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001236 StatementMatcher Constructor = expr(constructExpr());
Manuel Klimek4da21662012-07-06 05:48:52 +00001237
1238 EXPECT_TRUE(
1239 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1240 EXPECT_TRUE(
1241 matches("class X { public: X(); }; void x() { X x = X(); }",
1242 Constructor));
1243 EXPECT_TRUE(
1244 matches("class X { public: X(int); }; void x() { X x = 0; }",
1245 Constructor));
1246 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1247}
1248
1249TEST(Matcher, ConstructorArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001250 StatementMatcher Constructor = expr(constructExpr(
1251 hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001252
1253 EXPECT_TRUE(
1254 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1255 Constructor));
1256 EXPECT_TRUE(
1257 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1258 Constructor));
1259 EXPECT_TRUE(
1260 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1261 Constructor));
1262 EXPECT_TRUE(
1263 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1264 Constructor));
1265
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001266 StatementMatcher WrongIndex = expr(constructExpr(
1267 hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001268 EXPECT_TRUE(
1269 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1270 WrongIndex));
1271}
1272
1273TEST(Matcher, ConstructorArgumentCount) {
1274 StatementMatcher Constructor1Arg =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001275 expr(constructExpr(argumentCountIs(1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00001276
1277 EXPECT_TRUE(
1278 matches("class X { public: X(int); }; void x() { X x(0); }",
1279 Constructor1Arg));
1280 EXPECT_TRUE(
1281 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1282 Constructor1Arg));
1283 EXPECT_TRUE(
1284 matches("class X { public: X(int); }; void x() { X x = 0; }",
1285 Constructor1Arg));
1286 EXPECT_TRUE(
1287 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1288 Constructor1Arg));
1289}
1290
1291TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001292 StatementMatcher TempExpression = expr(bindTemporaryExpr());
Manuel Klimek4da21662012-07-06 05:48:52 +00001293
1294 std::string ClassString = "class string { public: string(); ~string(); }; ";
1295
1296 EXPECT_TRUE(
1297 matches(ClassString +
1298 "string GetStringByValue();"
1299 "void FunctionTakesString(string s);"
1300 "void run() { FunctionTakesString(GetStringByValue()); }",
1301 TempExpression));
1302
1303 EXPECT_TRUE(
1304 notMatches(ClassString +
1305 "string* GetStringPointer(); "
1306 "void FunctionTakesStringPtr(string* s);"
1307 "void run() {"
1308 " string* s = GetStringPointer();"
1309 " FunctionTakesStringPtr(GetStringPointer());"
1310 " FunctionTakesStringPtr(s);"
1311 "}",
1312 TempExpression));
1313
1314 EXPECT_TRUE(
1315 notMatches("class no_dtor {};"
1316 "no_dtor GetObjByValue();"
1317 "void ConsumeObj(no_dtor param);"
1318 "void run() { ConsumeObj(GetObjByValue()); }",
1319 TempExpression));
1320}
1321
Sam Panzere16acd32012-08-24 22:04:44 +00001322TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1323 std::string ClassString =
1324 "class string { public: string(); int length(); }; ";
1325
1326 EXPECT_TRUE(
1327 matches(ClassString +
1328 "string GetStringByValue();"
1329 "void FunctionTakesString(string s);"
1330 "void run() { FunctionTakesString(GetStringByValue()); }",
1331 materializeTemporaryExpr()));
1332
1333 EXPECT_TRUE(
1334 notMatches(ClassString +
1335 "string* GetStringPointer(); "
1336 "void FunctionTakesStringPtr(string* s);"
1337 "void run() {"
1338 " string* s = GetStringPointer();"
1339 " FunctionTakesStringPtr(GetStringPointer());"
1340 " FunctionTakesStringPtr(s);"
1341 "}",
1342 materializeTemporaryExpr()));
1343
1344 EXPECT_TRUE(
1345 notMatches(ClassString +
1346 "string GetStringByValue();"
1347 "void run() { int k = GetStringByValue().length(); }",
1348 materializeTemporaryExpr()));
1349
1350 EXPECT_TRUE(
1351 notMatches(ClassString +
1352 "string GetStringByValue();"
1353 "void run() { GetStringByValue(); }",
1354 materializeTemporaryExpr()));
1355}
1356
Manuel Klimek4da21662012-07-06 05:48:52 +00001357TEST(ConstructorDeclaration, SimpleCase) {
1358 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001359 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001360 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001361 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001362}
1363
1364TEST(ConstructorDeclaration, IsImplicit) {
1365 // This one doesn't match because the constructor is not added by the
1366 // compiler (it is not needed).
1367 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001368 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001369 // The compiler added the implicit default constructor.
1370 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001371 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001372 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001373 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001374}
1375
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001376TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1377 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001378 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001379}
1380
1381TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001382 EXPECT_TRUE(notMatches("class Foo {};",
1383 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001384}
1385
Manuel Klimek4da21662012-07-06 05:48:52 +00001386TEST(HasAnyConstructorInitializer, SimpleCase) {
1387 EXPECT_TRUE(notMatches(
1388 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001389 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001390 EXPECT_TRUE(matches(
1391 "class Foo {"
1392 " Foo() : foo_() { }"
1393 " int foo_;"
1394 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001395 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001396}
1397
1398TEST(HasAnyConstructorInitializer, ForField) {
1399 static const char Code[] =
1400 "class Baz { };"
1401 "class Foo {"
1402 " Foo() : foo_() { }"
1403 " Baz foo_;"
1404 " Baz bar_;"
1405 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001406 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1407 forField(hasType(recordDecl(hasName("Baz"))))))));
1408 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001409 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001410 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1411 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001412}
1413
1414TEST(HasAnyConstructorInitializer, WithInitializer) {
1415 static const char Code[] =
1416 "class Foo {"
1417 " Foo() : foo_(0) { }"
1418 " int foo_;"
1419 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001420 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001421 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001422 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001423 withInitializer(integerLiteral(equals(1)))))));
1424}
1425
1426TEST(HasAnyConstructorInitializer, IsWritten) {
1427 static const char Code[] =
1428 "struct Bar { Bar(){} };"
1429 "class Foo {"
1430 " Foo() : foo_() { }"
1431 " Bar foo_;"
1432 " Bar bar_;"
1433 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001434 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001435 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001436 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001437 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001438 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001439 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1440}
1441
1442TEST(Matcher, NewExpression) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001443 StatementMatcher New = expr(newExpr());
Manuel Klimek4da21662012-07-06 05:48:52 +00001444
1445 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1446 EXPECT_TRUE(
1447 matches("class X { public: X(); }; void x() { new X(); }", New));
1448 EXPECT_TRUE(
1449 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1450 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1451}
1452
1453TEST(Matcher, NewExpressionArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001454 StatementMatcher New = expr(constructExpr(
1455 hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001456
1457 EXPECT_TRUE(
1458 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1459 New));
1460 EXPECT_TRUE(
1461 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1462 New));
1463 EXPECT_TRUE(
1464 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1465 New));
1466
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001467 StatementMatcher WrongIndex = expr(constructExpr(
1468 hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001469 EXPECT_TRUE(
1470 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1471 WrongIndex));
1472}
1473
1474TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001475 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001476
1477 EXPECT_TRUE(
1478 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1479 EXPECT_TRUE(
1480 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1481 New));
1482}
1483
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001484TEST(Matcher, DeleteExpression) {
1485 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001486 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001487}
1488
Manuel Klimek4da21662012-07-06 05:48:52 +00001489TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001490 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001491
1492 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1493 EXPECT_TRUE(
1494 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1495 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1496}
1497
1498TEST(Matcher, StringLiterals) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001499 StatementMatcher Literal = expr(stringLiteral());
Manuel Klimek4da21662012-07-06 05:48:52 +00001500 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1501 // wide string
1502 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1503 // with escaped characters
1504 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1505 // no matching -- though the data type is the same, there is no string literal
1506 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1507}
1508
1509TEST(Matcher, CharacterLiterals) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001510 StatementMatcher CharLiteral = expr(characterLiteral());
Manuel Klimek4da21662012-07-06 05:48:52 +00001511 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1512 // wide character
1513 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1514 // wide character, Hex encoded, NOT MATCHED!
1515 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1516 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1517}
1518
1519TEST(Matcher, IntegerLiterals) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001520 StatementMatcher HasIntLiteral = expr(integerLiteral());
Manuel Klimek4da21662012-07-06 05:48:52 +00001521 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1522 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1523 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1524 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1525
1526 // Non-matching cases (character literals, float and double)
1527 EXPECT_TRUE(notMatches("int i = L'a';",
1528 HasIntLiteral)); // this is actually a character
1529 // literal cast to int
1530 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1531 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1532 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1533}
1534
1535TEST(Matcher, Conditions) {
1536 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1537
1538 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1539 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1540 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1541 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1542 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1543}
1544
1545TEST(MatchBinaryOperator, HasOperatorName) {
1546 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1547
1548 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1549 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1550}
1551
1552TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1553 StatementMatcher OperatorTrueFalse =
1554 binaryOperator(hasLHS(boolLiteral(equals(true))),
1555 hasRHS(boolLiteral(equals(false))));
1556
1557 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1558 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1559 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1560}
1561
1562TEST(MatchBinaryOperator, HasEitherOperand) {
1563 StatementMatcher HasOperand =
1564 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1565
1566 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1567 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1568 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1569}
1570
1571TEST(Matcher, BinaryOperatorTypes) {
1572 // Integration test that verifies the AST provides all binary operators in
1573 // a way we expect.
1574 // FIXME: Operator ','
1575 EXPECT_TRUE(
1576 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1577 EXPECT_TRUE(
1578 matches("bool b; bool c = (b = true);",
1579 binaryOperator(hasOperatorName("="))));
1580 EXPECT_TRUE(
1581 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1582 EXPECT_TRUE(
1583 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1584 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1585 EXPECT_TRUE(
1586 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1587 EXPECT_TRUE(
1588 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1589 EXPECT_TRUE(
1590 matches("int i = 1; int j = (i <<= 2);",
1591 binaryOperator(hasOperatorName("<<="))));
1592 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1593 EXPECT_TRUE(
1594 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1595 EXPECT_TRUE(
1596 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1597 EXPECT_TRUE(
1598 matches("int i = 1; int j = (i >>= 2);",
1599 binaryOperator(hasOperatorName(">>="))));
1600 EXPECT_TRUE(
1601 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1602 EXPECT_TRUE(
1603 matches("int i = 42; int j = (i ^= 42);",
1604 binaryOperator(hasOperatorName("^="))));
1605 EXPECT_TRUE(
1606 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1607 EXPECT_TRUE(
1608 matches("int i = 42; int j = (i %= 42);",
1609 binaryOperator(hasOperatorName("%="))));
1610 EXPECT_TRUE(
1611 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1612 EXPECT_TRUE(
1613 matches("bool b = true && false;",
1614 binaryOperator(hasOperatorName("&&"))));
1615 EXPECT_TRUE(
1616 matches("bool b = true; bool c = (b &= false);",
1617 binaryOperator(hasOperatorName("&="))));
1618 EXPECT_TRUE(
1619 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1620 EXPECT_TRUE(
1621 matches("bool b = true || false;",
1622 binaryOperator(hasOperatorName("||"))));
1623 EXPECT_TRUE(
1624 matches("bool b = true; bool c = (b |= false);",
1625 binaryOperator(hasOperatorName("|="))));
1626 EXPECT_TRUE(
1627 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1628 EXPECT_TRUE(
1629 matches("int i = 42; int j = (i *= 23);",
1630 binaryOperator(hasOperatorName("*="))));
1631 EXPECT_TRUE(
1632 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1633 EXPECT_TRUE(
1634 matches("int i = 42; int j = (i /= 23);",
1635 binaryOperator(hasOperatorName("/="))));
1636 EXPECT_TRUE(
1637 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1638 EXPECT_TRUE(
1639 matches("int i = 42; int j = (i += 23);",
1640 binaryOperator(hasOperatorName("+="))));
1641 EXPECT_TRUE(
1642 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1643 EXPECT_TRUE(
1644 matches("int i = 42; int j = (i -= 23);",
1645 binaryOperator(hasOperatorName("-="))));
1646 EXPECT_TRUE(
1647 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1648 binaryOperator(hasOperatorName("->*"))));
1649 EXPECT_TRUE(
1650 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1651 binaryOperator(hasOperatorName(".*"))));
1652
1653 // Member expressions as operators are not supported in matches.
1654 EXPECT_TRUE(
1655 notMatches("struct A { void x(A *a) { a->x(this); } };",
1656 binaryOperator(hasOperatorName("->"))));
1657
1658 // Initializer assignments are not represented as operator equals.
1659 EXPECT_TRUE(
1660 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1661
1662 // Array indexing is not represented as operator.
1663 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1664
1665 // Overloaded operators do not match at all.
1666 EXPECT_TRUE(notMatches(
1667 "struct A { bool operator&&(const A &a) const { return false; } };"
1668 "void x() { A a, b; a && b; }",
1669 binaryOperator()));
1670}
1671
1672TEST(MatchUnaryOperator, HasOperatorName) {
1673 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1674
1675 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1676 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1677}
1678
1679TEST(MatchUnaryOperator, HasUnaryOperand) {
1680 StatementMatcher OperatorOnFalse =
1681 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1682
1683 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1684 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1685}
1686
1687TEST(Matcher, UnaryOperatorTypes) {
1688 // Integration test that verifies the AST provides all unary operators in
1689 // a way we expect.
1690 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1691 EXPECT_TRUE(
1692 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1693 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1694 EXPECT_TRUE(
1695 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1696 EXPECT_TRUE(
1697 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1698 EXPECT_TRUE(
1699 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1700 EXPECT_TRUE(
1701 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1702 EXPECT_TRUE(
1703 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1704 EXPECT_TRUE(
1705 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1706 EXPECT_TRUE(
1707 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1708
1709 // We don't match conversion operators.
1710 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1711
1712 // Function calls are not represented as operator.
1713 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1714
1715 // Overloaded operators do not match at all.
1716 // FIXME: We probably want to add that.
1717 EXPECT_TRUE(notMatches(
1718 "struct A { bool operator!() const { return false; } };"
1719 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1720}
1721
1722TEST(Matcher, ConditionalOperator) {
1723 StatementMatcher Conditional = conditionalOperator(
1724 hasCondition(boolLiteral(equals(true))),
1725 hasTrueExpression(boolLiteral(equals(false))));
1726
1727 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1728 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1729 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1730
1731 StatementMatcher ConditionalFalse = conditionalOperator(
1732 hasFalseExpression(boolLiteral(equals(false))));
1733
1734 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1735 EXPECT_TRUE(
1736 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1737}
1738
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001739TEST(ArraySubscriptMatchers, ArraySubscripts) {
1740 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1741 arraySubscriptExpr()));
1742 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1743 arraySubscriptExpr()));
1744}
1745
1746TEST(ArraySubscriptMatchers, ArrayIndex) {
1747 EXPECT_TRUE(matches(
1748 "int i[2]; void f() { i[1] = 1; }",
1749 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1750 EXPECT_TRUE(matches(
1751 "int i[2]; void f() { 1[i] = 1; }",
1752 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1753 EXPECT_TRUE(notMatches(
1754 "int i[2]; void f() { i[1] = 1; }",
1755 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1756}
1757
1758TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1759 EXPECT_TRUE(matches(
1760 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001761 arraySubscriptExpr(hasBase(implicitCastExpr(
1762 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001763}
1764
Manuel Klimek4da21662012-07-06 05:48:52 +00001765TEST(Matcher, HasNameSupportsNamespaces) {
1766 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001767 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001768 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001769 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001770 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001771 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001772 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001773 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001774 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001775 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001776 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001777 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001778 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001779 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001780 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001781 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001782 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001783 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001784 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001785 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001786 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001787 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001788 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001789 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001790}
1791
1792TEST(Matcher, HasNameSupportsOuterClasses) {
1793 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001794 matches("class A { class B { class C; }; };",
1795 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001796 EXPECT_TRUE(
1797 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001798 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001799 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001800 matches("class A { class B { class C; }; };",
1801 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001802 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001803 matches("class A { class B { class C; }; };",
1804 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001805 EXPECT_TRUE(
1806 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001807 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001808 EXPECT_TRUE(
1809 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001810 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001811 EXPECT_TRUE(
1812 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001813 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001814 EXPECT_TRUE(
1815 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001816 recordDecl(hasName("::C"))));
1817 EXPECT_TRUE(
1818 notMatches("class A { class B { class C; }; };",
1819 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001820 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001821 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001822 EXPECT_TRUE(
1823 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001824 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001825}
1826
1827TEST(Matcher, IsDefinition) {
1828 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001829 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001830 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1831 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1832
1833 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001834 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001835 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1836 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1837
1838 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001839 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001840 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1841 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1842}
1843
1844TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001845 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00001846 ofClass(hasName("X")))));
1847
1848 EXPECT_TRUE(
1849 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
1850 EXPECT_TRUE(
1851 matches("class X { public: X(); }; void x(int) { X x = X(); }",
1852 Constructor));
1853 EXPECT_TRUE(
1854 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
1855 Constructor));
1856}
1857
1858TEST(Matcher, VisitsTemplateInstantiations) {
1859 EXPECT_TRUE(matches(
1860 "class A { public: void x(); };"
1861 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001862 "void f() { B<A> b; b.y(); }",
1863 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001864
1865 EXPECT_TRUE(matches(
1866 "class A { public: void x(); };"
1867 "class C {"
1868 " public:"
1869 " template <typename T> class B { public: void y() { T t; t.x(); } };"
1870 "};"
1871 "void f() {"
1872 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001873 "}",
1874 recordDecl(hasName("C"),
1875 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001876}
1877
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001878TEST(Matcher, HandlesNullQualTypes) {
1879 // FIXME: Add a Type matcher so we can replace uses of this
1880 // variable with Type(True())
1881 const TypeMatcher AnyType = anything();
1882
1883 // We don't really care whether this matcher succeeds; we're testing that
1884 // it completes without crashing.
1885 EXPECT_TRUE(matches(
1886 "struct A { };"
1887 "template <typename T>"
1888 "void f(T t) {"
1889 " T local_t(t /* this becomes a null QualType in the AST */);"
1890 "}"
1891 "void g() {"
1892 " f(0);"
1893 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001894 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001895 anyOf(
1896 TypeMatcher(hasDeclaration(anything())),
1897 pointsTo(AnyType),
1898 references(AnyType)
1899 // Other QualType matchers should go here.
1900 ))))));
1901}
1902
Manuel Klimek4da21662012-07-06 05:48:52 +00001903// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001904AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00001905 // Make sure all special variables are used: node, match_finder,
1906 // bound_nodes_builder, and the parameter named 'AMatcher'.
1907 return AMatcher.matches(Node, Finder, Builder);
1908}
1909
1910TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001911 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001912
1913 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001914 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001915
1916 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001917 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001918
1919 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001920 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001921}
1922
1923AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001924 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
1925 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
1926 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00001927 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00001928 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00001929 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00001930 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
1931 ASTMatchFinder::BK_First);
1932}
1933
1934TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001935 DeclarationMatcher HasClassB =
1936 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001937
1938 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001939 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001940
1941 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001942 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001943
1944 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001945 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001946
1947 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001948 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001949
1950 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
1951}
1952
1953TEST(For, FindsForLoops) {
1954 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
1955 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
1956}
1957
Daniel Jasper6a124492012-07-12 08:50:38 +00001958TEST(For, ForLoopInternals) {
1959 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
1960 forStmt(hasCondition(anything()))));
1961 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
1962 forStmt(hasLoopInit(anything()))));
1963}
1964
1965TEST(For, NegativeForLoopInternals) {
1966 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001967 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001968 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
1969 forStmt(hasLoopInit(anything()))));
1970}
1971
Manuel Klimek4da21662012-07-06 05:48:52 +00001972TEST(For, ReportsNoFalsePositives) {
1973 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
1974 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
1975}
1976
1977TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001978 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
1979 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
1980 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001981}
1982
1983TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
1984 // It's not a compound statement just because there's "{}" in the source
1985 // text. This is an AST search, not grep.
1986 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001987 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001988 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001989 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001990}
1991
Daniel Jasper6a124492012-07-12 08:50:38 +00001992TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00001993 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001994 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001995 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001996 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001997 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001998 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001999 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002000 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002001}
2002
2003TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2004 // The simplest case: every compound statement is in a function
2005 // definition, and the function body itself must be a compound
2006 // statement.
2007 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002008 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002009}
2010
2011TEST(HasAnySubstatement, IsNotRecursive) {
2012 // It's really "has any immediate substatement".
2013 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002014 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002015}
2016
2017TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2018 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002019 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002020}
2021
2022TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2023 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002024 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002025}
2026
2027TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2028 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002029 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002030 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002031 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002032}
2033
2034TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2035 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002036 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002037 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002038 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002039 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002040 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002041}
2042
2043TEST(StatementCountIs, WorksWithMultipleStatements) {
2044 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002045 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002046}
2047
2048TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2049 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002050 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002051 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002052 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002053 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002054 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002055 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002056 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002057}
2058
2059TEST(Member, WorksInSimplestCase) {
2060 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002061 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002062}
2063
2064TEST(Member, DoesNotMatchTheBaseExpression) {
2065 // Don't pick out the wrong part of the member expression, this should
2066 // be checking the member (name) only.
2067 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002068 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002069}
2070
2071TEST(Member, MatchesInMemberFunctionCall) {
2072 EXPECT_TRUE(matches("void f() {"
2073 " struct { void first() {}; } s;"
2074 " s.first();"
2075 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002076 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002077}
2078
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002079TEST(Member, MatchesMemberAllocationFunction) {
Dmitri Gribenko02ed37f2012-08-18 00:41:04 +00002080 EXPECT_TRUE(matches("namespace std { typedef typeof(sizeof(int)) size_t; }"
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002081 "class X { void *operator new(std::size_t); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002082 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002083
2084 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002085 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002086
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002087 EXPECT_TRUE(matches(
2088 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2089 "class X { void operator delete[](void*, std::size_t); };",
2090 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002091}
2092
Manuel Klimek4da21662012-07-06 05:48:52 +00002093TEST(HasObjectExpression, DoesNotMatchMember) {
2094 EXPECT_TRUE(notMatches(
2095 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002096 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002097}
2098
2099TEST(HasObjectExpression, MatchesBaseOfVariable) {
2100 EXPECT_TRUE(matches(
2101 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002102 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002103 EXPECT_TRUE(matches(
2104 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002105 memberExpr(hasObjectExpression(
2106 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002107}
2108
2109TEST(HasObjectExpression,
2110 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2111 EXPECT_TRUE(matches(
2112 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002113 memberExpr(hasObjectExpression(
2114 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002115 EXPECT_TRUE(matches(
2116 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002117 memberExpr(hasObjectExpression(
2118 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002119}
2120
2121TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002122 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2123 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2124 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2125 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002126}
2127
2128TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002129 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002130}
2131
2132TEST(IsConstQualified, MatchesConstInt) {
2133 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002134 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002135}
2136
2137TEST(IsConstQualified, MatchesConstPointer) {
2138 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002139 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002140}
2141
2142TEST(IsConstQualified, MatchesThroughTypedef) {
2143 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002144 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002145 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002146 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002147}
2148
2149TEST(IsConstQualified, DoesNotMatchInappropriately) {
2150 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002151 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002152 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002153 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002154}
2155
Sam Panzer089e5b32012-08-16 16:58:10 +00002156TEST(CastExpression, MatchesExplicitCasts) {
2157 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002158 expr(castExpr())));
2159 EXPECT_TRUE(matches("void *p = (void *)(&p);", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002160 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002161 expr(castExpr())));
2162 EXPECT_TRUE(matches("char c = char(0);", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002163}
2164TEST(CastExpression, MatchesImplicitCasts) {
2165 // This test creates an implicit cast from int to char.
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002166 EXPECT_TRUE(matches("char c = 0;", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002167 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002168 EXPECT_TRUE(matches("char c = 0, d = c;", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002169}
2170
2171TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002172 EXPECT_TRUE(notMatches("char c = '0';", expr(castExpr())));
2173 EXPECT_TRUE(notMatches("char c, &q = c;", expr(castExpr())));
2174 EXPECT_TRUE(notMatches("int i = (0);", expr(castExpr())));
2175 EXPECT_TRUE(notMatches("int i = 0;", expr(castExpr())));
Sam Panzer089e5b32012-08-16 16:58:10 +00002176}
2177
Manuel Klimek4da21662012-07-06 05:48:52 +00002178TEST(ReinterpretCast, MatchesSimpleCase) {
2179 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002180 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002181}
2182
2183TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
2184 EXPECT_TRUE(notMatches("char* p = (char*)(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002185 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002186 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002187 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002188 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002189 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002190 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2191 "B b;"
2192 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002193 expr(reinterpretCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002194}
2195
2196TEST(FunctionalCast, MatchesSimpleCase) {
2197 std::string foo_class = "class Foo { public: Foo(char*); };";
2198 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002199 expr(functionalCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002200}
2201
2202TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2203 std::string FooClass = "class Foo { public: Foo(char*); };";
2204 EXPECT_TRUE(
2205 notMatches(FooClass + "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 EXPECT_TRUE(
2208 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002209 expr(functionalCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002210}
2211
2212TEST(DynamicCast, MatchesSimpleCase) {
2213 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2214 "B b;"
2215 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002216 expr(dynamicCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002217}
2218
2219TEST(StaticCast, MatchesSimpleCase) {
2220 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002221 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002222}
2223
2224TEST(StaticCast, DoesNotMatchOtherCasts) {
2225 EXPECT_TRUE(notMatches("char* p = (char*)(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002226 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002227 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002228 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002229 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002230 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002231 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2232 "B b;"
2233 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002234 expr(staticCastExpr())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002235}
2236
2237TEST(HasDestinationType, MatchesSimpleCase) {
2238 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002239 expr(staticCastExpr(hasDestinationType(
2240 pointsTo(TypeMatcher(anything())))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002241}
2242
Sam Panzer089e5b32012-08-16 16:58:10 +00002243TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2244 // This test creates an implicit const cast.
2245 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002246 expr(implicitCastExpr(
Sam Panzer089e5b32012-08-16 16:58:10 +00002247 hasImplicitDestinationType(isInteger())))));
2248 // This test creates an implicit array-to-pointer cast.
2249 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002250 expr(implicitCastExpr(hasImplicitDestinationType(
Sam Panzer089e5b32012-08-16 16:58:10 +00002251 pointsTo(TypeMatcher(anything())))))));
2252}
2253
2254TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2255 // This test creates an implicit cast from int to char.
2256 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002257 expr(implicitCastExpr(hasImplicitDestinationType(
Sam Panzer089e5b32012-08-16 16:58:10 +00002258 unless(anything()))))));
2259 // This test creates an implicit array-to-pointer cast.
2260 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002261 expr(implicitCastExpr(hasImplicitDestinationType(
Sam Panzer089e5b32012-08-16 16:58:10 +00002262 unless(anything()))))));
2263}
2264
2265TEST(ImplicitCast, MatchesSimpleCase) {
2266 // This test creates an implicit const cast.
2267 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002268 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002269 // This test creates an implicit cast from int to char.
2270 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002271 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002272 // This test creates an implicit array-to-pointer cast.
2273 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002274 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002275}
2276
2277TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002278 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002279 // are present, and that it ignores explicit and paren casts.
2280
2281 // These two test cases have no casts.
2282 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002283 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002284 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002285 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002286
2287 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002288 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002289 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002290 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002291
2292 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002293 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002294}
2295
2296TEST(IgnoringImpCasts, MatchesImpCasts) {
2297 // This test checks that ignoringImpCasts matches when implicit casts are
2298 // present and its inner matcher alone does not match.
2299 // Note that this test creates an implicit const cast.
2300 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002301 varDecl(hasInitializer(ignoringImpCasts(
2302 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002303 // This test creates an implict cast from int to char.
2304 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002305 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002306 integerLiteral(equals(0)))))));
2307}
2308
2309TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2310 // These tests verify that ignoringImpCasts does not match if the inner
2311 // matcher does not match.
2312 // Note that the first test creates an implicit const cast.
2313 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002314 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002315 unless(anything()))))));
2316 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002317 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002318 unless(anything()))))));
2319
2320 // These tests verify that ignoringImplictCasts does not look through explicit
2321 // casts or parentheses.
2322 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002323 varDecl(hasInitializer(ignoringImpCasts(
2324 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002325 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002326 varDecl(hasInitializer(ignoringImpCasts(
2327 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002328 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002329 varDecl(hasInitializer(ignoringImpCasts(
2330 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002331 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002332 varDecl(hasInitializer(ignoringImpCasts(
2333 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002334}
2335
2336TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2337 // This test verifies that expressions that do not have implicit casts
2338 // still match the inner matcher.
2339 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002340 varDecl(hasInitializer(ignoringImpCasts(
2341 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002342}
2343
2344TEST(IgnoringParenCasts, MatchesParenCasts) {
2345 // This test checks that ignoringParenCasts matches when parentheses and/or
2346 // casts are present and its inner matcher alone does not match.
2347 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002348 varDecl(hasInitializer(ignoringParenCasts(
2349 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002350 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002351 varDecl(hasInitializer(ignoringParenCasts(
2352 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002353
2354 // This test creates an implict cast from int to char in addition to the
2355 // parentheses.
2356 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002357 varDecl(hasInitializer(ignoringParenCasts(
2358 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002359
2360 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002361 varDecl(hasInitializer(ignoringParenCasts(
2362 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002363 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002364 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002365 integerLiteral(equals(0)))))));
2366}
2367
2368TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2369 // This test verifies that expressions that do not have any casts still match.
2370 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002371 varDecl(hasInitializer(ignoringParenCasts(
2372 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002373}
2374
2375TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2376 // These tests verify that ignoringImpCasts does not match if the inner
2377 // matcher does not match.
2378 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002379 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002380 unless(anything()))))));
2381
2382 // This test creates an implicit cast from int to char in addition to the
2383 // parentheses.
2384 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002385 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002386 unless(anything()))))));
2387
2388 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002389 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002390 unless(anything()))))));
2391}
2392
2393TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2394 // This test checks that ignoringParenAndImpCasts matches when
2395 // parentheses and/or implicit casts are present and its inner matcher alone
2396 // does not match.
2397 // Note that this test creates an implicit const cast.
2398 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002399 varDecl(hasInitializer(ignoringParenImpCasts(
2400 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002401 // This test creates an implicit cast from int to char.
2402 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002403 varDecl(hasInitializer(ignoringParenImpCasts(
2404 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002405}
2406
2407TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2408 // This test verifies that expressions that do not have parentheses or
2409 // implicit casts still match.
2410 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002411 varDecl(hasInitializer(ignoringParenImpCasts(
2412 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002413 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002414 varDecl(hasInitializer(ignoringParenImpCasts(
2415 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002416}
2417
2418TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2419 // These tests verify that ignoringParenImpCasts does not match if
2420 // the inner matcher does not match.
2421 // This test creates an implicit cast.
2422 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002423 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002424 unless(anything()))))));
2425 // These tests verify that ignoringParenAndImplictCasts does not look
2426 // through explicit casts.
2427 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002428 varDecl(hasInitializer(ignoringParenImpCasts(
2429 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002430 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002431 varDecl(hasInitializer(ignoringParenImpCasts(
2432 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002433 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002434 varDecl(hasInitializer(ignoringParenImpCasts(
2435 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002436}
2437
Manuel Klimek715c9562012-07-25 10:02:02 +00002438TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002439 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2440 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002441 expr(implicitCastExpr(
2442 hasSourceExpression(constructExpr())))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002443}
2444
Manuel Klimek715c9562012-07-25 10:02:02 +00002445TEST(HasSourceExpression, MatchesExplicitCasts) {
2446 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002447 expr(explicitCastExpr(
2448 hasSourceExpression(hasDescendant(
2449 expr(integerLiteral())))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002450}
2451
Manuel Klimek4da21662012-07-06 05:48:52 +00002452TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002453 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002454}
2455
2456TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002457 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002458}
2459
2460TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002461 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002462}
2463
2464TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002465 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002466}
2467
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002468TEST(InitListExpression, MatchesInitListExpression) {
2469 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2470 initListExpr(hasType(asString("int [2]")))));
2471 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002472 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002473}
2474
2475TEST(UsingDeclaration, MatchesUsingDeclarations) {
2476 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2477 usingDecl()));
2478}
2479
2480TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2481 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2482 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2483}
2484
2485TEST(UsingDeclaration, MatchesSpecificTarget) {
2486 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2487 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002488 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002489 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2490 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002491 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002492}
2493
2494TEST(UsingDeclaration, ThroughUsingDeclaration) {
2495 EXPECT_TRUE(matches(
2496 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002497 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002498 EXPECT_TRUE(notMatches(
2499 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002500 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002501}
2502
Sam Panzer425f41b2012-08-16 17:20:59 +00002503TEST(SingleDecl, IsSingleDecl) {
2504 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002505 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002506 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2507 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2508 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2509 SingleDeclStmt));
2510}
2511
2512TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002513 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002514
2515 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002516 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002517 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002518 declStmt(containsDeclaration(0, MatchesInit),
2519 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002520 unsigned WrongIndex = 42;
2521 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002522 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002523 MatchesInit))));
2524}
2525
2526TEST(DeclCount, DeclCountIsCorrect) {
2527 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002528 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002529 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002530 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002531 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002532 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002533}
2534
Manuel Klimek4da21662012-07-06 05:48:52 +00002535TEST(While, MatchesWhileLoops) {
2536 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2537 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2538 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2539}
2540
2541TEST(Do, MatchesDoLoops) {
2542 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2543 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2544}
2545
2546TEST(Do, DoesNotMatchWhileLoops) {
2547 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2548}
2549
2550TEST(SwitchCase, MatchesCase) {
2551 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2552 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2553 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2554 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2555}
2556
2557TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2558 EXPECT_TRUE(notMatches(
2559 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002560 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002561 EXPECT_TRUE(notMatches(
2562 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002563 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002564}
2565
2566TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2567 EXPECT_TRUE(matches(
2568 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002569 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002570}
2571
2572TEST(ForEach, BindsOneNode) {
2573 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002574 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002575 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002576}
2577
2578TEST(ForEach, BindsMultipleNodes) {
2579 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002580 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002581 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002582}
2583
2584TEST(ForEach, BindsRecursiveCombinations) {
2585 EXPECT_TRUE(matchAndVerifyResultTrue(
2586 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002587 recordDecl(hasName("C"),
2588 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002589 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002590}
2591
2592TEST(ForEachDescendant, BindsOneNode) {
2593 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002594 recordDecl(hasName("C"),
2595 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002596 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002597}
2598
2599TEST(ForEachDescendant, BindsMultipleNodes) {
2600 EXPECT_TRUE(matchAndVerifyResultTrue(
2601 "class C { class D { int x; int y; }; "
2602 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002603 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002604 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002605}
2606
2607TEST(ForEachDescendant, BindsRecursiveCombinations) {
2608 EXPECT_TRUE(matchAndVerifyResultTrue(
2609 "class C { class D { "
2610 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002611 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2612 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002613 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002614}
2615
2616
2617TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2618 // Make sure that we can both match the class by name (::X) and by the type
2619 // the template was instantiated with (via a field).
2620
2621 EXPECT_TRUE(matches(
2622 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002623 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002624
2625 EXPECT_TRUE(matches(
2626 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002627 recordDecl(isTemplateInstantiation(), hasDescendant(
2628 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002629}
2630
2631TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2632 EXPECT_TRUE(matches(
2633 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002634 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002635 isTemplateInstantiation())));
2636}
2637
2638TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2639 EXPECT_TRUE(matches(
2640 "template <typename T> class X { T t; }; class A {};"
2641 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002642 recordDecl(isTemplateInstantiation(), hasDescendant(
2643 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002644}
2645
2646TEST(IsTemplateInstantiation,
2647 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2648 EXPECT_TRUE(matches(
2649 "template <typename T> class X {};"
2650 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002651 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002652}
2653
2654TEST(IsTemplateInstantiation,
2655 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2656 EXPECT_TRUE(matches(
2657 "class A {};"
2658 "class X {"
2659 " template <typename U> class Y { U u; };"
2660 " Y<A> y;"
2661 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002662 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002663}
2664
2665TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2666 // FIXME: Figure out whether this makes sense. It doesn't affect the
2667 // normal use case as long as the uppermost instantiation always is marked
2668 // as template instantiation, but it might be confusing as a predicate.
2669 EXPECT_TRUE(matches(
2670 "class A {};"
2671 "template <typename T> class X {"
2672 " template <typename U> class Y { U u; };"
2673 " Y<T> y;"
2674 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002675 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002676}
2677
2678TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2679 EXPECT_TRUE(notMatches(
2680 "template <typename T> class X {}; class A {};"
2681 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002682 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002683}
2684
2685TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2686 EXPECT_TRUE(notMatches(
2687 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002688 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002689}
2690
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002691TEST(IsExplicitTemplateSpecialization,
2692 DoesNotMatchPrimaryTemplate) {
2693 EXPECT_TRUE(notMatches(
2694 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002695 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002696 EXPECT_TRUE(notMatches(
2697 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002698 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002699}
2700
2701TEST(IsExplicitTemplateSpecialization,
2702 DoesNotMatchExplicitTemplateInstantiations) {
2703 EXPECT_TRUE(notMatches(
2704 "template <typename T> class X {};"
2705 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002706 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002707 EXPECT_TRUE(notMatches(
2708 "template <typename T> void f(T t) {}"
2709 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002710 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002711}
2712
2713TEST(IsExplicitTemplateSpecialization,
2714 DoesNotMatchImplicitTemplateInstantiations) {
2715 EXPECT_TRUE(notMatches(
2716 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002717 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002718 EXPECT_TRUE(notMatches(
2719 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002720 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002721}
2722
2723TEST(IsExplicitTemplateSpecialization,
2724 MatchesExplicitTemplateSpecializations) {
2725 EXPECT_TRUE(matches(
2726 "template <typename T> class X {};"
2727 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002728 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002729 EXPECT_TRUE(matches(
2730 "template <typename T> void f(T t) {}"
2731 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002732 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002733}
2734
Manuel Klimek579b1202012-09-07 09:26:10 +00002735TEST(HasAncenstor, MatchesDeclarationAncestors) {
2736 EXPECT_TRUE(matches(
2737 "class A { class B { class C {}; }; };",
2738 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2739}
2740
2741TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2742 EXPECT_TRUE(notMatches(
2743 "class A { class B { class C {}; }; };",
2744 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2745}
2746
2747TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2748 EXPECT_TRUE(matches(
2749 "class A { class B { void f() { C c; } class C {}; }; };",
2750 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2751 hasAncestor(recordDecl(hasName("A"))))))));
2752}
2753
2754TEST(HasAncenstor, MatchesStatementAncestors) {
2755 EXPECT_TRUE(matches(
2756 "void f() { if (true) { while (false) { 42; } } }",
2757 expr(integerLiteral(equals(42), hasAncestor(ifStmt())))));
2758}
2759
2760TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2761 EXPECT_TRUE(matches(
2762 "void f() { if (true) { int x = 42; } }",
2763 expr(integerLiteral(
2764 equals(42), hasAncestor(functionDecl(hasName("f")))))));
2765}
2766
2767TEST(HasAncestor, BindsRecursiveCombinations) {
2768 EXPECT_TRUE(matchAndVerifyResultTrue(
2769 "class C { class D { class E { class F { int y; }; }; }; };",
2770 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002771 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00002772}
2773
2774TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
2775 EXPECT_TRUE(matchAndVerifyResultTrue(
2776 "class C { class D { class E { class F { int y; }; }; }; };",
2777 fieldDecl(hasAncestor(
2778 decl(
2779 hasDescendant(recordDecl(isDefinition(),
2780 hasAncestor(recordDecl())))
2781 ).bind("d")
2782 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00002783 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00002784}
2785
2786TEST(HasAncestor, MatchesInTemplateInstantiations) {
2787 EXPECT_TRUE(matches(
2788 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
2789 "A<int>::B::C a;",
2790 fieldDecl(hasType(asString("int")),
2791 hasAncestor(recordDecl(hasName("A"))))));
2792}
2793
2794TEST(HasAncestor, MatchesInImplicitCode) {
2795 EXPECT_TRUE(matches(
2796 "struct X {}; struct A { A() {} X x; };",
2797 constructorDecl(
2798 hasAnyConstructorInitializer(withInitializer(expr(
2799 hasAncestor(recordDecl(hasName("A")))))))));
2800}
2801
Daniel Jaspera7564432012-09-13 13:11:25 +00002802TEST(NNS, MatchesNestedNameSpecifiers) {
2803 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
2804 nestedNameSpecifier()));
2805 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
2806 nestedNameSpecifier()));
2807 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
2808 nestedNameSpecifier()));
2809
2810 EXPECT_TRUE(matches(
2811 "struct A { static void f() {} }; void g() { A::f(); }",
2812 nestedNameSpecifier()));
2813 EXPECT_TRUE(notMatches(
2814 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
2815 nestedNameSpecifier()));
2816}
2817
2818TEST(NNS, MatchesTypes) {
2819 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
2820 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
2821 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
2822 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
2823 Matcher));
2824 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
2825}
2826
2827TEST(NNS, MatchesNamespaceDecls) {
2828 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
2829 specifiesNamespace(hasName("ns")));
2830 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
2831 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
2832 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
2833}
2834
2835TEST(NNS, BindsNestedNameSpecifiers) {
2836 EXPECT_TRUE(matchAndVerifyResultTrue(
2837 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
2838 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
2839 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
2840}
2841
2842TEST(NNS, BindsNestedNameSpecifierLocs) {
2843 EXPECT_TRUE(matchAndVerifyResultTrue(
2844 "namespace ns { struct B {}; } ns::B b;",
2845 loc(nestedNameSpecifier()).bind("loc"),
2846 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
2847}
2848
2849TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
2850 EXPECT_TRUE(matches(
2851 "struct A { struct B { struct C {}; }; }; A::B::C c;",
2852 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
2853 EXPECT_TRUE(matches(
2854 "struct A { struct B { struct C {}; }; }; A::B::C c;",
2855 nestedNameSpecifierLoc(hasPrefix(loc(
2856 specifiesType(asString("struct A")))))));
2857}
2858
Manuel Klimek4da21662012-07-06 05:48:52 +00002859} // end namespace ast_matchers
2860} // end namespace clang