blob: b8ffaa43001413277b3d6d3bd3fb042f3acf7693 [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 Jasper3680b4f2012-09-18 13:09:13 +0000913 StatementMatcher CallOnVariableY =
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 Jasper3680b4f2012-09-18 13:09:13 +00001086 StatementMatcher CallArgumentY = 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 Jasper3680b4f2012-09-18 13:09:13 +00001094 StatementMatcher WrongIndex = 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 Jasper3680b4f2012-09-18 13:09:13 +00001100 StatementMatcher CallArgumentY = 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 Jasper3680b4f2012-09-18 13:09:13 +00001108 StatementMatcher Call1Arg = 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 Jasper3680b4f2012-09-18 13:09:13 +00001236 StatementMatcher Constructor = 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 Jasper3680b4f2012-09-18 13:09:13 +00001250 StatementMatcher Constructor = 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 Jasper3680b4f2012-09-18 13:09:13 +00001266 StatementMatcher WrongIndex = 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) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001274 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001275
1276 EXPECT_TRUE(
1277 matches("class X { public: X(int); }; void x() { X x(0); }",
1278 Constructor1Arg));
1279 EXPECT_TRUE(
1280 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1281 Constructor1Arg));
1282 EXPECT_TRUE(
1283 matches("class X { public: X(int); }; void x() { X x = 0; }",
1284 Constructor1Arg));
1285 EXPECT_TRUE(
1286 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1287 Constructor1Arg));
1288}
1289
1290TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001291 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001292
1293 std::string ClassString = "class string { public: string(); ~string(); }; ";
1294
1295 EXPECT_TRUE(
1296 matches(ClassString +
1297 "string GetStringByValue();"
1298 "void FunctionTakesString(string s);"
1299 "void run() { FunctionTakesString(GetStringByValue()); }",
1300 TempExpression));
1301
1302 EXPECT_TRUE(
1303 notMatches(ClassString +
1304 "string* GetStringPointer(); "
1305 "void FunctionTakesStringPtr(string* s);"
1306 "void run() {"
1307 " string* s = GetStringPointer();"
1308 " FunctionTakesStringPtr(GetStringPointer());"
1309 " FunctionTakesStringPtr(s);"
1310 "}",
1311 TempExpression));
1312
1313 EXPECT_TRUE(
1314 notMatches("class no_dtor {};"
1315 "no_dtor GetObjByValue();"
1316 "void ConsumeObj(no_dtor param);"
1317 "void run() { ConsumeObj(GetObjByValue()); }",
1318 TempExpression));
1319}
1320
Sam Panzere16acd32012-08-24 22:04:44 +00001321TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1322 std::string ClassString =
1323 "class string { public: string(); int length(); }; ";
1324
1325 EXPECT_TRUE(
1326 matches(ClassString +
1327 "string GetStringByValue();"
1328 "void FunctionTakesString(string s);"
1329 "void run() { FunctionTakesString(GetStringByValue()); }",
1330 materializeTemporaryExpr()));
1331
1332 EXPECT_TRUE(
1333 notMatches(ClassString +
1334 "string* GetStringPointer(); "
1335 "void FunctionTakesStringPtr(string* s);"
1336 "void run() {"
1337 " string* s = GetStringPointer();"
1338 " FunctionTakesStringPtr(GetStringPointer());"
1339 " FunctionTakesStringPtr(s);"
1340 "}",
1341 materializeTemporaryExpr()));
1342
1343 EXPECT_TRUE(
1344 notMatches(ClassString +
1345 "string GetStringByValue();"
1346 "void run() { int k = GetStringByValue().length(); }",
1347 materializeTemporaryExpr()));
1348
1349 EXPECT_TRUE(
1350 notMatches(ClassString +
1351 "string GetStringByValue();"
1352 "void run() { GetStringByValue(); }",
1353 materializeTemporaryExpr()));
1354}
1355
Manuel Klimek4da21662012-07-06 05:48:52 +00001356TEST(ConstructorDeclaration, SimpleCase) {
1357 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001358 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001359 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001360 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001361}
1362
1363TEST(ConstructorDeclaration, IsImplicit) {
1364 // This one doesn't match because the constructor is not added by the
1365 // compiler (it is not needed).
1366 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001367 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001368 // The compiler added the implicit default constructor.
1369 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001370 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001371 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001372 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001373}
1374
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001375TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1376 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001377 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001378}
1379
1380TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001381 EXPECT_TRUE(notMatches("class Foo {};",
1382 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001383}
1384
Manuel Klimek4da21662012-07-06 05:48:52 +00001385TEST(HasAnyConstructorInitializer, SimpleCase) {
1386 EXPECT_TRUE(notMatches(
1387 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001388 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001389 EXPECT_TRUE(matches(
1390 "class Foo {"
1391 " Foo() : foo_() { }"
1392 " int foo_;"
1393 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001394 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001395}
1396
1397TEST(HasAnyConstructorInitializer, ForField) {
1398 static const char Code[] =
1399 "class Baz { };"
1400 "class Foo {"
1401 " Foo() : foo_() { }"
1402 " Baz foo_;"
1403 " Baz bar_;"
1404 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001405 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1406 forField(hasType(recordDecl(hasName("Baz"))))))));
1407 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001408 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001409 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1410 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001411}
1412
1413TEST(HasAnyConstructorInitializer, WithInitializer) {
1414 static const char Code[] =
1415 "class Foo {"
1416 " Foo() : foo_(0) { }"
1417 " int foo_;"
1418 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001419 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001420 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001421 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001422 withInitializer(integerLiteral(equals(1)))))));
1423}
1424
1425TEST(HasAnyConstructorInitializer, IsWritten) {
1426 static const char Code[] =
1427 "struct Bar { Bar(){} };"
1428 "class Foo {"
1429 " Foo() : foo_() { }"
1430 " Bar foo_;"
1431 " Bar bar_;"
1432 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001433 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001434 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001435 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001436 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001437 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001438 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1439}
1440
1441TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001442 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001443
1444 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1445 EXPECT_TRUE(
1446 matches("class X { public: X(); }; void x() { new X(); }", New));
1447 EXPECT_TRUE(
1448 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1449 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1450}
1451
1452TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001453 StatementMatcher New = constructExpr(
1454 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001455
1456 EXPECT_TRUE(
1457 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1458 New));
1459 EXPECT_TRUE(
1460 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1461 New));
1462 EXPECT_TRUE(
1463 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1464 New));
1465
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001466 StatementMatcher WrongIndex = constructExpr(
1467 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001468 EXPECT_TRUE(
1469 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1470 WrongIndex));
1471}
1472
1473TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001474 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001475
1476 EXPECT_TRUE(
1477 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1478 EXPECT_TRUE(
1479 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1480 New));
1481}
1482
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001483TEST(Matcher, DeleteExpression) {
1484 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001485 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001486}
1487
Manuel Klimek4da21662012-07-06 05:48:52 +00001488TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001489 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001490
1491 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1492 EXPECT_TRUE(
1493 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1494 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1495}
1496
1497TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001498 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001499 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1500 // wide string
1501 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1502 // with escaped characters
1503 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1504 // no matching -- though the data type is the same, there is no string literal
1505 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1506}
1507
1508TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001509 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001510 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1511 // wide character
1512 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1513 // wide character, Hex encoded, NOT MATCHED!
1514 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1515 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1516}
1517
1518TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001519 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001520 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1521 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1522 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1523 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1524
1525 // Non-matching cases (character literals, float and double)
1526 EXPECT_TRUE(notMatches("int i = L'a';",
1527 HasIntLiteral)); // this is actually a character
1528 // literal cast to int
1529 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1530 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1531 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1532}
1533
1534TEST(Matcher, Conditions) {
1535 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1536
1537 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1538 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1539 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1540 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1541 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1542}
1543
1544TEST(MatchBinaryOperator, HasOperatorName) {
1545 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1546
1547 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1548 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1549}
1550
1551TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1552 StatementMatcher OperatorTrueFalse =
1553 binaryOperator(hasLHS(boolLiteral(equals(true))),
1554 hasRHS(boolLiteral(equals(false))));
1555
1556 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1557 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1558 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1559}
1560
1561TEST(MatchBinaryOperator, HasEitherOperand) {
1562 StatementMatcher HasOperand =
1563 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1564
1565 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1566 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1567 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1568}
1569
1570TEST(Matcher, BinaryOperatorTypes) {
1571 // Integration test that verifies the AST provides all binary operators in
1572 // a way we expect.
1573 // FIXME: Operator ','
1574 EXPECT_TRUE(
1575 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1576 EXPECT_TRUE(
1577 matches("bool b; bool c = (b = true);",
1578 binaryOperator(hasOperatorName("="))));
1579 EXPECT_TRUE(
1580 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1581 EXPECT_TRUE(
1582 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1583 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1584 EXPECT_TRUE(
1585 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1586 EXPECT_TRUE(
1587 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1588 EXPECT_TRUE(
1589 matches("int i = 1; int j = (i <<= 2);",
1590 binaryOperator(hasOperatorName("<<="))));
1591 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1592 EXPECT_TRUE(
1593 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1594 EXPECT_TRUE(
1595 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1596 EXPECT_TRUE(
1597 matches("int i = 1; int j = (i >>= 2);",
1598 binaryOperator(hasOperatorName(">>="))));
1599 EXPECT_TRUE(
1600 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1601 EXPECT_TRUE(
1602 matches("int i = 42; int j = (i ^= 42);",
1603 binaryOperator(hasOperatorName("^="))));
1604 EXPECT_TRUE(
1605 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1606 EXPECT_TRUE(
1607 matches("int i = 42; int j = (i %= 42);",
1608 binaryOperator(hasOperatorName("%="))));
1609 EXPECT_TRUE(
1610 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1611 EXPECT_TRUE(
1612 matches("bool b = true && false;",
1613 binaryOperator(hasOperatorName("&&"))));
1614 EXPECT_TRUE(
1615 matches("bool b = true; bool c = (b &= false);",
1616 binaryOperator(hasOperatorName("&="))));
1617 EXPECT_TRUE(
1618 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1619 EXPECT_TRUE(
1620 matches("bool b = true || false;",
1621 binaryOperator(hasOperatorName("||"))));
1622 EXPECT_TRUE(
1623 matches("bool b = true; bool c = (b |= false);",
1624 binaryOperator(hasOperatorName("|="))));
1625 EXPECT_TRUE(
1626 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1627 EXPECT_TRUE(
1628 matches("int i = 42; int j = (i *= 23);",
1629 binaryOperator(hasOperatorName("*="))));
1630 EXPECT_TRUE(
1631 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1632 EXPECT_TRUE(
1633 matches("int i = 42; int j = (i /= 23);",
1634 binaryOperator(hasOperatorName("/="))));
1635 EXPECT_TRUE(
1636 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1637 EXPECT_TRUE(
1638 matches("int i = 42; int j = (i += 23);",
1639 binaryOperator(hasOperatorName("+="))));
1640 EXPECT_TRUE(
1641 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1642 EXPECT_TRUE(
1643 matches("int i = 42; int j = (i -= 23);",
1644 binaryOperator(hasOperatorName("-="))));
1645 EXPECT_TRUE(
1646 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1647 binaryOperator(hasOperatorName("->*"))));
1648 EXPECT_TRUE(
1649 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1650 binaryOperator(hasOperatorName(".*"))));
1651
1652 // Member expressions as operators are not supported in matches.
1653 EXPECT_TRUE(
1654 notMatches("struct A { void x(A *a) { a->x(this); } };",
1655 binaryOperator(hasOperatorName("->"))));
1656
1657 // Initializer assignments are not represented as operator equals.
1658 EXPECT_TRUE(
1659 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1660
1661 // Array indexing is not represented as operator.
1662 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1663
1664 // Overloaded operators do not match at all.
1665 EXPECT_TRUE(notMatches(
1666 "struct A { bool operator&&(const A &a) const { return false; } };"
1667 "void x() { A a, b; a && b; }",
1668 binaryOperator()));
1669}
1670
1671TEST(MatchUnaryOperator, HasOperatorName) {
1672 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1673
1674 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1675 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1676}
1677
1678TEST(MatchUnaryOperator, HasUnaryOperand) {
1679 StatementMatcher OperatorOnFalse =
1680 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1681
1682 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1683 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1684}
1685
1686TEST(Matcher, UnaryOperatorTypes) {
1687 // Integration test that verifies the AST provides all unary operators in
1688 // a way we expect.
1689 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1690 EXPECT_TRUE(
1691 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1692 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1693 EXPECT_TRUE(
1694 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1695 EXPECT_TRUE(
1696 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1697 EXPECT_TRUE(
1698 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1699 EXPECT_TRUE(
1700 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1701 EXPECT_TRUE(
1702 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1703 EXPECT_TRUE(
1704 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1705 EXPECT_TRUE(
1706 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1707
1708 // We don't match conversion operators.
1709 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1710
1711 // Function calls are not represented as operator.
1712 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1713
1714 // Overloaded operators do not match at all.
1715 // FIXME: We probably want to add that.
1716 EXPECT_TRUE(notMatches(
1717 "struct A { bool operator!() const { return false; } };"
1718 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1719}
1720
1721TEST(Matcher, ConditionalOperator) {
1722 StatementMatcher Conditional = conditionalOperator(
1723 hasCondition(boolLiteral(equals(true))),
1724 hasTrueExpression(boolLiteral(equals(false))));
1725
1726 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1727 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1728 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1729
1730 StatementMatcher ConditionalFalse = conditionalOperator(
1731 hasFalseExpression(boolLiteral(equals(false))));
1732
1733 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1734 EXPECT_TRUE(
1735 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1736}
1737
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001738TEST(ArraySubscriptMatchers, ArraySubscripts) {
1739 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1740 arraySubscriptExpr()));
1741 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1742 arraySubscriptExpr()));
1743}
1744
1745TEST(ArraySubscriptMatchers, ArrayIndex) {
1746 EXPECT_TRUE(matches(
1747 "int i[2]; void f() { i[1] = 1; }",
1748 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1749 EXPECT_TRUE(matches(
1750 "int i[2]; void f() { 1[i] = 1; }",
1751 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1752 EXPECT_TRUE(notMatches(
1753 "int i[2]; void f() { i[1] = 1; }",
1754 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1755}
1756
1757TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1758 EXPECT_TRUE(matches(
1759 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001760 arraySubscriptExpr(hasBase(implicitCastExpr(
1761 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001762}
1763
Manuel Klimek4da21662012-07-06 05:48:52 +00001764TEST(Matcher, HasNameSupportsNamespaces) {
1765 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001766 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001767 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001768 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001769 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001770 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001771 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001772 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001773 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001774 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001775 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001776 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001777 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001778 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001779 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001780 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001781 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001782 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001783 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001784 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001785 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001786 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001787 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001788 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001789}
1790
1791TEST(Matcher, HasNameSupportsOuterClasses) {
1792 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001793 matches("class A { class B { class C; }; };",
1794 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001795 EXPECT_TRUE(
1796 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001797 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001798 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001799 matches("class A { class B { class C; }; };",
1800 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001801 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001802 matches("class A { class B { class C; }; };",
1803 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001804 EXPECT_TRUE(
1805 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001806 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001807 EXPECT_TRUE(
1808 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001809 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001810 EXPECT_TRUE(
1811 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001812 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001813 EXPECT_TRUE(
1814 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001815 recordDecl(hasName("::C"))));
1816 EXPECT_TRUE(
1817 notMatches("class A { class B { class C; }; };",
1818 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001819 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001820 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001821 EXPECT_TRUE(
1822 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001823 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001824}
1825
1826TEST(Matcher, IsDefinition) {
1827 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001828 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001829 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1830 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1831
1832 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001833 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001834 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1835 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1836
1837 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001838 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001839 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1840 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1841}
1842
1843TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001844 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00001845 ofClass(hasName("X")))));
1846
1847 EXPECT_TRUE(
1848 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
1849 EXPECT_TRUE(
1850 matches("class X { public: X(); }; void x(int) { X x = X(); }",
1851 Constructor));
1852 EXPECT_TRUE(
1853 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
1854 Constructor));
1855}
1856
1857TEST(Matcher, VisitsTemplateInstantiations) {
1858 EXPECT_TRUE(matches(
1859 "class A { public: void x(); };"
1860 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001861 "void f() { B<A> b; b.y(); }",
1862 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001863
1864 EXPECT_TRUE(matches(
1865 "class A { public: void x(); };"
1866 "class C {"
1867 " public:"
1868 " template <typename T> class B { public: void y() { T t; t.x(); } };"
1869 "};"
1870 "void f() {"
1871 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001872 "}",
1873 recordDecl(hasName("C"),
1874 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001875}
1876
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001877TEST(Matcher, HandlesNullQualTypes) {
1878 // FIXME: Add a Type matcher so we can replace uses of this
1879 // variable with Type(True())
1880 const TypeMatcher AnyType = anything();
1881
1882 // We don't really care whether this matcher succeeds; we're testing that
1883 // it completes without crashing.
1884 EXPECT_TRUE(matches(
1885 "struct A { };"
1886 "template <typename T>"
1887 "void f(T t) {"
1888 " T local_t(t /* this becomes a null QualType in the AST */);"
1889 "}"
1890 "void g() {"
1891 " f(0);"
1892 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001893 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001894 anyOf(
1895 TypeMatcher(hasDeclaration(anything())),
1896 pointsTo(AnyType),
1897 references(AnyType)
1898 // Other QualType matchers should go here.
1899 ))))));
1900}
1901
Manuel Klimek4da21662012-07-06 05:48:52 +00001902// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001903AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00001904 // Make sure all special variables are used: node, match_finder,
1905 // bound_nodes_builder, and the parameter named 'AMatcher'.
1906 return AMatcher.matches(Node, Finder, Builder);
1907}
1908
1909TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001910 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001911
1912 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001913 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001914
1915 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001916 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001917
1918 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001919 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001920}
1921
1922AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001923 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
1924 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
1925 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00001926 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00001927 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00001928 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00001929 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
1930 ASTMatchFinder::BK_First);
1931}
1932
1933TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001934 DeclarationMatcher HasClassB =
1935 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001936
1937 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001938 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001939
1940 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001941 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001942
1943 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001944 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001945
1946 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001947 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001948
1949 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
1950}
1951
1952TEST(For, FindsForLoops) {
1953 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
1954 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
1955}
1956
Daniel Jasper6a124492012-07-12 08:50:38 +00001957TEST(For, ForLoopInternals) {
1958 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
1959 forStmt(hasCondition(anything()))));
1960 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
1961 forStmt(hasLoopInit(anything()))));
1962}
1963
1964TEST(For, NegativeForLoopInternals) {
1965 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001966 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001967 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
1968 forStmt(hasLoopInit(anything()))));
1969}
1970
Manuel Klimek4da21662012-07-06 05:48:52 +00001971TEST(For, ReportsNoFalsePositives) {
1972 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
1973 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
1974}
1975
1976TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001977 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
1978 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
1979 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001980}
1981
1982TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
1983 // It's not a compound statement just because there's "{}" in the source
1984 // text. This is an AST search, not grep.
1985 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001986 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001987 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001988 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001989}
1990
Daniel Jasper6a124492012-07-12 08:50:38 +00001991TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00001992 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001993 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001994 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001995 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001996 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001997 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001998 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001999 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002000}
2001
2002TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2003 // The simplest case: every compound statement is in a function
2004 // definition, and the function body itself must be a compound
2005 // statement.
2006 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002007 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002008}
2009
2010TEST(HasAnySubstatement, IsNotRecursive) {
2011 // It's really "has any immediate substatement".
2012 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002013 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002014}
2015
2016TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2017 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002018 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002019}
2020
2021TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2022 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002023 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002024}
2025
2026TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2027 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002028 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002029 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002030 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002031}
2032
2033TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2034 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002035 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002036 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002037 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002038 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002039 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002040}
2041
2042TEST(StatementCountIs, WorksWithMultipleStatements) {
2043 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002044 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002045}
2046
2047TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2048 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002049 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002050 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002051 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002052 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002053 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002054 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002055 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002056}
2057
2058TEST(Member, WorksInSimplestCase) {
2059 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002060 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002061}
2062
2063TEST(Member, DoesNotMatchTheBaseExpression) {
2064 // Don't pick out the wrong part of the member expression, this should
2065 // be checking the member (name) only.
2066 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002067 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002068}
2069
2070TEST(Member, MatchesInMemberFunctionCall) {
2071 EXPECT_TRUE(matches("void f() {"
2072 " struct { void first() {}; } s;"
2073 " s.first();"
2074 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002075 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002076}
2077
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002078TEST(Member, MatchesMemberAllocationFunction) {
Dmitri Gribenko02ed37f2012-08-18 00:41:04 +00002079 EXPECT_TRUE(matches("namespace std { typedef typeof(sizeof(int)) size_t; }"
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002080 "class X { void *operator new(std::size_t); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002081 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002082
2083 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002084 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002085
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002086 EXPECT_TRUE(matches(
2087 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2088 "class X { void operator delete[](void*, std::size_t); };",
2089 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002090}
2091
Manuel Klimek4da21662012-07-06 05:48:52 +00002092TEST(HasObjectExpression, DoesNotMatchMember) {
2093 EXPECT_TRUE(notMatches(
2094 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002095 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002096}
2097
2098TEST(HasObjectExpression, MatchesBaseOfVariable) {
2099 EXPECT_TRUE(matches(
2100 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002101 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002102 EXPECT_TRUE(matches(
2103 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002104 memberExpr(hasObjectExpression(
2105 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002106}
2107
2108TEST(HasObjectExpression,
2109 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2110 EXPECT_TRUE(matches(
2111 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002112 memberExpr(hasObjectExpression(
2113 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002114 EXPECT_TRUE(matches(
2115 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002116 memberExpr(hasObjectExpression(
2117 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002118}
2119
2120TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002121 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2122 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2123 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2124 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002125}
2126
2127TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002128 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002129}
2130
2131TEST(IsConstQualified, MatchesConstInt) {
2132 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002133 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002134}
2135
2136TEST(IsConstQualified, MatchesConstPointer) {
2137 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002138 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002139}
2140
2141TEST(IsConstQualified, MatchesThroughTypedef) {
2142 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002143 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002144 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002145 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002146}
2147
2148TEST(IsConstQualified, DoesNotMatchInappropriately) {
2149 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002150 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002151 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002152 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002153}
2154
Sam Panzer089e5b32012-08-16 16:58:10 +00002155TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002156 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2157 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2158 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2159 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002160}
2161TEST(CastExpression, MatchesImplicitCasts) {
2162 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002163 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002164 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002165 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002166}
2167
2168TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002169 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2170 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2171 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2172 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002173}
2174
Manuel Klimek4da21662012-07-06 05:48:52 +00002175TEST(ReinterpretCast, MatchesSimpleCase) {
2176 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002177 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002178}
2179
2180TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002181 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002182 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002183 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002184 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002185 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002186 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2187 "B b;"
2188 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002189 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002190}
2191
2192TEST(FunctionalCast, MatchesSimpleCase) {
2193 std::string foo_class = "class Foo { public: Foo(char*); };";
2194 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002195 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002196}
2197
2198TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2199 std::string FooClass = "class Foo { public: Foo(char*); };";
2200 EXPECT_TRUE(
2201 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002202 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002203 EXPECT_TRUE(
2204 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002205 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002206}
2207
2208TEST(DynamicCast, MatchesSimpleCase) {
2209 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2210 "B b;"
2211 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002212 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002213}
2214
2215TEST(StaticCast, MatchesSimpleCase) {
2216 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002217 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002218}
2219
2220TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002221 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002222 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002223 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002224 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002225 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002226 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2227 "B b;"
2228 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002229 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002230}
2231
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002232TEST(CStyleCast, MatchesSimpleCase) {
2233 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2234}
2235
2236TEST(CStyleCast, DoesNotMatchOtherCasts) {
2237 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2238 "char q, *r = const_cast<char*>(&q);"
2239 "void* s = reinterpret_cast<char*>(&s);"
2240 "struct B { virtual ~B() {} }; struct D : B {};"
2241 "B b;"
2242 "D* t = dynamic_cast<D*>(&b);",
2243 cStyleCastExpr()));
2244}
2245
Manuel Klimek4da21662012-07-06 05:48:52 +00002246TEST(HasDestinationType, MatchesSimpleCase) {
2247 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002248 staticCastExpr(hasDestinationType(
2249 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002250}
2251
Sam Panzer089e5b32012-08-16 16:58:10 +00002252TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2253 // This test creates an implicit const cast.
2254 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002255 implicitCastExpr(
2256 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002257 // This test creates an implicit array-to-pointer cast.
2258 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002259 implicitCastExpr(hasImplicitDestinationType(
2260 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002261}
2262
2263TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2264 // This test creates an implicit cast from int to char.
2265 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002266 implicitCastExpr(hasImplicitDestinationType(
2267 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002268 // This test creates an implicit array-to-pointer cast.
2269 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002270 implicitCastExpr(hasImplicitDestinationType(
2271 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002272}
2273
2274TEST(ImplicitCast, MatchesSimpleCase) {
2275 // This test creates an implicit const cast.
2276 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002277 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002278 // This test creates an implicit cast from int to char.
2279 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002280 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002281 // This test creates an implicit array-to-pointer cast.
2282 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002283 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002284}
2285
2286TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002287 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002288 // are present, and that it ignores explicit and paren casts.
2289
2290 // These two test cases have no casts.
2291 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002292 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002293 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002294 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002295
2296 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002297 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002298 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002299 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002300
2301 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002302 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002303}
2304
2305TEST(IgnoringImpCasts, MatchesImpCasts) {
2306 // This test checks that ignoringImpCasts matches when implicit casts are
2307 // present and its inner matcher alone does not match.
2308 // Note that this test creates an implicit const cast.
2309 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002310 varDecl(hasInitializer(ignoringImpCasts(
2311 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002312 // This test creates an implict cast from int to char.
2313 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002314 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002315 integerLiteral(equals(0)))))));
2316}
2317
2318TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2319 // These tests verify that ignoringImpCasts does not match if the inner
2320 // matcher does not match.
2321 // Note that the first test creates an implicit const cast.
2322 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002323 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002324 unless(anything()))))));
2325 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002326 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002327 unless(anything()))))));
2328
2329 // These tests verify that ignoringImplictCasts does not look through explicit
2330 // casts or parentheses.
2331 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002332 varDecl(hasInitializer(ignoringImpCasts(
2333 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002334 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002335 varDecl(hasInitializer(ignoringImpCasts(
2336 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002337 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002338 varDecl(hasInitializer(ignoringImpCasts(
2339 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002340 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002341 varDecl(hasInitializer(ignoringImpCasts(
2342 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002343}
2344
2345TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2346 // This test verifies that expressions that do not have implicit casts
2347 // still match the inner matcher.
2348 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002349 varDecl(hasInitializer(ignoringImpCasts(
2350 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002351}
2352
2353TEST(IgnoringParenCasts, MatchesParenCasts) {
2354 // This test checks that ignoringParenCasts matches when parentheses and/or
2355 // casts are present and its inner matcher alone does not match.
2356 EXPECT_TRUE(matches("int 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 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002360 varDecl(hasInitializer(ignoringParenCasts(
2361 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002362
2363 // This test creates an implict cast from int to char in addition to the
2364 // parentheses.
2365 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002366 varDecl(hasInitializer(ignoringParenCasts(
2367 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002368
2369 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002370 varDecl(hasInitializer(ignoringParenCasts(
2371 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002372 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002373 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002374 integerLiteral(equals(0)))))));
2375}
2376
2377TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2378 // This test verifies that expressions that do not have any casts still match.
2379 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002380 varDecl(hasInitializer(ignoringParenCasts(
2381 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002382}
2383
2384TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2385 // These tests verify that ignoringImpCasts does not match if the inner
2386 // matcher does not match.
2387 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002388 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002389 unless(anything()))))));
2390
2391 // This test creates an implicit cast from int to char in addition to the
2392 // parentheses.
2393 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002394 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002395 unless(anything()))))));
2396
2397 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002398 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002399 unless(anything()))))));
2400}
2401
2402TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2403 // This test checks that ignoringParenAndImpCasts matches when
2404 // parentheses and/or implicit casts are present and its inner matcher alone
2405 // does not match.
2406 // Note that this test creates an implicit const cast.
2407 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002408 varDecl(hasInitializer(ignoringParenImpCasts(
2409 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002410 // This test creates an implicit cast from int to char.
2411 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002412 varDecl(hasInitializer(ignoringParenImpCasts(
2413 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002414}
2415
2416TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2417 // This test verifies that expressions that do not have parentheses or
2418 // implicit casts still match.
2419 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002420 varDecl(hasInitializer(ignoringParenImpCasts(
2421 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002422 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002423 varDecl(hasInitializer(ignoringParenImpCasts(
2424 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002425}
2426
2427TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2428 // These tests verify that ignoringParenImpCasts does not match if
2429 // the inner matcher does not match.
2430 // This test creates an implicit cast.
2431 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002432 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002433 unless(anything()))))));
2434 // These tests verify that ignoringParenAndImplictCasts does not look
2435 // through explicit casts.
2436 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002437 varDecl(hasInitializer(ignoringParenImpCasts(
2438 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002439 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002440 varDecl(hasInitializer(ignoringParenImpCasts(
2441 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002442 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002443 varDecl(hasInitializer(ignoringParenImpCasts(
2444 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002445}
2446
Manuel Klimek715c9562012-07-25 10:02:02 +00002447TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002448 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2449 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002450 implicitCastExpr(
2451 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002452}
2453
Manuel Klimek715c9562012-07-25 10:02:02 +00002454TEST(HasSourceExpression, MatchesExplicitCasts) {
2455 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002456 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002457 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002458 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002459}
2460
Manuel Klimek4da21662012-07-06 05:48:52 +00002461TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002462 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002463}
2464
2465TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002466 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002467}
2468
2469TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002470 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002471}
2472
2473TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002474 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002475}
2476
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002477TEST(InitListExpression, MatchesInitListExpression) {
2478 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2479 initListExpr(hasType(asString("int [2]")))));
2480 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002481 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002482}
2483
2484TEST(UsingDeclaration, MatchesUsingDeclarations) {
2485 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2486 usingDecl()));
2487}
2488
2489TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2490 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2491 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2492}
2493
2494TEST(UsingDeclaration, MatchesSpecificTarget) {
2495 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2496 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002497 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002498 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2499 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002500 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002501}
2502
2503TEST(UsingDeclaration, ThroughUsingDeclaration) {
2504 EXPECT_TRUE(matches(
2505 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002506 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002507 EXPECT_TRUE(notMatches(
2508 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002509 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002510}
2511
Sam Panzer425f41b2012-08-16 17:20:59 +00002512TEST(SingleDecl, IsSingleDecl) {
2513 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002514 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002515 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2516 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2517 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2518 SingleDeclStmt));
2519}
2520
2521TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002522 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002523
2524 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002525 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002526 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002527 declStmt(containsDeclaration(0, MatchesInit),
2528 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002529 unsigned WrongIndex = 42;
2530 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002531 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002532 MatchesInit))));
2533}
2534
2535TEST(DeclCount, DeclCountIsCorrect) {
2536 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002537 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002538 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002539 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002540 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002541 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002542}
2543
Manuel Klimek4da21662012-07-06 05:48:52 +00002544TEST(While, MatchesWhileLoops) {
2545 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2546 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2547 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2548}
2549
2550TEST(Do, MatchesDoLoops) {
2551 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2552 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2553}
2554
2555TEST(Do, DoesNotMatchWhileLoops) {
2556 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2557}
2558
2559TEST(SwitchCase, MatchesCase) {
2560 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2561 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2562 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2563 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2564}
2565
2566TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2567 EXPECT_TRUE(notMatches(
2568 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002569 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002570 EXPECT_TRUE(notMatches(
2571 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002572 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002573}
2574
2575TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2576 EXPECT_TRUE(matches(
2577 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002578 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002579}
2580
2581TEST(ForEach, BindsOneNode) {
2582 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002583 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002584 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002585}
2586
2587TEST(ForEach, BindsMultipleNodes) {
2588 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002589 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002590 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002591}
2592
2593TEST(ForEach, BindsRecursiveCombinations) {
2594 EXPECT_TRUE(matchAndVerifyResultTrue(
2595 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002596 recordDecl(hasName("C"),
2597 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002598 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002599}
2600
2601TEST(ForEachDescendant, BindsOneNode) {
2602 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002603 recordDecl(hasName("C"),
2604 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002605 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002606}
2607
2608TEST(ForEachDescendant, BindsMultipleNodes) {
2609 EXPECT_TRUE(matchAndVerifyResultTrue(
2610 "class C { class D { int x; int y; }; "
2611 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002612 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002613 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002614}
2615
2616TEST(ForEachDescendant, BindsRecursiveCombinations) {
2617 EXPECT_TRUE(matchAndVerifyResultTrue(
2618 "class C { class D { "
2619 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002620 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2621 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002622 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002623}
2624
2625
2626TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2627 // Make sure that we can both match the class by name (::X) and by the type
2628 // the template was instantiated with (via a field).
2629
2630 EXPECT_TRUE(matches(
2631 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002632 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002633
2634 EXPECT_TRUE(matches(
2635 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002636 recordDecl(isTemplateInstantiation(), hasDescendant(
2637 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002638}
2639
2640TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2641 EXPECT_TRUE(matches(
2642 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002643 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002644 isTemplateInstantiation())));
2645}
2646
2647TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2648 EXPECT_TRUE(matches(
2649 "template <typename T> class X { T t; }; class A {};"
2650 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002651 recordDecl(isTemplateInstantiation(), hasDescendant(
2652 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002653}
2654
2655TEST(IsTemplateInstantiation,
2656 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2657 EXPECT_TRUE(matches(
2658 "template <typename T> class X {};"
2659 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002660 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002661}
2662
2663TEST(IsTemplateInstantiation,
2664 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2665 EXPECT_TRUE(matches(
2666 "class A {};"
2667 "class X {"
2668 " template <typename U> class Y { U u; };"
2669 " Y<A> y;"
2670 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002671 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002672}
2673
2674TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2675 // FIXME: Figure out whether this makes sense. It doesn't affect the
2676 // normal use case as long as the uppermost instantiation always is marked
2677 // as template instantiation, but it might be confusing as a predicate.
2678 EXPECT_TRUE(matches(
2679 "class A {};"
2680 "template <typename T> class X {"
2681 " template <typename U> class Y { U u; };"
2682 " Y<T> y;"
2683 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002684 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002685}
2686
2687TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2688 EXPECT_TRUE(notMatches(
2689 "template <typename T> class X {}; class A {};"
2690 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002691 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002692}
2693
2694TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2695 EXPECT_TRUE(notMatches(
2696 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002697 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002698}
2699
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002700TEST(IsExplicitTemplateSpecialization,
2701 DoesNotMatchPrimaryTemplate) {
2702 EXPECT_TRUE(notMatches(
2703 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002704 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002705 EXPECT_TRUE(notMatches(
2706 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002707 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002708}
2709
2710TEST(IsExplicitTemplateSpecialization,
2711 DoesNotMatchExplicitTemplateInstantiations) {
2712 EXPECT_TRUE(notMatches(
2713 "template <typename T> class X {};"
2714 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002715 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002716 EXPECT_TRUE(notMatches(
2717 "template <typename T> void f(T t) {}"
2718 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002719 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002720}
2721
2722TEST(IsExplicitTemplateSpecialization,
2723 DoesNotMatchImplicitTemplateInstantiations) {
2724 EXPECT_TRUE(notMatches(
2725 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002726 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002727 EXPECT_TRUE(notMatches(
2728 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002729 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002730}
2731
2732TEST(IsExplicitTemplateSpecialization,
2733 MatchesExplicitTemplateSpecializations) {
2734 EXPECT_TRUE(matches(
2735 "template <typename T> class X {};"
2736 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002737 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002738 EXPECT_TRUE(matches(
2739 "template <typename T> void f(T t) {}"
2740 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002741 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002742}
2743
Manuel Klimek579b1202012-09-07 09:26:10 +00002744TEST(HasAncenstor, MatchesDeclarationAncestors) {
2745 EXPECT_TRUE(matches(
2746 "class A { class B { class C {}; }; };",
2747 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2748}
2749
2750TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2751 EXPECT_TRUE(notMatches(
2752 "class A { class B { class C {}; }; };",
2753 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2754}
2755
2756TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2757 EXPECT_TRUE(matches(
2758 "class A { class B { void f() { C c; } class C {}; }; };",
2759 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2760 hasAncestor(recordDecl(hasName("A"))))))));
2761}
2762
2763TEST(HasAncenstor, MatchesStatementAncestors) {
2764 EXPECT_TRUE(matches(
2765 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002766 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002767}
2768
2769TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2770 EXPECT_TRUE(matches(
2771 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002772 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002773}
2774
2775TEST(HasAncestor, BindsRecursiveCombinations) {
2776 EXPECT_TRUE(matchAndVerifyResultTrue(
2777 "class C { class D { class E { class F { int y; }; }; }; };",
2778 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002779 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00002780}
2781
2782TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
2783 EXPECT_TRUE(matchAndVerifyResultTrue(
2784 "class C { class D { class E { class F { int y; }; }; }; };",
2785 fieldDecl(hasAncestor(
2786 decl(
2787 hasDescendant(recordDecl(isDefinition(),
2788 hasAncestor(recordDecl())))
2789 ).bind("d")
2790 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00002791 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00002792}
2793
2794TEST(HasAncestor, MatchesInTemplateInstantiations) {
2795 EXPECT_TRUE(matches(
2796 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
2797 "A<int>::B::C a;",
2798 fieldDecl(hasType(asString("int")),
2799 hasAncestor(recordDecl(hasName("A"))))));
2800}
2801
2802TEST(HasAncestor, MatchesInImplicitCode) {
2803 EXPECT_TRUE(matches(
2804 "struct X {}; struct A { A() {} X x; };",
2805 constructorDecl(
2806 hasAnyConstructorInitializer(withInitializer(expr(
2807 hasAncestor(recordDecl(hasName("A")))))))));
2808}
2809
Daniel Jaspera7564432012-09-13 13:11:25 +00002810TEST(NNS, MatchesNestedNameSpecifiers) {
2811 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
2812 nestedNameSpecifier()));
2813 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
2814 nestedNameSpecifier()));
2815 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
2816 nestedNameSpecifier()));
2817
2818 EXPECT_TRUE(matches(
2819 "struct A { static void f() {} }; void g() { A::f(); }",
2820 nestedNameSpecifier()));
2821 EXPECT_TRUE(notMatches(
2822 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
2823 nestedNameSpecifier()));
2824}
2825
2826TEST(NNS, MatchesTypes) {
2827 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
2828 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
2829 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
2830 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
2831 Matcher));
2832 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
2833}
2834
2835TEST(NNS, MatchesNamespaceDecls) {
2836 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
2837 specifiesNamespace(hasName("ns")));
2838 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
2839 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
2840 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
2841}
2842
2843TEST(NNS, BindsNestedNameSpecifiers) {
2844 EXPECT_TRUE(matchAndVerifyResultTrue(
2845 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
2846 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
2847 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
2848}
2849
2850TEST(NNS, BindsNestedNameSpecifierLocs) {
2851 EXPECT_TRUE(matchAndVerifyResultTrue(
2852 "namespace ns { struct B {}; } ns::B b;",
2853 loc(nestedNameSpecifier()).bind("loc"),
2854 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
2855}
2856
2857TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
2858 EXPECT_TRUE(matches(
2859 "struct A { struct B { struct C {}; }; }; A::B::C c;",
2860 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
2861 EXPECT_TRUE(matches(
2862 "struct A { struct B { struct C {}; }; }; A::B::C c;",
2863 nestedNameSpecifierLoc(hasPrefix(loc(
2864 specifiesType(asString("struct A")))))));
2865}
2866
Manuel Klimek4da21662012-07-06 05:48:52 +00002867} // end namespace ast_matchers
2868} // end namespace clang