blob: 3eddccc652a842a4f2c7af2b0b8757d65494b87b [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
Daniel Jasper08f0c532012-09-18 14:17:42 +0000297TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
298 EXPECT_TRUE(matches(
299 "template <typename T> struct A {"
300 " template <typename T2> struct F {};"
301 "};"
302 "template <typename T> struct B : A<T>::template F<T> {};"
303 "B<int> b;",
304 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
305}
306
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000307TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000308 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000309 EXPECT_TRUE(notMatches("class X;", ClassX));
310 EXPECT_TRUE(notMatches("class X {};", ClassX));
311}
312
313TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000314 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000315 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
316 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
317}
318
319TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
320 EXPECT_TRUE(notMatches("template<typename T> class X { };"
321 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000322 classTemplateDecl(hasName("X"),
323 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000324}
325
326TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
327 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
328 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000329 classTemplateDecl(hasName("X"),
330 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000331}
332
Daniel Jasper6a124492012-07-12 08:50:38 +0000333TEST(AllOf, AllOverloadsWork) {
334 const char Program[] =
335 "struct T { }; int f(int, T*); void g(int x) { T t; f(x, &t); }";
336 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000337 callExpr(allOf(callee(functionDecl(hasName("f"))),
338 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000339 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000340 callExpr(allOf(callee(functionDecl(hasName("f"))),
341 hasArgument(0, declRefExpr(to(varDecl()))),
342 hasArgument(1, hasType(pointsTo(
343 recordDecl(hasName("T")))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000344}
345
Manuel Klimek4da21662012-07-06 05:48:52 +0000346TEST(DeclarationMatcher, MatchAnyOf) {
347 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000348 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000349 EXPECT_TRUE(
350 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
351 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
352 EXPECT_TRUE(
353 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
354 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
355
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000356 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000357 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000358 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
359 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
360
Manuel Klimek4da21662012-07-06 05:48:52 +0000361 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000362 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
363 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000364 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
365 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
366 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
367 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
368 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
369 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
370}
371
372TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000373 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000374 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
375 EXPECT_TRUE(matches("class X {};", HasClassX));
376
377 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000378 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000379 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
380 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
381 EXPECT_TRUE(
382 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
383}
384
385TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
386 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000387 recordDecl(
388 has(recordDecl(
389 has(recordDecl(hasName("X"))),
390 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000391 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000392 has(recordDecl(
393 has(recordDecl(hasName("A"))),
394 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000395 hasName("C"))),
396 hasName("F"));
397
398 EXPECT_TRUE(matches(
399 "class F {"
400 " class Z {"
401 " class X {};"
402 " class Y {};"
403 " };"
404 " class C {"
405 " class A {};"
406 " class B {};"
407 " };"
408 "};", Recursive));
409
410 EXPECT_TRUE(matches(
411 "class F {"
412 " class Z {"
413 " class A {};"
414 " class X {};"
415 " class Y {};"
416 " };"
417 " class C {"
418 " class X {};"
419 " class A {};"
420 " class B {};"
421 " };"
422 "};", Recursive));
423
424 EXPECT_TRUE(matches(
425 "class O1 {"
426 " class O2 {"
427 " class F {"
428 " class Z {"
429 " class A {};"
430 " class X {};"
431 " class Y {};"
432 " };"
433 " class C {"
434 " class X {};"
435 " class A {};"
436 " class B {};"
437 " };"
438 " };"
439 " };"
440 "};", Recursive));
441}
442
443TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
444 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000445 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000446 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000447 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000448 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000449 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000450 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000451 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000452 hasName("Y"))),
453 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000454 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000455 anyOf(
456 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000457 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000458 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000459 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000460 hasName("B")))))),
461 hasName("F")));
462
463 EXPECT_TRUE(matches("class F {};", Recursive));
464 EXPECT_TRUE(matches("class Z {};", Recursive));
465 EXPECT_TRUE(matches("class C {};", Recursive));
466 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
467 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
468 EXPECT_TRUE(
469 matches("class O1 { class O2 {"
470 " class M { class N { class B {}; }; }; "
471 "}; };", Recursive));
472}
473
474TEST(DeclarationMatcher, MatchNot) {
475 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000476 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000477 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000478 unless(hasName("X")));
479 EXPECT_TRUE(notMatches("", NotClassX));
480 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
481 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
482 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
483 EXPECT_TRUE(
484 notMatches("class Y {}; class Z {}; class X : public Y {};",
485 NotClassX));
486
487 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000488 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000489 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000490 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000491 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000492 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000493 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
494 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
495 ClassXHasNotClassY));
496}
497
498TEST(DeclarationMatcher, HasDescendant) {
499 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000500 recordDecl(
501 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000502 hasName("Z"));
503 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
504 EXPECT_TRUE(
505 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
506 EXPECT_TRUE(
507 matches("class Z { class A { class Y { class X {}; }; }; };",
508 ZDescendantClassX));
509 EXPECT_TRUE(
510 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
511 ZDescendantClassX));
512 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
513
514 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000515 recordDecl(
516 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000517 hasName("X"))),
518 hasName("Z"));
519 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
520 ZDescendantClassXHasClassY));
521 EXPECT_TRUE(
522 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
523 ZDescendantClassXHasClassY));
524 EXPECT_TRUE(notMatches(
525 "class Z {"
526 " class A {"
527 " class B {"
528 " class X {"
529 " class C {"
530 " class Y {};"
531 " };"
532 " };"
533 " }; "
534 " };"
535 "};", ZDescendantClassXHasClassY));
536
537 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000538 recordDecl(
539 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
540 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000541 hasName("Z"));
542 EXPECT_TRUE(
543 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
544 ZDescendantClassXDescendantClassY));
545 EXPECT_TRUE(matches(
546 "class Z {"
547 " class A {"
548 " class X {"
549 " class B {"
550 " class Y {};"
551 " };"
552 " class Y {};"
553 " };"
554 " };"
555 "};", ZDescendantClassXDescendantClassY));
556}
557
Daniel Jaspera267cf62012-10-29 10:14:44 +0000558// Implements a run method that returns whether BoundNodes contains a
559// Decl bound to Id that can be dynamically cast to T.
560// Optionally checks that the check succeeded a specific number of times.
561template <typename T>
562class VerifyIdIsBoundTo : public BoundNodesCallback {
563public:
564 // Create an object that checks that a node of type \c T was bound to \c Id.
565 // Does not check for a certain number of matches.
566 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
567 : Id(Id), ExpectedCount(-1), Count(0) {}
568
569 // Create an object that checks that a node of type \c T was bound to \c Id.
570 // Checks that there were exactly \c ExpectedCount matches.
571 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
572 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
573
574 // Create an object that checks that a node of type \c T was bound to \c Id.
575 // Checks that there was exactly one match with the name \c ExpectedName.
576 // Note that \c T must be a NamedDecl for this to work.
577 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName)
578 : Id(Id), ExpectedCount(1), Count(0), ExpectedName(ExpectedName) {}
579
580 ~VerifyIdIsBoundTo() {
581 if (ExpectedCount != -1)
582 EXPECT_EQ(ExpectedCount, Count);
583 if (!ExpectedName.empty())
584 EXPECT_EQ(ExpectedName, Name);
585 }
586
587 virtual bool run(const BoundNodes *Nodes) {
588 if (Nodes->getNodeAs<T>(Id)) {
589 ++Count;
590 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
591 Name = Named->getNameAsString();
592 } else if (const NestedNameSpecifier *NNS =
593 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
594 llvm::raw_string_ostream OS(Name);
595 NNS->print(OS, PrintingPolicy(LangOptions()));
596 }
597 return true;
598 }
599 return false;
600 }
601
Daniel Jasper452abbc2012-10-29 10:48:25 +0000602 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
603 return run(Nodes);
604 }
605
Daniel Jaspera267cf62012-10-29 10:14:44 +0000606private:
607 const std::string Id;
608 const int ExpectedCount;
609 int Count;
610 const std::string ExpectedName;
611 std::string Name;
612};
613
614TEST(HasDescendant, MatchesDescendantTypes) {
615 EXPECT_TRUE(matches("void f() { int i = 3; }",
616 decl(hasDescendant(loc(builtinType())))));
617 EXPECT_TRUE(matches("void f() { int i = 3; }",
618 stmt(hasDescendant(builtinType()))));
619
620 EXPECT_TRUE(matches("void f() { int i = 3; }",
621 stmt(hasDescendant(loc(builtinType())))));
622 EXPECT_TRUE(matches("void f() { int i = 3; }",
623 stmt(hasDescendant(qualType(builtinType())))));
624
625 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
626 stmt(hasDescendant(isInteger()))));
627
628 EXPECT_TRUE(matchAndVerifyResultTrue(
629 "void f() { int a; float c; int d; int e; }",
630 functionDecl(forEachDescendant(
631 varDecl(hasDescendant(isInteger())).bind("x"))),
632 new VerifyIdIsBoundTo<Decl>("x", 3)));
633}
634
635TEST(HasDescendant, MatchesDescendantsOfTypes) {
636 EXPECT_TRUE(matches("void f() { int*** i; }",
637 qualType(hasDescendant(builtinType()))));
638 EXPECT_TRUE(matches("void f() { int*** i; }",
639 qualType(hasDescendant(
640 pointerType(pointee(builtinType()))))));
641 EXPECT_TRUE(matches("void f() { int*** i; }",
642 typeLoc(hasDescendant(builtinTypeLoc()))));
643
644 EXPECT_TRUE(matchAndVerifyResultTrue(
645 "void f() { int*** i; }",
646 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
647 new VerifyIdIsBoundTo<Type>("x", 2)));
648}
649
650TEST(Has, MatchesChildrenOfTypes) {
651 EXPECT_TRUE(matches("int i;",
652 varDecl(hasName("i"), has(isInteger()))));
653 EXPECT_TRUE(notMatches("int** i;",
654 varDecl(hasName("i"), has(isInteger()))));
655 EXPECT_TRUE(matchAndVerifyResultTrue(
656 "int (*f)(float, int);",
657 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
658 new VerifyIdIsBoundTo<QualType>("x", 2)));
659}
660
661TEST(Has, MatchesChildTypes) {
662 EXPECT_TRUE(matches(
663 "int* i;",
664 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
665 EXPECT_TRUE(notMatches(
666 "int* i;",
667 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
668}
669
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000670TEST(Enum, DoesNotMatchClasses) {
671 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
672}
673
674TEST(Enum, MatchesEnums) {
675 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
676}
677
678TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000679 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000680 EXPECT_TRUE(matches("enum X{ A };", Matcher));
681 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
682 EXPECT_TRUE(notMatches("enum X {};", Matcher));
683}
684
Manuel Klimek4da21662012-07-06 05:48:52 +0000685TEST(StatementMatcher, Has) {
686 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000687 expr(hasType(pointsTo(recordDecl(hasName("X")))),
688 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000689
690 EXPECT_TRUE(matches(
691 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
692 EXPECT_TRUE(notMatches(
693 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
694}
695
696TEST(StatementMatcher, HasDescendant) {
697 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000698 expr(hasType(pointsTo(recordDecl(hasName("X")))),
699 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000700
701 EXPECT_TRUE(matches(
702 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
703 HasDescendantVariableI));
704 EXPECT_TRUE(notMatches(
705 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
706 HasDescendantVariableI));
707}
708
709TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000710 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000711
712 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
713 EXPECT_TRUE(notMatches("class A {};", TypeA));
714
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000715 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000716
717 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
718 TypeDerivedFromA));
719 EXPECT_TRUE(notMatches("class A {};", TypeA));
720
721 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000722 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000723
724 EXPECT_TRUE(
725 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
726}
727
Manuel Klimek4da21662012-07-06 05:48:52 +0000728TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000729 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000730
731 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000732 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000733
734 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000735 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000736
737 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000738 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000739
740 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
741 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000742 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000743
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000744 StatementMatcher MethodX =
745 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000746
747 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
748 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000749 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000750}
751
752TEST(Matcher, BindTheSameNameInAlternatives) {
753 StatementMatcher matcher = anyOf(
754 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000755 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000756 hasRHS(integerLiteral(equals(0)))),
757 binaryOperator(hasOperatorName("+"),
758 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000759 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000760
761 EXPECT_TRUE(matchAndVerifyResultTrue(
762 // The first branch of the matcher binds x to 0 but then fails.
763 // The second branch binds x to f() and succeeds.
764 "int f() { return 0 + f(); }",
765 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000766 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000767}
768
Manuel Klimek66341c52012-08-30 19:41:06 +0000769TEST(Matcher, BindsIDForMemoizedResults) {
770 // Using the same matcher in two match expressions will make memoization
771 // kick in.
772 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
773 EXPECT_TRUE(matchAndVerifyResultTrue(
774 "class A { class B { class X {}; }; };",
775 DeclarationMatcher(anyOf(
776 recordDecl(hasName("A"), hasDescendant(ClassX)),
777 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000778 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000779}
780
Manuel Klimek4da21662012-07-06 05:48:52 +0000781TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000782 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000783 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000784 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000785 EXPECT_TRUE(
786 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000787 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000788 EXPECT_TRUE(
789 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000790 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000791}
792
793TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000794 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000795 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000796 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000797 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000798 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000799 EXPECT_TRUE(
800 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000801 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000802}
803
804TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000805 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000806 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000807 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000808 EXPECT_TRUE(
809 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000810 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000811}
812
813TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000814 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000815 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000816 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000817 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000818 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000819}
820
821TEST(Matcher, Call) {
822 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000823 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000824 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000825
826 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
827 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
828
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000829 StatementMatcher MethodOnY =
830 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000831
832 EXPECT_TRUE(
833 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
834 MethodOnY));
835 EXPECT_TRUE(
836 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
837 MethodOnY));
838 EXPECT_TRUE(
839 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
840 MethodOnY));
841 EXPECT_TRUE(
842 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
843 MethodOnY));
844 EXPECT_TRUE(
845 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
846 MethodOnY));
847
848 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000849 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000850
851 EXPECT_TRUE(
852 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
853 MethodOnYPointer));
854 EXPECT_TRUE(
855 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
856 MethodOnYPointer));
857 EXPECT_TRUE(
858 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
859 MethodOnYPointer));
860 EXPECT_TRUE(
861 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
862 MethodOnYPointer));
863 EXPECT_TRUE(
864 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
865 MethodOnYPointer));
866}
867
Daniel Jasper31f7c082012-10-01 13:40:41 +0000868TEST(Matcher, Lambda) {
869 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
870 lambdaExpr()));
871}
872
873TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000874 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
875 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000876 forRangeStmt()));
877 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
878 forRangeStmt()));
879}
880
881TEST(Matcher, UserDefinedLiteral) {
882 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
883 " return i + 1;"
884 "}"
885 "char c = 'a'_inc;",
886 userDefinedLiteral()));
887}
888
Daniel Jasperb54b7642012-09-20 14:12:57 +0000889TEST(Matcher, FlowControl) {
890 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
891 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
892 continueStmt()));
893 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
894 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
895 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
896}
897
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000898TEST(HasType, MatchesAsString) {
899 EXPECT_TRUE(
900 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000901 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000902 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000903 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000904 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000905 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000906 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000907 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000908}
909
Manuel Klimek4da21662012-07-06 05:48:52 +0000910TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000911 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000912 // Unary operator
913 EXPECT_TRUE(matches("class Y { }; "
914 "bool operator!(Y x) { return false; }; "
915 "Y y; bool c = !y;", OpCall));
916 // No match -- special operators like "new", "delete"
917 // FIXME: operator new takes size_t, for which we need stddef.h, for which
918 // we need to figure out include paths in the test.
919 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
920 // "class Y { }; "
921 // "void *operator new(size_t size) { return 0; } "
922 // "Y *y = new Y;", OpCall));
923 EXPECT_TRUE(notMatches("class Y { }; "
924 "void operator delete(void *p) { } "
925 "void a() {Y *y = new Y; delete y;}", OpCall));
926 // Binary operator
927 EXPECT_TRUE(matches("class Y { }; "
928 "bool operator&&(Y x, Y y) { return true; }; "
929 "Y a; Y b; bool c = a && b;",
930 OpCall));
931 // No match -- normal operator, not an overloaded one.
932 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
933 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
934}
935
936TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
937 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000938 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000939 EXPECT_TRUE(matches("class Y { }; "
940 "bool operator&&(Y x, Y y) { return true; }; "
941 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
942 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000943 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000944 EXPECT_TRUE(notMatches("class Y { }; "
945 "bool operator&&(Y x, Y y) { return true; }; "
946 "Y a; Y b; bool c = a && b;",
947 OpCallLessLess));
948}
949
Daniel Jasper278057f2012-11-15 03:29:05 +0000950TEST(Matcher, NestedOverloadedOperatorCalls) {
951 EXPECT_TRUE(matchAndVerifyResultTrue(
952 "class Y { }; "
953 "Y& operator&&(Y& x, Y& y) { return x; }; "
954 "Y a; Y b; Y c; Y d = a && b && c;",
955 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
956 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
957 EXPECT_TRUE(matches(
958 "class Y { }; "
959 "Y& operator&&(Y& x, Y& y) { return x; }; "
960 "Y a; Y b; Y c; Y d = a && b && c;",
961 operatorCallExpr(hasParent(operatorCallExpr()))));
962 EXPECT_TRUE(matches(
963 "class Y { }; "
964 "Y& operator&&(Y& x, Y& y) { return x; }; "
965 "Y a; Y b; Y c; Y d = a && b && c;",
966 operatorCallExpr(hasDescendant(operatorCallExpr()))));
967}
968
Manuel Klimek4da21662012-07-06 05:48:52 +0000969TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +0000970 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000971 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000972
973 EXPECT_TRUE(
974 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
975 MethodOnY));
976 EXPECT_TRUE(
977 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
978 MethodOnY));
979 EXPECT_TRUE(
980 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
981 MethodOnY));
982 EXPECT_TRUE(
983 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
984 MethodOnY));
985 EXPECT_TRUE(
986 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
987 MethodOnY));
988
989 EXPECT_TRUE(matches(
990 "class Y {"
991 " public: virtual void x();"
992 "};"
993 "class X : public Y {"
994 " public: virtual void x();"
995 "};"
996 "void z() { X *x; x->Y::x(); }", MethodOnY));
997}
998
999TEST(Matcher, VariableUsage) {
1000 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001001 declRefExpr(to(
1002 varDecl(hasInitializer(
1003 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001004
1005 EXPECT_TRUE(matches(
1006 "class Y {"
1007 " public:"
1008 " bool x() const;"
1009 "};"
1010 "void z(const Y &y) {"
1011 " bool b = y.x();"
1012 " if (b) {}"
1013 "}", Reference));
1014
1015 EXPECT_TRUE(notMatches(
1016 "class Y {"
1017 " public:"
1018 " bool x() const;"
1019 "};"
1020 "void z(const Y &y) {"
1021 " bool b = y.x();"
1022 "}", Reference));
1023}
1024
Daniel Jasper9bd28092012-07-30 05:03:25 +00001025TEST(Matcher, FindsVarDeclInFuncitonParameter) {
1026 EXPECT_TRUE(matches(
1027 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001028 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001029}
1030
Manuel Klimek4da21662012-07-06 05:48:52 +00001031TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001032 StatementMatcher CallOnVariableY =
1033 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001034
1035 EXPECT_TRUE(matches(
1036 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1037 EXPECT_TRUE(matches(
1038 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1039 EXPECT_TRUE(matches(
1040 "class Y { public: void x(); };"
1041 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1042 EXPECT_TRUE(matches(
1043 "class Y { public: void x(); };"
1044 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1045 EXPECT_TRUE(notMatches(
1046 "class Y { public: void x(); };"
1047 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1048 CallOnVariableY));
1049}
1050
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001051TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1052 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1053 unaryExprOrTypeTraitExpr()));
1054 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1055 alignOfExpr(anything())));
1056 // FIXME: Uncomment once alignof is enabled.
1057 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1058 // unaryExprOrTypeTraitExpr()));
1059 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1060 // sizeOfExpr()));
1061}
1062
1063TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1064 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1065 hasArgumentOfType(asString("int")))));
1066 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1067 hasArgumentOfType(asString("float")))));
1068 EXPECT_TRUE(matches(
1069 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001070 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001071 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001072 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001073}
1074
Manuel Klimek4da21662012-07-06 05:48:52 +00001075TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001076 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001077}
1078
1079TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001080 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001081}
1082
1083TEST(MemberExpression, MatchesVariable) {
1084 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001085 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001086 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001087 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001088 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001089 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001090}
1091
1092TEST(MemberExpression, MatchesStaticVariable) {
1093 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001094 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001095 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001096 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001097 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001098 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001099}
1100
Daniel Jasper6a124492012-07-12 08:50:38 +00001101TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001102 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1103 EXPECT_TRUE(matches(
1104 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1105 callExpr(hasArgument(0, declRefExpr(
1106 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001107}
1108
1109TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001110 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001111 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001112 callExpr(hasArgument(0, declRefExpr(
1113 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001114}
1115
Manuel Klimek4da21662012-07-06 05:48:52 +00001116TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1117 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001118 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001119 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001120 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001121 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001122 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001123}
1124
1125TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1126 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001127 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001128 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001129 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001130 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001131 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001132}
1133
1134TEST(IsArrow, MatchesMemberCallsViaArrow) {
1135 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001136 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001137 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001138 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001139 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001140 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001141}
1142
1143TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001144 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001145
1146 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1147 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1148}
1149
1150TEST(Callee, MatchesMemberExpressions) {
1151 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001152 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001153 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001154 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001155}
1156
1157TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001158 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001159
1160 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1161 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1162
Manuel Klimeke265c872012-07-10 14:21:30 +00001163#if !defined(_MSC_VER)
1164 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001165 // Dependent contexts, but a non-dependent call.
1166 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1167 CallFunctionF));
1168 EXPECT_TRUE(
1169 matches("void f(); template <int N> struct S { void g() { f(); } };",
1170 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001171#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001172
1173 // Depedent calls don't match.
1174 EXPECT_TRUE(
1175 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1176 CallFunctionF));
1177 EXPECT_TRUE(
1178 notMatches("void f(int);"
1179 "template <typename T> struct S { void g(T t) { f(t); } };",
1180 CallFunctionF));
1181}
1182
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001183TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1184 EXPECT_TRUE(
1185 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001186 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001187}
1188
1189TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1190 EXPECT_TRUE(
1191 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001192 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001193}
1194
1195TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1196 EXPECT_TRUE(
1197 notMatches("void g(); template <typename T> void f(T t) {}"
1198 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001199 functionTemplateDecl(hasName("f"),
1200 hasDescendant(declRefExpr(to(
1201 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001202}
1203
Manuel Klimek4da21662012-07-06 05:48:52 +00001204TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001205 StatementMatcher CallArgumentY = callExpr(
1206 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001207
1208 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1209 EXPECT_TRUE(
1210 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1211 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1212
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001213 StatementMatcher WrongIndex = callExpr(
1214 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001215 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1216}
1217
1218TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001219 StatementMatcher CallArgumentY = callExpr(
1220 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001221 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1222 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1223 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1224}
1225
1226TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001227 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001228
1229 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1230 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1231 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1232}
1233
1234TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001235 DeclarationMatcher ReferenceClassX = varDecl(
1236 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001237 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1238 ReferenceClassX));
1239 EXPECT_TRUE(
1240 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1241 EXPECT_TRUE(
1242 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1243 EXPECT_TRUE(
1244 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1245}
1246
1247TEST(HasParameter, CallsInnerMatcher) {
1248 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001249 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001250 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001251 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001252}
1253
1254TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1255 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001256 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001257}
1258
1259TEST(HasType, MatchesParameterVariableTypesStrictly) {
1260 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001261 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001262 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001263 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001264 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001265 methodDecl(hasParameter(0,
1266 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001267 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001268 methodDecl(hasParameter(0,
1269 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001270}
1271
1272TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1273 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001274 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001275 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001276 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001277}
1278
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001279TEST(Returns, MatchesReturnTypes) {
1280 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001281 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001282 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001283 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001284 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001285 functionDecl(returns(hasDeclaration(
1286 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001287}
1288
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001289TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001290 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1291 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1292 functionDecl(isExternC())));
1293 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001294}
1295
Manuel Klimek4da21662012-07-06 05:48:52 +00001296TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1297 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001298 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001299}
1300
1301TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1302 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001303 methodDecl(hasAnyParameter(hasType(pointsTo(
1304 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001305}
1306
1307TEST(HasName, MatchesParameterVariableDeclartions) {
1308 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001309 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001310 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001311 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001312}
1313
Daniel Jasper371f9392012-08-01 08:40:24 +00001314TEST(Matcher, MatchesClassTemplateSpecialization) {
1315 EXPECT_TRUE(matches("template<typename T> struct A {};"
1316 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001317 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001318 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001319 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001320 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001321 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001322}
1323
1324TEST(Matcher, MatchesTypeTemplateArgument) {
1325 EXPECT_TRUE(matches(
1326 "template<typename T> struct B {};"
1327 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001328 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001329 asString("int"))))));
1330}
1331
1332TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1333 EXPECT_TRUE(matches(
1334 "struct B { int next; };"
1335 "template<int(B::*next_ptr)> struct A {};"
1336 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001337 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1338 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001339
1340 EXPECT_TRUE(notMatches(
1341 "template <typename T> struct A {};"
1342 "A<int> a;",
1343 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1344 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001345}
1346
1347TEST(Matcher, MatchesSpecificArgument) {
1348 EXPECT_TRUE(matches(
1349 "template<typename T, typename U> class A {};"
1350 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001351 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001352 1, refersToType(asString("int"))))));
1353 EXPECT_TRUE(notMatches(
1354 "template<typename T, typename U> class A {};"
1355 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001356 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001357 1, refersToType(asString("int"))))));
1358}
1359
Manuel Klimek4da21662012-07-06 05:48:52 +00001360TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001361 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001362
1363 EXPECT_TRUE(
1364 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1365 EXPECT_TRUE(
1366 matches("class X { public: X(); }; void x() { X x = X(); }",
1367 Constructor));
1368 EXPECT_TRUE(
1369 matches("class X { public: X(int); }; void x() { X x = 0; }",
1370 Constructor));
1371 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1372}
1373
1374TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001375 StatementMatcher Constructor = constructExpr(
1376 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001377
1378 EXPECT_TRUE(
1379 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1380 Constructor));
1381 EXPECT_TRUE(
1382 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1383 Constructor));
1384 EXPECT_TRUE(
1385 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1386 Constructor));
1387 EXPECT_TRUE(
1388 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1389 Constructor));
1390
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001391 StatementMatcher WrongIndex = constructExpr(
1392 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001393 EXPECT_TRUE(
1394 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1395 WrongIndex));
1396}
1397
1398TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001399 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001400
1401 EXPECT_TRUE(
1402 matches("class X { public: X(int); }; void x() { X x(0); }",
1403 Constructor1Arg));
1404 EXPECT_TRUE(
1405 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1406 Constructor1Arg));
1407 EXPECT_TRUE(
1408 matches("class X { public: X(int); }; void x() { X x = 0; }",
1409 Constructor1Arg));
1410 EXPECT_TRUE(
1411 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1412 Constructor1Arg));
1413}
1414
Manuel Klimek70b9db92012-10-23 10:40:50 +00001415TEST(Matcher,ThisExpr) {
1416 EXPECT_TRUE(
1417 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1418 EXPECT_TRUE(
1419 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1420}
1421
Manuel Klimek4da21662012-07-06 05:48:52 +00001422TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001423 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001424
1425 std::string ClassString = "class string { public: string(); ~string(); }; ";
1426
1427 EXPECT_TRUE(
1428 matches(ClassString +
1429 "string GetStringByValue();"
1430 "void FunctionTakesString(string s);"
1431 "void run() { FunctionTakesString(GetStringByValue()); }",
1432 TempExpression));
1433
1434 EXPECT_TRUE(
1435 notMatches(ClassString +
1436 "string* GetStringPointer(); "
1437 "void FunctionTakesStringPtr(string* s);"
1438 "void run() {"
1439 " string* s = GetStringPointer();"
1440 " FunctionTakesStringPtr(GetStringPointer());"
1441 " FunctionTakesStringPtr(s);"
1442 "}",
1443 TempExpression));
1444
1445 EXPECT_TRUE(
1446 notMatches("class no_dtor {};"
1447 "no_dtor GetObjByValue();"
1448 "void ConsumeObj(no_dtor param);"
1449 "void run() { ConsumeObj(GetObjByValue()); }",
1450 TempExpression));
1451}
1452
Sam Panzere16acd32012-08-24 22:04:44 +00001453TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1454 std::string ClassString =
1455 "class string { public: string(); int length(); }; ";
1456
1457 EXPECT_TRUE(
1458 matches(ClassString +
1459 "string GetStringByValue();"
1460 "void FunctionTakesString(string s);"
1461 "void run() { FunctionTakesString(GetStringByValue()); }",
1462 materializeTemporaryExpr()));
1463
1464 EXPECT_TRUE(
1465 notMatches(ClassString +
1466 "string* GetStringPointer(); "
1467 "void FunctionTakesStringPtr(string* s);"
1468 "void run() {"
1469 " string* s = GetStringPointer();"
1470 " FunctionTakesStringPtr(GetStringPointer());"
1471 " FunctionTakesStringPtr(s);"
1472 "}",
1473 materializeTemporaryExpr()));
1474
1475 EXPECT_TRUE(
1476 notMatches(ClassString +
1477 "string GetStringByValue();"
1478 "void run() { int k = GetStringByValue().length(); }",
1479 materializeTemporaryExpr()));
1480
1481 EXPECT_TRUE(
1482 notMatches(ClassString +
1483 "string GetStringByValue();"
1484 "void run() { GetStringByValue(); }",
1485 materializeTemporaryExpr()));
1486}
1487
Manuel Klimek4da21662012-07-06 05:48:52 +00001488TEST(ConstructorDeclaration, SimpleCase) {
1489 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001490 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001491 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001492 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001493}
1494
1495TEST(ConstructorDeclaration, IsImplicit) {
1496 // This one doesn't match because the constructor is not added by the
1497 // compiler (it is not needed).
1498 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001499 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001500 // The compiler added the implicit default constructor.
1501 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001502 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001503 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001504 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001505}
1506
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001507TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1508 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001509 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001510}
1511
1512TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001513 EXPECT_TRUE(notMatches("class Foo {};",
1514 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001515}
1516
Manuel Klimek4da21662012-07-06 05:48:52 +00001517TEST(HasAnyConstructorInitializer, SimpleCase) {
1518 EXPECT_TRUE(notMatches(
1519 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001520 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001521 EXPECT_TRUE(matches(
1522 "class Foo {"
1523 " Foo() : foo_() { }"
1524 " int foo_;"
1525 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001526 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001527}
1528
1529TEST(HasAnyConstructorInitializer, ForField) {
1530 static const char Code[] =
1531 "class Baz { };"
1532 "class Foo {"
1533 " Foo() : foo_() { }"
1534 " Baz foo_;"
1535 " Baz bar_;"
1536 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001537 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1538 forField(hasType(recordDecl(hasName("Baz"))))))));
1539 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001540 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001541 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1542 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001543}
1544
1545TEST(HasAnyConstructorInitializer, WithInitializer) {
1546 static const char Code[] =
1547 "class Foo {"
1548 " Foo() : foo_(0) { }"
1549 " int foo_;"
1550 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001551 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001552 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001553 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001554 withInitializer(integerLiteral(equals(1)))))));
1555}
1556
1557TEST(HasAnyConstructorInitializer, IsWritten) {
1558 static const char Code[] =
1559 "struct Bar { Bar(){} };"
1560 "class Foo {"
1561 " Foo() : foo_() { }"
1562 " Bar foo_;"
1563 " Bar bar_;"
1564 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001565 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001566 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001567 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001568 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001569 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001570 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1571}
1572
1573TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001574 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001575
1576 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1577 EXPECT_TRUE(
1578 matches("class X { public: X(); }; void x() { new X(); }", New));
1579 EXPECT_TRUE(
1580 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1581 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1582}
1583
1584TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001585 StatementMatcher New = constructExpr(
1586 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001587
1588 EXPECT_TRUE(
1589 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1590 New));
1591 EXPECT_TRUE(
1592 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1593 New));
1594 EXPECT_TRUE(
1595 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1596 New));
1597
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001598 StatementMatcher WrongIndex = constructExpr(
1599 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001600 EXPECT_TRUE(
1601 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1602 WrongIndex));
1603}
1604
1605TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001606 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001607
1608 EXPECT_TRUE(
1609 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1610 EXPECT_TRUE(
1611 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1612 New));
1613}
1614
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001615TEST(Matcher, DeleteExpression) {
1616 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001617 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001618}
1619
Manuel Klimek4da21662012-07-06 05:48:52 +00001620TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001621 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001622
1623 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1624 EXPECT_TRUE(
1625 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1626 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1627}
1628
1629TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001630 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001631 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1632 // wide string
1633 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1634 // with escaped characters
1635 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1636 // no matching -- though the data type is the same, there is no string literal
1637 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1638}
1639
1640TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001641 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001642 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1643 // wide character
1644 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1645 // wide character, Hex encoded, NOT MATCHED!
1646 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1647 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1648}
1649
1650TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001651 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001652 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1653 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1654 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1655 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1656
1657 // Non-matching cases (character literals, float and double)
1658 EXPECT_TRUE(notMatches("int i = L'a';",
1659 HasIntLiteral)); // this is actually a character
1660 // literal cast to int
1661 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1662 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1663 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1664}
1665
Daniel Jasper31f7c082012-10-01 13:40:41 +00001666TEST(Matcher, NullPtrLiteral) {
1667 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1668}
1669
Daniel Jasperb54b7642012-09-20 14:12:57 +00001670TEST(Matcher, AsmStatement) {
1671 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1672}
1673
Manuel Klimek4da21662012-07-06 05:48:52 +00001674TEST(Matcher, Conditions) {
1675 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1676
1677 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1678 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1679 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1680 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1681 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1682}
1683
1684TEST(MatchBinaryOperator, HasOperatorName) {
1685 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1686
1687 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1688 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1689}
1690
1691TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1692 StatementMatcher OperatorTrueFalse =
1693 binaryOperator(hasLHS(boolLiteral(equals(true))),
1694 hasRHS(boolLiteral(equals(false))));
1695
1696 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1697 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1698 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1699}
1700
1701TEST(MatchBinaryOperator, HasEitherOperand) {
1702 StatementMatcher HasOperand =
1703 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1704
1705 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1706 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1707 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1708}
1709
1710TEST(Matcher, BinaryOperatorTypes) {
1711 // Integration test that verifies the AST provides all binary operators in
1712 // a way we expect.
1713 // FIXME: Operator ','
1714 EXPECT_TRUE(
1715 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1716 EXPECT_TRUE(
1717 matches("bool b; bool c = (b = true);",
1718 binaryOperator(hasOperatorName("="))));
1719 EXPECT_TRUE(
1720 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1721 EXPECT_TRUE(
1722 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1723 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1724 EXPECT_TRUE(
1725 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1726 EXPECT_TRUE(
1727 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1728 EXPECT_TRUE(
1729 matches("int i = 1; int j = (i <<= 2);",
1730 binaryOperator(hasOperatorName("<<="))));
1731 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1732 EXPECT_TRUE(
1733 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1734 EXPECT_TRUE(
1735 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1736 EXPECT_TRUE(
1737 matches("int i = 1; int j = (i >>= 2);",
1738 binaryOperator(hasOperatorName(">>="))));
1739 EXPECT_TRUE(
1740 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1741 EXPECT_TRUE(
1742 matches("int i = 42; int j = (i ^= 42);",
1743 binaryOperator(hasOperatorName("^="))));
1744 EXPECT_TRUE(
1745 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1746 EXPECT_TRUE(
1747 matches("int i = 42; int j = (i %= 42);",
1748 binaryOperator(hasOperatorName("%="))));
1749 EXPECT_TRUE(
1750 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1751 EXPECT_TRUE(
1752 matches("bool b = true && false;",
1753 binaryOperator(hasOperatorName("&&"))));
1754 EXPECT_TRUE(
1755 matches("bool b = true; bool c = (b &= false);",
1756 binaryOperator(hasOperatorName("&="))));
1757 EXPECT_TRUE(
1758 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1759 EXPECT_TRUE(
1760 matches("bool b = true || false;",
1761 binaryOperator(hasOperatorName("||"))));
1762 EXPECT_TRUE(
1763 matches("bool b = true; bool c = (b |= false);",
1764 binaryOperator(hasOperatorName("|="))));
1765 EXPECT_TRUE(
1766 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1767 EXPECT_TRUE(
1768 matches("int i = 42; int j = (i *= 23);",
1769 binaryOperator(hasOperatorName("*="))));
1770 EXPECT_TRUE(
1771 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1772 EXPECT_TRUE(
1773 matches("int i = 42; int j = (i /= 23);",
1774 binaryOperator(hasOperatorName("/="))));
1775 EXPECT_TRUE(
1776 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1777 EXPECT_TRUE(
1778 matches("int i = 42; int j = (i += 23);",
1779 binaryOperator(hasOperatorName("+="))));
1780 EXPECT_TRUE(
1781 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1782 EXPECT_TRUE(
1783 matches("int i = 42; int j = (i -= 23);",
1784 binaryOperator(hasOperatorName("-="))));
1785 EXPECT_TRUE(
1786 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1787 binaryOperator(hasOperatorName("->*"))));
1788 EXPECT_TRUE(
1789 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1790 binaryOperator(hasOperatorName(".*"))));
1791
1792 // Member expressions as operators are not supported in matches.
1793 EXPECT_TRUE(
1794 notMatches("struct A { void x(A *a) { a->x(this); } };",
1795 binaryOperator(hasOperatorName("->"))));
1796
1797 // Initializer assignments are not represented as operator equals.
1798 EXPECT_TRUE(
1799 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1800
1801 // Array indexing is not represented as operator.
1802 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1803
1804 // Overloaded operators do not match at all.
1805 EXPECT_TRUE(notMatches(
1806 "struct A { bool operator&&(const A &a) const { return false; } };"
1807 "void x() { A a, b; a && b; }",
1808 binaryOperator()));
1809}
1810
1811TEST(MatchUnaryOperator, HasOperatorName) {
1812 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1813
1814 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1815 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1816}
1817
1818TEST(MatchUnaryOperator, HasUnaryOperand) {
1819 StatementMatcher OperatorOnFalse =
1820 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1821
1822 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1823 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1824}
1825
1826TEST(Matcher, UnaryOperatorTypes) {
1827 // Integration test that verifies the AST provides all unary operators in
1828 // a way we expect.
1829 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1830 EXPECT_TRUE(
1831 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1832 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1833 EXPECT_TRUE(
1834 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1835 EXPECT_TRUE(
1836 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1837 EXPECT_TRUE(
1838 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1839 EXPECT_TRUE(
1840 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1841 EXPECT_TRUE(
1842 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1843 EXPECT_TRUE(
1844 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1845 EXPECT_TRUE(
1846 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1847
1848 // We don't match conversion operators.
1849 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1850
1851 // Function calls are not represented as operator.
1852 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1853
1854 // Overloaded operators do not match at all.
1855 // FIXME: We probably want to add that.
1856 EXPECT_TRUE(notMatches(
1857 "struct A { bool operator!() const { return false; } };"
1858 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1859}
1860
1861TEST(Matcher, ConditionalOperator) {
1862 StatementMatcher Conditional = conditionalOperator(
1863 hasCondition(boolLiteral(equals(true))),
1864 hasTrueExpression(boolLiteral(equals(false))));
1865
1866 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1867 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1868 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1869
1870 StatementMatcher ConditionalFalse = conditionalOperator(
1871 hasFalseExpression(boolLiteral(equals(false))));
1872
1873 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1874 EXPECT_TRUE(
1875 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1876}
1877
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001878TEST(ArraySubscriptMatchers, ArraySubscripts) {
1879 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1880 arraySubscriptExpr()));
1881 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1882 arraySubscriptExpr()));
1883}
1884
1885TEST(ArraySubscriptMatchers, ArrayIndex) {
1886 EXPECT_TRUE(matches(
1887 "int i[2]; void f() { i[1] = 1; }",
1888 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1889 EXPECT_TRUE(matches(
1890 "int i[2]; void f() { 1[i] = 1; }",
1891 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1892 EXPECT_TRUE(notMatches(
1893 "int i[2]; void f() { i[1] = 1; }",
1894 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1895}
1896
1897TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1898 EXPECT_TRUE(matches(
1899 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001900 arraySubscriptExpr(hasBase(implicitCastExpr(
1901 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001902}
1903
Manuel Klimek4da21662012-07-06 05:48:52 +00001904TEST(Matcher, HasNameSupportsNamespaces) {
1905 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001906 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001907 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001908 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001909 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001910 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001911 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001912 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001913 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001914 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001915 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001916 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001917 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001918 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001919 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001920 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001921 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001922 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001923 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001924 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001925 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001926 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001927 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001928 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001929}
1930
1931TEST(Matcher, HasNameSupportsOuterClasses) {
1932 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001933 matches("class A { class B { class C; }; };",
1934 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001935 EXPECT_TRUE(
1936 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001937 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001938 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001939 matches("class A { class B { class C; }; };",
1940 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001941 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001942 matches("class A { class B { class C; }; };",
1943 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001944 EXPECT_TRUE(
1945 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001946 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001947 EXPECT_TRUE(
1948 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001949 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001950 EXPECT_TRUE(
1951 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001952 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001953 EXPECT_TRUE(
1954 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001955 recordDecl(hasName("::C"))));
1956 EXPECT_TRUE(
1957 notMatches("class A { class B { class C; }; };",
1958 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001959 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001960 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001961 EXPECT_TRUE(
1962 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001963 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001964}
1965
1966TEST(Matcher, IsDefinition) {
1967 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001968 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001969 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1970 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1971
1972 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001973 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001974 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1975 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1976
1977 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001978 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001979 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1980 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1981}
1982
1983TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001984 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00001985 ofClass(hasName("X")))));
1986
1987 EXPECT_TRUE(
1988 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
1989 EXPECT_TRUE(
1990 matches("class X { public: X(); }; void x(int) { X x = X(); }",
1991 Constructor));
1992 EXPECT_TRUE(
1993 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
1994 Constructor));
1995}
1996
1997TEST(Matcher, VisitsTemplateInstantiations) {
1998 EXPECT_TRUE(matches(
1999 "class A { public: void x(); };"
2000 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002001 "void f() { B<A> b; b.y(); }",
2002 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002003
2004 EXPECT_TRUE(matches(
2005 "class A { public: void x(); };"
2006 "class C {"
2007 " public:"
2008 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2009 "};"
2010 "void f() {"
2011 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002012 "}",
2013 recordDecl(hasName("C"),
2014 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002015}
2016
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002017TEST(Matcher, HandlesNullQualTypes) {
2018 // FIXME: Add a Type matcher so we can replace uses of this
2019 // variable with Type(True())
2020 const TypeMatcher AnyType = anything();
2021
2022 // We don't really care whether this matcher succeeds; we're testing that
2023 // it completes without crashing.
2024 EXPECT_TRUE(matches(
2025 "struct A { };"
2026 "template <typename T>"
2027 "void f(T t) {"
2028 " T local_t(t /* this becomes a null QualType in the AST */);"
2029 "}"
2030 "void g() {"
2031 " f(0);"
2032 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002033 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002034 anyOf(
2035 TypeMatcher(hasDeclaration(anything())),
2036 pointsTo(AnyType),
2037 references(AnyType)
2038 // Other QualType matchers should go here.
2039 ))))));
2040}
2041
Manuel Klimek4da21662012-07-06 05:48:52 +00002042// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002043AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002044 // Make sure all special variables are used: node, match_finder,
2045 // bound_nodes_builder, and the parameter named 'AMatcher'.
2046 return AMatcher.matches(Node, Finder, Builder);
2047}
2048
2049TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002050 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002051
2052 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002053 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002054
2055 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002056 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002057
2058 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002059 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002060}
2061
2062AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002063 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
2064 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
2065 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00002066 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00002067 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002068 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002069 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2070 ASTMatchFinder::BK_First);
2071}
2072
2073TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002074 DeclarationMatcher HasClassB =
2075 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002076
2077 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002078 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002079
2080 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002081 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002082
2083 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002084 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002085
2086 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002087 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002088
2089 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2090}
2091
2092TEST(For, FindsForLoops) {
2093 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2094 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002095 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2096 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002097 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002098}
2099
Daniel Jasper6a124492012-07-12 08:50:38 +00002100TEST(For, ForLoopInternals) {
2101 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2102 forStmt(hasCondition(anything()))));
2103 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2104 forStmt(hasLoopInit(anything()))));
2105}
2106
2107TEST(For, NegativeForLoopInternals) {
2108 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002109 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002110 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2111 forStmt(hasLoopInit(anything()))));
2112}
2113
Manuel Klimek4da21662012-07-06 05:48:52 +00002114TEST(For, ReportsNoFalsePositives) {
2115 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2116 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2117}
2118
2119TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002120 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2121 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2122 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002123}
2124
2125TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2126 // It's not a compound statement just because there's "{}" in the source
2127 // text. This is an AST search, not grep.
2128 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002129 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002130 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002131 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002132}
2133
Daniel Jasper6a124492012-07-12 08:50:38 +00002134TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002135 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002136 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002137 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002138 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002139 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002140 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002141 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002142 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002143}
2144
2145TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2146 // The simplest case: every compound statement is in a function
2147 // definition, and the function body itself must be a compound
2148 // statement.
2149 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002150 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002151}
2152
2153TEST(HasAnySubstatement, IsNotRecursive) {
2154 // It's really "has any immediate substatement".
2155 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002156 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002157}
2158
2159TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2160 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002161 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002162}
2163
2164TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2165 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002166 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002167}
2168
2169TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2170 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002171 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002172 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002173 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002174}
2175
2176TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2177 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002178 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002179 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002180 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002181 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002182 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002183}
2184
2185TEST(StatementCountIs, WorksWithMultipleStatements) {
2186 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002187 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002188}
2189
2190TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2191 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002192 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002193 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002194 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002195 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002196 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002197 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002198 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002199}
2200
2201TEST(Member, WorksInSimplestCase) {
2202 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002203 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002204}
2205
2206TEST(Member, DoesNotMatchTheBaseExpression) {
2207 // Don't pick out the wrong part of the member expression, this should
2208 // be checking the member (name) only.
2209 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002210 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002211}
2212
2213TEST(Member, MatchesInMemberFunctionCall) {
2214 EXPECT_TRUE(matches("void f() {"
2215 " struct { void first() {}; } s;"
2216 " s.first();"
2217 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002218 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002219}
2220
Daniel Jasperc711af22012-10-23 15:46:39 +00002221TEST(Member, MatchesMember) {
2222 EXPECT_TRUE(matches(
2223 "struct A { int i; }; void f() { A a; a.i = 2; }",
2224 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2225 EXPECT_TRUE(notMatches(
2226 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2227 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2228}
2229
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002230TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002231 // Fails in C++11 mode
2232 EXPECT_TRUE(matchesConditionally(
2233 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2234 "class X { void *operator new(std::size_t); };",
2235 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002236
2237 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002238 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002239
Daniel Jasper31f7c082012-10-01 13:40:41 +00002240 // Fails in C++11 mode
2241 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002242 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2243 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002244 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002245}
2246
Manuel Klimek4da21662012-07-06 05:48:52 +00002247TEST(HasObjectExpression, DoesNotMatchMember) {
2248 EXPECT_TRUE(notMatches(
2249 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002250 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002251}
2252
2253TEST(HasObjectExpression, MatchesBaseOfVariable) {
2254 EXPECT_TRUE(matches(
2255 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002256 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002257 EXPECT_TRUE(matches(
2258 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002259 memberExpr(hasObjectExpression(
2260 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002261}
2262
2263TEST(HasObjectExpression,
2264 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2265 EXPECT_TRUE(matches(
2266 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002267 memberExpr(hasObjectExpression(
2268 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002269 EXPECT_TRUE(matches(
2270 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002271 memberExpr(hasObjectExpression(
2272 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002273}
2274
2275TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002276 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2277 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2278 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2279 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002280}
2281
2282TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002283 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002284}
2285
2286TEST(IsConstQualified, MatchesConstInt) {
2287 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002288 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002289}
2290
2291TEST(IsConstQualified, MatchesConstPointer) {
2292 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002293 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002294}
2295
2296TEST(IsConstQualified, MatchesThroughTypedef) {
2297 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002298 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002299 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002300 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002301}
2302
2303TEST(IsConstQualified, DoesNotMatchInappropriately) {
2304 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002305 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002306 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002307 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002308}
2309
Sam Panzer089e5b32012-08-16 16:58:10 +00002310TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002311 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2312 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2313 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2314 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002315}
2316TEST(CastExpression, MatchesImplicitCasts) {
2317 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002318 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002319 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002320 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002321}
2322
2323TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002324 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2325 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2326 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2327 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002328}
2329
Manuel Klimek4da21662012-07-06 05:48:52 +00002330TEST(ReinterpretCast, MatchesSimpleCase) {
2331 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002332 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002333}
2334
2335TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002336 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002337 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002338 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002339 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002340 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002341 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2342 "B b;"
2343 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002344 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002345}
2346
2347TEST(FunctionalCast, MatchesSimpleCase) {
2348 std::string foo_class = "class Foo { public: Foo(char*); };";
2349 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002350 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002351}
2352
2353TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2354 std::string FooClass = "class Foo { public: Foo(char*); };";
2355 EXPECT_TRUE(
2356 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002357 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002358 EXPECT_TRUE(
2359 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002360 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002361}
2362
2363TEST(DynamicCast, MatchesSimpleCase) {
2364 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2365 "B b;"
2366 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002367 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002368}
2369
2370TEST(StaticCast, MatchesSimpleCase) {
2371 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002372 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002373}
2374
2375TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002376 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002377 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002378 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002379 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002380 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002381 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2382 "B b;"
2383 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002384 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002385}
2386
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002387TEST(CStyleCast, MatchesSimpleCase) {
2388 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2389}
2390
2391TEST(CStyleCast, DoesNotMatchOtherCasts) {
2392 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2393 "char q, *r = const_cast<char*>(&q);"
2394 "void* s = reinterpret_cast<char*>(&s);"
2395 "struct B { virtual ~B() {} }; struct D : B {};"
2396 "B b;"
2397 "D* t = dynamic_cast<D*>(&b);",
2398 cStyleCastExpr()));
2399}
2400
Manuel Klimek4da21662012-07-06 05:48:52 +00002401TEST(HasDestinationType, MatchesSimpleCase) {
2402 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002403 staticCastExpr(hasDestinationType(
2404 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002405}
2406
Sam Panzer089e5b32012-08-16 16:58:10 +00002407TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2408 // This test creates an implicit const cast.
2409 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002410 implicitCastExpr(
2411 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002412 // This test creates an implicit array-to-pointer cast.
2413 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002414 implicitCastExpr(hasImplicitDestinationType(
2415 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002416}
2417
2418TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2419 // This test creates an implicit cast from int to char.
2420 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002421 implicitCastExpr(hasImplicitDestinationType(
2422 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002423 // This test creates an implicit array-to-pointer cast.
2424 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002425 implicitCastExpr(hasImplicitDestinationType(
2426 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002427}
2428
2429TEST(ImplicitCast, MatchesSimpleCase) {
2430 // This test creates an implicit const cast.
2431 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002432 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002433 // This test creates an implicit cast from int to char.
2434 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002435 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002436 // This test creates an implicit array-to-pointer cast.
2437 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002438 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002439}
2440
2441TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002442 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002443 // are present, and that it ignores explicit and paren casts.
2444
2445 // These two test cases have no casts.
2446 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002447 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002448 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002449 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002450
2451 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002452 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002453 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002454 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002455
2456 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002457 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002458}
2459
2460TEST(IgnoringImpCasts, MatchesImpCasts) {
2461 // This test checks that ignoringImpCasts matches when implicit casts are
2462 // present and its inner matcher alone does not match.
2463 // Note that this test creates an implicit const cast.
2464 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002465 varDecl(hasInitializer(ignoringImpCasts(
2466 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002467 // This test creates an implict cast from int to char.
2468 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002469 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002470 integerLiteral(equals(0)))))));
2471}
2472
2473TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2474 // These tests verify that ignoringImpCasts does not match if the inner
2475 // matcher does not match.
2476 // Note that the first test creates an implicit const cast.
2477 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002478 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002479 unless(anything()))))));
2480 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002481 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002482 unless(anything()))))));
2483
2484 // These tests verify that ignoringImplictCasts does not look through explicit
2485 // casts or parentheses.
2486 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002487 varDecl(hasInitializer(ignoringImpCasts(
2488 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002489 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002490 varDecl(hasInitializer(ignoringImpCasts(
2491 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002492 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002493 varDecl(hasInitializer(ignoringImpCasts(
2494 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002495 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002496 varDecl(hasInitializer(ignoringImpCasts(
2497 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002498}
2499
2500TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2501 // This test verifies that expressions that do not have implicit casts
2502 // still match the inner matcher.
2503 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002504 varDecl(hasInitializer(ignoringImpCasts(
2505 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002506}
2507
2508TEST(IgnoringParenCasts, MatchesParenCasts) {
2509 // This test checks that ignoringParenCasts matches when parentheses and/or
2510 // casts are present and its inner matcher alone does not match.
2511 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002512 varDecl(hasInitializer(ignoringParenCasts(
2513 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002514 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002515 varDecl(hasInitializer(ignoringParenCasts(
2516 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002517
2518 // This test creates an implict cast from int to char in addition to the
2519 // parentheses.
2520 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002521 varDecl(hasInitializer(ignoringParenCasts(
2522 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002523
2524 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002525 varDecl(hasInitializer(ignoringParenCasts(
2526 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002527 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002528 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002529 integerLiteral(equals(0)))))));
2530}
2531
2532TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2533 // This test verifies that expressions that do not have any casts still match.
2534 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002535 varDecl(hasInitializer(ignoringParenCasts(
2536 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002537}
2538
2539TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2540 // These tests verify that ignoringImpCasts does not match if the inner
2541 // matcher does not match.
2542 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002543 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002544 unless(anything()))))));
2545
2546 // This test creates an implicit cast from int to char in addition to the
2547 // parentheses.
2548 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002549 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002550 unless(anything()))))));
2551
2552 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002553 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002554 unless(anything()))))));
2555}
2556
2557TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2558 // This test checks that ignoringParenAndImpCasts matches when
2559 // parentheses and/or implicit casts are present and its inner matcher alone
2560 // does not match.
2561 // Note that this test creates an implicit const cast.
2562 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002563 varDecl(hasInitializer(ignoringParenImpCasts(
2564 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002565 // This test creates an implicit cast from int to char.
2566 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002567 varDecl(hasInitializer(ignoringParenImpCasts(
2568 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002569}
2570
2571TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2572 // This test verifies that expressions that do not have parentheses or
2573 // implicit casts still match.
2574 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002575 varDecl(hasInitializer(ignoringParenImpCasts(
2576 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002577 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002578 varDecl(hasInitializer(ignoringParenImpCasts(
2579 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002580}
2581
2582TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2583 // These tests verify that ignoringParenImpCasts does not match if
2584 // the inner matcher does not match.
2585 // This test creates an implicit cast.
2586 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002587 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002588 unless(anything()))))));
2589 // These tests verify that ignoringParenAndImplictCasts does not look
2590 // through explicit casts.
2591 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002592 varDecl(hasInitializer(ignoringParenImpCasts(
2593 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002594 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002595 varDecl(hasInitializer(ignoringParenImpCasts(
2596 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002597 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002598 varDecl(hasInitializer(ignoringParenImpCasts(
2599 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002600}
2601
Manuel Klimek715c9562012-07-25 10:02:02 +00002602TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002603 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2604 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002605 implicitCastExpr(
2606 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002607}
2608
Manuel Klimek715c9562012-07-25 10:02:02 +00002609TEST(HasSourceExpression, MatchesExplicitCasts) {
2610 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002611 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002612 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002613 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002614}
2615
Manuel Klimek4da21662012-07-06 05:48:52 +00002616TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002617 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002618}
2619
2620TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002621 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002622}
2623
2624TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002625 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002626}
2627
2628TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002629 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002630}
2631
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002632TEST(InitListExpression, MatchesInitListExpression) {
2633 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2634 initListExpr(hasType(asString("int [2]")))));
2635 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002636 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002637}
2638
2639TEST(UsingDeclaration, MatchesUsingDeclarations) {
2640 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2641 usingDecl()));
2642}
2643
2644TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2645 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2646 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2647}
2648
2649TEST(UsingDeclaration, MatchesSpecificTarget) {
2650 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2651 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002652 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002653 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2654 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002655 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002656}
2657
2658TEST(UsingDeclaration, ThroughUsingDeclaration) {
2659 EXPECT_TRUE(matches(
2660 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002661 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002662 EXPECT_TRUE(notMatches(
2663 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002664 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002665}
2666
Sam Panzer425f41b2012-08-16 17:20:59 +00002667TEST(SingleDecl, IsSingleDecl) {
2668 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002669 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002670 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2671 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2672 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2673 SingleDeclStmt));
2674}
2675
2676TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002677 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002678
2679 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002680 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002681 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002682 declStmt(containsDeclaration(0, MatchesInit),
2683 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002684 unsigned WrongIndex = 42;
2685 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002686 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002687 MatchesInit))));
2688}
2689
2690TEST(DeclCount, DeclCountIsCorrect) {
2691 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002692 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002693 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002694 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002695 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002696 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002697}
2698
Manuel Klimek4da21662012-07-06 05:48:52 +00002699TEST(While, MatchesWhileLoops) {
2700 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2701 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2702 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2703}
2704
2705TEST(Do, MatchesDoLoops) {
2706 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2707 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2708}
2709
2710TEST(Do, DoesNotMatchWhileLoops) {
2711 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2712}
2713
2714TEST(SwitchCase, MatchesCase) {
2715 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2716 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2717 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2718 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2719}
2720
Daniel Jasperb54b7642012-09-20 14:12:57 +00002721TEST(SwitchCase, MatchesSwitch) {
2722 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2723 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2724 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2725 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2726}
2727
2728TEST(ExceptionHandling, SimpleCases) {
2729 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2730 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2731 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2732 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2733 throwExpr()));
2734 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2735 throwExpr()));
2736}
2737
Manuel Klimek4da21662012-07-06 05:48:52 +00002738TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2739 EXPECT_TRUE(notMatches(
2740 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002741 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002742 EXPECT_TRUE(notMatches(
2743 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002744 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002745}
2746
2747TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2748 EXPECT_TRUE(matches(
2749 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002750 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002751}
2752
2753TEST(ForEach, BindsOneNode) {
2754 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002755 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002756 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002757}
2758
2759TEST(ForEach, BindsMultipleNodes) {
2760 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002761 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002762 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002763}
2764
2765TEST(ForEach, BindsRecursiveCombinations) {
2766 EXPECT_TRUE(matchAndVerifyResultTrue(
2767 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002768 recordDecl(hasName("C"),
2769 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002770 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002771}
2772
2773TEST(ForEachDescendant, BindsOneNode) {
2774 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002775 recordDecl(hasName("C"),
2776 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002777 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002778}
2779
Daniel Jasper5f684e92012-11-16 18:39:22 +00002780TEST(ForEachDescendant, NestedForEachDescendant) {
2781 DeclarationMatcher m = recordDecl(
2782 isDefinition(), decl().bind("x"), hasName("C"));
2783 EXPECT_TRUE(matchAndVerifyResultTrue(
2784 "class A { class B { class C {}; }; };",
2785 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
2786 new VerifyIdIsBoundTo<Decl>("x", "C")));
2787
2788 // FIXME: This is not really a useful matcher, but the result is still
2789 // surprising (currently binds "A").
2790 //EXPECT_TRUE(matchAndVerifyResultTrue(
2791 // "class A { class B { class C {}; }; };",
2792 // recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
2793 // new VerifyIdIsBoundTo<Decl>("x", "C")));
2794}
2795
Manuel Klimek4da21662012-07-06 05:48:52 +00002796TEST(ForEachDescendant, BindsMultipleNodes) {
2797 EXPECT_TRUE(matchAndVerifyResultTrue(
2798 "class C { class D { int x; int y; }; "
2799 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002800 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002801 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002802}
2803
2804TEST(ForEachDescendant, BindsRecursiveCombinations) {
2805 EXPECT_TRUE(matchAndVerifyResultTrue(
2806 "class C { class D { "
2807 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002808 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2809 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002810 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002811}
2812
Daniel Jasper11c98772012-11-11 22:14:55 +00002813TEST(ForEachDescendant, BindsCorrectNodes) {
2814 EXPECT_TRUE(matchAndVerifyResultTrue(
2815 "class C { void f(); int i; };",
2816 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2817 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
2818 EXPECT_TRUE(matchAndVerifyResultTrue(
2819 "class C { void f() {} int i; };",
2820 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2821 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
2822}
2823
Manuel Klimek4da21662012-07-06 05:48:52 +00002824
2825TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2826 // Make sure that we can both match the class by name (::X) and by the type
2827 // the template was instantiated with (via a field).
2828
2829 EXPECT_TRUE(matches(
2830 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002831 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002832
2833 EXPECT_TRUE(matches(
2834 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002835 recordDecl(isTemplateInstantiation(), hasDescendant(
2836 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002837}
2838
2839TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2840 EXPECT_TRUE(matches(
2841 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002842 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002843 isTemplateInstantiation())));
2844}
2845
2846TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2847 EXPECT_TRUE(matches(
2848 "template <typename T> class X { T t; }; class A {};"
2849 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002850 recordDecl(isTemplateInstantiation(), hasDescendant(
2851 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002852}
2853
2854TEST(IsTemplateInstantiation,
2855 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2856 EXPECT_TRUE(matches(
2857 "template <typename T> class X {};"
2858 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002859 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002860}
2861
2862TEST(IsTemplateInstantiation,
2863 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2864 EXPECT_TRUE(matches(
2865 "class A {};"
2866 "class X {"
2867 " template <typename U> class Y { U u; };"
2868 " Y<A> y;"
2869 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002870 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002871}
2872
2873TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2874 // FIXME: Figure out whether this makes sense. It doesn't affect the
2875 // normal use case as long as the uppermost instantiation always is marked
2876 // as template instantiation, but it might be confusing as a predicate.
2877 EXPECT_TRUE(matches(
2878 "class A {};"
2879 "template <typename T> class X {"
2880 " template <typename U> class Y { U u; };"
2881 " Y<T> y;"
2882 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002883 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002884}
2885
2886TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2887 EXPECT_TRUE(notMatches(
2888 "template <typename T> class X {}; class A {};"
2889 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002890 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002891}
2892
2893TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2894 EXPECT_TRUE(notMatches(
2895 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002896 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002897}
2898
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002899TEST(IsExplicitTemplateSpecialization,
2900 DoesNotMatchPrimaryTemplate) {
2901 EXPECT_TRUE(notMatches(
2902 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002903 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002904 EXPECT_TRUE(notMatches(
2905 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002906 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002907}
2908
2909TEST(IsExplicitTemplateSpecialization,
2910 DoesNotMatchExplicitTemplateInstantiations) {
2911 EXPECT_TRUE(notMatches(
2912 "template <typename T> class X {};"
2913 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002914 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002915 EXPECT_TRUE(notMatches(
2916 "template <typename T> void f(T t) {}"
2917 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002918 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002919}
2920
2921TEST(IsExplicitTemplateSpecialization,
2922 DoesNotMatchImplicitTemplateInstantiations) {
2923 EXPECT_TRUE(notMatches(
2924 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002925 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002926 EXPECT_TRUE(notMatches(
2927 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002928 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002929}
2930
2931TEST(IsExplicitTemplateSpecialization,
2932 MatchesExplicitTemplateSpecializations) {
2933 EXPECT_TRUE(matches(
2934 "template <typename T> class X {};"
2935 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002936 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002937 EXPECT_TRUE(matches(
2938 "template <typename T> void f(T t) {}"
2939 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002940 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002941}
2942
Manuel Klimek579b1202012-09-07 09:26:10 +00002943TEST(HasAncenstor, MatchesDeclarationAncestors) {
2944 EXPECT_TRUE(matches(
2945 "class A { class B { class C {}; }; };",
2946 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2947}
2948
2949TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2950 EXPECT_TRUE(notMatches(
2951 "class A { class B { class C {}; }; };",
2952 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2953}
2954
2955TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2956 EXPECT_TRUE(matches(
2957 "class A { class B { void f() { C c; } class C {}; }; };",
2958 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2959 hasAncestor(recordDecl(hasName("A"))))))));
2960}
2961
2962TEST(HasAncenstor, MatchesStatementAncestors) {
2963 EXPECT_TRUE(matches(
2964 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002965 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002966}
2967
2968TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2969 EXPECT_TRUE(matches(
2970 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002971 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002972}
2973
2974TEST(HasAncestor, BindsRecursiveCombinations) {
2975 EXPECT_TRUE(matchAndVerifyResultTrue(
2976 "class C { class D { class E { class F { int y; }; }; }; };",
2977 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002978 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00002979}
2980
2981TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
2982 EXPECT_TRUE(matchAndVerifyResultTrue(
2983 "class C { class D { class E { class F { int y; }; }; }; };",
2984 fieldDecl(hasAncestor(
2985 decl(
2986 hasDescendant(recordDecl(isDefinition(),
2987 hasAncestor(recordDecl())))
2988 ).bind("d")
2989 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00002990 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00002991}
2992
2993TEST(HasAncestor, MatchesInTemplateInstantiations) {
2994 EXPECT_TRUE(matches(
2995 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
2996 "A<int>::B::C a;",
2997 fieldDecl(hasType(asString("int")),
2998 hasAncestor(recordDecl(hasName("A"))))));
2999}
3000
3001TEST(HasAncestor, MatchesInImplicitCode) {
3002 EXPECT_TRUE(matches(
3003 "struct X {}; struct A { A() {} X x; };",
3004 constructorDecl(
3005 hasAnyConstructorInitializer(withInitializer(expr(
3006 hasAncestor(recordDecl(hasName("A")))))))));
3007}
3008
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003009TEST(HasParent, MatchesOnlyParent) {
3010 EXPECT_TRUE(matches(
3011 "void f() { if (true) { int x = 42; } }",
3012 compoundStmt(hasParent(ifStmt()))));
3013 EXPECT_TRUE(notMatches(
3014 "void f() { for (;;) { int x = 42; } }",
3015 compoundStmt(hasParent(ifStmt()))));
3016 EXPECT_TRUE(notMatches(
3017 "void f() { if (true) for (;;) { int x = 42; } }",
3018 compoundStmt(hasParent(ifStmt()))));
3019}
3020
Daniel Jasperce620072012-10-17 08:52:59 +00003021TEST(TypeMatching, MatchesTypes) {
3022 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3023}
3024
3025TEST(TypeMatching, MatchesArrayTypes) {
3026 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3027 EXPECT_TRUE(matches("int a[42];", arrayType()));
3028 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3029
3030 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3031 arrayType(hasElementType(builtinType()))));
3032
3033 EXPECT_TRUE(matches(
3034 "int const a[] = { 2, 3 };",
3035 qualType(arrayType(hasElementType(builtinType())))));
3036 EXPECT_TRUE(matches(
3037 "int const a[] = { 2, 3 };",
3038 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3039 EXPECT_TRUE(matches(
3040 "typedef const int T; T x[] = { 1, 2 };",
3041 qualType(isConstQualified(), arrayType())));
3042
3043 EXPECT_TRUE(notMatches(
3044 "int a[] = { 2, 3 };",
3045 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3046 EXPECT_TRUE(notMatches(
3047 "int a[] = { 2, 3 };",
3048 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3049 EXPECT_TRUE(notMatches(
3050 "int const a[] = { 2, 3 };",
3051 qualType(arrayType(hasElementType(builtinType())),
3052 unless(isConstQualified()))));
3053
3054 EXPECT_TRUE(matches("int a[2];",
3055 constantArrayType(hasElementType(builtinType()))));
3056 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3057}
3058
3059TEST(TypeMatching, MatchesComplexTypes) {
3060 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3061 EXPECT_TRUE(matches(
3062 "_Complex float f;",
3063 complexType(hasElementType(builtinType()))));
3064 EXPECT_TRUE(notMatches(
3065 "_Complex float f;",
3066 complexType(hasElementType(isInteger()))));
3067}
3068
3069TEST(TypeMatching, MatchesConstantArrayTypes) {
3070 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3071 EXPECT_TRUE(notMatches(
3072 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3073 constantArrayType(hasElementType(builtinType()))));
3074
3075 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3076 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3077 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3078}
3079
3080TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3081 EXPECT_TRUE(matches(
3082 "template <typename T, int Size> class array { T data[Size]; };",
3083 dependentSizedArrayType()));
3084 EXPECT_TRUE(notMatches(
3085 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3086 dependentSizedArrayType()));
3087}
3088
3089TEST(TypeMatching, MatchesIncompleteArrayType) {
3090 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3091 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3092
3093 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3094 incompleteArrayType()));
3095}
3096
3097TEST(TypeMatching, MatchesVariableArrayType) {
3098 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3099 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3100
3101 EXPECT_TRUE(matches(
3102 "void f(int b) { int a[b]; }",
3103 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3104 varDecl(hasName("b")))))))));
3105}
3106
3107TEST(TypeMatching, MatchesAtomicTypes) {
3108 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3109
3110 EXPECT_TRUE(matches("_Atomic(int) i;",
3111 atomicType(hasValueType(isInteger()))));
3112 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3113 atomicType(hasValueType(isInteger()))));
3114}
3115
3116TEST(TypeMatching, MatchesAutoTypes) {
3117 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3118 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3119 autoType()));
3120
3121 EXPECT_TRUE(matches("auto a = 1;",
3122 autoType(hasDeducedType(isInteger()))));
3123 EXPECT_TRUE(notMatches("auto b = 2.0;",
3124 autoType(hasDeducedType(isInteger()))));
3125}
3126
Daniel Jaspera267cf62012-10-29 10:14:44 +00003127TEST(TypeMatching, MatchesFunctionTypes) {
3128 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3129 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3130}
3131
Daniel Jasperce620072012-10-17 08:52:59 +00003132TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003133 // FIXME: Reactive when these tests can be more specific (not matching
3134 // implicit code on certain platforms), likely when we have hasDescendant for
3135 // Types/TypeLocs.
3136 //EXPECT_TRUE(matchAndVerifyResultTrue(
3137 // "int* a;",
3138 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3139 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3140 //EXPECT_TRUE(matchAndVerifyResultTrue(
3141 // "int* a;",
3142 // pointerTypeLoc().bind("loc"),
3143 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003144 EXPECT_TRUE(matches(
3145 "int** a;",
3146 pointerTypeLoc(pointeeLoc(loc(qualType())))));
3147 EXPECT_TRUE(matches(
3148 "int** a;",
3149 loc(pointerType(pointee(pointerType())))));
3150 EXPECT_TRUE(matches(
3151 "int* b; int* * const a = &b;",
3152 loc(qualType(isConstQualified(), pointerType()))));
3153
3154 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003155 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3156 hasType(blockPointerType()))));
3157 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3158 hasType(memberPointerType()))));
3159 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3160 hasType(pointerType()))));
3161 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3162 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003163
Daniel Jasper1802daf2012-10-17 13:35:36 +00003164 Fragment = "int *ptr;";
3165 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3166 hasType(blockPointerType()))));
3167 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3168 hasType(memberPointerType()))));
3169 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3170 hasType(pointerType()))));
3171 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3172 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003173
Daniel Jasper1802daf2012-10-17 13:35:36 +00003174 Fragment = "int a; int &ref = a;";
3175 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3176 hasType(blockPointerType()))));
3177 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3178 hasType(memberPointerType()))));
3179 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3180 hasType(pointerType()))));
3181 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3182 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003183}
3184
3185TEST(TypeMatching, PointeeTypes) {
3186 EXPECT_TRUE(matches("int b; int &a = b;",
3187 referenceType(pointee(builtinType()))));
3188 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3189
3190 EXPECT_TRUE(matches("int *a;",
3191 pointerTypeLoc(pointeeLoc(loc(builtinType())))));
3192
3193 EXPECT_TRUE(matches(
3194 "int const *A;",
3195 pointerType(pointee(isConstQualified(), builtinType()))));
3196 EXPECT_TRUE(notMatches(
3197 "int *A;",
3198 pointerType(pointee(isConstQualified(), builtinType()))));
3199}
3200
3201TEST(TypeMatching, MatchesPointersToConstTypes) {
3202 EXPECT_TRUE(matches("int b; int * const a = &b;",
3203 loc(pointerType())));
3204 EXPECT_TRUE(matches("int b; int * const a = &b;",
3205 pointerTypeLoc()));
3206 EXPECT_TRUE(matches(
3207 "int b; const int * a = &b;",
3208 pointerTypeLoc(pointeeLoc(builtinTypeLoc()))));
3209 EXPECT_TRUE(matches(
3210 "int b; const int * a = &b;",
3211 pointerType(pointee(builtinType()))));
3212}
3213
3214TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003215 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3216 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003217
Daniel Jasper1802daf2012-10-17 13:35:36 +00003218 EXPECT_TRUE(matches("typedef int X; X a;",
3219 varDecl(hasName("a"),
3220 hasType(typedefType(hasDecl(decl()))))));
Daniel Jasperce620072012-10-17 08:52:59 +00003221}
3222
Daniel Jaspera7564432012-09-13 13:11:25 +00003223TEST(NNS, MatchesNestedNameSpecifiers) {
3224 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3225 nestedNameSpecifier()));
3226 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3227 nestedNameSpecifier()));
3228 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3229 nestedNameSpecifier()));
3230
3231 EXPECT_TRUE(matches(
3232 "struct A { static void f() {} }; void g() { A::f(); }",
3233 nestedNameSpecifier()));
3234 EXPECT_TRUE(notMatches(
3235 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3236 nestedNameSpecifier()));
3237}
3238
Daniel Jasperb54b7642012-09-20 14:12:57 +00003239TEST(NullStatement, SimpleCases) {
3240 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3241 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3242}
3243
Daniel Jaspera7564432012-09-13 13:11:25 +00003244TEST(NNS, MatchesTypes) {
3245 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3246 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3247 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3248 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3249 Matcher));
3250 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3251}
3252
3253TEST(NNS, MatchesNamespaceDecls) {
3254 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3255 specifiesNamespace(hasName("ns")));
3256 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3257 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3258 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3259}
3260
3261TEST(NNS, BindsNestedNameSpecifiers) {
3262 EXPECT_TRUE(matchAndVerifyResultTrue(
3263 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3264 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3265 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3266}
3267
3268TEST(NNS, BindsNestedNameSpecifierLocs) {
3269 EXPECT_TRUE(matchAndVerifyResultTrue(
3270 "namespace ns { struct B {}; } ns::B b;",
3271 loc(nestedNameSpecifier()).bind("loc"),
3272 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3273}
3274
3275TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3276 EXPECT_TRUE(matches(
3277 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3278 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3279 EXPECT_TRUE(matches(
3280 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003281 nestedNameSpecifierLoc(hasPrefix(
3282 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003283}
3284
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003285TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3286 std::string Fragment =
3287 "namespace a { struct A { struct B { struct C {}; }; }; };"
3288 "void f() { a::A::B::C c; }";
3289 EXPECT_TRUE(matches(
3290 Fragment,
3291 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3292 hasDescendant(nestedNameSpecifier(
3293 specifiesNamespace(hasName("a")))))));
3294 EXPECT_TRUE(notMatches(
3295 Fragment,
3296 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3297 has(nestedNameSpecifier(
3298 specifiesNamespace(hasName("a")))))));
3299 EXPECT_TRUE(matches(
3300 Fragment,
3301 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3302 has(nestedNameSpecifier(
3303 specifiesNamespace(hasName("a")))))));
3304
3305 // Not really useful because a NestedNameSpecifier can af at most one child,
3306 // but to complete the interface.
3307 EXPECT_TRUE(matchAndVerifyResultTrue(
3308 Fragment,
3309 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3310 forEach(nestedNameSpecifier().bind("x"))),
3311 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3312}
3313
3314TEST(NNS, NestedNameSpecifiersAsDescendants) {
3315 std::string Fragment =
3316 "namespace a { struct A { struct B { struct C {}; }; }; };"
3317 "void f() { a::A::B::C c; }";
3318 EXPECT_TRUE(matches(
3319 Fragment,
3320 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3321 asString("struct a::A")))))));
3322 EXPECT_TRUE(matchAndVerifyResultTrue(
3323 Fragment,
3324 functionDecl(hasName("f"),
3325 forEachDescendant(nestedNameSpecifier().bind("x"))),
3326 // Nested names: a, a::A and a::A::B.
3327 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3328}
3329
3330TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3331 std::string Fragment =
3332 "namespace a { struct A { struct B { struct C {}; }; }; };"
3333 "void f() { a::A::B::C c; }";
3334 EXPECT_TRUE(matches(
3335 Fragment,
3336 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3337 hasDescendant(loc(nestedNameSpecifier(
3338 specifiesNamespace(hasName("a"))))))));
3339 EXPECT_TRUE(notMatches(
3340 Fragment,
3341 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3342 has(loc(nestedNameSpecifier(
3343 specifiesNamespace(hasName("a"))))))));
3344 EXPECT_TRUE(matches(
3345 Fragment,
3346 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3347 has(loc(nestedNameSpecifier(
3348 specifiesNamespace(hasName("a"))))))));
3349
3350 EXPECT_TRUE(matchAndVerifyResultTrue(
3351 Fragment,
3352 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3353 forEach(nestedNameSpecifierLoc().bind("x"))),
3354 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3355}
3356
3357TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3358 std::string Fragment =
3359 "namespace a { struct A { struct B { struct C {}; }; }; };"
3360 "void f() { a::A::B::C c; }";
3361 EXPECT_TRUE(matches(
3362 Fragment,
3363 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3364 asString("struct a::A"))))))));
3365 EXPECT_TRUE(matchAndVerifyResultTrue(
3366 Fragment,
3367 functionDecl(hasName("f"),
3368 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3369 // Nested names: a, a::A and a::A::B.
3370 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3371}
3372
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003373template <typename T>
3374class VerifyRecursiveMatch : public BoundNodesCallback {
3375public:
3376 explicit VerifyRecursiveMatch(StringRef Id,
3377 const internal::Matcher<T> &InnerMatcher)
3378 : Id(Id), InnerMatcher(InnerMatcher) {}
Daniel Jasper452abbc2012-10-29 10:48:25 +00003379
3380 virtual bool run(const BoundNodes *Nodes) {
3381 return false;
3382 }
3383
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003384 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3385 const T *Node = Nodes->getNodeAs<T>(Id);
3386 bool Found = false;
3387 MatchFinder Finder;
3388 Finder.addMatcher(InnerMatcher, new VerifyMatch(0, &Found));
3389 Finder.findAll(*Node, *Context);
3390 return Found;
3391 }
3392private:
3393 std::string Id;
3394 internal::Matcher<T> InnerMatcher;
3395};
3396
3397TEST(MatchFinder, CanMatchDeclarationsRecursively) {
3398 EXPECT_TRUE(matchAndVerifyResultTrue("class X { class Y {}; };",
3399 recordDecl(hasName("::X")).bind("X"),
3400 new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Y")))));
3401 EXPECT_TRUE(matchAndVerifyResultFalse("class X { class Y {}; };",
3402 recordDecl(hasName("::X")).bind("X"),
3403 new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Z")))));
3404}
3405
3406TEST(MatchFinder, CanMatchStatementsRecursively) {
3407 EXPECT_TRUE(matchAndVerifyResultTrue("void f() { if (1) { for (;;) { } } }",
3408 ifStmt().bind("if"),
3409 new VerifyRecursiveMatch<clang::Stmt>("if", forStmt())));
3410 EXPECT_TRUE(matchAndVerifyResultFalse("void f() { if (1) { for (;;) { } } }",
3411 ifStmt().bind("if"),
3412 new VerifyRecursiveMatch<clang::Stmt>("if", declStmt())));
3413}
3414
Manuel Klimeke5793282012-11-02 01:31:03 +00003415class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
3416public:
3417 VerifyStartOfTranslationUnit() : Called(false) {}
3418 virtual void run(const MatchFinder::MatchResult &Result) {
3419 EXPECT_TRUE(Called);
3420 }
3421 virtual void onStartOfTranslationUnit() {
3422 Called = true;
3423 }
3424 bool Called;
3425};
3426
3427TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
3428 MatchFinder Finder;
3429 VerifyStartOfTranslationUnit VerifyCallback;
3430 Finder.addMatcher(decl(), &VerifyCallback);
3431 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
3432 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
3433 EXPECT_TRUE(VerifyCallback.Called);
3434}
3435
Manuel Klimek4da21662012-07-06 05:48:52 +00003436} // end namespace ast_matchers
3437} // end namespace clang