blob: e94191400119f3dd84a55bae5a7109bfce133eb7 [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"
Benjamin Kramer8b9ed712012-12-01 17:22:05 +000011#include "clang/AST/PrettyPrinter.h"
Manuel Klimek4da21662012-07-06 05:48:52 +000012#include "clang/ASTMatchers/ASTMatchers.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Tooling/Tooling.h"
15#include "gtest/gtest.h"
16
17namespace clang {
18namespace ast_matchers {
19
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000020#if GTEST_HAS_DEATH_TEST
Manuel Klimek4da21662012-07-06 05:48:52 +000021TEST(HasNameDeathTest, DiesOnEmptyName) {
22 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000023 DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000024 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
25 }, "");
26}
27
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000028TEST(HasNameDeathTest, DiesOnEmptyPattern) {
29 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000030 DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000031 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
32 }, "");
33}
34
Manuel Klimek4da21662012-07-06 05:48:52 +000035TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
36 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000037 DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000038 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
39 }, "");
40}
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000041#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000042
Manuel Klimek715c9562012-07-25 10:02:02 +000043TEST(Decl, MatchesDeclarations) {
44 EXPECT_TRUE(notMatches("", decl(usingDecl())));
45 EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
46 decl(usingDecl())));
47}
48
Manuel Klimek4da21662012-07-06 05:48:52 +000049TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000050 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +000051 EXPECT_TRUE(matches("typedef int X;", NamedX));
52 EXPECT_TRUE(matches("int X;", NamedX));
53 EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
54 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
55 EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
56 EXPECT_TRUE(matches("namespace X { }", NamedX));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000057 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek4da21662012-07-06 05:48:52 +000058
59 EXPECT_TRUE(notMatches("#define X 1", NamedX));
60}
61
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000062TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000063 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000064 EXPECT_TRUE(matches("typedef int Xa;", NamedX));
65 EXPECT_TRUE(matches("int Xb;", NamedX));
66 EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
67 EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
68 EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
69 EXPECT_TRUE(matches("namespace Xij { }", NamedX));
70 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
71
72 EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
73
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000074 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000075 EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
76 EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
77
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000078 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000079 EXPECT_TRUE(matches("int abc;", Abc));
80 EXPECT_TRUE(matches("int aFOObBARc;", Abc));
81 EXPECT_TRUE(notMatches("int cab;", Abc));
82 EXPECT_TRUE(matches("int cabc;", Abc));
83}
84
Manuel Klimek4da21662012-07-06 05:48:52 +000085TEST(DeclarationMatcher, MatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000086 DeclarationMatcher ClassMatcher(recordDecl());
Manuel Klimeke265c872012-07-10 14:21:30 +000087#if !defined(_MSC_VER)
Manuel Klimek4da21662012-07-06 05:48:52 +000088 EXPECT_FALSE(matches("", ClassMatcher));
Manuel Klimeke265c872012-07-10 14:21:30 +000089#else
90 // Matches class type_info.
91 EXPECT_TRUE(matches("", ClassMatcher));
92#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000093
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000094 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +000095 EXPECT_TRUE(matches("class X;", ClassX));
96 EXPECT_TRUE(matches("class X {};", ClassX));
97 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
98 EXPECT_TRUE(notMatches("", ClassX));
99}
100
101TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000102 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000103
104 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000105 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
106 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek4da21662012-07-06 05:48:52 +0000107 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
108 EXPECT_TRUE(notMatches("", IsDerivedFromX));
109
Daniel Jasper63d88722012-09-12 21:14:15 +0000110 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000111
112 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
113 EXPECT_TRUE(matches("class X {};", IsAX));
114 EXPECT_TRUE(matches("class X;", IsAX));
115 EXPECT_TRUE(notMatches("class Y;", IsAX));
116 EXPECT_TRUE(notMatches("", IsAX));
117
Manuel Klimek4da21662012-07-06 05:48:52 +0000118 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000119 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000120 EXPECT_TRUE(
121 matches("class X {}; class Y : public X {}; class Z : public Y {};",
122 ZIsDerivedFromX));
123 EXPECT_TRUE(
124 matches("class X {};"
125 "template<class T> class Y : public X {};"
126 "class Z : public Y<int> {};", ZIsDerivedFromX));
127 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
128 ZIsDerivedFromX));
129 EXPECT_TRUE(
130 matches("template<class T> class X {}; "
131 "template<class T> class Z : public X<T> {};",
132 ZIsDerivedFromX));
133 EXPECT_TRUE(
134 matches("template<class T, class U=T> class X {}; "
135 "template<class T> class Z : public X<T> {};",
136 ZIsDerivedFromX));
137 EXPECT_TRUE(
138 notMatches("template<class X> class A { class Z : public X {}; };",
139 ZIsDerivedFromX));
140 EXPECT_TRUE(
141 matches("template<class X> class A { public: class Z : public X {}; }; "
142 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
143 EXPECT_TRUE(
144 matches("template <class T> class X {}; "
145 "template<class Y> class A { class Z : public X<Y> {}; };",
146 ZIsDerivedFromX));
147 EXPECT_TRUE(
148 notMatches("template<template<class T> class X> class A { "
149 " class Z : public X<int> {}; };", ZIsDerivedFromX));
150 EXPECT_TRUE(
151 matches("template<template<class T> class X> class A { "
152 " public: class Z : public X<int> {}; }; "
153 "template<class T> class X {}; void y() { A<X>::Z z; }",
154 ZIsDerivedFromX));
155 EXPECT_TRUE(
156 notMatches("template<class X> class A { class Z : public X::D {}; };",
157 ZIsDerivedFromX));
158 EXPECT_TRUE(
159 matches("template<class X> class A { public: "
160 " class Z : public X::D {}; }; "
161 "class Y { public: class X {}; typedef X D; }; "
162 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
163 EXPECT_TRUE(
164 matches("class X {}; typedef X Y; class Z : public Y {};",
165 ZIsDerivedFromX));
166 EXPECT_TRUE(
167 matches("template<class T> class Y { typedef typename T::U X; "
168 " class Z : public X {}; };", ZIsDerivedFromX));
169 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
170 ZIsDerivedFromX));
171 EXPECT_TRUE(
172 notMatches("template<class T> class X {}; "
173 "template<class T> class A { class Z : public X<T>::D {}; };",
174 ZIsDerivedFromX));
175 EXPECT_TRUE(
176 matches("template<class T> class X { public: typedef X<T> D; }; "
177 "template<class T> class A { public: "
178 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
179 ZIsDerivedFromX));
180 EXPECT_TRUE(
181 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
182 ZIsDerivedFromX));
183 EXPECT_TRUE(
184 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
185 ZIsDerivedFromX));
186 EXPECT_TRUE(
187 matches("class X {}; class Y : public X {}; "
188 "typedef Y V; typedef V W; class Z : public W {};",
189 ZIsDerivedFromX));
190 EXPECT_TRUE(
191 matches("template<class T, class U> class X {}; "
192 "template<class T> class A { class Z : public X<T, int> {}; };",
193 ZIsDerivedFromX));
194 EXPECT_TRUE(
195 notMatches("template<class X> class D { typedef X A; typedef A B; "
196 " typedef B C; class Z : public C {}; };",
197 ZIsDerivedFromX));
198 EXPECT_TRUE(
199 matches("class X {}; typedef X A; typedef A B; "
200 "class Z : public B {};", ZIsDerivedFromX));
201 EXPECT_TRUE(
202 matches("class X {}; typedef X A; typedef A B; typedef B C; "
203 "class Z : public C {};", ZIsDerivedFromX));
204 EXPECT_TRUE(
205 matches("class U {}; typedef U X; typedef X V; "
206 "class Z : public V {};", ZIsDerivedFromX));
207 EXPECT_TRUE(
208 matches("class Base {}; typedef Base X; "
209 "class Z : public Base {};", ZIsDerivedFromX));
210 EXPECT_TRUE(
211 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
212 "class Z : public Base {};", ZIsDerivedFromX));
213 EXPECT_TRUE(
214 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
215 "class Z : public Base {};", ZIsDerivedFromX));
216 EXPECT_TRUE(
217 matches("class A {}; typedef A X; typedef A Y; "
218 "class Z : public Y {};", ZIsDerivedFromX));
219 EXPECT_TRUE(
220 notMatches("template <typename T> class Z;"
221 "template <> class Z<void> {};"
222 "template <typename T> class Z : public Z<void> {};",
223 IsDerivedFromX));
224 EXPECT_TRUE(
225 matches("template <typename T> class X;"
226 "template <> class X<void> {};"
227 "template <typename T> class X : public X<void> {};",
228 IsDerivedFromX));
229 EXPECT_TRUE(matches(
230 "class X {};"
231 "template <typename T> class Z;"
232 "template <> class Z<void> {};"
233 "template <typename T> class Z : public Z<void>, public X {};",
234 ZIsDerivedFromX));
235
236 // FIXME: Once we have better matchers for template type matching,
237 // get rid of the Variable(...) matching and match the right template
238 // declarations directly.
239 const char *RecursiveTemplateOneParameter =
240 "class Base1 {}; class Base2 {};"
241 "template <typename T> class Z;"
242 "template <> class Z<void> : public Base1 {};"
243 "template <> class Z<int> : public Base2 {};"
244 "template <> class Z<float> : public Z<void> {};"
245 "template <> class Z<double> : public Z<int> {};"
246 "template <typename T> class Z : public Z<float>, public Z<double> {};"
247 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
248 EXPECT_TRUE(matches(
249 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000250 varDecl(hasName("z_float"),
251 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000252 EXPECT_TRUE(notMatches(
253 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000254 varDecl(hasName("z_float"),
255 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000256 EXPECT_TRUE(matches(
257 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000258 varDecl(hasName("z_char"),
259 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
260 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000261
262 const char *RecursiveTemplateTwoParameters =
263 "class Base1 {}; class Base2 {};"
264 "template <typename T1, typename T2> class Z;"
265 "template <typename T> class Z<void, T> : public Base1 {};"
266 "template <typename T> class Z<int, T> : public Base2 {};"
267 "template <typename T> class Z<float, T> : public Z<void, T> {};"
268 "template <typename T> class Z<double, T> : public Z<int, T> {};"
269 "template <typename T1, typename T2> class Z : "
270 " public Z<float, T2>, public Z<double, T2> {};"
271 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
272 " Z<char, void> z_char; }";
273 EXPECT_TRUE(matches(
274 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000275 varDecl(hasName("z_float"),
276 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000277 EXPECT_TRUE(notMatches(
278 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000279 varDecl(hasName("z_float"),
280 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000281 EXPECT_TRUE(matches(
282 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000283 varDecl(hasName("z_char"),
284 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
285 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000286 EXPECT_TRUE(matches(
287 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000288 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000289 EXPECT_TRUE(notMatches(
290 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000291 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000292
293 EXPECT_TRUE(matches(
294 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000295 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000296}
297
Daniel Jasper08f0c532012-09-18 14:17:42 +0000298TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
299 EXPECT_TRUE(matches(
300 "template <typename T> struct A {"
301 " template <typename T2> struct F {};"
302 "};"
303 "template <typename T> struct B : A<T>::template F<T> {};"
304 "B<int> b;",
305 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
306}
307
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000308TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000309 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000310 EXPECT_TRUE(notMatches("class X;", ClassX));
311 EXPECT_TRUE(notMatches("class X {};", ClassX));
312}
313
314TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000315 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000316 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
317 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
318}
319
320TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
321 EXPECT_TRUE(notMatches("template<typename T> class X { };"
322 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000323 classTemplateDecl(hasName("X"),
324 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000325}
326
327TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
328 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
329 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000330 classTemplateDecl(hasName("X"),
331 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000332}
333
Daniel Jasper6a124492012-07-12 08:50:38 +0000334TEST(AllOf, AllOverloadsWork) {
335 const char Program[] =
336 "struct T { }; int f(int, T*); void g(int x) { T t; f(x, &t); }";
337 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000338 callExpr(allOf(callee(functionDecl(hasName("f"))),
339 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000340 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000341 callExpr(allOf(callee(functionDecl(hasName("f"))),
342 hasArgument(0, declRefExpr(to(varDecl()))),
343 hasArgument(1, hasType(pointsTo(
344 recordDecl(hasName("T")))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000345}
346
Manuel Klimek4da21662012-07-06 05:48:52 +0000347TEST(DeclarationMatcher, MatchAnyOf) {
348 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000349 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000350 EXPECT_TRUE(
351 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
352 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
353 EXPECT_TRUE(
354 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
355 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
356
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000357 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000358 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000359 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
360 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
361
Manuel Klimek4da21662012-07-06 05:48:52 +0000362 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000363 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
364 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000365 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
366 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
367 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
368 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
369 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
370 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
371}
372
373TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000374 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000375 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
376 EXPECT_TRUE(matches("class X {};", HasClassX));
377
378 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000379 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000380 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
381 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
382 EXPECT_TRUE(
383 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
384}
385
386TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
387 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000388 recordDecl(
389 has(recordDecl(
390 has(recordDecl(hasName("X"))),
391 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000392 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000393 has(recordDecl(
394 has(recordDecl(hasName("A"))),
395 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000396 hasName("C"))),
397 hasName("F"));
398
399 EXPECT_TRUE(matches(
400 "class F {"
401 " class Z {"
402 " class X {};"
403 " class Y {};"
404 " };"
405 " class C {"
406 " class A {};"
407 " class B {};"
408 " };"
409 "};", Recursive));
410
411 EXPECT_TRUE(matches(
412 "class F {"
413 " class Z {"
414 " class A {};"
415 " class X {};"
416 " class Y {};"
417 " };"
418 " class C {"
419 " class X {};"
420 " class A {};"
421 " class B {};"
422 " };"
423 "};", Recursive));
424
425 EXPECT_TRUE(matches(
426 "class O1 {"
427 " class O2 {"
428 " class F {"
429 " class Z {"
430 " class A {};"
431 " class X {};"
432 " class Y {};"
433 " };"
434 " class C {"
435 " class X {};"
436 " class A {};"
437 " class B {};"
438 " };"
439 " };"
440 " };"
441 "};", Recursive));
442}
443
444TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
445 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000446 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000447 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000448 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000449 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000450 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000451 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000452 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000453 hasName("Y"))),
454 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000455 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000456 anyOf(
457 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000458 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000459 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000460 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000461 hasName("B")))))),
462 hasName("F")));
463
464 EXPECT_TRUE(matches("class F {};", Recursive));
465 EXPECT_TRUE(matches("class Z {};", Recursive));
466 EXPECT_TRUE(matches("class C {};", Recursive));
467 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
468 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
469 EXPECT_TRUE(
470 matches("class O1 { class O2 {"
471 " class M { class N { class B {}; }; }; "
472 "}; };", Recursive));
473}
474
475TEST(DeclarationMatcher, MatchNot) {
476 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000477 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000478 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000479 unless(hasName("X")));
480 EXPECT_TRUE(notMatches("", NotClassX));
481 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
482 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
483 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
484 EXPECT_TRUE(
485 notMatches("class Y {}; class Z {}; class X : public Y {};",
486 NotClassX));
487
488 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000489 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000490 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000491 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000492 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000493 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000494 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
495 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
496 ClassXHasNotClassY));
497}
498
499TEST(DeclarationMatcher, HasDescendant) {
500 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000501 recordDecl(
502 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000503 hasName("Z"));
504 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
505 EXPECT_TRUE(
506 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
507 EXPECT_TRUE(
508 matches("class Z { class A { class Y { class X {}; }; }; };",
509 ZDescendantClassX));
510 EXPECT_TRUE(
511 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
512 ZDescendantClassX));
513 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
514
515 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000516 recordDecl(
517 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000518 hasName("X"))),
519 hasName("Z"));
520 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
521 ZDescendantClassXHasClassY));
522 EXPECT_TRUE(
523 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
524 ZDescendantClassXHasClassY));
525 EXPECT_TRUE(notMatches(
526 "class Z {"
527 " class A {"
528 " class B {"
529 " class X {"
530 " class C {"
531 " class Y {};"
532 " };"
533 " };"
534 " }; "
535 " };"
536 "};", ZDescendantClassXHasClassY));
537
538 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000539 recordDecl(
540 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
541 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000542 hasName("Z"));
543 EXPECT_TRUE(
544 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
545 ZDescendantClassXDescendantClassY));
546 EXPECT_TRUE(matches(
547 "class Z {"
548 " class A {"
549 " class X {"
550 " class B {"
551 " class Y {};"
552 " };"
553 " class Y {};"
554 " };"
555 " };"
556 "};", ZDescendantClassXDescendantClassY));
557}
558
Daniel Jaspera267cf62012-10-29 10:14:44 +0000559// Implements a run method that returns whether BoundNodes contains a
560// Decl bound to Id that can be dynamically cast to T.
561// Optionally checks that the check succeeded a specific number of times.
562template <typename T>
563class VerifyIdIsBoundTo : public BoundNodesCallback {
564public:
565 // Create an object that checks that a node of type \c T was bound to \c Id.
566 // Does not check for a certain number of matches.
567 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
568 : Id(Id), ExpectedCount(-1), Count(0) {}
569
570 // Create an object that checks that a node of type \c T was bound to \c Id.
571 // Checks that there were exactly \c ExpectedCount matches.
572 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
573 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
574
575 // Create an object that checks that a node of type \c T was bound to \c Id.
576 // Checks that there was exactly one match with the name \c ExpectedName.
577 // Note that \c T must be a NamedDecl for this to work.
578 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName)
579 : Id(Id), ExpectedCount(1), Count(0), ExpectedName(ExpectedName) {}
580
581 ~VerifyIdIsBoundTo() {
582 if (ExpectedCount != -1)
583 EXPECT_EQ(ExpectedCount, Count);
584 if (!ExpectedName.empty())
585 EXPECT_EQ(ExpectedName, Name);
586 }
587
588 virtual bool run(const BoundNodes *Nodes) {
589 if (Nodes->getNodeAs<T>(Id)) {
590 ++Count;
591 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
592 Name = Named->getNameAsString();
593 } else if (const NestedNameSpecifier *NNS =
594 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
595 llvm::raw_string_ostream OS(Name);
596 NNS->print(OS, PrintingPolicy(LangOptions()));
597 }
598 return true;
599 }
600 return false;
601 }
602
Daniel Jasper452abbc2012-10-29 10:48:25 +0000603 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
604 return run(Nodes);
605 }
606
Daniel Jaspera267cf62012-10-29 10:14:44 +0000607private:
608 const std::string Id;
609 const int ExpectedCount;
610 int Count;
611 const std::string ExpectedName;
612 std::string Name;
613};
614
615TEST(HasDescendant, MatchesDescendantTypes) {
616 EXPECT_TRUE(matches("void f() { int i = 3; }",
617 decl(hasDescendant(loc(builtinType())))));
618 EXPECT_TRUE(matches("void f() { int i = 3; }",
619 stmt(hasDescendant(builtinType()))));
620
621 EXPECT_TRUE(matches("void f() { int i = 3; }",
622 stmt(hasDescendant(loc(builtinType())))));
623 EXPECT_TRUE(matches("void f() { int i = 3; }",
624 stmt(hasDescendant(qualType(builtinType())))));
625
626 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
627 stmt(hasDescendant(isInteger()))));
628
629 EXPECT_TRUE(matchAndVerifyResultTrue(
630 "void f() { int a; float c; int d; int e; }",
631 functionDecl(forEachDescendant(
632 varDecl(hasDescendant(isInteger())).bind("x"))),
633 new VerifyIdIsBoundTo<Decl>("x", 3)));
634}
635
636TEST(HasDescendant, MatchesDescendantsOfTypes) {
637 EXPECT_TRUE(matches("void f() { int*** i; }",
638 qualType(hasDescendant(builtinType()))));
639 EXPECT_TRUE(matches("void f() { int*** i; }",
640 qualType(hasDescendant(
641 pointerType(pointee(builtinType()))))));
642 EXPECT_TRUE(matches("void f() { int*** i; }",
643 typeLoc(hasDescendant(builtinTypeLoc()))));
644
645 EXPECT_TRUE(matchAndVerifyResultTrue(
646 "void f() { int*** i; }",
647 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
648 new VerifyIdIsBoundTo<Type>("x", 2)));
649}
650
651TEST(Has, MatchesChildrenOfTypes) {
652 EXPECT_TRUE(matches("int i;",
653 varDecl(hasName("i"), has(isInteger()))));
654 EXPECT_TRUE(notMatches("int** i;",
655 varDecl(hasName("i"), has(isInteger()))));
656 EXPECT_TRUE(matchAndVerifyResultTrue(
657 "int (*f)(float, int);",
658 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
659 new VerifyIdIsBoundTo<QualType>("x", 2)));
660}
661
662TEST(Has, MatchesChildTypes) {
663 EXPECT_TRUE(matches(
664 "int* i;",
665 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
666 EXPECT_TRUE(notMatches(
667 "int* i;",
668 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
669}
670
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000671TEST(Enum, DoesNotMatchClasses) {
672 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
673}
674
675TEST(Enum, MatchesEnums) {
676 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
677}
678
679TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000680 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000681 EXPECT_TRUE(matches("enum X{ A };", Matcher));
682 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
683 EXPECT_TRUE(notMatches("enum X {};", Matcher));
684}
685
Manuel Klimek4da21662012-07-06 05:48:52 +0000686TEST(StatementMatcher, Has) {
687 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000688 expr(hasType(pointsTo(recordDecl(hasName("X")))),
689 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000690
691 EXPECT_TRUE(matches(
692 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
693 EXPECT_TRUE(notMatches(
694 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
695}
696
697TEST(StatementMatcher, HasDescendant) {
698 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000699 expr(hasType(pointsTo(recordDecl(hasName("X")))),
700 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000701
702 EXPECT_TRUE(matches(
703 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
704 HasDescendantVariableI));
705 EXPECT_TRUE(notMatches(
706 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
707 HasDescendantVariableI));
708}
709
710TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000711 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000712
713 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
714 EXPECT_TRUE(notMatches("class A {};", TypeA));
715
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000716 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000717
718 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
719 TypeDerivedFromA));
720 EXPECT_TRUE(notMatches("class A {};", TypeA));
721
722 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000723 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000724
725 EXPECT_TRUE(
726 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
727}
728
Manuel Klimek4da21662012-07-06 05:48:52 +0000729TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000730 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000731
732 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000733 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000734
735 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000736 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000737
738 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000739 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000740
741 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
742 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000743 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000744
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000745 StatementMatcher MethodX =
746 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000747
748 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
749 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000750 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000751}
752
753TEST(Matcher, BindTheSameNameInAlternatives) {
754 StatementMatcher matcher = anyOf(
755 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000756 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000757 hasRHS(integerLiteral(equals(0)))),
758 binaryOperator(hasOperatorName("+"),
759 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000760 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000761
762 EXPECT_TRUE(matchAndVerifyResultTrue(
763 // The first branch of the matcher binds x to 0 but then fails.
764 // The second branch binds x to f() and succeeds.
765 "int f() { return 0 + f(); }",
766 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000767 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000768}
769
Manuel Klimek66341c52012-08-30 19:41:06 +0000770TEST(Matcher, BindsIDForMemoizedResults) {
771 // Using the same matcher in two match expressions will make memoization
772 // kick in.
773 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
774 EXPECT_TRUE(matchAndVerifyResultTrue(
775 "class A { class B { class X {}; }; };",
776 DeclarationMatcher(anyOf(
777 recordDecl(hasName("A"), hasDescendant(ClassX)),
778 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000779 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000780}
781
Manuel Klimek4da21662012-07-06 05:48:52 +0000782TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000783 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000784 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000785 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000786 EXPECT_TRUE(
787 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000788 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000789 EXPECT_TRUE(
790 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000791 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000792}
793
794TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000795 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000796 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000797 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000798 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000799 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000800 EXPECT_TRUE(
801 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000802 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000803}
804
805TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000806 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000807 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000808 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000809 EXPECT_TRUE(
810 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000811 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000812}
813
814TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000815 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000816 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000817 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000818 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000819 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000820}
821
822TEST(Matcher, Call) {
823 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000824 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000825 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000826
827 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
828 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
829
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000830 StatementMatcher MethodOnY =
831 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000832
833 EXPECT_TRUE(
834 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
835 MethodOnY));
836 EXPECT_TRUE(
837 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
838 MethodOnY));
839 EXPECT_TRUE(
840 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
841 MethodOnY));
842 EXPECT_TRUE(
843 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
844 MethodOnY));
845 EXPECT_TRUE(
846 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
847 MethodOnY));
848
849 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000850 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000851
852 EXPECT_TRUE(
853 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
854 MethodOnYPointer));
855 EXPECT_TRUE(
856 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
857 MethodOnYPointer));
858 EXPECT_TRUE(
859 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
860 MethodOnYPointer));
861 EXPECT_TRUE(
862 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
863 MethodOnYPointer));
864 EXPECT_TRUE(
865 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
866 MethodOnYPointer));
867}
868
Daniel Jasper31f7c082012-10-01 13:40:41 +0000869TEST(Matcher, Lambda) {
870 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
871 lambdaExpr()));
872}
873
874TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000875 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
876 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000877 forRangeStmt()));
878 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
879 forRangeStmt()));
880}
881
882TEST(Matcher, UserDefinedLiteral) {
883 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
884 " return i + 1;"
885 "}"
886 "char c = 'a'_inc;",
887 userDefinedLiteral()));
888}
889
Daniel Jasperb54b7642012-09-20 14:12:57 +0000890TEST(Matcher, FlowControl) {
891 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
892 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
893 continueStmt()));
894 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
895 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
896 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
897}
898
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000899TEST(HasType, MatchesAsString) {
900 EXPECT_TRUE(
901 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000902 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000903 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000904 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000905 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000906 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000907 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000908 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000909}
910
Manuel Klimek4da21662012-07-06 05:48:52 +0000911TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000912 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000913 // Unary operator
914 EXPECT_TRUE(matches("class Y { }; "
915 "bool operator!(Y x) { return false; }; "
916 "Y y; bool c = !y;", OpCall));
917 // No match -- special operators like "new", "delete"
918 // FIXME: operator new takes size_t, for which we need stddef.h, for which
919 // we need to figure out include paths in the test.
920 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
921 // "class Y { }; "
922 // "void *operator new(size_t size) { return 0; } "
923 // "Y *y = new Y;", OpCall));
924 EXPECT_TRUE(notMatches("class Y { }; "
925 "void operator delete(void *p) { } "
926 "void a() {Y *y = new Y; delete y;}", OpCall));
927 // Binary operator
928 EXPECT_TRUE(matches("class Y { }; "
929 "bool operator&&(Y x, Y y) { return true; }; "
930 "Y a; Y b; bool c = a && b;",
931 OpCall));
932 // No match -- normal operator, not an overloaded one.
933 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
934 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
935}
936
937TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
938 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000939 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000940 EXPECT_TRUE(matches("class Y { }; "
941 "bool operator&&(Y x, Y y) { return true; }; "
942 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
943 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000944 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000945 EXPECT_TRUE(notMatches("class Y { }; "
946 "bool operator&&(Y x, Y y) { return true; }; "
947 "Y a; Y b; bool c = a && b;",
948 OpCallLessLess));
949}
950
Daniel Jasper278057f2012-11-15 03:29:05 +0000951TEST(Matcher, NestedOverloadedOperatorCalls) {
952 EXPECT_TRUE(matchAndVerifyResultTrue(
953 "class Y { }; "
954 "Y& operator&&(Y& x, Y& y) { return x; }; "
955 "Y a; Y b; Y c; Y d = a && b && c;",
956 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
957 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
958 EXPECT_TRUE(matches(
959 "class Y { }; "
960 "Y& operator&&(Y& x, Y& y) { return x; }; "
961 "Y a; Y b; Y c; Y d = a && b && c;",
962 operatorCallExpr(hasParent(operatorCallExpr()))));
963 EXPECT_TRUE(matches(
964 "class Y { }; "
965 "Y& operator&&(Y& x, Y& y) { return x; }; "
966 "Y a; Y b; Y c; Y d = a && b && c;",
967 operatorCallExpr(hasDescendant(operatorCallExpr()))));
968}
969
Manuel Klimek4da21662012-07-06 05:48:52 +0000970TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +0000971 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000972 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000973
974 EXPECT_TRUE(
975 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
976 MethodOnY));
977 EXPECT_TRUE(
978 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
979 MethodOnY));
980 EXPECT_TRUE(
981 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
982 MethodOnY));
983 EXPECT_TRUE(
984 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
985 MethodOnY));
986 EXPECT_TRUE(
987 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
988 MethodOnY));
989
990 EXPECT_TRUE(matches(
991 "class Y {"
992 " public: virtual void x();"
993 "};"
994 "class X : public Y {"
995 " public: virtual void x();"
996 "};"
997 "void z() { X *x; x->Y::x(); }", MethodOnY));
998}
999
1000TEST(Matcher, VariableUsage) {
1001 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001002 declRefExpr(to(
1003 varDecl(hasInitializer(
1004 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001005
1006 EXPECT_TRUE(matches(
1007 "class Y {"
1008 " public:"
1009 " bool x() const;"
1010 "};"
1011 "void z(const Y &y) {"
1012 " bool b = y.x();"
1013 " if (b) {}"
1014 "}", Reference));
1015
1016 EXPECT_TRUE(notMatches(
1017 "class Y {"
1018 " public:"
1019 " bool x() const;"
1020 "};"
1021 "void z(const Y &y) {"
1022 " bool b = y.x();"
1023 "}", Reference));
1024}
1025
Daniel Jasper9bd28092012-07-30 05:03:25 +00001026TEST(Matcher, FindsVarDeclInFuncitonParameter) {
1027 EXPECT_TRUE(matches(
1028 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001029 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001030}
1031
Manuel Klimek4da21662012-07-06 05:48:52 +00001032TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001033 StatementMatcher CallOnVariableY =
1034 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001035
1036 EXPECT_TRUE(matches(
1037 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1038 EXPECT_TRUE(matches(
1039 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1040 EXPECT_TRUE(matches(
1041 "class Y { public: void x(); };"
1042 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1043 EXPECT_TRUE(matches(
1044 "class Y { public: void x(); };"
1045 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1046 EXPECT_TRUE(notMatches(
1047 "class Y { public: void x(); };"
1048 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1049 CallOnVariableY));
1050}
1051
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001052TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1053 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1054 unaryExprOrTypeTraitExpr()));
1055 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1056 alignOfExpr(anything())));
1057 // FIXME: Uncomment once alignof is enabled.
1058 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1059 // unaryExprOrTypeTraitExpr()));
1060 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1061 // sizeOfExpr()));
1062}
1063
1064TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1065 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1066 hasArgumentOfType(asString("int")))));
1067 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1068 hasArgumentOfType(asString("float")))));
1069 EXPECT_TRUE(matches(
1070 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001071 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001072 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001073 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001074}
1075
Manuel Klimek4da21662012-07-06 05:48:52 +00001076TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001077 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001078}
1079
1080TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001081 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001082}
1083
1084TEST(MemberExpression, MatchesVariable) {
1085 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001086 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001087 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001088 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001089 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001090 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001091}
1092
1093TEST(MemberExpression, MatchesStaticVariable) {
1094 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001095 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001096 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001097 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001098 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001099 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001100}
1101
Daniel Jasper6a124492012-07-12 08:50:38 +00001102TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001103 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1104 EXPECT_TRUE(matches(
1105 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1106 callExpr(hasArgument(0, declRefExpr(
1107 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001108}
1109
1110TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001111 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001112 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001113 callExpr(hasArgument(0, declRefExpr(
1114 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001115}
1116
Manuel Klimek4da21662012-07-06 05:48:52 +00001117TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1118 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001119 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001120 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001121 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001122 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001123 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001124}
1125
1126TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1127 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001128 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001129 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001130 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001131 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001132 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001133}
1134
1135TEST(IsArrow, MatchesMemberCallsViaArrow) {
1136 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001137 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001138 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001139 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001140 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001141 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001142}
1143
1144TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001145 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001146
1147 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1148 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1149}
1150
1151TEST(Callee, MatchesMemberExpressions) {
1152 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001153 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001154 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001155 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001156}
1157
1158TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001159 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001160
1161 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1162 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1163
Manuel Klimeke265c872012-07-10 14:21:30 +00001164#if !defined(_MSC_VER)
1165 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001166 // Dependent contexts, but a non-dependent call.
1167 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1168 CallFunctionF));
1169 EXPECT_TRUE(
1170 matches("void f(); template <int N> struct S { void g() { f(); } };",
1171 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001172#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001173
1174 // Depedent calls don't match.
1175 EXPECT_TRUE(
1176 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1177 CallFunctionF));
1178 EXPECT_TRUE(
1179 notMatches("void f(int);"
1180 "template <typename T> struct S { void g(T t) { f(t); } };",
1181 CallFunctionF));
1182}
1183
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001184TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1185 EXPECT_TRUE(
1186 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001187 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001188}
1189
1190TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1191 EXPECT_TRUE(
1192 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001193 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001194}
1195
1196TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1197 EXPECT_TRUE(
1198 notMatches("void g(); template <typename T> void f(T t) {}"
1199 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001200 functionTemplateDecl(hasName("f"),
1201 hasDescendant(declRefExpr(to(
1202 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001203}
1204
Manuel Klimek4da21662012-07-06 05:48:52 +00001205TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001206 StatementMatcher CallArgumentY = callExpr(
1207 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001208
1209 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1210 EXPECT_TRUE(
1211 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1212 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1213
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001214 StatementMatcher WrongIndex = callExpr(
1215 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001216 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1217}
1218
1219TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001220 StatementMatcher CallArgumentY = callExpr(
1221 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001222 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1223 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1224 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1225}
1226
1227TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001228 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001229
1230 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1231 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1232 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1233}
1234
1235TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001236 DeclarationMatcher ReferenceClassX = varDecl(
1237 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001238 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1239 ReferenceClassX));
1240 EXPECT_TRUE(
1241 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1242 EXPECT_TRUE(
1243 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1244 EXPECT_TRUE(
1245 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1246}
1247
1248TEST(HasParameter, CallsInnerMatcher) {
1249 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001250 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001251 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001252 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001253}
1254
1255TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1256 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001257 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001258}
1259
1260TEST(HasType, MatchesParameterVariableTypesStrictly) {
1261 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001262 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001263 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001264 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001265 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001266 methodDecl(hasParameter(0,
1267 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001268 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001269 methodDecl(hasParameter(0,
1270 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001271}
1272
1273TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1274 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001275 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001276 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001277 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001278}
1279
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001280TEST(Returns, MatchesReturnTypes) {
1281 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001282 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001283 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001284 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001285 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001286 functionDecl(returns(hasDeclaration(
1287 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001288}
1289
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001290TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001291 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1292 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1293 functionDecl(isExternC())));
1294 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001295}
1296
Manuel Klimek4da21662012-07-06 05:48:52 +00001297TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1298 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001299 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001300}
1301
1302TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1303 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001304 methodDecl(hasAnyParameter(hasType(pointsTo(
1305 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001306}
1307
1308TEST(HasName, MatchesParameterVariableDeclartions) {
1309 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001310 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001311 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001312 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001313}
1314
Daniel Jasper371f9392012-08-01 08:40:24 +00001315TEST(Matcher, MatchesClassTemplateSpecialization) {
1316 EXPECT_TRUE(matches("template<typename T> struct A {};"
1317 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001318 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001319 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001320 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001321 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001322 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001323}
1324
1325TEST(Matcher, MatchesTypeTemplateArgument) {
1326 EXPECT_TRUE(matches(
1327 "template<typename T> struct B {};"
1328 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001329 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001330 asString("int"))))));
1331}
1332
1333TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1334 EXPECT_TRUE(matches(
1335 "struct B { int next; };"
1336 "template<int(B::*next_ptr)> struct A {};"
1337 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001338 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1339 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001340
1341 EXPECT_TRUE(notMatches(
1342 "template <typename T> struct A {};"
1343 "A<int> a;",
1344 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1345 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001346}
1347
1348TEST(Matcher, MatchesSpecificArgument) {
1349 EXPECT_TRUE(matches(
1350 "template<typename T, typename U> class A {};"
1351 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001352 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001353 1, refersToType(asString("int"))))));
1354 EXPECT_TRUE(notMatches(
1355 "template<typename T, typename U> class A {};"
1356 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001357 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001358 1, refersToType(asString("int"))))));
1359}
1360
Manuel Klimek4da21662012-07-06 05:48:52 +00001361TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001362 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001363
1364 EXPECT_TRUE(
1365 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1366 EXPECT_TRUE(
1367 matches("class X { public: X(); }; void x() { X x = X(); }",
1368 Constructor));
1369 EXPECT_TRUE(
1370 matches("class X { public: X(int); }; void x() { X x = 0; }",
1371 Constructor));
1372 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1373}
1374
1375TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001376 StatementMatcher Constructor = constructExpr(
1377 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001378
1379 EXPECT_TRUE(
1380 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1381 Constructor));
1382 EXPECT_TRUE(
1383 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1384 Constructor));
1385 EXPECT_TRUE(
1386 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1387 Constructor));
1388 EXPECT_TRUE(
1389 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1390 Constructor));
1391
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001392 StatementMatcher WrongIndex = constructExpr(
1393 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001394 EXPECT_TRUE(
1395 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1396 WrongIndex));
1397}
1398
1399TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001400 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001401
1402 EXPECT_TRUE(
1403 matches("class X { public: X(int); }; void x() { X x(0); }",
1404 Constructor1Arg));
1405 EXPECT_TRUE(
1406 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1407 Constructor1Arg));
1408 EXPECT_TRUE(
1409 matches("class X { public: X(int); }; void x() { X x = 0; }",
1410 Constructor1Arg));
1411 EXPECT_TRUE(
1412 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1413 Constructor1Arg));
1414}
1415
Manuel Klimek70b9db92012-10-23 10:40:50 +00001416TEST(Matcher,ThisExpr) {
1417 EXPECT_TRUE(
1418 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1419 EXPECT_TRUE(
1420 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1421}
1422
Manuel Klimek4da21662012-07-06 05:48:52 +00001423TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001424 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001425
1426 std::string ClassString = "class string { public: string(); ~string(); }; ";
1427
1428 EXPECT_TRUE(
1429 matches(ClassString +
1430 "string GetStringByValue();"
1431 "void FunctionTakesString(string s);"
1432 "void run() { FunctionTakesString(GetStringByValue()); }",
1433 TempExpression));
1434
1435 EXPECT_TRUE(
1436 notMatches(ClassString +
1437 "string* GetStringPointer(); "
1438 "void FunctionTakesStringPtr(string* s);"
1439 "void run() {"
1440 " string* s = GetStringPointer();"
1441 " FunctionTakesStringPtr(GetStringPointer());"
1442 " FunctionTakesStringPtr(s);"
1443 "}",
1444 TempExpression));
1445
1446 EXPECT_TRUE(
1447 notMatches("class no_dtor {};"
1448 "no_dtor GetObjByValue();"
1449 "void ConsumeObj(no_dtor param);"
1450 "void run() { ConsumeObj(GetObjByValue()); }",
1451 TempExpression));
1452}
1453
Sam Panzere16acd32012-08-24 22:04:44 +00001454TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1455 std::string ClassString =
1456 "class string { public: string(); int length(); }; ";
1457
1458 EXPECT_TRUE(
1459 matches(ClassString +
1460 "string GetStringByValue();"
1461 "void FunctionTakesString(string s);"
1462 "void run() { FunctionTakesString(GetStringByValue()); }",
1463 materializeTemporaryExpr()));
1464
1465 EXPECT_TRUE(
1466 notMatches(ClassString +
1467 "string* GetStringPointer(); "
1468 "void FunctionTakesStringPtr(string* s);"
1469 "void run() {"
1470 " string* s = GetStringPointer();"
1471 " FunctionTakesStringPtr(GetStringPointer());"
1472 " FunctionTakesStringPtr(s);"
1473 "}",
1474 materializeTemporaryExpr()));
1475
1476 EXPECT_TRUE(
1477 notMatches(ClassString +
1478 "string GetStringByValue();"
1479 "void run() { int k = GetStringByValue().length(); }",
1480 materializeTemporaryExpr()));
1481
1482 EXPECT_TRUE(
1483 notMatches(ClassString +
1484 "string GetStringByValue();"
1485 "void run() { GetStringByValue(); }",
1486 materializeTemporaryExpr()));
1487}
1488
Manuel Klimek4da21662012-07-06 05:48:52 +00001489TEST(ConstructorDeclaration, SimpleCase) {
1490 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001491 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001492 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001493 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001494}
1495
1496TEST(ConstructorDeclaration, IsImplicit) {
1497 // This one doesn't match because the constructor is not added by the
1498 // compiler (it is not needed).
1499 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001500 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001501 // The compiler added the implicit default constructor.
1502 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001503 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001504 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001505 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001506}
1507
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001508TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1509 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001510 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001511}
1512
1513TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001514 EXPECT_TRUE(notMatches("class Foo {};",
1515 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001516}
1517
Manuel Klimek4da21662012-07-06 05:48:52 +00001518TEST(HasAnyConstructorInitializer, SimpleCase) {
1519 EXPECT_TRUE(notMatches(
1520 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001521 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001522 EXPECT_TRUE(matches(
1523 "class Foo {"
1524 " Foo() : foo_() { }"
1525 " int foo_;"
1526 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001527 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001528}
1529
1530TEST(HasAnyConstructorInitializer, ForField) {
1531 static const char Code[] =
1532 "class Baz { };"
1533 "class Foo {"
1534 " Foo() : foo_() { }"
1535 " Baz foo_;"
1536 " Baz bar_;"
1537 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001538 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1539 forField(hasType(recordDecl(hasName("Baz"))))))));
1540 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001541 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001542 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1543 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001544}
1545
1546TEST(HasAnyConstructorInitializer, WithInitializer) {
1547 static const char Code[] =
1548 "class Foo {"
1549 " Foo() : foo_(0) { }"
1550 " int foo_;"
1551 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001552 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001553 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001554 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001555 withInitializer(integerLiteral(equals(1)))))));
1556}
1557
1558TEST(HasAnyConstructorInitializer, IsWritten) {
1559 static const char Code[] =
1560 "struct Bar { Bar(){} };"
1561 "class Foo {"
1562 " Foo() : foo_() { }"
1563 " Bar foo_;"
1564 " Bar bar_;"
1565 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001566 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001567 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001568 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001569 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001570 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001571 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1572}
1573
1574TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001575 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001576
1577 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1578 EXPECT_TRUE(
1579 matches("class X { public: X(); }; void x() { new X(); }", New));
1580 EXPECT_TRUE(
1581 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1582 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1583}
1584
1585TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001586 StatementMatcher New = constructExpr(
1587 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001588
1589 EXPECT_TRUE(
1590 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1591 New));
1592 EXPECT_TRUE(
1593 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1594 New));
1595 EXPECT_TRUE(
1596 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1597 New));
1598
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001599 StatementMatcher WrongIndex = constructExpr(
1600 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001601 EXPECT_TRUE(
1602 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1603 WrongIndex));
1604}
1605
1606TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001607 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001608
1609 EXPECT_TRUE(
1610 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1611 EXPECT_TRUE(
1612 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1613 New));
1614}
1615
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001616TEST(Matcher, DeleteExpression) {
1617 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001618 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001619}
1620
Manuel Klimek4da21662012-07-06 05:48:52 +00001621TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001622 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001623
1624 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1625 EXPECT_TRUE(
1626 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1627 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1628}
1629
1630TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001631 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001632 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1633 // wide string
1634 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1635 // with escaped characters
1636 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1637 // no matching -- though the data type is the same, there is no string literal
1638 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1639}
1640
1641TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001642 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001643 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1644 // wide character
1645 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1646 // wide character, Hex encoded, NOT MATCHED!
1647 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1648 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1649}
1650
1651TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001652 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001653 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1654 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1655 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1656 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1657
1658 // Non-matching cases (character literals, float and double)
1659 EXPECT_TRUE(notMatches("int i = L'a';",
1660 HasIntLiteral)); // this is actually a character
1661 // literal cast to int
1662 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1663 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1664 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1665}
1666
Daniel Jasper31f7c082012-10-01 13:40:41 +00001667TEST(Matcher, NullPtrLiteral) {
1668 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1669}
1670
Daniel Jasperb54b7642012-09-20 14:12:57 +00001671TEST(Matcher, AsmStatement) {
1672 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1673}
1674
Manuel Klimek4da21662012-07-06 05:48:52 +00001675TEST(Matcher, Conditions) {
1676 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1677
1678 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1679 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1680 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1681 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1682 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1683}
1684
1685TEST(MatchBinaryOperator, HasOperatorName) {
1686 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1687
1688 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1689 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1690}
1691
1692TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1693 StatementMatcher OperatorTrueFalse =
1694 binaryOperator(hasLHS(boolLiteral(equals(true))),
1695 hasRHS(boolLiteral(equals(false))));
1696
1697 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1698 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1699 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1700}
1701
1702TEST(MatchBinaryOperator, HasEitherOperand) {
1703 StatementMatcher HasOperand =
1704 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1705
1706 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1707 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1708 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1709}
1710
1711TEST(Matcher, BinaryOperatorTypes) {
1712 // Integration test that verifies the AST provides all binary operators in
1713 // a way we expect.
1714 // FIXME: Operator ','
1715 EXPECT_TRUE(
1716 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1717 EXPECT_TRUE(
1718 matches("bool b; bool c = (b = true);",
1719 binaryOperator(hasOperatorName("="))));
1720 EXPECT_TRUE(
1721 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1722 EXPECT_TRUE(
1723 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1724 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1725 EXPECT_TRUE(
1726 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1727 EXPECT_TRUE(
1728 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1729 EXPECT_TRUE(
1730 matches("int i = 1; int j = (i <<= 2);",
1731 binaryOperator(hasOperatorName("<<="))));
1732 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1733 EXPECT_TRUE(
1734 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1735 EXPECT_TRUE(
1736 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1737 EXPECT_TRUE(
1738 matches("int i = 1; int j = (i >>= 2);",
1739 binaryOperator(hasOperatorName(">>="))));
1740 EXPECT_TRUE(
1741 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1742 EXPECT_TRUE(
1743 matches("int i = 42; int j = (i ^= 42);",
1744 binaryOperator(hasOperatorName("^="))));
1745 EXPECT_TRUE(
1746 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1747 EXPECT_TRUE(
1748 matches("int i = 42; int j = (i %= 42);",
1749 binaryOperator(hasOperatorName("%="))));
1750 EXPECT_TRUE(
1751 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1752 EXPECT_TRUE(
1753 matches("bool b = true && false;",
1754 binaryOperator(hasOperatorName("&&"))));
1755 EXPECT_TRUE(
1756 matches("bool b = true; bool c = (b &= false);",
1757 binaryOperator(hasOperatorName("&="))));
1758 EXPECT_TRUE(
1759 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1760 EXPECT_TRUE(
1761 matches("bool b = true || false;",
1762 binaryOperator(hasOperatorName("||"))));
1763 EXPECT_TRUE(
1764 matches("bool b = true; bool c = (b |= false);",
1765 binaryOperator(hasOperatorName("|="))));
1766 EXPECT_TRUE(
1767 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1768 EXPECT_TRUE(
1769 matches("int i = 42; int j = (i *= 23);",
1770 binaryOperator(hasOperatorName("*="))));
1771 EXPECT_TRUE(
1772 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1773 EXPECT_TRUE(
1774 matches("int i = 42; int j = (i /= 23);",
1775 binaryOperator(hasOperatorName("/="))));
1776 EXPECT_TRUE(
1777 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1778 EXPECT_TRUE(
1779 matches("int i = 42; int j = (i += 23);",
1780 binaryOperator(hasOperatorName("+="))));
1781 EXPECT_TRUE(
1782 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1783 EXPECT_TRUE(
1784 matches("int i = 42; int j = (i -= 23);",
1785 binaryOperator(hasOperatorName("-="))));
1786 EXPECT_TRUE(
1787 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1788 binaryOperator(hasOperatorName("->*"))));
1789 EXPECT_TRUE(
1790 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1791 binaryOperator(hasOperatorName(".*"))));
1792
1793 // Member expressions as operators are not supported in matches.
1794 EXPECT_TRUE(
1795 notMatches("struct A { void x(A *a) { a->x(this); } };",
1796 binaryOperator(hasOperatorName("->"))));
1797
1798 // Initializer assignments are not represented as operator equals.
1799 EXPECT_TRUE(
1800 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1801
1802 // Array indexing is not represented as operator.
1803 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1804
1805 // Overloaded operators do not match at all.
1806 EXPECT_TRUE(notMatches(
1807 "struct A { bool operator&&(const A &a) const { return false; } };"
1808 "void x() { A a, b; a && b; }",
1809 binaryOperator()));
1810}
1811
1812TEST(MatchUnaryOperator, HasOperatorName) {
1813 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1814
1815 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1816 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1817}
1818
1819TEST(MatchUnaryOperator, HasUnaryOperand) {
1820 StatementMatcher OperatorOnFalse =
1821 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1822
1823 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1824 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1825}
1826
1827TEST(Matcher, UnaryOperatorTypes) {
1828 // Integration test that verifies the AST provides all unary operators in
1829 // a way we expect.
1830 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1831 EXPECT_TRUE(
1832 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1833 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1834 EXPECT_TRUE(
1835 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1836 EXPECT_TRUE(
1837 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1838 EXPECT_TRUE(
1839 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1840 EXPECT_TRUE(
1841 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1842 EXPECT_TRUE(
1843 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1844 EXPECT_TRUE(
1845 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1846 EXPECT_TRUE(
1847 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1848
1849 // We don't match conversion operators.
1850 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1851
1852 // Function calls are not represented as operator.
1853 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1854
1855 // Overloaded operators do not match at all.
1856 // FIXME: We probably want to add that.
1857 EXPECT_TRUE(notMatches(
1858 "struct A { bool operator!() const { return false; } };"
1859 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1860}
1861
1862TEST(Matcher, ConditionalOperator) {
1863 StatementMatcher Conditional = conditionalOperator(
1864 hasCondition(boolLiteral(equals(true))),
1865 hasTrueExpression(boolLiteral(equals(false))));
1866
1867 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1868 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1869 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1870
1871 StatementMatcher ConditionalFalse = conditionalOperator(
1872 hasFalseExpression(boolLiteral(equals(false))));
1873
1874 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1875 EXPECT_TRUE(
1876 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1877}
1878
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001879TEST(ArraySubscriptMatchers, ArraySubscripts) {
1880 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1881 arraySubscriptExpr()));
1882 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1883 arraySubscriptExpr()));
1884}
1885
1886TEST(ArraySubscriptMatchers, ArrayIndex) {
1887 EXPECT_TRUE(matches(
1888 "int i[2]; void f() { i[1] = 1; }",
1889 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1890 EXPECT_TRUE(matches(
1891 "int i[2]; void f() { 1[i] = 1; }",
1892 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1893 EXPECT_TRUE(notMatches(
1894 "int i[2]; void f() { i[1] = 1; }",
1895 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1896}
1897
1898TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1899 EXPECT_TRUE(matches(
1900 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001901 arraySubscriptExpr(hasBase(implicitCastExpr(
1902 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001903}
1904
Manuel Klimek4da21662012-07-06 05:48:52 +00001905TEST(Matcher, HasNameSupportsNamespaces) {
1906 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001907 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001908 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001909 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001910 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001911 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001912 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001913 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001914 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001915 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001916 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001917 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001918 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001919 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001920 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001921 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001922 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001923 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001924 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001925 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001926 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001927 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001928 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001929 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001930}
1931
1932TEST(Matcher, HasNameSupportsOuterClasses) {
1933 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001934 matches("class A { class B { class C; }; };",
1935 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001936 EXPECT_TRUE(
1937 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001938 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001939 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001940 matches("class A { class B { class C; }; };",
1941 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001942 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001943 matches("class A { class B { class C; }; };",
1944 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001945 EXPECT_TRUE(
1946 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001947 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001948 EXPECT_TRUE(
1949 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001950 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001951 EXPECT_TRUE(
1952 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001953 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001954 EXPECT_TRUE(
1955 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001956 recordDecl(hasName("::C"))));
1957 EXPECT_TRUE(
1958 notMatches("class A { class B { class C; }; };",
1959 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001960 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001961 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001962 EXPECT_TRUE(
1963 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001964 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001965}
1966
1967TEST(Matcher, IsDefinition) {
1968 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001969 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001970 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1971 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1972
1973 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001974 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001975 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1976 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1977
1978 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001979 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001980 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1981 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1982}
1983
1984TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001985 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00001986 ofClass(hasName("X")))));
1987
1988 EXPECT_TRUE(
1989 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
1990 EXPECT_TRUE(
1991 matches("class X { public: X(); }; void x(int) { X x = X(); }",
1992 Constructor));
1993 EXPECT_TRUE(
1994 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
1995 Constructor));
1996}
1997
1998TEST(Matcher, VisitsTemplateInstantiations) {
1999 EXPECT_TRUE(matches(
2000 "class A { public: void x(); };"
2001 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002002 "void f() { B<A> b; b.y(); }",
2003 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002004
2005 EXPECT_TRUE(matches(
2006 "class A { public: void x(); };"
2007 "class C {"
2008 " public:"
2009 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2010 "};"
2011 "void f() {"
2012 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002013 "}",
2014 recordDecl(hasName("C"),
2015 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002016}
2017
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002018TEST(Matcher, HandlesNullQualTypes) {
2019 // FIXME: Add a Type matcher so we can replace uses of this
2020 // variable with Type(True())
2021 const TypeMatcher AnyType = anything();
2022
2023 // We don't really care whether this matcher succeeds; we're testing that
2024 // it completes without crashing.
2025 EXPECT_TRUE(matches(
2026 "struct A { };"
2027 "template <typename T>"
2028 "void f(T t) {"
2029 " T local_t(t /* this becomes a null QualType in the AST */);"
2030 "}"
2031 "void g() {"
2032 " f(0);"
2033 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002034 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002035 anyOf(
2036 TypeMatcher(hasDeclaration(anything())),
2037 pointsTo(AnyType),
2038 references(AnyType)
2039 // Other QualType matchers should go here.
2040 ))))));
2041}
2042
Manuel Klimek4da21662012-07-06 05:48:52 +00002043// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002044AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002045 // Make sure all special variables are used: node, match_finder,
2046 // bound_nodes_builder, and the parameter named 'AMatcher'.
2047 return AMatcher.matches(Node, Finder, Builder);
2048}
2049
2050TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002051 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002052
2053 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002054 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002055
2056 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002057 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002058
2059 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002060 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002061}
2062
2063AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002064 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
2065 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
2066 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00002067 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00002068 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002069 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002070 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2071 ASTMatchFinder::BK_First);
2072}
2073
2074TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002075 DeclarationMatcher HasClassB =
2076 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002077
2078 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002079 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002080
2081 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002082 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002083
2084 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002085 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002086
2087 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002088 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002089
2090 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2091}
2092
2093TEST(For, FindsForLoops) {
2094 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2095 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002096 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2097 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002098 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002099}
2100
Daniel Jasper6a124492012-07-12 08:50:38 +00002101TEST(For, ForLoopInternals) {
2102 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2103 forStmt(hasCondition(anything()))));
2104 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2105 forStmt(hasLoopInit(anything()))));
2106}
2107
2108TEST(For, NegativeForLoopInternals) {
2109 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002110 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002111 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2112 forStmt(hasLoopInit(anything()))));
2113}
2114
Manuel Klimek4da21662012-07-06 05:48:52 +00002115TEST(For, ReportsNoFalsePositives) {
2116 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2117 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2118}
2119
2120TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002121 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2122 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2123 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002124}
2125
2126TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2127 // It's not a compound statement just because there's "{}" in the source
2128 // text. This is an AST search, not grep.
2129 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002130 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002131 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002132 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002133}
2134
Daniel Jasper6a124492012-07-12 08:50:38 +00002135TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002136 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002137 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002138 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002139 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002140 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002141 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002142 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002143 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002144}
2145
2146TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2147 // The simplest case: every compound statement is in a function
2148 // definition, and the function body itself must be a compound
2149 // statement.
2150 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002151 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002152}
2153
2154TEST(HasAnySubstatement, IsNotRecursive) {
2155 // It's really "has any immediate substatement".
2156 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002157 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002158}
2159
2160TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2161 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002162 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002163}
2164
2165TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2166 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002167 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002168}
2169
2170TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2171 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002172 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002173 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002174 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002175}
2176
2177TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2178 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002179 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002180 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002181 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002182 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002183 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002184}
2185
2186TEST(StatementCountIs, WorksWithMultipleStatements) {
2187 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002188 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002189}
2190
2191TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2192 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002193 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002194 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002195 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002196 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002197 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002198 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002199 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002200}
2201
2202TEST(Member, WorksInSimplestCase) {
2203 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002204 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002205}
2206
2207TEST(Member, DoesNotMatchTheBaseExpression) {
2208 // Don't pick out the wrong part of the member expression, this should
2209 // be checking the member (name) only.
2210 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002211 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002212}
2213
2214TEST(Member, MatchesInMemberFunctionCall) {
2215 EXPECT_TRUE(matches("void f() {"
2216 " struct { void first() {}; } s;"
2217 " s.first();"
2218 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002219 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002220}
2221
Daniel Jasperc711af22012-10-23 15:46:39 +00002222TEST(Member, MatchesMember) {
2223 EXPECT_TRUE(matches(
2224 "struct A { int i; }; void f() { A a; a.i = 2; }",
2225 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2226 EXPECT_TRUE(notMatches(
2227 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2228 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2229}
2230
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002231TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002232 // Fails in C++11 mode
2233 EXPECT_TRUE(matchesConditionally(
2234 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2235 "class X { void *operator new(std::size_t); };",
2236 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002237
2238 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002239 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002240
Daniel Jasper31f7c082012-10-01 13:40:41 +00002241 // Fails in C++11 mode
2242 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002243 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2244 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002245 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002246}
2247
Manuel Klimek4da21662012-07-06 05:48:52 +00002248TEST(HasObjectExpression, DoesNotMatchMember) {
2249 EXPECT_TRUE(notMatches(
2250 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002251 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002252}
2253
2254TEST(HasObjectExpression, MatchesBaseOfVariable) {
2255 EXPECT_TRUE(matches(
2256 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002257 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002258 EXPECT_TRUE(matches(
2259 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002260 memberExpr(hasObjectExpression(
2261 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002262}
2263
2264TEST(HasObjectExpression,
2265 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2266 EXPECT_TRUE(matches(
2267 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002268 memberExpr(hasObjectExpression(
2269 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002270 EXPECT_TRUE(matches(
2271 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002272 memberExpr(hasObjectExpression(
2273 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002274}
2275
2276TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002277 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2278 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2279 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2280 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002281}
2282
2283TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002284 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002285}
2286
2287TEST(IsConstQualified, MatchesConstInt) {
2288 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002289 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002290}
2291
2292TEST(IsConstQualified, MatchesConstPointer) {
2293 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002294 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002295}
2296
2297TEST(IsConstQualified, MatchesThroughTypedef) {
2298 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002299 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002300 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002301 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002302}
2303
2304TEST(IsConstQualified, DoesNotMatchInappropriately) {
2305 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002306 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002307 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002308 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002309}
2310
Sam Panzer089e5b32012-08-16 16:58:10 +00002311TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002312 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2313 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2314 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2315 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002316}
2317TEST(CastExpression, MatchesImplicitCasts) {
2318 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002319 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002320 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002321 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002322}
2323
2324TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002325 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2326 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2327 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2328 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002329}
2330
Manuel Klimek4da21662012-07-06 05:48:52 +00002331TEST(ReinterpretCast, MatchesSimpleCase) {
2332 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002333 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002334}
2335
2336TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002337 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002338 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002339 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002340 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002341 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002342 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2343 "B b;"
2344 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002345 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002346}
2347
2348TEST(FunctionalCast, MatchesSimpleCase) {
2349 std::string foo_class = "class Foo { public: Foo(char*); };";
2350 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002351 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002352}
2353
2354TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2355 std::string FooClass = "class Foo { public: Foo(char*); };";
2356 EXPECT_TRUE(
2357 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002358 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002359 EXPECT_TRUE(
2360 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002361 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002362}
2363
2364TEST(DynamicCast, MatchesSimpleCase) {
2365 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2366 "B b;"
2367 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002368 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002369}
2370
2371TEST(StaticCast, MatchesSimpleCase) {
2372 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002373 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002374}
2375
2376TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002377 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002378 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002379 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002380 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002381 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002382 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2383 "B b;"
2384 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002385 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002386}
2387
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002388TEST(CStyleCast, MatchesSimpleCase) {
2389 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2390}
2391
2392TEST(CStyleCast, DoesNotMatchOtherCasts) {
2393 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2394 "char q, *r = const_cast<char*>(&q);"
2395 "void* s = reinterpret_cast<char*>(&s);"
2396 "struct B { virtual ~B() {} }; struct D : B {};"
2397 "B b;"
2398 "D* t = dynamic_cast<D*>(&b);",
2399 cStyleCastExpr()));
2400}
2401
Manuel Klimek4da21662012-07-06 05:48:52 +00002402TEST(HasDestinationType, MatchesSimpleCase) {
2403 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002404 staticCastExpr(hasDestinationType(
2405 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002406}
2407
Sam Panzer089e5b32012-08-16 16:58:10 +00002408TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2409 // This test creates an implicit const cast.
2410 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002411 implicitCastExpr(
2412 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002413 // This test creates an implicit array-to-pointer cast.
2414 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002415 implicitCastExpr(hasImplicitDestinationType(
2416 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002417}
2418
2419TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2420 // This test creates an implicit cast from int to char.
2421 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002422 implicitCastExpr(hasImplicitDestinationType(
2423 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002424 // This test creates an implicit array-to-pointer cast.
2425 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002426 implicitCastExpr(hasImplicitDestinationType(
2427 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002428}
2429
2430TEST(ImplicitCast, MatchesSimpleCase) {
2431 // This test creates an implicit const cast.
2432 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002433 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002434 // This test creates an implicit cast from int to char.
2435 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002436 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002437 // This test creates an implicit array-to-pointer cast.
2438 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002439 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002440}
2441
2442TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002443 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002444 // are present, and that it ignores explicit and paren casts.
2445
2446 // These two test cases have no casts.
2447 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002448 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002449 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002450 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002451
2452 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002453 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002454 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002455 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002456
2457 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002458 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002459}
2460
2461TEST(IgnoringImpCasts, MatchesImpCasts) {
2462 // This test checks that ignoringImpCasts matches when implicit casts are
2463 // present and its inner matcher alone does not match.
2464 // Note that this test creates an implicit const cast.
2465 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002466 varDecl(hasInitializer(ignoringImpCasts(
2467 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002468 // This test creates an implict cast from int to char.
2469 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002470 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002471 integerLiteral(equals(0)))))));
2472}
2473
2474TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2475 // These tests verify that ignoringImpCasts does not match if the inner
2476 // matcher does not match.
2477 // Note that the first test creates an implicit const cast.
2478 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002479 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002480 unless(anything()))))));
2481 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002482 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002483 unless(anything()))))));
2484
2485 // These tests verify that ignoringImplictCasts does not look through explicit
2486 // casts or parentheses.
2487 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002488 varDecl(hasInitializer(ignoringImpCasts(
2489 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002490 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002491 varDecl(hasInitializer(ignoringImpCasts(
2492 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002493 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002494 varDecl(hasInitializer(ignoringImpCasts(
2495 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002496 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002497 varDecl(hasInitializer(ignoringImpCasts(
2498 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002499}
2500
2501TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2502 // This test verifies that expressions that do not have implicit casts
2503 // still match the inner matcher.
2504 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002505 varDecl(hasInitializer(ignoringImpCasts(
2506 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002507}
2508
2509TEST(IgnoringParenCasts, MatchesParenCasts) {
2510 // This test checks that ignoringParenCasts matches when parentheses and/or
2511 // casts are present and its inner matcher alone does not match.
2512 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002513 varDecl(hasInitializer(ignoringParenCasts(
2514 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002515 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002516 varDecl(hasInitializer(ignoringParenCasts(
2517 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002518
2519 // This test creates an implict cast from int to char in addition to the
2520 // parentheses.
2521 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002522 varDecl(hasInitializer(ignoringParenCasts(
2523 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002524
2525 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002526 varDecl(hasInitializer(ignoringParenCasts(
2527 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002528 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002529 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002530 integerLiteral(equals(0)))))));
2531}
2532
2533TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2534 // This test verifies that expressions that do not have any casts still match.
2535 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002536 varDecl(hasInitializer(ignoringParenCasts(
2537 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002538}
2539
2540TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2541 // These tests verify that ignoringImpCasts does not match if the inner
2542 // matcher does not match.
2543 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002544 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002545 unless(anything()))))));
2546
2547 // This test creates an implicit cast from int to char in addition to the
2548 // parentheses.
2549 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002550 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002551 unless(anything()))))));
2552
2553 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002554 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002555 unless(anything()))))));
2556}
2557
2558TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2559 // This test checks that ignoringParenAndImpCasts matches when
2560 // parentheses and/or implicit casts are present and its inner matcher alone
2561 // does not match.
2562 // Note that this test creates an implicit const cast.
2563 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002564 varDecl(hasInitializer(ignoringParenImpCasts(
2565 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002566 // This test creates an implicit cast from int to char.
2567 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002568 varDecl(hasInitializer(ignoringParenImpCasts(
2569 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002570}
2571
2572TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2573 // This test verifies that expressions that do not have parentheses or
2574 // implicit casts still match.
2575 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002576 varDecl(hasInitializer(ignoringParenImpCasts(
2577 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002578 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002579 varDecl(hasInitializer(ignoringParenImpCasts(
2580 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002581}
2582
2583TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2584 // These tests verify that ignoringParenImpCasts does not match if
2585 // the inner matcher does not match.
2586 // This test creates an implicit cast.
2587 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002588 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002589 unless(anything()))))));
2590 // These tests verify that ignoringParenAndImplictCasts does not look
2591 // through explicit casts.
2592 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002593 varDecl(hasInitializer(ignoringParenImpCasts(
2594 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002595 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002596 varDecl(hasInitializer(ignoringParenImpCasts(
2597 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002598 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002599 varDecl(hasInitializer(ignoringParenImpCasts(
2600 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002601}
2602
Manuel Klimek715c9562012-07-25 10:02:02 +00002603TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002604 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2605 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002606 implicitCastExpr(
2607 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002608}
2609
Manuel Klimek715c9562012-07-25 10:02:02 +00002610TEST(HasSourceExpression, MatchesExplicitCasts) {
2611 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002612 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002613 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002614 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002615}
2616
Manuel Klimek4da21662012-07-06 05:48:52 +00002617TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002618 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002619}
2620
2621TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002622 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002623}
2624
2625TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002626 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002627}
2628
2629TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002630 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002631}
2632
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002633TEST(InitListExpression, MatchesInitListExpression) {
2634 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2635 initListExpr(hasType(asString("int [2]")))));
2636 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002637 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002638}
2639
2640TEST(UsingDeclaration, MatchesUsingDeclarations) {
2641 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2642 usingDecl()));
2643}
2644
2645TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2646 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2647 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2648}
2649
2650TEST(UsingDeclaration, MatchesSpecificTarget) {
2651 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2652 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002653 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002654 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2655 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002656 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002657}
2658
2659TEST(UsingDeclaration, ThroughUsingDeclaration) {
2660 EXPECT_TRUE(matches(
2661 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002662 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002663 EXPECT_TRUE(notMatches(
2664 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002665 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002666}
2667
Sam Panzer425f41b2012-08-16 17:20:59 +00002668TEST(SingleDecl, IsSingleDecl) {
2669 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002670 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002671 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2672 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2673 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2674 SingleDeclStmt));
2675}
2676
2677TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002678 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002679
2680 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002681 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002682 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002683 declStmt(containsDeclaration(0, MatchesInit),
2684 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002685 unsigned WrongIndex = 42;
2686 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002687 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002688 MatchesInit))));
2689}
2690
2691TEST(DeclCount, DeclCountIsCorrect) {
2692 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002693 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002694 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002695 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002696 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002697 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002698}
2699
Manuel Klimek4da21662012-07-06 05:48:52 +00002700TEST(While, MatchesWhileLoops) {
2701 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2702 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2703 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2704}
2705
2706TEST(Do, MatchesDoLoops) {
2707 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2708 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2709}
2710
2711TEST(Do, DoesNotMatchWhileLoops) {
2712 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2713}
2714
2715TEST(SwitchCase, MatchesCase) {
2716 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2717 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2718 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2719 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2720}
2721
Daniel Jasperb54b7642012-09-20 14:12:57 +00002722TEST(SwitchCase, MatchesSwitch) {
2723 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2724 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2725 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2726 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2727}
2728
2729TEST(ExceptionHandling, SimpleCases) {
2730 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2731 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2732 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2733 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2734 throwExpr()));
2735 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2736 throwExpr()));
2737}
2738
Manuel Klimek4da21662012-07-06 05:48:52 +00002739TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2740 EXPECT_TRUE(notMatches(
2741 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002742 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002743 EXPECT_TRUE(notMatches(
2744 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002745 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002746}
2747
2748TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2749 EXPECT_TRUE(matches(
2750 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002751 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002752}
2753
2754TEST(ForEach, BindsOneNode) {
2755 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002756 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002757 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002758}
2759
2760TEST(ForEach, BindsMultipleNodes) {
2761 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002762 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002763 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002764}
2765
2766TEST(ForEach, BindsRecursiveCombinations) {
2767 EXPECT_TRUE(matchAndVerifyResultTrue(
2768 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002769 recordDecl(hasName("C"),
2770 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002771 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002772}
2773
2774TEST(ForEachDescendant, BindsOneNode) {
2775 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002776 recordDecl(hasName("C"),
2777 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002778 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002779}
2780
Daniel Jasper5f684e92012-11-16 18:39:22 +00002781TEST(ForEachDescendant, NestedForEachDescendant) {
2782 DeclarationMatcher m = recordDecl(
2783 isDefinition(), decl().bind("x"), hasName("C"));
2784 EXPECT_TRUE(matchAndVerifyResultTrue(
2785 "class A { class B { class C {}; }; };",
2786 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
2787 new VerifyIdIsBoundTo<Decl>("x", "C")));
2788
2789 // FIXME: This is not really a useful matcher, but the result is still
2790 // surprising (currently binds "A").
2791 //EXPECT_TRUE(matchAndVerifyResultTrue(
2792 // "class A { class B { class C {}; }; };",
2793 // recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
2794 // new VerifyIdIsBoundTo<Decl>("x", "C")));
2795}
2796
Manuel Klimek4da21662012-07-06 05:48:52 +00002797TEST(ForEachDescendant, BindsMultipleNodes) {
2798 EXPECT_TRUE(matchAndVerifyResultTrue(
2799 "class C { class D { int x; int y; }; "
2800 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002801 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002802 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002803}
2804
2805TEST(ForEachDescendant, BindsRecursiveCombinations) {
2806 EXPECT_TRUE(matchAndVerifyResultTrue(
2807 "class C { class D { "
2808 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002809 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2810 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002811 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002812}
2813
Daniel Jasper11c98772012-11-11 22:14:55 +00002814TEST(ForEachDescendant, BindsCorrectNodes) {
2815 EXPECT_TRUE(matchAndVerifyResultTrue(
2816 "class C { void f(); int i; };",
2817 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2818 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
2819 EXPECT_TRUE(matchAndVerifyResultTrue(
2820 "class C { void f() {} int i; };",
2821 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2822 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
2823}
2824
Manuel Klimek4da21662012-07-06 05:48:52 +00002825
2826TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2827 // Make sure that we can both match the class by name (::X) and by the type
2828 // the template was instantiated with (via a field).
2829
2830 EXPECT_TRUE(matches(
2831 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002832 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002833
2834 EXPECT_TRUE(matches(
2835 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002836 recordDecl(isTemplateInstantiation(), hasDescendant(
2837 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002838}
2839
2840TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2841 EXPECT_TRUE(matches(
2842 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002843 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002844 isTemplateInstantiation())));
2845}
2846
2847TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2848 EXPECT_TRUE(matches(
2849 "template <typename T> class X { T t; }; class A {};"
2850 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002851 recordDecl(isTemplateInstantiation(), hasDescendant(
2852 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002853}
2854
2855TEST(IsTemplateInstantiation,
2856 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2857 EXPECT_TRUE(matches(
2858 "template <typename T> class X {};"
2859 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002860 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002861}
2862
2863TEST(IsTemplateInstantiation,
2864 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2865 EXPECT_TRUE(matches(
2866 "class A {};"
2867 "class X {"
2868 " template <typename U> class Y { U u; };"
2869 " Y<A> y;"
2870 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002871 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002872}
2873
2874TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2875 // FIXME: Figure out whether this makes sense. It doesn't affect the
2876 // normal use case as long as the uppermost instantiation always is marked
2877 // as template instantiation, but it might be confusing as a predicate.
2878 EXPECT_TRUE(matches(
2879 "class A {};"
2880 "template <typename T> class X {"
2881 " template <typename U> class Y { U u; };"
2882 " Y<T> y;"
2883 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002884 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002885}
2886
2887TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2888 EXPECT_TRUE(notMatches(
2889 "template <typename T> class X {}; class A {};"
2890 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002891 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002892}
2893
2894TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2895 EXPECT_TRUE(notMatches(
2896 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002897 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002898}
2899
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002900TEST(IsExplicitTemplateSpecialization,
2901 DoesNotMatchPrimaryTemplate) {
2902 EXPECT_TRUE(notMatches(
2903 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002904 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002905 EXPECT_TRUE(notMatches(
2906 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002907 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002908}
2909
2910TEST(IsExplicitTemplateSpecialization,
2911 DoesNotMatchExplicitTemplateInstantiations) {
2912 EXPECT_TRUE(notMatches(
2913 "template <typename T> class X {};"
2914 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002915 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002916 EXPECT_TRUE(notMatches(
2917 "template <typename T> void f(T t) {}"
2918 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002919 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002920}
2921
2922TEST(IsExplicitTemplateSpecialization,
2923 DoesNotMatchImplicitTemplateInstantiations) {
2924 EXPECT_TRUE(notMatches(
2925 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002926 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002927 EXPECT_TRUE(notMatches(
2928 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002929 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002930}
2931
2932TEST(IsExplicitTemplateSpecialization,
2933 MatchesExplicitTemplateSpecializations) {
2934 EXPECT_TRUE(matches(
2935 "template <typename T> class X {};"
2936 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002937 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002938 EXPECT_TRUE(matches(
2939 "template <typename T> void f(T t) {}"
2940 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002941 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002942}
2943
Manuel Klimek579b1202012-09-07 09:26:10 +00002944TEST(HasAncenstor, MatchesDeclarationAncestors) {
2945 EXPECT_TRUE(matches(
2946 "class A { class B { class C {}; }; };",
2947 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2948}
2949
2950TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2951 EXPECT_TRUE(notMatches(
2952 "class A { class B { class C {}; }; };",
2953 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2954}
2955
2956TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2957 EXPECT_TRUE(matches(
2958 "class A { class B { void f() { C c; } class C {}; }; };",
2959 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2960 hasAncestor(recordDecl(hasName("A"))))))));
2961}
2962
2963TEST(HasAncenstor, MatchesStatementAncestors) {
2964 EXPECT_TRUE(matches(
2965 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002966 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002967}
2968
2969TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2970 EXPECT_TRUE(matches(
2971 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002972 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002973}
2974
2975TEST(HasAncestor, BindsRecursiveCombinations) {
2976 EXPECT_TRUE(matchAndVerifyResultTrue(
2977 "class C { class D { class E { class F { int y; }; }; }; };",
2978 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002979 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00002980}
2981
2982TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
2983 EXPECT_TRUE(matchAndVerifyResultTrue(
2984 "class C { class D { class E { class F { int y; }; }; }; };",
2985 fieldDecl(hasAncestor(
2986 decl(
2987 hasDescendant(recordDecl(isDefinition(),
2988 hasAncestor(recordDecl())))
2989 ).bind("d")
2990 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00002991 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00002992}
2993
2994TEST(HasAncestor, MatchesInTemplateInstantiations) {
2995 EXPECT_TRUE(matches(
2996 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
2997 "A<int>::B::C a;",
2998 fieldDecl(hasType(asString("int")),
2999 hasAncestor(recordDecl(hasName("A"))))));
3000}
3001
3002TEST(HasAncestor, MatchesInImplicitCode) {
3003 EXPECT_TRUE(matches(
3004 "struct X {}; struct A { A() {} X x; };",
3005 constructorDecl(
3006 hasAnyConstructorInitializer(withInitializer(expr(
3007 hasAncestor(recordDecl(hasName("A")))))))));
3008}
3009
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003010TEST(HasParent, MatchesOnlyParent) {
3011 EXPECT_TRUE(matches(
3012 "void f() { if (true) { int x = 42; } }",
3013 compoundStmt(hasParent(ifStmt()))));
3014 EXPECT_TRUE(notMatches(
3015 "void f() { for (;;) { int x = 42; } }",
3016 compoundStmt(hasParent(ifStmt()))));
3017 EXPECT_TRUE(notMatches(
3018 "void f() { if (true) for (;;) { int x = 42; } }",
3019 compoundStmt(hasParent(ifStmt()))));
3020}
3021
Daniel Jasperce620072012-10-17 08:52:59 +00003022TEST(TypeMatching, MatchesTypes) {
3023 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3024}
3025
3026TEST(TypeMatching, MatchesArrayTypes) {
3027 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3028 EXPECT_TRUE(matches("int a[42];", arrayType()));
3029 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3030
3031 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3032 arrayType(hasElementType(builtinType()))));
3033
3034 EXPECT_TRUE(matches(
3035 "int const a[] = { 2, 3 };",
3036 qualType(arrayType(hasElementType(builtinType())))));
3037 EXPECT_TRUE(matches(
3038 "int const a[] = { 2, 3 };",
3039 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3040 EXPECT_TRUE(matches(
3041 "typedef const int T; T x[] = { 1, 2 };",
3042 qualType(isConstQualified(), arrayType())));
3043
3044 EXPECT_TRUE(notMatches(
3045 "int a[] = { 2, 3 };",
3046 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3047 EXPECT_TRUE(notMatches(
3048 "int a[] = { 2, 3 };",
3049 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3050 EXPECT_TRUE(notMatches(
3051 "int const a[] = { 2, 3 };",
3052 qualType(arrayType(hasElementType(builtinType())),
3053 unless(isConstQualified()))));
3054
3055 EXPECT_TRUE(matches("int a[2];",
3056 constantArrayType(hasElementType(builtinType()))));
3057 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3058}
3059
3060TEST(TypeMatching, MatchesComplexTypes) {
3061 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3062 EXPECT_TRUE(matches(
3063 "_Complex float f;",
3064 complexType(hasElementType(builtinType()))));
3065 EXPECT_TRUE(notMatches(
3066 "_Complex float f;",
3067 complexType(hasElementType(isInteger()))));
3068}
3069
3070TEST(TypeMatching, MatchesConstantArrayTypes) {
3071 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3072 EXPECT_TRUE(notMatches(
3073 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3074 constantArrayType(hasElementType(builtinType()))));
3075
3076 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3077 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3078 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3079}
3080
3081TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3082 EXPECT_TRUE(matches(
3083 "template <typename T, int Size> class array { T data[Size]; };",
3084 dependentSizedArrayType()));
3085 EXPECT_TRUE(notMatches(
3086 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3087 dependentSizedArrayType()));
3088}
3089
3090TEST(TypeMatching, MatchesIncompleteArrayType) {
3091 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3092 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3093
3094 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3095 incompleteArrayType()));
3096}
3097
3098TEST(TypeMatching, MatchesVariableArrayType) {
3099 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3100 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3101
3102 EXPECT_TRUE(matches(
3103 "void f(int b) { int a[b]; }",
3104 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3105 varDecl(hasName("b")))))))));
3106}
3107
3108TEST(TypeMatching, MatchesAtomicTypes) {
3109 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3110
3111 EXPECT_TRUE(matches("_Atomic(int) i;",
3112 atomicType(hasValueType(isInteger()))));
3113 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3114 atomicType(hasValueType(isInteger()))));
3115}
3116
3117TEST(TypeMatching, MatchesAutoTypes) {
3118 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3119 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3120 autoType()));
3121
3122 EXPECT_TRUE(matches("auto a = 1;",
3123 autoType(hasDeducedType(isInteger()))));
3124 EXPECT_TRUE(notMatches("auto b = 2.0;",
3125 autoType(hasDeducedType(isInteger()))));
3126}
3127
Daniel Jaspera267cf62012-10-29 10:14:44 +00003128TEST(TypeMatching, MatchesFunctionTypes) {
3129 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3130 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3131}
3132
Daniel Jasperce620072012-10-17 08:52:59 +00003133TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003134 // FIXME: Reactive when these tests can be more specific (not matching
3135 // implicit code on certain platforms), likely when we have hasDescendant for
3136 // Types/TypeLocs.
3137 //EXPECT_TRUE(matchAndVerifyResultTrue(
3138 // "int* a;",
3139 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3140 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3141 //EXPECT_TRUE(matchAndVerifyResultTrue(
3142 // "int* a;",
3143 // pointerTypeLoc().bind("loc"),
3144 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003145 EXPECT_TRUE(matches(
3146 "int** a;",
3147 pointerTypeLoc(pointeeLoc(loc(qualType())))));
3148 EXPECT_TRUE(matches(
3149 "int** a;",
3150 loc(pointerType(pointee(pointerType())))));
3151 EXPECT_TRUE(matches(
3152 "int* b; int* * const a = &b;",
3153 loc(qualType(isConstQualified(), pointerType()))));
3154
3155 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003156 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3157 hasType(blockPointerType()))));
3158 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3159 hasType(memberPointerType()))));
3160 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3161 hasType(pointerType()))));
3162 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3163 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003164
Daniel Jasper1802daf2012-10-17 13:35:36 +00003165 Fragment = "int *ptr;";
3166 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3167 hasType(blockPointerType()))));
3168 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3169 hasType(memberPointerType()))));
3170 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3171 hasType(pointerType()))));
3172 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3173 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003174
Daniel Jasper1802daf2012-10-17 13:35:36 +00003175 Fragment = "int a; int &ref = a;";
3176 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3177 hasType(blockPointerType()))));
3178 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3179 hasType(memberPointerType()))));
3180 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3181 hasType(pointerType()))));
3182 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3183 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003184}
3185
3186TEST(TypeMatching, PointeeTypes) {
3187 EXPECT_TRUE(matches("int b; int &a = b;",
3188 referenceType(pointee(builtinType()))));
3189 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3190
3191 EXPECT_TRUE(matches("int *a;",
3192 pointerTypeLoc(pointeeLoc(loc(builtinType())))));
3193
3194 EXPECT_TRUE(matches(
3195 "int const *A;",
3196 pointerType(pointee(isConstQualified(), builtinType()))));
3197 EXPECT_TRUE(notMatches(
3198 "int *A;",
3199 pointerType(pointee(isConstQualified(), builtinType()))));
3200}
3201
3202TEST(TypeMatching, MatchesPointersToConstTypes) {
3203 EXPECT_TRUE(matches("int b; int * const a = &b;",
3204 loc(pointerType())));
3205 EXPECT_TRUE(matches("int b; int * const a = &b;",
3206 pointerTypeLoc()));
3207 EXPECT_TRUE(matches(
3208 "int b; const int * a = &b;",
3209 pointerTypeLoc(pointeeLoc(builtinTypeLoc()))));
3210 EXPECT_TRUE(matches(
3211 "int b; const int * a = &b;",
3212 pointerType(pointee(builtinType()))));
3213}
3214
3215TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003216 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3217 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003218
Daniel Jasper1802daf2012-10-17 13:35:36 +00003219 EXPECT_TRUE(matches("typedef int X; X a;",
3220 varDecl(hasName("a"),
3221 hasType(typedefType(hasDecl(decl()))))));
Daniel Jasperce620072012-10-17 08:52:59 +00003222}
3223
Daniel Jaspera7564432012-09-13 13:11:25 +00003224TEST(NNS, MatchesNestedNameSpecifiers) {
3225 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3226 nestedNameSpecifier()));
3227 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3228 nestedNameSpecifier()));
3229 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3230 nestedNameSpecifier()));
3231
3232 EXPECT_TRUE(matches(
3233 "struct A { static void f() {} }; void g() { A::f(); }",
3234 nestedNameSpecifier()));
3235 EXPECT_TRUE(notMatches(
3236 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3237 nestedNameSpecifier()));
3238}
3239
Daniel Jasperb54b7642012-09-20 14:12:57 +00003240TEST(NullStatement, SimpleCases) {
3241 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3242 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3243}
3244
Daniel Jaspera7564432012-09-13 13:11:25 +00003245TEST(NNS, MatchesTypes) {
3246 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3247 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3248 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3249 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3250 Matcher));
3251 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3252}
3253
3254TEST(NNS, MatchesNamespaceDecls) {
3255 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3256 specifiesNamespace(hasName("ns")));
3257 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3258 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3259 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3260}
3261
3262TEST(NNS, BindsNestedNameSpecifiers) {
3263 EXPECT_TRUE(matchAndVerifyResultTrue(
3264 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3265 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3266 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3267}
3268
3269TEST(NNS, BindsNestedNameSpecifierLocs) {
3270 EXPECT_TRUE(matchAndVerifyResultTrue(
3271 "namespace ns { struct B {}; } ns::B b;",
3272 loc(nestedNameSpecifier()).bind("loc"),
3273 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3274}
3275
3276TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3277 EXPECT_TRUE(matches(
3278 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3279 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3280 EXPECT_TRUE(matches(
3281 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003282 nestedNameSpecifierLoc(hasPrefix(
3283 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003284}
3285
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003286TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3287 std::string Fragment =
3288 "namespace a { struct A { struct B { struct C {}; }; }; };"
3289 "void f() { a::A::B::C c; }";
3290 EXPECT_TRUE(matches(
3291 Fragment,
3292 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3293 hasDescendant(nestedNameSpecifier(
3294 specifiesNamespace(hasName("a")))))));
3295 EXPECT_TRUE(notMatches(
3296 Fragment,
3297 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3298 has(nestedNameSpecifier(
3299 specifiesNamespace(hasName("a")))))));
3300 EXPECT_TRUE(matches(
3301 Fragment,
3302 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3303 has(nestedNameSpecifier(
3304 specifiesNamespace(hasName("a")))))));
3305
3306 // Not really useful because a NestedNameSpecifier can af at most one child,
3307 // but to complete the interface.
3308 EXPECT_TRUE(matchAndVerifyResultTrue(
3309 Fragment,
3310 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3311 forEach(nestedNameSpecifier().bind("x"))),
3312 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3313}
3314
3315TEST(NNS, NestedNameSpecifiersAsDescendants) {
3316 std::string Fragment =
3317 "namespace a { struct A { struct B { struct C {}; }; }; };"
3318 "void f() { a::A::B::C c; }";
3319 EXPECT_TRUE(matches(
3320 Fragment,
3321 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3322 asString("struct a::A")))))));
3323 EXPECT_TRUE(matchAndVerifyResultTrue(
3324 Fragment,
3325 functionDecl(hasName("f"),
3326 forEachDescendant(nestedNameSpecifier().bind("x"))),
3327 // Nested names: a, a::A and a::A::B.
3328 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3329}
3330
3331TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3332 std::string Fragment =
3333 "namespace a { struct A { struct B { struct C {}; }; }; };"
3334 "void f() { a::A::B::C c; }";
3335 EXPECT_TRUE(matches(
3336 Fragment,
3337 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3338 hasDescendant(loc(nestedNameSpecifier(
3339 specifiesNamespace(hasName("a"))))))));
3340 EXPECT_TRUE(notMatches(
3341 Fragment,
3342 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3343 has(loc(nestedNameSpecifier(
3344 specifiesNamespace(hasName("a"))))))));
3345 EXPECT_TRUE(matches(
3346 Fragment,
3347 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3348 has(loc(nestedNameSpecifier(
3349 specifiesNamespace(hasName("a"))))))));
3350
3351 EXPECT_TRUE(matchAndVerifyResultTrue(
3352 Fragment,
3353 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3354 forEach(nestedNameSpecifierLoc().bind("x"))),
3355 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3356}
3357
3358TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3359 std::string Fragment =
3360 "namespace a { struct A { struct B { struct C {}; }; }; };"
3361 "void f() { a::A::B::C c; }";
3362 EXPECT_TRUE(matches(
3363 Fragment,
3364 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3365 asString("struct a::A"))))))));
3366 EXPECT_TRUE(matchAndVerifyResultTrue(
3367 Fragment,
3368 functionDecl(hasName("f"),
3369 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3370 // Nested names: a, a::A and a::A::B.
3371 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3372}
3373
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003374template <typename T>
3375class VerifyRecursiveMatch : public BoundNodesCallback {
3376public:
3377 explicit VerifyRecursiveMatch(StringRef Id,
3378 const internal::Matcher<T> &InnerMatcher)
3379 : Id(Id), InnerMatcher(InnerMatcher) {}
Daniel Jasper452abbc2012-10-29 10:48:25 +00003380
3381 virtual bool run(const BoundNodes *Nodes) {
3382 return false;
3383 }
3384
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003385 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3386 const T *Node = Nodes->getNodeAs<T>(Id);
3387 bool Found = false;
3388 MatchFinder Finder;
3389 Finder.addMatcher(InnerMatcher, new VerifyMatch(0, &Found));
3390 Finder.findAll(*Node, *Context);
3391 return Found;
3392 }
3393private:
3394 std::string Id;
3395 internal::Matcher<T> InnerMatcher;
3396};
3397
3398TEST(MatchFinder, CanMatchDeclarationsRecursively) {
3399 EXPECT_TRUE(matchAndVerifyResultTrue("class X { class Y {}; };",
3400 recordDecl(hasName("::X")).bind("X"),
3401 new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Y")))));
3402 EXPECT_TRUE(matchAndVerifyResultFalse("class X { class Y {}; };",
3403 recordDecl(hasName("::X")).bind("X"),
3404 new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Z")))));
3405}
3406
3407TEST(MatchFinder, CanMatchStatementsRecursively) {
3408 EXPECT_TRUE(matchAndVerifyResultTrue("void f() { if (1) { for (;;) { } } }",
3409 ifStmt().bind("if"),
3410 new VerifyRecursiveMatch<clang::Stmt>("if", forStmt())));
3411 EXPECT_TRUE(matchAndVerifyResultFalse("void f() { if (1) { for (;;) { } } }",
3412 ifStmt().bind("if"),
3413 new VerifyRecursiveMatch<clang::Stmt>("if", declStmt())));
3414}
3415
Manuel Klimeke5793282012-11-02 01:31:03 +00003416class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
3417public:
3418 VerifyStartOfTranslationUnit() : Called(false) {}
3419 virtual void run(const MatchFinder::MatchResult &Result) {
3420 EXPECT_TRUE(Called);
3421 }
3422 virtual void onStartOfTranslationUnit() {
3423 Called = true;
3424 }
3425 bool Called;
3426};
3427
3428TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
3429 MatchFinder Finder;
3430 VerifyStartOfTranslationUnit VerifyCallback;
3431 Finder.addMatcher(decl(), &VerifyCallback);
3432 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
3433 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
3434 EXPECT_TRUE(VerifyCallback.Called);
3435}
3436
Manuel Klimek4da21662012-07-06 05:48:52 +00003437} // end namespace ast_matchers
3438} // end namespace clang