blob: 69e61f296ff7802dcabb6be4e0bb207b489469d4 [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 Jasper371f9392012-08-01 08:40:24 +00001239}
1240
1241TEST(Matcher, MatchesSpecificArgument) {
1242 EXPECT_TRUE(matches(
1243 "template<typename T, typename U> class A {};"
1244 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001245 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001246 1, refersToType(asString("int"))))));
1247 EXPECT_TRUE(notMatches(
1248 "template<typename T, typename U> class A {};"
1249 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001250 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001251 1, refersToType(asString("int"))))));
1252}
1253
Manuel Klimek4da21662012-07-06 05:48:52 +00001254TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001255 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001256
1257 EXPECT_TRUE(
1258 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1259 EXPECT_TRUE(
1260 matches("class X { public: X(); }; void x() { X x = X(); }",
1261 Constructor));
1262 EXPECT_TRUE(
1263 matches("class X { public: X(int); }; void x() { X x = 0; }",
1264 Constructor));
1265 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1266}
1267
1268TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001269 StatementMatcher Constructor = constructExpr(
1270 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001271
1272 EXPECT_TRUE(
1273 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1274 Constructor));
1275 EXPECT_TRUE(
1276 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1277 Constructor));
1278 EXPECT_TRUE(
1279 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1280 Constructor));
1281 EXPECT_TRUE(
1282 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1283 Constructor));
1284
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001285 StatementMatcher WrongIndex = constructExpr(
1286 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001287 EXPECT_TRUE(
1288 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1289 WrongIndex));
1290}
1291
1292TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001293 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001294
1295 EXPECT_TRUE(
1296 matches("class X { public: X(int); }; void x() { X x(0); }",
1297 Constructor1Arg));
1298 EXPECT_TRUE(
1299 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1300 Constructor1Arg));
1301 EXPECT_TRUE(
1302 matches("class X { public: X(int); }; void x() { X x = 0; }",
1303 Constructor1Arg));
1304 EXPECT_TRUE(
1305 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1306 Constructor1Arg));
1307}
1308
1309TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001310 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001311
1312 std::string ClassString = "class string { public: string(); ~string(); }; ";
1313
1314 EXPECT_TRUE(
1315 matches(ClassString +
1316 "string GetStringByValue();"
1317 "void FunctionTakesString(string s);"
1318 "void run() { FunctionTakesString(GetStringByValue()); }",
1319 TempExpression));
1320
1321 EXPECT_TRUE(
1322 notMatches(ClassString +
1323 "string* GetStringPointer(); "
1324 "void FunctionTakesStringPtr(string* s);"
1325 "void run() {"
1326 " string* s = GetStringPointer();"
1327 " FunctionTakesStringPtr(GetStringPointer());"
1328 " FunctionTakesStringPtr(s);"
1329 "}",
1330 TempExpression));
1331
1332 EXPECT_TRUE(
1333 notMatches("class no_dtor {};"
1334 "no_dtor GetObjByValue();"
1335 "void ConsumeObj(no_dtor param);"
1336 "void run() { ConsumeObj(GetObjByValue()); }",
1337 TempExpression));
1338}
1339
Sam Panzere16acd32012-08-24 22:04:44 +00001340TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1341 std::string ClassString =
1342 "class string { public: string(); int length(); }; ";
1343
1344 EXPECT_TRUE(
1345 matches(ClassString +
1346 "string GetStringByValue();"
1347 "void FunctionTakesString(string s);"
1348 "void run() { FunctionTakesString(GetStringByValue()); }",
1349 materializeTemporaryExpr()));
1350
1351 EXPECT_TRUE(
1352 notMatches(ClassString +
1353 "string* GetStringPointer(); "
1354 "void FunctionTakesStringPtr(string* s);"
1355 "void run() {"
1356 " string* s = GetStringPointer();"
1357 " FunctionTakesStringPtr(GetStringPointer());"
1358 " FunctionTakesStringPtr(s);"
1359 "}",
1360 materializeTemporaryExpr()));
1361
1362 EXPECT_TRUE(
1363 notMatches(ClassString +
1364 "string GetStringByValue();"
1365 "void run() { int k = GetStringByValue().length(); }",
1366 materializeTemporaryExpr()));
1367
1368 EXPECT_TRUE(
1369 notMatches(ClassString +
1370 "string GetStringByValue();"
1371 "void run() { GetStringByValue(); }",
1372 materializeTemporaryExpr()));
1373}
1374
Manuel Klimek4da21662012-07-06 05:48:52 +00001375TEST(ConstructorDeclaration, SimpleCase) {
1376 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001377 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001378 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001379 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001380}
1381
1382TEST(ConstructorDeclaration, IsImplicit) {
1383 // This one doesn't match because the constructor is not added by the
1384 // compiler (it is not needed).
1385 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001386 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001387 // The compiler added the implicit default constructor.
1388 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001389 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001390 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001391 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001392}
1393
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001394TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1395 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001396 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001397}
1398
1399TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001400 EXPECT_TRUE(notMatches("class Foo {};",
1401 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001402}
1403
Manuel Klimek4da21662012-07-06 05:48:52 +00001404TEST(HasAnyConstructorInitializer, SimpleCase) {
1405 EXPECT_TRUE(notMatches(
1406 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001407 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001408 EXPECT_TRUE(matches(
1409 "class Foo {"
1410 " Foo() : foo_() { }"
1411 " int foo_;"
1412 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001413 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001414}
1415
1416TEST(HasAnyConstructorInitializer, ForField) {
1417 static const char Code[] =
1418 "class Baz { };"
1419 "class Foo {"
1420 " Foo() : foo_() { }"
1421 " Baz foo_;"
1422 " Baz bar_;"
1423 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001424 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1425 forField(hasType(recordDecl(hasName("Baz"))))))));
1426 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001427 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001428 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1429 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001430}
1431
1432TEST(HasAnyConstructorInitializer, WithInitializer) {
1433 static const char Code[] =
1434 "class Foo {"
1435 " Foo() : foo_(0) { }"
1436 " int foo_;"
1437 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001438 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001439 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001440 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001441 withInitializer(integerLiteral(equals(1)))))));
1442}
1443
1444TEST(HasAnyConstructorInitializer, IsWritten) {
1445 static const char Code[] =
1446 "struct Bar { Bar(){} };"
1447 "class Foo {"
1448 " Foo() : foo_() { }"
1449 " Bar foo_;"
1450 " Bar bar_;"
1451 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001452 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001453 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001454 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001455 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001456 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001457 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1458}
1459
1460TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001461 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001462
1463 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1464 EXPECT_TRUE(
1465 matches("class X { public: X(); }; void x() { new X(); }", New));
1466 EXPECT_TRUE(
1467 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1468 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1469}
1470
1471TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001472 StatementMatcher New = constructExpr(
1473 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001474
1475 EXPECT_TRUE(
1476 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1477 New));
1478 EXPECT_TRUE(
1479 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1480 New));
1481 EXPECT_TRUE(
1482 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1483 New));
1484
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001485 StatementMatcher WrongIndex = constructExpr(
1486 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001487 EXPECT_TRUE(
1488 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1489 WrongIndex));
1490}
1491
1492TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001493 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001494
1495 EXPECT_TRUE(
1496 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1497 EXPECT_TRUE(
1498 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1499 New));
1500}
1501
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001502TEST(Matcher, DeleteExpression) {
1503 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001504 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001505}
1506
Manuel Klimek4da21662012-07-06 05:48:52 +00001507TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001508 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001509
1510 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1511 EXPECT_TRUE(
1512 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1513 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1514}
1515
1516TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001517 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001518 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1519 // wide string
1520 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1521 // with escaped characters
1522 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1523 // no matching -- though the data type is the same, there is no string literal
1524 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1525}
1526
1527TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001528 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001529 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1530 // wide character
1531 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1532 // wide character, Hex encoded, NOT MATCHED!
1533 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1534 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1535}
1536
1537TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001538 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001539 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1540 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1541 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1542 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1543
1544 // Non-matching cases (character literals, float and double)
1545 EXPECT_TRUE(notMatches("int i = L'a';",
1546 HasIntLiteral)); // this is actually a character
1547 // literal cast to int
1548 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1549 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1550 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1551}
1552
Daniel Jasperb54b7642012-09-20 14:12:57 +00001553TEST(Matcher, AsmStatement) {
1554 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1555}
1556
Manuel Klimek4da21662012-07-06 05:48:52 +00001557TEST(Matcher, Conditions) {
1558 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1559
1560 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1561 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1562 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1563 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1564 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1565}
1566
1567TEST(MatchBinaryOperator, HasOperatorName) {
1568 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1569
1570 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1571 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1572}
1573
1574TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1575 StatementMatcher OperatorTrueFalse =
1576 binaryOperator(hasLHS(boolLiteral(equals(true))),
1577 hasRHS(boolLiteral(equals(false))));
1578
1579 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1580 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1581 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1582}
1583
1584TEST(MatchBinaryOperator, HasEitherOperand) {
1585 StatementMatcher HasOperand =
1586 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1587
1588 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1589 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1590 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1591}
1592
1593TEST(Matcher, BinaryOperatorTypes) {
1594 // Integration test that verifies the AST provides all binary operators in
1595 // a way we expect.
1596 // FIXME: Operator ','
1597 EXPECT_TRUE(
1598 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1599 EXPECT_TRUE(
1600 matches("bool b; bool c = (b = true);",
1601 binaryOperator(hasOperatorName("="))));
1602 EXPECT_TRUE(
1603 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1604 EXPECT_TRUE(
1605 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1606 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1607 EXPECT_TRUE(
1608 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1609 EXPECT_TRUE(
1610 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1611 EXPECT_TRUE(
1612 matches("int i = 1; int j = (i <<= 2);",
1613 binaryOperator(hasOperatorName("<<="))));
1614 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1615 EXPECT_TRUE(
1616 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1617 EXPECT_TRUE(
1618 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1619 EXPECT_TRUE(
1620 matches("int i = 1; int j = (i >>= 2);",
1621 binaryOperator(hasOperatorName(">>="))));
1622 EXPECT_TRUE(
1623 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1624 EXPECT_TRUE(
1625 matches("int i = 42; int j = (i ^= 42);",
1626 binaryOperator(hasOperatorName("^="))));
1627 EXPECT_TRUE(
1628 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1629 EXPECT_TRUE(
1630 matches("int i = 42; int j = (i %= 42);",
1631 binaryOperator(hasOperatorName("%="))));
1632 EXPECT_TRUE(
1633 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1634 EXPECT_TRUE(
1635 matches("bool b = true && false;",
1636 binaryOperator(hasOperatorName("&&"))));
1637 EXPECT_TRUE(
1638 matches("bool b = true; bool c = (b &= false);",
1639 binaryOperator(hasOperatorName("&="))));
1640 EXPECT_TRUE(
1641 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1642 EXPECT_TRUE(
1643 matches("bool b = true || false;",
1644 binaryOperator(hasOperatorName("||"))));
1645 EXPECT_TRUE(
1646 matches("bool b = true; bool c = (b |= false);",
1647 binaryOperator(hasOperatorName("|="))));
1648 EXPECT_TRUE(
1649 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1650 EXPECT_TRUE(
1651 matches("int i = 42; int j = (i *= 23);",
1652 binaryOperator(hasOperatorName("*="))));
1653 EXPECT_TRUE(
1654 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1655 EXPECT_TRUE(
1656 matches("int i = 42; int j = (i /= 23);",
1657 binaryOperator(hasOperatorName("/="))));
1658 EXPECT_TRUE(
1659 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1660 EXPECT_TRUE(
1661 matches("int i = 42; int j = (i += 23);",
1662 binaryOperator(hasOperatorName("+="))));
1663 EXPECT_TRUE(
1664 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1665 EXPECT_TRUE(
1666 matches("int i = 42; int j = (i -= 23);",
1667 binaryOperator(hasOperatorName("-="))));
1668 EXPECT_TRUE(
1669 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1670 binaryOperator(hasOperatorName("->*"))));
1671 EXPECT_TRUE(
1672 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1673 binaryOperator(hasOperatorName(".*"))));
1674
1675 // Member expressions as operators are not supported in matches.
1676 EXPECT_TRUE(
1677 notMatches("struct A { void x(A *a) { a->x(this); } };",
1678 binaryOperator(hasOperatorName("->"))));
1679
1680 // Initializer assignments are not represented as operator equals.
1681 EXPECT_TRUE(
1682 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1683
1684 // Array indexing is not represented as operator.
1685 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1686
1687 // Overloaded operators do not match at all.
1688 EXPECT_TRUE(notMatches(
1689 "struct A { bool operator&&(const A &a) const { return false; } };"
1690 "void x() { A a, b; a && b; }",
1691 binaryOperator()));
1692}
1693
1694TEST(MatchUnaryOperator, HasOperatorName) {
1695 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1696
1697 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1698 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1699}
1700
1701TEST(MatchUnaryOperator, HasUnaryOperand) {
1702 StatementMatcher OperatorOnFalse =
1703 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1704
1705 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1706 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1707}
1708
1709TEST(Matcher, UnaryOperatorTypes) {
1710 // Integration test that verifies the AST provides all unary operators in
1711 // a way we expect.
1712 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1713 EXPECT_TRUE(
1714 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1715 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1716 EXPECT_TRUE(
1717 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1718 EXPECT_TRUE(
1719 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1720 EXPECT_TRUE(
1721 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1722 EXPECT_TRUE(
1723 matches("int i; int j = ++i;", 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
1731 // We don't match conversion operators.
1732 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1733
1734 // Function calls are not represented as operator.
1735 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1736
1737 // Overloaded operators do not match at all.
1738 // FIXME: We probably want to add that.
1739 EXPECT_TRUE(notMatches(
1740 "struct A { bool operator!() const { return false; } };"
1741 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1742}
1743
1744TEST(Matcher, ConditionalOperator) {
1745 StatementMatcher Conditional = conditionalOperator(
1746 hasCondition(boolLiteral(equals(true))),
1747 hasTrueExpression(boolLiteral(equals(false))));
1748
1749 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1750 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1751 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1752
1753 StatementMatcher ConditionalFalse = conditionalOperator(
1754 hasFalseExpression(boolLiteral(equals(false))));
1755
1756 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1757 EXPECT_TRUE(
1758 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1759}
1760
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001761TEST(ArraySubscriptMatchers, ArraySubscripts) {
1762 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1763 arraySubscriptExpr()));
1764 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1765 arraySubscriptExpr()));
1766}
1767
1768TEST(ArraySubscriptMatchers, ArrayIndex) {
1769 EXPECT_TRUE(matches(
1770 "int i[2]; void f() { i[1] = 1; }",
1771 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1772 EXPECT_TRUE(matches(
1773 "int i[2]; void f() { 1[i] = 1; }",
1774 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1775 EXPECT_TRUE(notMatches(
1776 "int i[2]; void f() { i[1] = 1; }",
1777 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1778}
1779
1780TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1781 EXPECT_TRUE(matches(
1782 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001783 arraySubscriptExpr(hasBase(implicitCastExpr(
1784 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001785}
1786
Manuel Klimek4da21662012-07-06 05:48:52 +00001787TEST(Matcher, HasNameSupportsNamespaces) {
1788 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001789 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001790 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001791 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001792 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001793 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001794 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001795 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001796 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001797 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001798 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001799 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001800 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001801 recordDecl(hasName("a::b::A"))));
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"))));
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("::b::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("z::a::b::C"))));
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("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001810 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001811 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001812}
1813
1814TEST(Matcher, HasNameSupportsOuterClasses) {
1815 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001816 matches("class A { class B { class C; }; };",
1817 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001818 EXPECT_TRUE(
1819 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001820 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001821 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001822 matches("class A { class B { class C; }; };",
1823 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001824 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001825 matches("class A { class B { class C; }; };",
1826 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001827 EXPECT_TRUE(
1828 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001829 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001830 EXPECT_TRUE(
1831 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001832 recordDecl(hasName("A::c::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("A::B::A"))));
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("::C"))));
1839 EXPECT_TRUE(
1840 notMatches("class A { class B { class C; }; };",
1841 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001842 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001843 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001844 EXPECT_TRUE(
1845 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001846 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001847}
1848
1849TEST(Matcher, IsDefinition) {
1850 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001851 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001852 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1853 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1854
1855 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001856 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001857 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1858 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1859
1860 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001861 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001862 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1863 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1864}
1865
1866TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001867 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00001868 ofClass(hasName("X")))));
1869
1870 EXPECT_TRUE(
1871 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
1872 EXPECT_TRUE(
1873 matches("class X { public: X(); }; void x(int) { X x = X(); }",
1874 Constructor));
1875 EXPECT_TRUE(
1876 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
1877 Constructor));
1878}
1879
1880TEST(Matcher, VisitsTemplateInstantiations) {
1881 EXPECT_TRUE(matches(
1882 "class A { public: void x(); };"
1883 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001884 "void f() { B<A> b; b.y(); }",
1885 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001886
1887 EXPECT_TRUE(matches(
1888 "class A { public: void x(); };"
1889 "class C {"
1890 " public:"
1891 " template <typename T> class B { public: void y() { T t; t.x(); } };"
1892 "};"
1893 "void f() {"
1894 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001895 "}",
1896 recordDecl(hasName("C"),
1897 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001898}
1899
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001900TEST(Matcher, HandlesNullQualTypes) {
1901 // FIXME: Add a Type matcher so we can replace uses of this
1902 // variable with Type(True())
1903 const TypeMatcher AnyType = anything();
1904
1905 // We don't really care whether this matcher succeeds; we're testing that
1906 // it completes without crashing.
1907 EXPECT_TRUE(matches(
1908 "struct A { };"
1909 "template <typename T>"
1910 "void f(T t) {"
1911 " T local_t(t /* this becomes a null QualType in the AST */);"
1912 "}"
1913 "void g() {"
1914 " f(0);"
1915 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001916 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001917 anyOf(
1918 TypeMatcher(hasDeclaration(anything())),
1919 pointsTo(AnyType),
1920 references(AnyType)
1921 // Other QualType matchers should go here.
1922 ))))));
1923}
1924
Manuel Klimek4da21662012-07-06 05:48:52 +00001925// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001926AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00001927 // Make sure all special variables are used: node, match_finder,
1928 // bound_nodes_builder, and the parameter named 'AMatcher'.
1929 return AMatcher.matches(Node, Finder, Builder);
1930}
1931
1932TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001933 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001934
1935 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001936 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001937
1938 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001939 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001940
1941 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001942 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001943}
1944
1945AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001946 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
1947 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
1948 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00001949 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00001950 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00001951 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00001952 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
1953 ASTMatchFinder::BK_First);
1954}
1955
1956TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001957 DeclarationMatcher HasClassB =
1958 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001959
1960 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001961 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001962
1963 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001964 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001965
1966 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001967 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001968
1969 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001970 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001971
1972 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
1973}
1974
1975TEST(For, FindsForLoops) {
1976 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
1977 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
1978}
1979
Daniel Jasper6a124492012-07-12 08:50:38 +00001980TEST(For, ForLoopInternals) {
1981 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
1982 forStmt(hasCondition(anything()))));
1983 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
1984 forStmt(hasLoopInit(anything()))));
1985}
1986
1987TEST(For, NegativeForLoopInternals) {
1988 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001989 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001990 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
1991 forStmt(hasLoopInit(anything()))));
1992}
1993
Manuel Klimek4da21662012-07-06 05:48:52 +00001994TEST(For, ReportsNoFalsePositives) {
1995 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
1996 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
1997}
1998
1999TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002000 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2001 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2002 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002003}
2004
2005TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2006 // It's not a compound statement just because there's "{}" in the source
2007 // text. This is an AST search, not grep.
2008 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002009 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002010 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002011 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002012}
2013
Daniel Jasper6a124492012-07-12 08:50:38 +00002014TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002015 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002016 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002017 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002018 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002019 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002020 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002021 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002022 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002023}
2024
2025TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2026 // The simplest case: every compound statement is in a function
2027 // definition, and the function body itself must be a compound
2028 // statement.
2029 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002030 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002031}
2032
2033TEST(HasAnySubstatement, IsNotRecursive) {
2034 // It's really "has any immediate substatement".
2035 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002036 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002037}
2038
2039TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2040 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002041 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002042}
2043
2044TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2045 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002046 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002047}
2048
2049TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2050 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002051 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002052 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002053 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002054}
2055
2056TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2057 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002058 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002059 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002060 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002061 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002062 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002063}
2064
2065TEST(StatementCountIs, WorksWithMultipleStatements) {
2066 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002067 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002068}
2069
2070TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2071 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002072 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002073 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002074 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002075 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002076 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002077 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002078 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002079}
2080
2081TEST(Member, WorksInSimplestCase) {
2082 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002083 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002084}
2085
2086TEST(Member, DoesNotMatchTheBaseExpression) {
2087 // Don't pick out the wrong part of the member expression, this should
2088 // be checking the member (name) only.
2089 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002090 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002091}
2092
2093TEST(Member, MatchesInMemberFunctionCall) {
2094 EXPECT_TRUE(matches("void f() {"
2095 " struct { void first() {}; } s;"
2096 " s.first();"
2097 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002098 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002099}
2100
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002101TEST(Member, MatchesMemberAllocationFunction) {
Dmitri Gribenko02ed37f2012-08-18 00:41:04 +00002102 EXPECT_TRUE(matches("namespace std { typedef typeof(sizeof(int)) size_t; }"
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002103 "class X { void *operator new(std::size_t); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002104 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002105
2106 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002107 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002108
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002109 EXPECT_TRUE(matches(
2110 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2111 "class X { void operator delete[](void*, std::size_t); };",
2112 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002113}
2114
Manuel Klimek4da21662012-07-06 05:48:52 +00002115TEST(HasObjectExpression, DoesNotMatchMember) {
2116 EXPECT_TRUE(notMatches(
2117 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002118 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002119}
2120
2121TEST(HasObjectExpression, MatchesBaseOfVariable) {
2122 EXPECT_TRUE(matches(
2123 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002124 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002125 EXPECT_TRUE(matches(
2126 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002127 memberExpr(hasObjectExpression(
2128 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002129}
2130
2131TEST(HasObjectExpression,
2132 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2133 EXPECT_TRUE(matches(
2134 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002135 memberExpr(hasObjectExpression(
2136 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002137 EXPECT_TRUE(matches(
2138 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002139 memberExpr(hasObjectExpression(
2140 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002141}
2142
2143TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002144 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2145 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2146 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2147 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002148}
2149
2150TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002151 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002152}
2153
2154TEST(IsConstQualified, MatchesConstInt) {
2155 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002156 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002157}
2158
2159TEST(IsConstQualified, MatchesConstPointer) {
2160 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002161 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002162}
2163
2164TEST(IsConstQualified, MatchesThroughTypedef) {
2165 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002166 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002167 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002168 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002169}
2170
2171TEST(IsConstQualified, DoesNotMatchInappropriately) {
2172 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002173 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002174 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002175 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002176}
2177
Sam Panzer089e5b32012-08-16 16:58:10 +00002178TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002179 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2180 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2181 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2182 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002183}
2184TEST(CastExpression, MatchesImplicitCasts) {
2185 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002186 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002187 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002188 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002189}
2190
2191TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002192 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2193 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2194 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2195 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002196}
2197
Manuel Klimek4da21662012-07-06 05:48:52 +00002198TEST(ReinterpretCast, MatchesSimpleCase) {
2199 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002200 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002201}
2202
2203TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002204 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002205 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002206 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002207 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002208 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002209 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2210 "B b;"
2211 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002212 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002213}
2214
2215TEST(FunctionalCast, MatchesSimpleCase) {
2216 std::string foo_class = "class Foo { public: Foo(char*); };";
2217 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002218 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002219}
2220
2221TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2222 std::string FooClass = "class Foo { public: Foo(char*); };";
2223 EXPECT_TRUE(
2224 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002225 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002226 EXPECT_TRUE(
2227 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002228 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002229}
2230
2231TEST(DynamicCast, MatchesSimpleCase) {
2232 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2233 "B b;"
2234 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002235 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002236}
2237
2238TEST(StaticCast, MatchesSimpleCase) {
2239 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002240 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002241}
2242
2243TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002244 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002245 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002246 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002247 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002248 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002249 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2250 "B b;"
2251 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002252 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002253}
2254
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002255TEST(CStyleCast, MatchesSimpleCase) {
2256 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2257}
2258
2259TEST(CStyleCast, DoesNotMatchOtherCasts) {
2260 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2261 "char q, *r = const_cast<char*>(&q);"
2262 "void* s = reinterpret_cast<char*>(&s);"
2263 "struct B { virtual ~B() {} }; struct D : B {};"
2264 "B b;"
2265 "D* t = dynamic_cast<D*>(&b);",
2266 cStyleCastExpr()));
2267}
2268
Manuel Klimek4da21662012-07-06 05:48:52 +00002269TEST(HasDestinationType, MatchesSimpleCase) {
2270 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002271 staticCastExpr(hasDestinationType(
2272 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002273}
2274
Sam Panzer089e5b32012-08-16 16:58:10 +00002275TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2276 // This test creates an implicit const cast.
2277 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002278 implicitCastExpr(
2279 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002280 // This test creates an implicit array-to-pointer cast.
2281 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002282 implicitCastExpr(hasImplicitDestinationType(
2283 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002284}
2285
2286TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2287 // This test creates an implicit cast from int to char.
2288 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002289 implicitCastExpr(hasImplicitDestinationType(
2290 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002291 // This test creates an implicit array-to-pointer cast.
2292 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002293 implicitCastExpr(hasImplicitDestinationType(
2294 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002295}
2296
2297TEST(ImplicitCast, MatchesSimpleCase) {
2298 // This test creates an implicit const cast.
2299 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002300 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002301 // This test creates an implicit cast from int to char.
2302 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002303 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002304 // This test creates an implicit array-to-pointer cast.
2305 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002306 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002307}
2308
2309TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002310 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002311 // are present, and that it ignores explicit and paren casts.
2312
2313 // These two test cases have no casts.
2314 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002315 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002316 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002317 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002318
2319 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002320 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002321 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002322 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002323
2324 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002325 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002326}
2327
2328TEST(IgnoringImpCasts, MatchesImpCasts) {
2329 // This test checks that ignoringImpCasts matches when implicit casts are
2330 // present and its inner matcher alone does not match.
2331 // Note that this test creates an implicit const cast.
2332 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002333 varDecl(hasInitializer(ignoringImpCasts(
2334 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002335 // This test creates an implict cast from int to char.
2336 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002337 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002338 integerLiteral(equals(0)))))));
2339}
2340
2341TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2342 // These tests verify that ignoringImpCasts does not match if the inner
2343 // matcher does not match.
2344 // Note that the first test creates an implicit const cast.
2345 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002346 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002347 unless(anything()))))));
2348 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002349 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002350 unless(anything()))))));
2351
2352 // These tests verify that ignoringImplictCasts does not look through explicit
2353 // casts or parentheses.
2354 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002355 varDecl(hasInitializer(ignoringImpCasts(
2356 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002357 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002358 varDecl(hasInitializer(ignoringImpCasts(
2359 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002360 EXPECT_TRUE(notMatches("float i = (float)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("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002364 varDecl(hasInitializer(ignoringImpCasts(
2365 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002366}
2367
2368TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2369 // This test verifies that expressions that do not have implicit casts
2370 // still match the inner matcher.
2371 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002372 varDecl(hasInitializer(ignoringImpCasts(
2373 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002374}
2375
2376TEST(IgnoringParenCasts, MatchesParenCasts) {
2377 // This test checks that ignoringParenCasts matches when parentheses and/or
2378 // casts are present and its inner matcher alone does not match.
2379 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002380 varDecl(hasInitializer(ignoringParenCasts(
2381 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002382 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002383 varDecl(hasInitializer(ignoringParenCasts(
2384 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002385
2386 // This test creates an implict cast from int to char in addition to the
2387 // parentheses.
2388 EXPECT_TRUE(matches("char 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 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002393 varDecl(hasInitializer(ignoringParenCasts(
2394 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002395 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002396 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002397 integerLiteral(equals(0)))))));
2398}
2399
2400TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2401 // This test verifies that expressions that do not have any casts still match.
2402 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002403 varDecl(hasInitializer(ignoringParenCasts(
2404 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002405}
2406
2407TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2408 // These tests verify that ignoringImpCasts does not match if the inner
2409 // matcher does not match.
2410 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002411 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002412 unless(anything()))))));
2413
2414 // This test creates an implicit cast from int to char in addition to the
2415 // parentheses.
2416 EXPECT_TRUE(notMatches("char 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 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002421 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002422 unless(anything()))))));
2423}
2424
2425TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2426 // This test checks that ignoringParenAndImpCasts matches when
2427 // parentheses and/or implicit casts are present and its inner matcher alone
2428 // does not match.
2429 // Note that this test creates an implicit const cast.
2430 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002431 varDecl(hasInitializer(ignoringParenImpCasts(
2432 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002433 // This test creates an implicit cast from int to char.
2434 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002435 varDecl(hasInitializer(ignoringParenImpCasts(
2436 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002437}
2438
2439TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2440 // This test verifies that expressions that do not have parentheses or
2441 // implicit casts still match.
2442 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002443 varDecl(hasInitializer(ignoringParenImpCasts(
2444 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002445 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002446 varDecl(hasInitializer(ignoringParenImpCasts(
2447 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002448}
2449
2450TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2451 // These tests verify that ignoringParenImpCasts does not match if
2452 // the inner matcher does not match.
2453 // This test creates an implicit cast.
2454 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002455 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002456 unless(anything()))))));
2457 // These tests verify that ignoringParenAndImplictCasts does not look
2458 // through explicit casts.
2459 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002460 varDecl(hasInitializer(ignoringParenImpCasts(
2461 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002462 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002463 varDecl(hasInitializer(ignoringParenImpCasts(
2464 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002465 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002466 varDecl(hasInitializer(ignoringParenImpCasts(
2467 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002468}
2469
Manuel Klimek715c9562012-07-25 10:02:02 +00002470TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002471 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2472 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002473 implicitCastExpr(
2474 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002475}
2476
Manuel Klimek715c9562012-07-25 10:02:02 +00002477TEST(HasSourceExpression, MatchesExplicitCasts) {
2478 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002479 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002480 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002481 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002482}
2483
Manuel Klimek4da21662012-07-06 05:48:52 +00002484TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002485 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002486}
2487
2488TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002489 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002490}
2491
2492TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002493 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002494}
2495
2496TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002497 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002498}
2499
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002500TEST(InitListExpression, MatchesInitListExpression) {
2501 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2502 initListExpr(hasType(asString("int [2]")))));
2503 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002504 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002505}
2506
2507TEST(UsingDeclaration, MatchesUsingDeclarations) {
2508 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2509 usingDecl()));
2510}
2511
2512TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2513 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2514 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2515}
2516
2517TEST(UsingDeclaration, MatchesSpecificTarget) {
2518 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2519 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002520 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002521 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2522 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002523 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002524}
2525
2526TEST(UsingDeclaration, ThroughUsingDeclaration) {
2527 EXPECT_TRUE(matches(
2528 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002529 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002530 EXPECT_TRUE(notMatches(
2531 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002532 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002533}
2534
Sam Panzer425f41b2012-08-16 17:20:59 +00002535TEST(SingleDecl, IsSingleDecl) {
2536 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002537 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002538 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2539 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2540 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2541 SingleDeclStmt));
2542}
2543
2544TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002545 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002546
2547 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002548 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002549 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002550 declStmt(containsDeclaration(0, MatchesInit),
2551 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002552 unsigned WrongIndex = 42;
2553 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002554 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002555 MatchesInit))));
2556}
2557
2558TEST(DeclCount, DeclCountIsCorrect) {
2559 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002560 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002561 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002562 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002563 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002564 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002565}
2566
Manuel Klimek4da21662012-07-06 05:48:52 +00002567TEST(While, MatchesWhileLoops) {
2568 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2569 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2570 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2571}
2572
2573TEST(Do, MatchesDoLoops) {
2574 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2575 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2576}
2577
2578TEST(Do, DoesNotMatchWhileLoops) {
2579 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2580}
2581
2582TEST(SwitchCase, MatchesCase) {
2583 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2584 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2585 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2586 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2587}
2588
Daniel Jasperb54b7642012-09-20 14:12:57 +00002589TEST(SwitchCase, MatchesSwitch) {
2590 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2591 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2592 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2593 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2594}
2595
2596TEST(ExceptionHandling, SimpleCases) {
2597 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2598 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2599 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2600 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2601 throwExpr()));
2602 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2603 throwExpr()));
2604}
2605
Manuel Klimek4da21662012-07-06 05:48:52 +00002606TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2607 EXPECT_TRUE(notMatches(
2608 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002609 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002610 EXPECT_TRUE(notMatches(
2611 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002612 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002613}
2614
2615TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2616 EXPECT_TRUE(matches(
2617 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002618 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002619}
2620
2621TEST(ForEach, BindsOneNode) {
2622 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002623 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002624 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002625}
2626
2627TEST(ForEach, BindsMultipleNodes) {
2628 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002629 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002630 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002631}
2632
2633TEST(ForEach, BindsRecursiveCombinations) {
2634 EXPECT_TRUE(matchAndVerifyResultTrue(
2635 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002636 recordDecl(hasName("C"),
2637 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002638 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002639}
2640
2641TEST(ForEachDescendant, BindsOneNode) {
2642 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002643 recordDecl(hasName("C"),
2644 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002645 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002646}
2647
2648TEST(ForEachDescendant, BindsMultipleNodes) {
2649 EXPECT_TRUE(matchAndVerifyResultTrue(
2650 "class C { class D { int x; int y; }; "
2651 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002652 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002653 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002654}
2655
2656TEST(ForEachDescendant, BindsRecursiveCombinations) {
2657 EXPECT_TRUE(matchAndVerifyResultTrue(
2658 "class C { class D { "
2659 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002660 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2661 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002662 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002663}
2664
2665
2666TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2667 // Make sure that we can both match the class by name (::X) and by the type
2668 // the template was instantiated with (via a field).
2669
2670 EXPECT_TRUE(matches(
2671 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002672 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002673
2674 EXPECT_TRUE(matches(
2675 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002676 recordDecl(isTemplateInstantiation(), hasDescendant(
2677 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002678}
2679
2680TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2681 EXPECT_TRUE(matches(
2682 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002683 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002684 isTemplateInstantiation())));
2685}
2686
2687TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2688 EXPECT_TRUE(matches(
2689 "template <typename T> class X { T t; }; class A {};"
2690 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002691 recordDecl(isTemplateInstantiation(), hasDescendant(
2692 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002693}
2694
2695TEST(IsTemplateInstantiation,
2696 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2697 EXPECT_TRUE(matches(
2698 "template <typename T> class X {};"
2699 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002700 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002701}
2702
2703TEST(IsTemplateInstantiation,
2704 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2705 EXPECT_TRUE(matches(
2706 "class A {};"
2707 "class X {"
2708 " template <typename U> class Y { U u; };"
2709 " Y<A> y;"
2710 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002711 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002712}
2713
2714TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2715 // FIXME: Figure out whether this makes sense. It doesn't affect the
2716 // normal use case as long as the uppermost instantiation always is marked
2717 // as template instantiation, but it might be confusing as a predicate.
2718 EXPECT_TRUE(matches(
2719 "class A {};"
2720 "template <typename T> class X {"
2721 " template <typename U> class Y { U u; };"
2722 " Y<T> y;"
2723 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002724 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002725}
2726
2727TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2728 EXPECT_TRUE(notMatches(
2729 "template <typename T> class X {}; class A {};"
2730 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002731 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002732}
2733
2734TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2735 EXPECT_TRUE(notMatches(
2736 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002737 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002738}
2739
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002740TEST(IsExplicitTemplateSpecialization,
2741 DoesNotMatchPrimaryTemplate) {
2742 EXPECT_TRUE(notMatches(
2743 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002744 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002745 EXPECT_TRUE(notMatches(
2746 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002747 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002748}
2749
2750TEST(IsExplicitTemplateSpecialization,
2751 DoesNotMatchExplicitTemplateInstantiations) {
2752 EXPECT_TRUE(notMatches(
2753 "template <typename T> class X {};"
2754 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002755 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002756 EXPECT_TRUE(notMatches(
2757 "template <typename T> void f(T t) {}"
2758 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002759 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002760}
2761
2762TEST(IsExplicitTemplateSpecialization,
2763 DoesNotMatchImplicitTemplateInstantiations) {
2764 EXPECT_TRUE(notMatches(
2765 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002766 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002767 EXPECT_TRUE(notMatches(
2768 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002769 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002770}
2771
2772TEST(IsExplicitTemplateSpecialization,
2773 MatchesExplicitTemplateSpecializations) {
2774 EXPECT_TRUE(matches(
2775 "template <typename T> class X {};"
2776 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002777 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002778 EXPECT_TRUE(matches(
2779 "template <typename T> void f(T t) {}"
2780 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002781 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002782}
2783
Manuel Klimek579b1202012-09-07 09:26:10 +00002784TEST(HasAncenstor, MatchesDeclarationAncestors) {
2785 EXPECT_TRUE(matches(
2786 "class A { class B { class C {}; }; };",
2787 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2788}
2789
2790TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2791 EXPECT_TRUE(notMatches(
2792 "class A { class B { class C {}; }; };",
2793 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2794}
2795
2796TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2797 EXPECT_TRUE(matches(
2798 "class A { class B { void f() { C c; } class C {}; }; };",
2799 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2800 hasAncestor(recordDecl(hasName("A"))))))));
2801}
2802
2803TEST(HasAncenstor, MatchesStatementAncestors) {
2804 EXPECT_TRUE(matches(
2805 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002806 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002807}
2808
2809TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2810 EXPECT_TRUE(matches(
2811 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002812 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002813}
2814
2815TEST(HasAncestor, BindsRecursiveCombinations) {
2816 EXPECT_TRUE(matchAndVerifyResultTrue(
2817 "class C { class D { class E { class F { int y; }; }; }; };",
2818 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002819 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00002820}
2821
2822TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
2823 EXPECT_TRUE(matchAndVerifyResultTrue(
2824 "class C { class D { class E { class F { int y; }; }; }; };",
2825 fieldDecl(hasAncestor(
2826 decl(
2827 hasDescendant(recordDecl(isDefinition(),
2828 hasAncestor(recordDecl())))
2829 ).bind("d")
2830 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00002831 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00002832}
2833
2834TEST(HasAncestor, MatchesInTemplateInstantiations) {
2835 EXPECT_TRUE(matches(
2836 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
2837 "A<int>::B::C a;",
2838 fieldDecl(hasType(asString("int")),
2839 hasAncestor(recordDecl(hasName("A"))))));
2840}
2841
2842TEST(HasAncestor, MatchesInImplicitCode) {
2843 EXPECT_TRUE(matches(
2844 "struct X {}; struct A { A() {} X x; };",
2845 constructorDecl(
2846 hasAnyConstructorInitializer(withInitializer(expr(
2847 hasAncestor(recordDecl(hasName("A")))))))));
2848}
2849
Daniel Jaspera7564432012-09-13 13:11:25 +00002850TEST(NNS, MatchesNestedNameSpecifiers) {
2851 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
2852 nestedNameSpecifier()));
2853 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
2854 nestedNameSpecifier()));
2855 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
2856 nestedNameSpecifier()));
2857
2858 EXPECT_TRUE(matches(
2859 "struct A { static void f() {} }; void g() { A::f(); }",
2860 nestedNameSpecifier()));
2861 EXPECT_TRUE(notMatches(
2862 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
2863 nestedNameSpecifier()));
2864}
2865
Daniel Jasperb54b7642012-09-20 14:12:57 +00002866TEST(NullStatement, SimpleCases) {
2867 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
2868 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
2869}
2870
Daniel Jaspera7564432012-09-13 13:11:25 +00002871TEST(NNS, MatchesTypes) {
2872 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
2873 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
2874 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
2875 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
2876 Matcher));
2877 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
2878}
2879
2880TEST(NNS, MatchesNamespaceDecls) {
2881 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
2882 specifiesNamespace(hasName("ns")));
2883 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
2884 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
2885 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
2886}
2887
2888TEST(NNS, BindsNestedNameSpecifiers) {
2889 EXPECT_TRUE(matchAndVerifyResultTrue(
2890 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
2891 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
2892 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
2893}
2894
2895TEST(NNS, BindsNestedNameSpecifierLocs) {
2896 EXPECT_TRUE(matchAndVerifyResultTrue(
2897 "namespace ns { struct B {}; } ns::B b;",
2898 loc(nestedNameSpecifier()).bind("loc"),
2899 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
2900}
2901
2902TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
2903 EXPECT_TRUE(matches(
2904 "struct A { struct B { struct C {}; }; }; A::B::C c;",
2905 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
2906 EXPECT_TRUE(matches(
2907 "struct A { struct B { struct C {}; }; }; A::B::C c;",
2908 nestedNameSpecifierLoc(hasPrefix(loc(
2909 specifiesType(asString("struct A")))))));
2910}
2911
Manuel Klimek4da21662012-07-06 05:48:52 +00002912} // end namespace ast_matchers
2913} // end namespace clang