blob: b49d082501b6f150c11775dd7600f9d05f05d308 [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 Jaspere0e6b9e2012-07-10 20:20:19 +0000558TEST(Enum, DoesNotMatchClasses) {
559 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
560}
561
562TEST(Enum, MatchesEnums) {
563 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
564}
565
566TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000567 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000568 EXPECT_TRUE(matches("enum X{ A };", Matcher));
569 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
570 EXPECT_TRUE(notMatches("enum X {};", Matcher));
571}
572
Manuel Klimek4da21662012-07-06 05:48:52 +0000573TEST(StatementMatcher, Has) {
574 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000575 expr(hasType(pointsTo(recordDecl(hasName("X")))),
576 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000577
578 EXPECT_TRUE(matches(
579 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
580 EXPECT_TRUE(notMatches(
581 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
582}
583
584TEST(StatementMatcher, HasDescendant) {
585 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000586 expr(hasType(pointsTo(recordDecl(hasName("X")))),
587 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000588
589 EXPECT_TRUE(matches(
590 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
591 HasDescendantVariableI));
592 EXPECT_TRUE(notMatches(
593 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
594 HasDescendantVariableI));
595}
596
597TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000598 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000599
600 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
601 EXPECT_TRUE(notMatches("class A {};", TypeA));
602
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000603 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000604
605 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
606 TypeDerivedFromA));
607 EXPECT_TRUE(notMatches("class A {};", TypeA));
608
609 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000610 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000611
612 EXPECT_TRUE(
613 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
614}
615
Manuel Klimek579b1202012-09-07 09:26:10 +0000616// Implements a run method that returns whether BoundNodes contains a
617// Decl bound to Id that can be dynamically cast to T.
Manuel Klimek4da21662012-07-06 05:48:52 +0000618// Optionally checks that the check succeeded a specific number of times.
619template <typename T>
Daniel Jaspera7564432012-09-13 13:11:25 +0000620class VerifyIdIsBoundTo : public BoundNodesCallback {
Manuel Klimek4da21662012-07-06 05:48:52 +0000621public:
Manuel Klimek579b1202012-09-07 09:26:10 +0000622 // Create an object that checks that a node of type \c T was bound to \c Id.
Manuel Klimek4da21662012-07-06 05:48:52 +0000623 // Does not check for a certain number of matches.
Daniel Jaspera7564432012-09-13 13:11:25 +0000624 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
Manuel Klimek4da21662012-07-06 05:48:52 +0000625 : Id(Id), ExpectedCount(-1), Count(0) {}
626
Manuel Klimek579b1202012-09-07 09:26:10 +0000627 // Create an object that checks that a node of type \c T was bound to \c Id.
628 // Checks that there were exactly \c ExpectedCount matches.
Daniel Jaspera7564432012-09-13 13:11:25 +0000629 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
Manuel Klimek4da21662012-07-06 05:48:52 +0000630 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
631
Manuel Klimek579b1202012-09-07 09:26:10 +0000632 // Create an object that checks that a node of type \c T was bound to \c Id.
Daniel Jaspera7564432012-09-13 13:11:25 +0000633 // Checks that there was exactly one match with the name \c ExpectedName.
Manuel Klimek579b1202012-09-07 09:26:10 +0000634 // Note that \c T must be a NamedDecl for this to work.
Daniel Jaspera7564432012-09-13 13:11:25 +0000635 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName)
636 : Id(Id), ExpectedCount(1), Count(0), ExpectedName(ExpectedName) {}
Manuel Klimek579b1202012-09-07 09:26:10 +0000637
Daniel Jaspera7564432012-09-13 13:11:25 +0000638 ~VerifyIdIsBoundTo() {
Manuel Klimek579b1202012-09-07 09:26:10 +0000639 if (ExpectedCount != -1)
Manuel Klimek4da21662012-07-06 05:48:52 +0000640 EXPECT_EQ(ExpectedCount, Count);
Daniel Jaspera7564432012-09-13 13:11:25 +0000641 if (!ExpectedName.empty())
642 EXPECT_EQ(ExpectedName, Name);
Manuel Klimek4da21662012-07-06 05:48:52 +0000643 }
644
645 virtual bool run(const BoundNodes *Nodes) {
Daniel Jaspera7564432012-09-13 13:11:25 +0000646 if (Nodes->getNodeAs<T>(Id)) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000647 ++Count;
Daniel Jaspera7564432012-09-13 13:11:25 +0000648 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
649 Name = Named->getNameAsString();
650 } else if (const NestedNameSpecifier *NNS =
651 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
652 llvm::raw_string_ostream OS(Name);
653 NNS->print(OS, PrintingPolicy(LangOptions()));
Manuel Klimek579b1202012-09-07 09:26:10 +0000654 }
Manuel Klimek4da21662012-07-06 05:48:52 +0000655 return true;
656 }
657 return false;
658 }
659
660private:
661 const std::string Id;
662 const int ExpectedCount;
663 int Count;
Daniel Jaspera7564432012-09-13 13:11:25 +0000664 const std::string ExpectedName;
665 std::string Name;
Manuel Klimek4da21662012-07-06 05:48:52 +0000666};
667
668TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000669 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000670
671 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000672 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000673
674 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000675 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000676
677 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000678 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000679
680 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
681 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000682 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000683
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000684 StatementMatcher MethodX =
685 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000686
687 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
688 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000689 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000690}
691
692TEST(Matcher, BindTheSameNameInAlternatives) {
693 StatementMatcher matcher = anyOf(
694 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000695 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000696 hasRHS(integerLiteral(equals(0)))),
697 binaryOperator(hasOperatorName("+"),
698 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000699 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000700
701 EXPECT_TRUE(matchAndVerifyResultTrue(
702 // The first branch of the matcher binds x to 0 but then fails.
703 // The second branch binds x to f() and succeeds.
704 "int f() { return 0 + f(); }",
705 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000706 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000707}
708
Manuel Klimek66341c52012-08-30 19:41:06 +0000709TEST(Matcher, BindsIDForMemoizedResults) {
710 // Using the same matcher in two match expressions will make memoization
711 // kick in.
712 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
713 EXPECT_TRUE(matchAndVerifyResultTrue(
714 "class A { class B { class X {}; }; };",
715 DeclarationMatcher(anyOf(
716 recordDecl(hasName("A"), hasDescendant(ClassX)),
717 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000718 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000719}
720
Manuel Klimek4da21662012-07-06 05:48:52 +0000721TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000722 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000723 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000724 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000725 EXPECT_TRUE(
726 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000727 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000728 EXPECT_TRUE(
729 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000730 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000731}
732
733TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000734 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000735 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000736 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000737 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000738 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000739 EXPECT_TRUE(
740 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000741 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000742}
743
744TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000745 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000746 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000747 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000748 EXPECT_TRUE(
749 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000750 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000751}
752
753TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000754 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000755 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000756 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000757 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000758 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000759}
760
761TEST(Matcher, Call) {
762 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000763 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000764 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000765
766 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
767 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
768
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000769 StatementMatcher MethodOnY =
770 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000771
772 EXPECT_TRUE(
773 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
774 MethodOnY));
775 EXPECT_TRUE(
776 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
777 MethodOnY));
778 EXPECT_TRUE(
779 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
780 MethodOnY));
781 EXPECT_TRUE(
782 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
783 MethodOnY));
784 EXPECT_TRUE(
785 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
786 MethodOnY));
787
788 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000789 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000790
791 EXPECT_TRUE(
792 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
793 MethodOnYPointer));
794 EXPECT_TRUE(
795 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
796 MethodOnYPointer));
797 EXPECT_TRUE(
798 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
799 MethodOnYPointer));
800 EXPECT_TRUE(
801 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
802 MethodOnYPointer));
803 EXPECT_TRUE(
804 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
805 MethodOnYPointer));
806}
807
Daniel Jasperb54b7642012-09-20 14:12:57 +0000808TEST(Matcher, FlowControl) {
809 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
810 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
811 continueStmt()));
812 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
813 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
814 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
815}
816
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000817TEST(HasType, MatchesAsString) {
818 EXPECT_TRUE(
819 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000820 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000821 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000822 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000823 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000824 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000825 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000826 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000827}
828
Manuel Klimek4da21662012-07-06 05:48:52 +0000829TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000830 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000831 // Unary operator
832 EXPECT_TRUE(matches("class Y { }; "
833 "bool operator!(Y x) { return false; }; "
834 "Y y; bool c = !y;", OpCall));
835 // No match -- special operators like "new", "delete"
836 // FIXME: operator new takes size_t, for which we need stddef.h, for which
837 // we need to figure out include paths in the test.
838 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
839 // "class Y { }; "
840 // "void *operator new(size_t size) { return 0; } "
841 // "Y *y = new Y;", OpCall));
842 EXPECT_TRUE(notMatches("class Y { }; "
843 "void operator delete(void *p) { } "
844 "void a() {Y *y = new Y; delete y;}", OpCall));
845 // Binary operator
846 EXPECT_TRUE(matches("class Y { }; "
847 "bool operator&&(Y x, Y y) { return true; }; "
848 "Y a; Y b; bool c = a && b;",
849 OpCall));
850 // No match -- normal operator, not an overloaded one.
851 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
852 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
853}
854
855TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
856 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000857 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000858 EXPECT_TRUE(matches("class Y { }; "
859 "bool operator&&(Y x, Y y) { return true; }; "
860 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
861 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000862 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000863 EXPECT_TRUE(notMatches("class Y { }; "
864 "bool operator&&(Y x, Y y) { return true; }; "
865 "Y a; Y b; bool c = a && b;",
866 OpCallLessLess));
867}
868
869TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +0000870 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000871 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000872
873 EXPECT_TRUE(
874 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
875 MethodOnY));
876 EXPECT_TRUE(
877 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
878 MethodOnY));
879 EXPECT_TRUE(
880 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
881 MethodOnY));
882 EXPECT_TRUE(
883 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
884 MethodOnY));
885 EXPECT_TRUE(
886 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
887 MethodOnY));
888
889 EXPECT_TRUE(matches(
890 "class Y {"
891 " public: virtual void x();"
892 "};"
893 "class X : public Y {"
894 " public: virtual void x();"
895 "};"
896 "void z() { X *x; x->Y::x(); }", MethodOnY));
897}
898
899TEST(Matcher, VariableUsage) {
900 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000901 declRefExpr(to(
902 varDecl(hasInitializer(
903 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000904
905 EXPECT_TRUE(matches(
906 "class Y {"
907 " public:"
908 " bool x() const;"
909 "};"
910 "void z(const Y &y) {"
911 " bool b = y.x();"
912 " if (b) {}"
913 "}", Reference));
914
915 EXPECT_TRUE(notMatches(
916 "class Y {"
917 " public:"
918 " bool x() const;"
919 "};"
920 "void z(const Y &y) {"
921 " bool b = y.x();"
922 "}", Reference));
923}
924
Daniel Jasper9bd28092012-07-30 05:03:25 +0000925TEST(Matcher, FindsVarDeclInFuncitonParameter) {
926 EXPECT_TRUE(matches(
927 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000928 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +0000929}
930
Manuel Klimek4da21662012-07-06 05:48:52 +0000931TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +0000932 StatementMatcher CallOnVariableY =
933 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000934
935 EXPECT_TRUE(matches(
936 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
937 EXPECT_TRUE(matches(
938 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
939 EXPECT_TRUE(matches(
940 "class Y { public: void x(); };"
941 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
942 EXPECT_TRUE(matches(
943 "class Y { public: void x(); };"
944 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
945 EXPECT_TRUE(notMatches(
946 "class Y { public: void x(); };"
947 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
948 CallOnVariableY));
949}
950
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000951TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
952 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
953 unaryExprOrTypeTraitExpr()));
954 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
955 alignOfExpr(anything())));
956 // FIXME: Uncomment once alignof is enabled.
957 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
958 // unaryExprOrTypeTraitExpr()));
959 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
960 // sizeOfExpr()));
961}
962
963TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
964 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
965 hasArgumentOfType(asString("int")))));
966 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
967 hasArgumentOfType(asString("float")))));
968 EXPECT_TRUE(matches(
969 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000970 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000971 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000972 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000973}
974
Manuel Klimek4da21662012-07-06 05:48:52 +0000975TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000976 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000977}
978
979TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000980 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000981}
982
983TEST(MemberExpression, MatchesVariable) {
984 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000985 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000986 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000987 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000988 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000989 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000990}
991
992TEST(MemberExpression, MatchesStaticVariable) {
993 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000994 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000995 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000996 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000997 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000998 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000999}
1000
Daniel Jasper6a124492012-07-12 08:50:38 +00001001TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001002 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1003 EXPECT_TRUE(matches(
1004 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1005 callExpr(hasArgument(0, declRefExpr(
1006 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001007}
1008
1009TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001010 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001011 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001012 callExpr(hasArgument(0, declRefExpr(
1013 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001014}
1015
Manuel Klimek4da21662012-07-06 05:48:52 +00001016TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1017 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001018 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001019 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001020 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001021 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001022 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001023}
1024
1025TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1026 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001027 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001028 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001029 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001030 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001031 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001032}
1033
1034TEST(IsArrow, MatchesMemberCallsViaArrow) {
1035 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001036 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001037 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001038 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001039 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001040 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001041}
1042
1043TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001044 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001045
1046 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1047 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1048}
1049
1050TEST(Callee, MatchesMemberExpressions) {
1051 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001052 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001053 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001054 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001055}
1056
1057TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001058 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001059
1060 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1061 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1062
Manuel Klimeke265c872012-07-10 14:21:30 +00001063#if !defined(_MSC_VER)
1064 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001065 // Dependent contexts, but a non-dependent call.
1066 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1067 CallFunctionF));
1068 EXPECT_TRUE(
1069 matches("void f(); template <int N> struct S { void g() { f(); } };",
1070 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001071#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001072
1073 // Depedent calls don't match.
1074 EXPECT_TRUE(
1075 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1076 CallFunctionF));
1077 EXPECT_TRUE(
1078 notMatches("void f(int);"
1079 "template <typename T> struct S { void g(T t) { f(t); } };",
1080 CallFunctionF));
1081}
1082
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001083TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1084 EXPECT_TRUE(
1085 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001086 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001087}
1088
1089TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1090 EXPECT_TRUE(
1091 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001092 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001093}
1094
1095TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1096 EXPECT_TRUE(
1097 notMatches("void g(); template <typename T> void f(T t) {}"
1098 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001099 functionTemplateDecl(hasName("f"),
1100 hasDescendant(declRefExpr(to(
1101 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001102}
1103
Manuel Klimek4da21662012-07-06 05:48:52 +00001104TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001105 StatementMatcher CallArgumentY = callExpr(
1106 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001107
1108 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1109 EXPECT_TRUE(
1110 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1111 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1112
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001113 StatementMatcher WrongIndex = callExpr(
1114 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001115 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1116}
1117
1118TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001119 StatementMatcher CallArgumentY = callExpr(
1120 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001121 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1122 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1123 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1124}
1125
1126TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001127 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001128
1129 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1130 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1131 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1132}
1133
1134TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001135 DeclarationMatcher ReferenceClassX = varDecl(
1136 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001137 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1138 ReferenceClassX));
1139 EXPECT_TRUE(
1140 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1141 EXPECT_TRUE(
1142 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1143 EXPECT_TRUE(
1144 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1145}
1146
1147TEST(HasParameter, CallsInnerMatcher) {
1148 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001149 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001150 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001151 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001152}
1153
1154TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1155 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001156 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001157}
1158
1159TEST(HasType, MatchesParameterVariableTypesStrictly) {
1160 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001161 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001162 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001163 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001164 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001165 methodDecl(hasParameter(0,
1166 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001167 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001168 methodDecl(hasParameter(0,
1169 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001170}
1171
1172TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1173 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001174 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001175 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001176 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001177}
1178
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001179TEST(Returns, MatchesReturnTypes) {
1180 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001181 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001182 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001183 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001184 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001185 functionDecl(returns(hasDeclaration(
1186 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001187}
1188
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001189TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001190 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1191 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1192 functionDecl(isExternC())));
1193 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001194}
1195
Manuel Klimek4da21662012-07-06 05:48:52 +00001196TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1197 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001198 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001199}
1200
1201TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1202 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001203 methodDecl(hasAnyParameter(hasType(pointsTo(
1204 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001205}
1206
1207TEST(HasName, MatchesParameterVariableDeclartions) {
1208 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001209 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001210 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001211 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001212}
1213
Daniel Jasper371f9392012-08-01 08:40:24 +00001214TEST(Matcher, MatchesClassTemplateSpecialization) {
1215 EXPECT_TRUE(matches("template<typename T> struct A {};"
1216 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001217 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001218 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001219 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001220 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001221 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001222}
1223
1224TEST(Matcher, MatchesTypeTemplateArgument) {
1225 EXPECT_TRUE(matches(
1226 "template<typename T> struct B {};"
1227 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001228 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001229 asString("int"))))));
1230}
1231
1232TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1233 EXPECT_TRUE(matches(
1234 "struct B { int next; };"
1235 "template<int(B::*next_ptr)> struct A {};"
1236 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001237 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1238 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001239
1240 EXPECT_TRUE(notMatches(
1241 "template <typename T> struct A {};"
1242 "A<int> a;",
1243 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1244 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001245}
1246
1247TEST(Matcher, MatchesSpecificArgument) {
1248 EXPECT_TRUE(matches(
1249 "template<typename T, typename U> class A {};"
1250 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001251 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001252 1, refersToType(asString("int"))))));
1253 EXPECT_TRUE(notMatches(
1254 "template<typename T, typename U> class A {};"
1255 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001256 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001257 1, refersToType(asString("int"))))));
1258}
1259
Manuel Klimek4da21662012-07-06 05:48:52 +00001260TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001261 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001262
1263 EXPECT_TRUE(
1264 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1265 EXPECT_TRUE(
1266 matches("class X { public: X(); }; void x() { X x = X(); }",
1267 Constructor));
1268 EXPECT_TRUE(
1269 matches("class X { public: X(int); }; void x() { X x = 0; }",
1270 Constructor));
1271 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1272}
1273
1274TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001275 StatementMatcher Constructor = constructExpr(
1276 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001277
1278 EXPECT_TRUE(
1279 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1280 Constructor));
1281 EXPECT_TRUE(
1282 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1283 Constructor));
1284 EXPECT_TRUE(
1285 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1286 Constructor));
1287 EXPECT_TRUE(
1288 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1289 Constructor));
1290
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001291 StatementMatcher WrongIndex = constructExpr(
1292 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001293 EXPECT_TRUE(
1294 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1295 WrongIndex));
1296}
1297
1298TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001299 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001300
1301 EXPECT_TRUE(
1302 matches("class X { public: X(int); }; void x() { X x(0); }",
1303 Constructor1Arg));
1304 EXPECT_TRUE(
1305 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1306 Constructor1Arg));
1307 EXPECT_TRUE(
1308 matches("class X { public: X(int); }; void x() { X x = 0; }",
1309 Constructor1Arg));
1310 EXPECT_TRUE(
1311 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1312 Constructor1Arg));
1313}
1314
1315TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001316 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001317
1318 std::string ClassString = "class string { public: string(); ~string(); }; ";
1319
1320 EXPECT_TRUE(
1321 matches(ClassString +
1322 "string GetStringByValue();"
1323 "void FunctionTakesString(string s);"
1324 "void run() { FunctionTakesString(GetStringByValue()); }",
1325 TempExpression));
1326
1327 EXPECT_TRUE(
1328 notMatches(ClassString +
1329 "string* GetStringPointer(); "
1330 "void FunctionTakesStringPtr(string* s);"
1331 "void run() {"
1332 " string* s = GetStringPointer();"
1333 " FunctionTakesStringPtr(GetStringPointer());"
1334 " FunctionTakesStringPtr(s);"
1335 "}",
1336 TempExpression));
1337
1338 EXPECT_TRUE(
1339 notMatches("class no_dtor {};"
1340 "no_dtor GetObjByValue();"
1341 "void ConsumeObj(no_dtor param);"
1342 "void run() { ConsumeObj(GetObjByValue()); }",
1343 TempExpression));
1344}
1345
Sam Panzere16acd32012-08-24 22:04:44 +00001346TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1347 std::string ClassString =
1348 "class string { public: string(); int length(); }; ";
1349
1350 EXPECT_TRUE(
1351 matches(ClassString +
1352 "string GetStringByValue();"
1353 "void FunctionTakesString(string s);"
1354 "void run() { FunctionTakesString(GetStringByValue()); }",
1355 materializeTemporaryExpr()));
1356
1357 EXPECT_TRUE(
1358 notMatches(ClassString +
1359 "string* GetStringPointer(); "
1360 "void FunctionTakesStringPtr(string* s);"
1361 "void run() {"
1362 " string* s = GetStringPointer();"
1363 " FunctionTakesStringPtr(GetStringPointer());"
1364 " FunctionTakesStringPtr(s);"
1365 "}",
1366 materializeTemporaryExpr()));
1367
1368 EXPECT_TRUE(
1369 notMatches(ClassString +
1370 "string GetStringByValue();"
1371 "void run() { int k = GetStringByValue().length(); }",
1372 materializeTemporaryExpr()));
1373
1374 EXPECT_TRUE(
1375 notMatches(ClassString +
1376 "string GetStringByValue();"
1377 "void run() { GetStringByValue(); }",
1378 materializeTemporaryExpr()));
1379}
1380
Manuel Klimek4da21662012-07-06 05:48:52 +00001381TEST(ConstructorDeclaration, SimpleCase) {
1382 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001383 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001384 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001385 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001386}
1387
1388TEST(ConstructorDeclaration, IsImplicit) {
1389 // This one doesn't match because the constructor is not added by the
1390 // compiler (it is not needed).
1391 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001392 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001393 // The compiler added the implicit default constructor.
1394 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001395 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001396 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001397 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001398}
1399
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001400TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1401 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001402 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001403}
1404
1405TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001406 EXPECT_TRUE(notMatches("class Foo {};",
1407 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001408}
1409
Manuel Klimek4da21662012-07-06 05:48:52 +00001410TEST(HasAnyConstructorInitializer, SimpleCase) {
1411 EXPECT_TRUE(notMatches(
1412 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001413 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001414 EXPECT_TRUE(matches(
1415 "class Foo {"
1416 " Foo() : foo_() { }"
1417 " int foo_;"
1418 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001419 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001420}
1421
1422TEST(HasAnyConstructorInitializer, ForField) {
1423 static const char Code[] =
1424 "class Baz { };"
1425 "class Foo {"
1426 " Foo() : foo_() { }"
1427 " Baz foo_;"
1428 " Baz bar_;"
1429 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001430 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1431 forField(hasType(recordDecl(hasName("Baz"))))))));
1432 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001433 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001434 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1435 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001436}
1437
1438TEST(HasAnyConstructorInitializer, WithInitializer) {
1439 static const char Code[] =
1440 "class Foo {"
1441 " Foo() : foo_(0) { }"
1442 " int foo_;"
1443 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001444 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001445 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001446 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001447 withInitializer(integerLiteral(equals(1)))))));
1448}
1449
1450TEST(HasAnyConstructorInitializer, IsWritten) {
1451 static const char Code[] =
1452 "struct Bar { Bar(){} };"
1453 "class Foo {"
1454 " Foo() : foo_() { }"
1455 " Bar foo_;"
1456 " Bar bar_;"
1457 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001458 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001459 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001460 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001461 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001462 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001463 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1464}
1465
1466TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001467 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001468
1469 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1470 EXPECT_TRUE(
1471 matches("class X { public: X(); }; void x() { new X(); }", New));
1472 EXPECT_TRUE(
1473 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1474 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1475}
1476
1477TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001478 StatementMatcher New = constructExpr(
1479 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001480
1481 EXPECT_TRUE(
1482 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1483 New));
1484 EXPECT_TRUE(
1485 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1486 New));
1487 EXPECT_TRUE(
1488 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1489 New));
1490
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001491 StatementMatcher WrongIndex = constructExpr(
1492 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001493 EXPECT_TRUE(
1494 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1495 WrongIndex));
1496}
1497
1498TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001499 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001500
1501 EXPECT_TRUE(
1502 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1503 EXPECT_TRUE(
1504 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1505 New));
1506}
1507
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001508TEST(Matcher, DeleteExpression) {
1509 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001510 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001511}
1512
Manuel Klimek4da21662012-07-06 05:48:52 +00001513TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001514 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001515
1516 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1517 EXPECT_TRUE(
1518 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1519 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1520}
1521
1522TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001523 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001524 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1525 // wide string
1526 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1527 // with escaped characters
1528 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1529 // no matching -- though the data type is the same, there is no string literal
1530 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1531}
1532
1533TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001534 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001535 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1536 // wide character
1537 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1538 // wide character, Hex encoded, NOT MATCHED!
1539 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1540 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1541}
1542
1543TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001544 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001545 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1546 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1547 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1548 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1549
1550 // Non-matching cases (character literals, float and double)
1551 EXPECT_TRUE(notMatches("int i = L'a';",
1552 HasIntLiteral)); // this is actually a character
1553 // literal cast to int
1554 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1555 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1556 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1557}
1558
Daniel Jasperb54b7642012-09-20 14:12:57 +00001559TEST(Matcher, AsmStatement) {
1560 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1561}
1562
Manuel Klimek4da21662012-07-06 05:48:52 +00001563TEST(Matcher, Conditions) {
1564 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1565
1566 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1567 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1568 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1569 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1570 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1571}
1572
1573TEST(MatchBinaryOperator, HasOperatorName) {
1574 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1575
1576 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1577 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1578}
1579
1580TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1581 StatementMatcher OperatorTrueFalse =
1582 binaryOperator(hasLHS(boolLiteral(equals(true))),
1583 hasRHS(boolLiteral(equals(false))));
1584
1585 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1586 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1587 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1588}
1589
1590TEST(MatchBinaryOperator, HasEitherOperand) {
1591 StatementMatcher HasOperand =
1592 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1593
1594 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1595 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1596 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1597}
1598
1599TEST(Matcher, BinaryOperatorTypes) {
1600 // Integration test that verifies the AST provides all binary operators in
1601 // a way we expect.
1602 // FIXME: Operator ','
1603 EXPECT_TRUE(
1604 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1605 EXPECT_TRUE(
1606 matches("bool b; bool c = (b = true);",
1607 binaryOperator(hasOperatorName("="))));
1608 EXPECT_TRUE(
1609 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1610 EXPECT_TRUE(
1611 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1612 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1613 EXPECT_TRUE(
1614 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1615 EXPECT_TRUE(
1616 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1617 EXPECT_TRUE(
1618 matches("int i = 1; int j = (i <<= 2);",
1619 binaryOperator(hasOperatorName("<<="))));
1620 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1621 EXPECT_TRUE(
1622 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1623 EXPECT_TRUE(
1624 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1625 EXPECT_TRUE(
1626 matches("int i = 1; int j = (i >>= 2);",
1627 binaryOperator(hasOperatorName(">>="))));
1628 EXPECT_TRUE(
1629 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1630 EXPECT_TRUE(
1631 matches("int i = 42; int j = (i ^= 42);",
1632 binaryOperator(hasOperatorName("^="))));
1633 EXPECT_TRUE(
1634 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1635 EXPECT_TRUE(
1636 matches("int i = 42; int j = (i %= 42);",
1637 binaryOperator(hasOperatorName("%="))));
1638 EXPECT_TRUE(
1639 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1640 EXPECT_TRUE(
1641 matches("bool b = true && false;",
1642 binaryOperator(hasOperatorName("&&"))));
1643 EXPECT_TRUE(
1644 matches("bool b = true; bool c = (b &= false);",
1645 binaryOperator(hasOperatorName("&="))));
1646 EXPECT_TRUE(
1647 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1648 EXPECT_TRUE(
1649 matches("bool b = true || false;",
1650 binaryOperator(hasOperatorName("||"))));
1651 EXPECT_TRUE(
1652 matches("bool b = true; bool c = (b |= false);",
1653 binaryOperator(hasOperatorName("|="))));
1654 EXPECT_TRUE(
1655 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1656 EXPECT_TRUE(
1657 matches("int i = 42; int j = (i *= 23);",
1658 binaryOperator(hasOperatorName("*="))));
1659 EXPECT_TRUE(
1660 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1661 EXPECT_TRUE(
1662 matches("int i = 42; int j = (i /= 23);",
1663 binaryOperator(hasOperatorName("/="))));
1664 EXPECT_TRUE(
1665 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1666 EXPECT_TRUE(
1667 matches("int i = 42; int j = (i += 23);",
1668 binaryOperator(hasOperatorName("+="))));
1669 EXPECT_TRUE(
1670 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1671 EXPECT_TRUE(
1672 matches("int i = 42; int j = (i -= 23);",
1673 binaryOperator(hasOperatorName("-="))));
1674 EXPECT_TRUE(
1675 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1676 binaryOperator(hasOperatorName("->*"))));
1677 EXPECT_TRUE(
1678 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1679 binaryOperator(hasOperatorName(".*"))));
1680
1681 // Member expressions as operators are not supported in matches.
1682 EXPECT_TRUE(
1683 notMatches("struct A { void x(A *a) { a->x(this); } };",
1684 binaryOperator(hasOperatorName("->"))));
1685
1686 // Initializer assignments are not represented as operator equals.
1687 EXPECT_TRUE(
1688 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1689
1690 // Array indexing is not represented as operator.
1691 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1692
1693 // Overloaded operators do not match at all.
1694 EXPECT_TRUE(notMatches(
1695 "struct A { bool operator&&(const A &a) const { return false; } };"
1696 "void x() { A a, b; a && b; }",
1697 binaryOperator()));
1698}
1699
1700TEST(MatchUnaryOperator, HasOperatorName) {
1701 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1702
1703 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1704 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1705}
1706
1707TEST(MatchUnaryOperator, HasUnaryOperand) {
1708 StatementMatcher OperatorOnFalse =
1709 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1710
1711 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1712 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1713}
1714
1715TEST(Matcher, UnaryOperatorTypes) {
1716 // Integration test that verifies the AST provides all unary operators in
1717 // a way we expect.
1718 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1719 EXPECT_TRUE(
1720 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1721 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1722 EXPECT_TRUE(
1723 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1724 EXPECT_TRUE(
1725 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1726 EXPECT_TRUE(
1727 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1728 EXPECT_TRUE(
1729 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1730 EXPECT_TRUE(
1731 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1732 EXPECT_TRUE(
1733 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1734 EXPECT_TRUE(
1735 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1736
1737 // We don't match conversion operators.
1738 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1739
1740 // Function calls are not represented as operator.
1741 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1742
1743 // Overloaded operators do not match at all.
1744 // FIXME: We probably want to add that.
1745 EXPECT_TRUE(notMatches(
1746 "struct A { bool operator!() const { return false; } };"
1747 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1748}
1749
1750TEST(Matcher, ConditionalOperator) {
1751 StatementMatcher Conditional = conditionalOperator(
1752 hasCondition(boolLiteral(equals(true))),
1753 hasTrueExpression(boolLiteral(equals(false))));
1754
1755 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1756 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1757 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1758
1759 StatementMatcher ConditionalFalse = conditionalOperator(
1760 hasFalseExpression(boolLiteral(equals(false))));
1761
1762 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1763 EXPECT_TRUE(
1764 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1765}
1766
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001767TEST(ArraySubscriptMatchers, ArraySubscripts) {
1768 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1769 arraySubscriptExpr()));
1770 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1771 arraySubscriptExpr()));
1772}
1773
1774TEST(ArraySubscriptMatchers, ArrayIndex) {
1775 EXPECT_TRUE(matches(
1776 "int i[2]; void f() { i[1] = 1; }",
1777 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1778 EXPECT_TRUE(matches(
1779 "int i[2]; void f() { 1[i] = 1; }",
1780 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1781 EXPECT_TRUE(notMatches(
1782 "int i[2]; void f() { i[1] = 1; }",
1783 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1784}
1785
1786TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1787 EXPECT_TRUE(matches(
1788 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001789 arraySubscriptExpr(hasBase(implicitCastExpr(
1790 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001791}
1792
Manuel Klimek4da21662012-07-06 05:48:52 +00001793TEST(Matcher, HasNameSupportsNamespaces) {
1794 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001795 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001796 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001797 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001798 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001799 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001800 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001801 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001802 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001803 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001804 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001805 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001806 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001807 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001808 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001809 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001810 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001811 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001812 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001813 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001814 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001815 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001816 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001817 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001818}
1819
1820TEST(Matcher, HasNameSupportsOuterClasses) {
1821 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001822 matches("class A { class B { class C; }; };",
1823 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001824 EXPECT_TRUE(
1825 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001826 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001827 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001828 matches("class A { class B { class C; }; };",
1829 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001830 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001831 matches("class A { class B { class C; }; };",
1832 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001833 EXPECT_TRUE(
1834 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001835 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001836 EXPECT_TRUE(
1837 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001838 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001839 EXPECT_TRUE(
1840 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001841 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001842 EXPECT_TRUE(
1843 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001844 recordDecl(hasName("::C"))));
1845 EXPECT_TRUE(
1846 notMatches("class A { class B { class C; }; };",
1847 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001848 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001849 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001850 EXPECT_TRUE(
1851 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001852 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001853}
1854
1855TEST(Matcher, IsDefinition) {
1856 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001857 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001858 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1859 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1860
1861 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001862 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001863 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1864 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1865
1866 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001867 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001868 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1869 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1870}
1871
1872TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001873 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00001874 ofClass(hasName("X")))));
1875
1876 EXPECT_TRUE(
1877 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
1878 EXPECT_TRUE(
1879 matches("class X { public: X(); }; void x(int) { X x = X(); }",
1880 Constructor));
1881 EXPECT_TRUE(
1882 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
1883 Constructor));
1884}
1885
1886TEST(Matcher, VisitsTemplateInstantiations) {
1887 EXPECT_TRUE(matches(
1888 "class A { public: void x(); };"
1889 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001890 "void f() { B<A> b; b.y(); }",
1891 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001892
1893 EXPECT_TRUE(matches(
1894 "class A { public: void x(); };"
1895 "class C {"
1896 " public:"
1897 " template <typename T> class B { public: void y() { T t; t.x(); } };"
1898 "};"
1899 "void f() {"
1900 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001901 "}",
1902 recordDecl(hasName("C"),
1903 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001904}
1905
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001906TEST(Matcher, HandlesNullQualTypes) {
1907 // FIXME: Add a Type matcher so we can replace uses of this
1908 // variable with Type(True())
1909 const TypeMatcher AnyType = anything();
1910
1911 // We don't really care whether this matcher succeeds; we're testing that
1912 // it completes without crashing.
1913 EXPECT_TRUE(matches(
1914 "struct A { };"
1915 "template <typename T>"
1916 "void f(T t) {"
1917 " T local_t(t /* this becomes a null QualType in the AST */);"
1918 "}"
1919 "void g() {"
1920 " f(0);"
1921 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001922 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001923 anyOf(
1924 TypeMatcher(hasDeclaration(anything())),
1925 pointsTo(AnyType),
1926 references(AnyType)
1927 // Other QualType matchers should go here.
1928 ))))));
1929}
1930
Manuel Klimek4da21662012-07-06 05:48:52 +00001931// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001932AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00001933 // Make sure all special variables are used: node, match_finder,
1934 // bound_nodes_builder, and the parameter named 'AMatcher'.
1935 return AMatcher.matches(Node, Finder, Builder);
1936}
1937
1938TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001939 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001940
1941 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001942 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001943
1944 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001945 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001946
1947 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001948 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001949}
1950
1951AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001952 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
1953 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
1954 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00001955 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00001956 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00001957 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00001958 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
1959 ASTMatchFinder::BK_First);
1960}
1961
1962TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001963 DeclarationMatcher HasClassB =
1964 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001965
1966 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001967 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001968
1969 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001970 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001971
1972 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001973 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001974
1975 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001976 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001977
1978 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
1979}
1980
1981TEST(For, FindsForLoops) {
1982 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
1983 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
1984}
1985
Daniel Jasper6a124492012-07-12 08:50:38 +00001986TEST(For, ForLoopInternals) {
1987 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
1988 forStmt(hasCondition(anything()))));
1989 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
1990 forStmt(hasLoopInit(anything()))));
1991}
1992
1993TEST(For, NegativeForLoopInternals) {
1994 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001995 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001996 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
1997 forStmt(hasLoopInit(anything()))));
1998}
1999
Manuel Klimek4da21662012-07-06 05:48:52 +00002000TEST(For, ReportsNoFalsePositives) {
2001 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2002 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2003}
2004
2005TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002006 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2007 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2008 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002009}
2010
2011TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2012 // It's not a compound statement just because there's "{}" in the source
2013 // text. This is an AST search, not grep.
2014 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002015 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002016 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002017 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002018}
2019
Daniel Jasper6a124492012-07-12 08:50:38 +00002020TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002021 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002022 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002023 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002024 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002025 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002026 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002027 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002028 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002029}
2030
2031TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2032 // The simplest case: every compound statement is in a function
2033 // definition, and the function body itself must be a compound
2034 // statement.
2035 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002036 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002037}
2038
2039TEST(HasAnySubstatement, IsNotRecursive) {
2040 // It's really "has any immediate substatement".
2041 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002042 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002043}
2044
2045TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2046 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002047 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002048}
2049
2050TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2051 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002052 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002053}
2054
2055TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2056 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002057 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002058 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002059 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002060}
2061
2062TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2063 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002064 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002065 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002066 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002067 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002068 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002069}
2070
2071TEST(StatementCountIs, WorksWithMultipleStatements) {
2072 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002073 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002074}
2075
2076TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2077 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002078 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002079 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002080 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002081 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002082 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002083 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002084 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002085}
2086
2087TEST(Member, WorksInSimplestCase) {
2088 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002089 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002090}
2091
2092TEST(Member, DoesNotMatchTheBaseExpression) {
2093 // Don't pick out the wrong part of the member expression, this should
2094 // be checking the member (name) only.
2095 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002096 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002097}
2098
2099TEST(Member, MatchesInMemberFunctionCall) {
2100 EXPECT_TRUE(matches("void f() {"
2101 " struct { void first() {}; } s;"
2102 " s.first();"
2103 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002104 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002105}
2106
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002107TEST(Member, MatchesMemberAllocationFunction) {
Dmitri Gribenko02ed37f2012-08-18 00:41:04 +00002108 EXPECT_TRUE(matches("namespace std { typedef typeof(sizeof(int)) size_t; }"
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002109 "class X { void *operator new(std::size_t); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002110 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002111
2112 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002113 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002114
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002115 EXPECT_TRUE(matches(
2116 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2117 "class X { void operator delete[](void*, std::size_t); };",
2118 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002119}
2120
Manuel Klimek4da21662012-07-06 05:48:52 +00002121TEST(HasObjectExpression, DoesNotMatchMember) {
2122 EXPECT_TRUE(notMatches(
2123 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002124 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002125}
2126
2127TEST(HasObjectExpression, MatchesBaseOfVariable) {
2128 EXPECT_TRUE(matches(
2129 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002130 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002131 EXPECT_TRUE(matches(
2132 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002133 memberExpr(hasObjectExpression(
2134 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002135}
2136
2137TEST(HasObjectExpression,
2138 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2139 EXPECT_TRUE(matches(
2140 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002141 memberExpr(hasObjectExpression(
2142 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002143 EXPECT_TRUE(matches(
2144 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002145 memberExpr(hasObjectExpression(
2146 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002147}
2148
2149TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002150 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2151 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2152 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2153 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002154}
2155
2156TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002157 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002158}
2159
2160TEST(IsConstQualified, MatchesConstInt) {
2161 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002162 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002163}
2164
2165TEST(IsConstQualified, MatchesConstPointer) {
2166 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002167 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002168}
2169
2170TEST(IsConstQualified, MatchesThroughTypedef) {
2171 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002172 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002173 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002174 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002175}
2176
2177TEST(IsConstQualified, DoesNotMatchInappropriately) {
2178 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002179 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002180 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002181 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002182}
2183
Sam Panzer089e5b32012-08-16 16:58:10 +00002184TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002185 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2186 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2187 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2188 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002189}
2190TEST(CastExpression, MatchesImplicitCasts) {
2191 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002192 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002193 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002194 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002195}
2196
2197TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002198 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2199 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2200 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2201 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002202}
2203
Manuel Klimek4da21662012-07-06 05:48:52 +00002204TEST(ReinterpretCast, MatchesSimpleCase) {
2205 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002206 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002207}
2208
2209TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002210 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002211 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002212 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002213 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002214 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002215 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2216 "B b;"
2217 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002218 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002219}
2220
2221TEST(FunctionalCast, MatchesSimpleCase) {
2222 std::string foo_class = "class Foo { public: Foo(char*); };";
2223 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002224 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002225}
2226
2227TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2228 std::string FooClass = "class Foo { public: Foo(char*); };";
2229 EXPECT_TRUE(
2230 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002231 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002232 EXPECT_TRUE(
2233 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002234 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002235}
2236
2237TEST(DynamicCast, MatchesSimpleCase) {
2238 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2239 "B b;"
2240 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002241 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002242}
2243
2244TEST(StaticCast, MatchesSimpleCase) {
2245 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002246 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002247}
2248
2249TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002250 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002251 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002252 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002253 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002254 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002255 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2256 "B b;"
2257 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002258 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002259}
2260
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002261TEST(CStyleCast, MatchesSimpleCase) {
2262 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2263}
2264
2265TEST(CStyleCast, DoesNotMatchOtherCasts) {
2266 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2267 "char q, *r = const_cast<char*>(&q);"
2268 "void* s = reinterpret_cast<char*>(&s);"
2269 "struct B { virtual ~B() {} }; struct D : B {};"
2270 "B b;"
2271 "D* t = dynamic_cast<D*>(&b);",
2272 cStyleCastExpr()));
2273}
2274
Manuel Klimek4da21662012-07-06 05:48:52 +00002275TEST(HasDestinationType, MatchesSimpleCase) {
2276 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002277 staticCastExpr(hasDestinationType(
2278 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002279}
2280
Sam Panzer089e5b32012-08-16 16:58:10 +00002281TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2282 // This test creates an implicit const cast.
2283 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002284 implicitCastExpr(
2285 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002286 // This test creates an implicit array-to-pointer cast.
2287 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002288 implicitCastExpr(hasImplicitDestinationType(
2289 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002290}
2291
2292TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2293 // This test creates an implicit cast from int to char.
2294 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002295 implicitCastExpr(hasImplicitDestinationType(
2296 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002297 // This test creates an implicit array-to-pointer cast.
2298 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002299 implicitCastExpr(hasImplicitDestinationType(
2300 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002301}
2302
2303TEST(ImplicitCast, MatchesSimpleCase) {
2304 // This test creates an implicit const cast.
2305 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002306 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002307 // This test creates an implicit cast from int to char.
2308 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002309 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002310 // This test creates an implicit array-to-pointer cast.
2311 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002312 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002313}
2314
2315TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002316 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002317 // are present, and that it ignores explicit and paren casts.
2318
2319 // These two test cases have no casts.
2320 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002321 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002322 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002323 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002324
2325 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002326 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002327 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002328 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002329
2330 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002331 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002332}
2333
2334TEST(IgnoringImpCasts, MatchesImpCasts) {
2335 // This test checks that ignoringImpCasts matches when implicit casts are
2336 // present and its inner matcher alone does not match.
2337 // Note that this test creates an implicit const cast.
2338 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002339 varDecl(hasInitializer(ignoringImpCasts(
2340 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002341 // This test creates an implict cast from int to char.
2342 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002343 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002344 integerLiteral(equals(0)))))));
2345}
2346
2347TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2348 // These tests verify that ignoringImpCasts does not match if the inner
2349 // matcher does not match.
2350 // Note that the first test creates an implicit const cast.
2351 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002352 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002353 unless(anything()))))));
2354 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002355 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002356 unless(anything()))))));
2357
2358 // These tests verify that ignoringImplictCasts does not look through explicit
2359 // casts or parentheses.
2360 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002361 varDecl(hasInitializer(ignoringImpCasts(
2362 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002363 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002364 varDecl(hasInitializer(ignoringImpCasts(
2365 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002366 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002367 varDecl(hasInitializer(ignoringImpCasts(
2368 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002369 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002370 varDecl(hasInitializer(ignoringImpCasts(
2371 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002372}
2373
2374TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2375 // This test verifies that expressions that do not have implicit casts
2376 // still match the inner matcher.
2377 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002378 varDecl(hasInitializer(ignoringImpCasts(
2379 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002380}
2381
2382TEST(IgnoringParenCasts, MatchesParenCasts) {
2383 // This test checks that ignoringParenCasts matches when parentheses and/or
2384 // casts are present and its inner matcher alone does not match.
2385 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002386 varDecl(hasInitializer(ignoringParenCasts(
2387 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002388 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002389 varDecl(hasInitializer(ignoringParenCasts(
2390 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002391
2392 // This test creates an implict cast from int to char in addition to the
2393 // parentheses.
2394 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002395 varDecl(hasInitializer(ignoringParenCasts(
2396 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002397
2398 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002399 varDecl(hasInitializer(ignoringParenCasts(
2400 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002401 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002402 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002403 integerLiteral(equals(0)))))));
2404}
2405
2406TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2407 // This test verifies that expressions that do not have any casts still match.
2408 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002409 varDecl(hasInitializer(ignoringParenCasts(
2410 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002411}
2412
2413TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2414 // These tests verify that ignoringImpCasts does not match if the inner
2415 // matcher does not match.
2416 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002417 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002418 unless(anything()))))));
2419
2420 // This test creates an implicit cast from int to char in addition to the
2421 // parentheses.
2422 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002423 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002424 unless(anything()))))));
2425
2426 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002427 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002428 unless(anything()))))));
2429}
2430
2431TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2432 // This test checks that ignoringParenAndImpCasts matches when
2433 // parentheses and/or implicit casts are present and its inner matcher alone
2434 // does not match.
2435 // Note that this test creates an implicit const cast.
2436 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002437 varDecl(hasInitializer(ignoringParenImpCasts(
2438 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002439 // This test creates an implicit cast from int to char.
2440 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002441 varDecl(hasInitializer(ignoringParenImpCasts(
2442 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002443}
2444
2445TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2446 // This test verifies that expressions that do not have parentheses or
2447 // implicit casts still match.
2448 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002449 varDecl(hasInitializer(ignoringParenImpCasts(
2450 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002451 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002452 varDecl(hasInitializer(ignoringParenImpCasts(
2453 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002454}
2455
2456TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2457 // These tests verify that ignoringParenImpCasts does not match if
2458 // the inner matcher does not match.
2459 // This test creates an implicit cast.
2460 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002461 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002462 unless(anything()))))));
2463 // These tests verify that ignoringParenAndImplictCasts does not look
2464 // through explicit casts.
2465 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002466 varDecl(hasInitializer(ignoringParenImpCasts(
2467 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002468 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002469 varDecl(hasInitializer(ignoringParenImpCasts(
2470 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002471 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002472 varDecl(hasInitializer(ignoringParenImpCasts(
2473 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002474}
2475
Manuel Klimek715c9562012-07-25 10:02:02 +00002476TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002477 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2478 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002479 implicitCastExpr(
2480 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002481}
2482
Manuel Klimek715c9562012-07-25 10:02:02 +00002483TEST(HasSourceExpression, MatchesExplicitCasts) {
2484 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002485 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002486 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002487 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002488}
2489
Manuel Klimek4da21662012-07-06 05:48:52 +00002490TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002491 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002492}
2493
2494TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002495 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002496}
2497
2498TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002499 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002500}
2501
2502TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002503 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002504}
2505
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002506TEST(InitListExpression, MatchesInitListExpression) {
2507 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2508 initListExpr(hasType(asString("int [2]")))));
2509 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002510 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002511}
2512
2513TEST(UsingDeclaration, MatchesUsingDeclarations) {
2514 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2515 usingDecl()));
2516}
2517
2518TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2519 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2520 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2521}
2522
2523TEST(UsingDeclaration, MatchesSpecificTarget) {
2524 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2525 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002526 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002527 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2528 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002529 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002530}
2531
2532TEST(UsingDeclaration, ThroughUsingDeclaration) {
2533 EXPECT_TRUE(matches(
2534 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002535 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002536 EXPECT_TRUE(notMatches(
2537 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002538 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002539}
2540
Sam Panzer425f41b2012-08-16 17:20:59 +00002541TEST(SingleDecl, IsSingleDecl) {
2542 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002543 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002544 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2545 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2546 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2547 SingleDeclStmt));
2548}
2549
2550TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002551 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002552
2553 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002554 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002555 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002556 declStmt(containsDeclaration(0, MatchesInit),
2557 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002558 unsigned WrongIndex = 42;
2559 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002560 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002561 MatchesInit))));
2562}
2563
2564TEST(DeclCount, DeclCountIsCorrect) {
2565 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002566 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002567 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002568 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002569 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002570 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002571}
2572
Manuel Klimek4da21662012-07-06 05:48:52 +00002573TEST(While, MatchesWhileLoops) {
2574 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2575 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2576 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2577}
2578
2579TEST(Do, MatchesDoLoops) {
2580 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2581 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2582}
2583
2584TEST(Do, DoesNotMatchWhileLoops) {
2585 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2586}
2587
2588TEST(SwitchCase, MatchesCase) {
2589 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2590 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2591 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2592 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2593}
2594
Daniel Jasperb54b7642012-09-20 14:12:57 +00002595TEST(SwitchCase, MatchesSwitch) {
2596 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2597 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2598 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2599 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2600}
2601
2602TEST(ExceptionHandling, SimpleCases) {
2603 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2604 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2605 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2606 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2607 throwExpr()));
2608 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2609 throwExpr()));
2610}
2611
Manuel Klimek4da21662012-07-06 05:48:52 +00002612TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2613 EXPECT_TRUE(notMatches(
2614 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002615 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002616 EXPECT_TRUE(notMatches(
2617 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002618 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002619}
2620
2621TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2622 EXPECT_TRUE(matches(
2623 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002624 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002625}
2626
2627TEST(ForEach, BindsOneNode) {
2628 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002629 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002630 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002631}
2632
2633TEST(ForEach, BindsMultipleNodes) {
2634 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002635 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002636 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002637}
2638
2639TEST(ForEach, BindsRecursiveCombinations) {
2640 EXPECT_TRUE(matchAndVerifyResultTrue(
2641 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002642 recordDecl(hasName("C"),
2643 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002644 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002645}
2646
2647TEST(ForEachDescendant, BindsOneNode) {
2648 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002649 recordDecl(hasName("C"),
2650 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002651 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002652}
2653
2654TEST(ForEachDescendant, BindsMultipleNodes) {
2655 EXPECT_TRUE(matchAndVerifyResultTrue(
2656 "class C { class D { int x; int y; }; "
2657 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002658 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002659 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002660}
2661
2662TEST(ForEachDescendant, BindsRecursiveCombinations) {
2663 EXPECT_TRUE(matchAndVerifyResultTrue(
2664 "class C { class D { "
2665 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002666 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2667 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002668 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002669}
2670
2671
2672TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2673 // Make sure that we can both match the class by name (::X) and by the type
2674 // the template was instantiated with (via a field).
2675
2676 EXPECT_TRUE(matches(
2677 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002678 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002679
2680 EXPECT_TRUE(matches(
2681 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002682 recordDecl(isTemplateInstantiation(), hasDescendant(
2683 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002684}
2685
2686TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2687 EXPECT_TRUE(matches(
2688 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002689 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002690 isTemplateInstantiation())));
2691}
2692
2693TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2694 EXPECT_TRUE(matches(
2695 "template <typename T> class X { T t; }; class A {};"
2696 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002697 recordDecl(isTemplateInstantiation(), hasDescendant(
2698 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002699}
2700
2701TEST(IsTemplateInstantiation,
2702 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2703 EXPECT_TRUE(matches(
2704 "template <typename T> class X {};"
2705 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002706 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002707}
2708
2709TEST(IsTemplateInstantiation,
2710 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2711 EXPECT_TRUE(matches(
2712 "class A {};"
2713 "class X {"
2714 " template <typename U> class Y { U u; };"
2715 " Y<A> y;"
2716 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002717 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002718}
2719
2720TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2721 // FIXME: Figure out whether this makes sense. It doesn't affect the
2722 // normal use case as long as the uppermost instantiation always is marked
2723 // as template instantiation, but it might be confusing as a predicate.
2724 EXPECT_TRUE(matches(
2725 "class A {};"
2726 "template <typename T> class X {"
2727 " template <typename U> class Y { U u; };"
2728 " Y<T> y;"
2729 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002730 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002731}
2732
2733TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2734 EXPECT_TRUE(notMatches(
2735 "template <typename T> class X {}; class A {};"
2736 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002737 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002738}
2739
2740TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2741 EXPECT_TRUE(notMatches(
2742 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002743 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002744}
2745
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002746TEST(IsExplicitTemplateSpecialization,
2747 DoesNotMatchPrimaryTemplate) {
2748 EXPECT_TRUE(notMatches(
2749 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002750 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002751 EXPECT_TRUE(notMatches(
2752 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002753 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002754}
2755
2756TEST(IsExplicitTemplateSpecialization,
2757 DoesNotMatchExplicitTemplateInstantiations) {
2758 EXPECT_TRUE(notMatches(
2759 "template <typename T> class X {};"
2760 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002761 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002762 EXPECT_TRUE(notMatches(
2763 "template <typename T> void f(T t) {}"
2764 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002765 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002766}
2767
2768TEST(IsExplicitTemplateSpecialization,
2769 DoesNotMatchImplicitTemplateInstantiations) {
2770 EXPECT_TRUE(notMatches(
2771 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002772 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002773 EXPECT_TRUE(notMatches(
2774 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002775 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002776}
2777
2778TEST(IsExplicitTemplateSpecialization,
2779 MatchesExplicitTemplateSpecializations) {
2780 EXPECT_TRUE(matches(
2781 "template <typename T> class X {};"
2782 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002783 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002784 EXPECT_TRUE(matches(
2785 "template <typename T> void f(T t) {}"
2786 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002787 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002788}
2789
Manuel Klimek579b1202012-09-07 09:26:10 +00002790TEST(HasAncenstor, MatchesDeclarationAncestors) {
2791 EXPECT_TRUE(matches(
2792 "class A { class B { class C {}; }; };",
2793 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2794}
2795
2796TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2797 EXPECT_TRUE(notMatches(
2798 "class A { class B { class C {}; }; };",
2799 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2800}
2801
2802TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2803 EXPECT_TRUE(matches(
2804 "class A { class B { void f() { C c; } class C {}; }; };",
2805 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2806 hasAncestor(recordDecl(hasName("A"))))))));
2807}
2808
2809TEST(HasAncenstor, MatchesStatementAncestors) {
2810 EXPECT_TRUE(matches(
2811 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002812 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002813}
2814
2815TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2816 EXPECT_TRUE(matches(
2817 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002818 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002819}
2820
2821TEST(HasAncestor, BindsRecursiveCombinations) {
2822 EXPECT_TRUE(matchAndVerifyResultTrue(
2823 "class C { class D { class E { class F { int y; }; }; }; };",
2824 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002825 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00002826}
2827
2828TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
2829 EXPECT_TRUE(matchAndVerifyResultTrue(
2830 "class C { class D { class E { class F { int y; }; }; }; };",
2831 fieldDecl(hasAncestor(
2832 decl(
2833 hasDescendant(recordDecl(isDefinition(),
2834 hasAncestor(recordDecl())))
2835 ).bind("d")
2836 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00002837 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00002838}
2839
2840TEST(HasAncestor, MatchesInTemplateInstantiations) {
2841 EXPECT_TRUE(matches(
2842 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
2843 "A<int>::B::C a;",
2844 fieldDecl(hasType(asString("int")),
2845 hasAncestor(recordDecl(hasName("A"))))));
2846}
2847
2848TEST(HasAncestor, MatchesInImplicitCode) {
2849 EXPECT_TRUE(matches(
2850 "struct X {}; struct A { A() {} X x; };",
2851 constructorDecl(
2852 hasAnyConstructorInitializer(withInitializer(expr(
2853 hasAncestor(recordDecl(hasName("A")))))))));
2854}
2855
Daniel Jaspera7564432012-09-13 13:11:25 +00002856TEST(NNS, MatchesNestedNameSpecifiers) {
2857 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
2858 nestedNameSpecifier()));
2859 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
2860 nestedNameSpecifier()));
2861 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
2862 nestedNameSpecifier()));
2863
2864 EXPECT_TRUE(matches(
2865 "struct A { static void f() {} }; void g() { A::f(); }",
2866 nestedNameSpecifier()));
2867 EXPECT_TRUE(notMatches(
2868 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
2869 nestedNameSpecifier()));
2870}
2871
Daniel Jasperb54b7642012-09-20 14:12:57 +00002872TEST(NullStatement, SimpleCases) {
2873 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
2874 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
2875}
2876
Daniel Jaspera7564432012-09-13 13:11:25 +00002877TEST(NNS, MatchesTypes) {
2878 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
2879 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
2880 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
2881 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
2882 Matcher));
2883 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
2884}
2885
2886TEST(NNS, MatchesNamespaceDecls) {
2887 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
2888 specifiesNamespace(hasName("ns")));
2889 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
2890 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
2891 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
2892}
2893
2894TEST(NNS, BindsNestedNameSpecifiers) {
2895 EXPECT_TRUE(matchAndVerifyResultTrue(
2896 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
2897 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
2898 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
2899}
2900
2901TEST(NNS, BindsNestedNameSpecifierLocs) {
2902 EXPECT_TRUE(matchAndVerifyResultTrue(
2903 "namespace ns { struct B {}; } ns::B b;",
2904 loc(nestedNameSpecifier()).bind("loc"),
2905 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
2906}
2907
2908TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
2909 EXPECT_TRUE(matches(
2910 "struct A { struct B { struct C {}; }; }; A::B::C c;",
2911 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
2912 EXPECT_TRUE(matches(
2913 "struct A { struct B { struct C {}; }; }; A::B::C c;",
2914 nestedNameSpecifierLoc(hasPrefix(loc(
2915 specifiesType(asString("struct A")))))));
2916}
2917
Manuel Klimek4da21662012-07-06 05:48:52 +00002918} // end namespace ast_matchers
2919} // end namespace clang