blob: ec4312e52e0f57bd58f5831d46b9dcfd55a33572 [file] [log] [blame]
Manuel Klimek4da21662012-07-06 05:48:52 +00001//===- unittest/Tooling/ASTMatchersTest.cpp - AST matcher unit tests ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ASTMatchersTest.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Tooling/Tooling.h"
14#include "gtest/gtest.h"
15
16namespace clang {
17namespace ast_matchers {
18
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000019#if GTEST_HAS_DEATH_TEST
Manuel Klimek4da21662012-07-06 05:48:52 +000020TEST(HasNameDeathTest, DiesOnEmptyName) {
21 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000022 DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000023 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
24 }, "");
25}
26
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000027TEST(HasNameDeathTest, DiesOnEmptyPattern) {
28 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000029 DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000030 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
31 }, "");
32}
33
Manuel Klimek4da21662012-07-06 05:48:52 +000034TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
35 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000036 DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000037 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
38 }, "");
39}
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000040#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000041
Manuel Klimek715c9562012-07-25 10:02:02 +000042TEST(Decl, MatchesDeclarations) {
43 EXPECT_TRUE(notMatches("", decl(usingDecl())));
44 EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
45 decl(usingDecl())));
46}
47
Manuel Klimek4da21662012-07-06 05:48:52 +000048TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000049 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +000050 EXPECT_TRUE(matches("typedef int X;", NamedX));
51 EXPECT_TRUE(matches("int X;", NamedX));
52 EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
53 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
54 EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
55 EXPECT_TRUE(matches("namespace X { }", NamedX));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000056 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek4da21662012-07-06 05:48:52 +000057
58 EXPECT_TRUE(notMatches("#define X 1", NamedX));
59}
60
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000061TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000062 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000063 EXPECT_TRUE(matches("typedef int Xa;", NamedX));
64 EXPECT_TRUE(matches("int Xb;", NamedX));
65 EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
66 EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
67 EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
68 EXPECT_TRUE(matches("namespace Xij { }", NamedX));
69 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
70
71 EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
72
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000073 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000074 EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
75 EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
76
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000077 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000078 EXPECT_TRUE(matches("int abc;", Abc));
79 EXPECT_TRUE(matches("int aFOObBARc;", Abc));
80 EXPECT_TRUE(notMatches("int cab;", Abc));
81 EXPECT_TRUE(matches("int cabc;", Abc));
82}
83
Manuel Klimek4da21662012-07-06 05:48:52 +000084TEST(DeclarationMatcher, MatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000085 DeclarationMatcher ClassMatcher(recordDecl());
Manuel Klimeke265c872012-07-10 14:21:30 +000086#if !defined(_MSC_VER)
Manuel Klimek4da21662012-07-06 05:48:52 +000087 EXPECT_FALSE(matches("", ClassMatcher));
Manuel Klimeke265c872012-07-10 14:21:30 +000088#else
89 // Matches class type_info.
90 EXPECT_TRUE(matches("", ClassMatcher));
91#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000092
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000093 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +000094 EXPECT_TRUE(matches("class X;", ClassX));
95 EXPECT_TRUE(matches("class X {};", ClassX));
96 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
97 EXPECT_TRUE(notMatches("", ClassX));
98}
99
100TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000101 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000102
103 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000104 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
105 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek4da21662012-07-06 05:48:52 +0000106 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
107 EXPECT_TRUE(notMatches("", IsDerivedFromX));
108
Daniel Jasper63d88722012-09-12 21:14:15 +0000109 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000110
111 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
112 EXPECT_TRUE(matches("class X {};", IsAX));
113 EXPECT_TRUE(matches("class X;", IsAX));
114 EXPECT_TRUE(notMatches("class Y;", IsAX));
115 EXPECT_TRUE(notMatches("", IsAX));
116
Manuel Klimek4da21662012-07-06 05:48:52 +0000117 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000118 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000119 EXPECT_TRUE(
120 matches("class X {}; class Y : public X {}; class Z : public Y {};",
121 ZIsDerivedFromX));
122 EXPECT_TRUE(
123 matches("class X {};"
124 "template<class T> class Y : public X {};"
125 "class Z : public Y<int> {};", ZIsDerivedFromX));
126 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
127 ZIsDerivedFromX));
128 EXPECT_TRUE(
129 matches("template<class T> class X {}; "
130 "template<class T> class Z : public X<T> {};",
131 ZIsDerivedFromX));
132 EXPECT_TRUE(
133 matches("template<class T, class U=T> class X {}; "
134 "template<class T> class Z : public X<T> {};",
135 ZIsDerivedFromX));
136 EXPECT_TRUE(
137 notMatches("template<class X> class A { class Z : public X {}; };",
138 ZIsDerivedFromX));
139 EXPECT_TRUE(
140 matches("template<class X> class A { public: class Z : public X {}; }; "
141 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
142 EXPECT_TRUE(
143 matches("template <class T> class X {}; "
144 "template<class Y> class A { class Z : public X<Y> {}; };",
145 ZIsDerivedFromX));
146 EXPECT_TRUE(
147 notMatches("template<template<class T> class X> class A { "
148 " class Z : public X<int> {}; };", ZIsDerivedFromX));
149 EXPECT_TRUE(
150 matches("template<template<class T> class X> class A { "
151 " public: class Z : public X<int> {}; }; "
152 "template<class T> class X {}; void y() { A<X>::Z z; }",
153 ZIsDerivedFromX));
154 EXPECT_TRUE(
155 notMatches("template<class X> class A { class Z : public X::D {}; };",
156 ZIsDerivedFromX));
157 EXPECT_TRUE(
158 matches("template<class X> class A { public: "
159 " class Z : public X::D {}; }; "
160 "class Y { public: class X {}; typedef X D; }; "
161 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
162 EXPECT_TRUE(
163 matches("class X {}; typedef X Y; class Z : public Y {};",
164 ZIsDerivedFromX));
165 EXPECT_TRUE(
166 matches("template<class T> class Y { typedef typename T::U X; "
167 " class Z : public X {}; };", ZIsDerivedFromX));
168 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
169 ZIsDerivedFromX));
170 EXPECT_TRUE(
171 notMatches("template<class T> class X {}; "
172 "template<class T> class A { class Z : public X<T>::D {}; };",
173 ZIsDerivedFromX));
174 EXPECT_TRUE(
175 matches("template<class T> class X { public: typedef X<T> D; }; "
176 "template<class T> class A { public: "
177 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
178 ZIsDerivedFromX));
179 EXPECT_TRUE(
180 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
181 ZIsDerivedFromX));
182 EXPECT_TRUE(
183 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
184 ZIsDerivedFromX));
185 EXPECT_TRUE(
186 matches("class X {}; class Y : public X {}; "
187 "typedef Y V; typedef V W; class Z : public W {};",
188 ZIsDerivedFromX));
189 EXPECT_TRUE(
190 matches("template<class T, class U> class X {}; "
191 "template<class T> class A { class Z : public X<T, int> {}; };",
192 ZIsDerivedFromX));
193 EXPECT_TRUE(
194 notMatches("template<class X> class D { typedef X A; typedef A B; "
195 " typedef B C; class Z : public C {}; };",
196 ZIsDerivedFromX));
197 EXPECT_TRUE(
198 matches("class X {}; typedef X A; typedef A B; "
199 "class Z : public B {};", ZIsDerivedFromX));
200 EXPECT_TRUE(
201 matches("class X {}; typedef X A; typedef A B; typedef B C; "
202 "class Z : public C {};", ZIsDerivedFromX));
203 EXPECT_TRUE(
204 matches("class U {}; typedef U X; typedef X V; "
205 "class Z : public V {};", ZIsDerivedFromX));
206 EXPECT_TRUE(
207 matches("class Base {}; typedef Base X; "
208 "class Z : public Base {};", ZIsDerivedFromX));
209 EXPECT_TRUE(
210 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
211 "class Z : public Base {};", ZIsDerivedFromX));
212 EXPECT_TRUE(
213 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
214 "class Z : public Base {};", ZIsDerivedFromX));
215 EXPECT_TRUE(
216 matches("class A {}; typedef A X; typedef A Y; "
217 "class Z : public Y {};", ZIsDerivedFromX));
218 EXPECT_TRUE(
219 notMatches("template <typename T> class Z;"
220 "template <> class Z<void> {};"
221 "template <typename T> class Z : public Z<void> {};",
222 IsDerivedFromX));
223 EXPECT_TRUE(
224 matches("template <typename T> class X;"
225 "template <> class X<void> {};"
226 "template <typename T> class X : public X<void> {};",
227 IsDerivedFromX));
228 EXPECT_TRUE(matches(
229 "class X {};"
230 "template <typename T> class Z;"
231 "template <> class Z<void> {};"
232 "template <typename T> class Z : public Z<void>, public X {};",
233 ZIsDerivedFromX));
234
235 // FIXME: Once we have better matchers for template type matching,
236 // get rid of the Variable(...) matching and match the right template
237 // declarations directly.
238 const char *RecursiveTemplateOneParameter =
239 "class Base1 {}; class Base2 {};"
240 "template <typename T> class Z;"
241 "template <> class Z<void> : public Base1 {};"
242 "template <> class Z<int> : public Base2 {};"
243 "template <> class Z<float> : public Z<void> {};"
244 "template <> class Z<double> : public Z<int> {};"
245 "template <typename T> class Z : public Z<float>, public Z<double> {};"
246 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
247 EXPECT_TRUE(matches(
248 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000249 varDecl(hasName("z_float"),
250 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000251 EXPECT_TRUE(notMatches(
252 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000253 varDecl(hasName("z_float"),
254 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000255 EXPECT_TRUE(matches(
256 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000257 varDecl(hasName("z_char"),
258 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
259 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000260
261 const char *RecursiveTemplateTwoParameters =
262 "class Base1 {}; class Base2 {};"
263 "template <typename T1, typename T2> class Z;"
264 "template <typename T> class Z<void, T> : public Base1 {};"
265 "template <typename T> class Z<int, T> : public Base2 {};"
266 "template <typename T> class Z<float, T> : public Z<void, T> {};"
267 "template <typename T> class Z<double, T> : public Z<int, T> {};"
268 "template <typename T1, typename T2> class Z : "
269 " public Z<float, T2>, public Z<double, T2> {};"
270 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
271 " Z<char, void> z_char; }";
272 EXPECT_TRUE(matches(
273 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000274 varDecl(hasName("z_float"),
275 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000276 EXPECT_TRUE(notMatches(
277 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000278 varDecl(hasName("z_float"),
279 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000280 EXPECT_TRUE(matches(
281 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000282 varDecl(hasName("z_char"),
283 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
284 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000285 EXPECT_TRUE(matches(
286 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000287 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000288 EXPECT_TRUE(notMatches(
289 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000290 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000291
292 EXPECT_TRUE(matches(
293 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000294 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000295}
296
Daniel Jasper08f0c532012-09-18 14:17:42 +0000297TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
298 EXPECT_TRUE(matches(
299 "template <typename T> struct A {"
300 " template <typename T2> struct F {};"
301 "};"
302 "template <typename T> struct B : A<T>::template F<T> {};"
303 "B<int> b;",
304 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
305}
306
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000307TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000308 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000309 EXPECT_TRUE(notMatches("class X;", ClassX));
310 EXPECT_TRUE(notMatches("class X {};", ClassX));
311}
312
313TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000314 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000315 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
316 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
317}
318
319TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
320 EXPECT_TRUE(notMatches("template<typename T> class X { };"
321 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000322 classTemplateDecl(hasName("X"),
323 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000324}
325
326TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
327 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
328 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000329 classTemplateDecl(hasName("X"),
330 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000331}
332
Daniel Jasper6a124492012-07-12 08:50:38 +0000333TEST(AllOf, AllOverloadsWork) {
334 const char Program[] =
335 "struct T { }; int f(int, T*); void g(int x) { T t; f(x, &t); }";
336 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000337 callExpr(allOf(callee(functionDecl(hasName("f"))),
338 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000339 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000340 callExpr(allOf(callee(functionDecl(hasName("f"))),
341 hasArgument(0, declRefExpr(to(varDecl()))),
342 hasArgument(1, hasType(pointsTo(
343 recordDecl(hasName("T")))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000344}
345
Manuel Klimek4da21662012-07-06 05:48:52 +0000346TEST(DeclarationMatcher, MatchAnyOf) {
347 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000348 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000349 EXPECT_TRUE(
350 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
351 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
352 EXPECT_TRUE(
353 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
354 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
355
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000356 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000357 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000358 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
359 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
360
Manuel Klimek4da21662012-07-06 05:48:52 +0000361 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000362 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
363 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000364 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
365 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
366 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
367 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
368 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
369 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
370}
371
372TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000373 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000374 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
375 EXPECT_TRUE(matches("class X {};", HasClassX));
376
377 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000378 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000379 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
380 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
381 EXPECT_TRUE(
382 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
383}
384
385TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
386 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000387 recordDecl(
388 has(recordDecl(
389 has(recordDecl(hasName("X"))),
390 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000391 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000392 has(recordDecl(
393 has(recordDecl(hasName("A"))),
394 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000395 hasName("C"))),
396 hasName("F"));
397
398 EXPECT_TRUE(matches(
399 "class F {"
400 " class Z {"
401 " class X {};"
402 " class Y {};"
403 " };"
404 " class C {"
405 " class A {};"
406 " class B {};"
407 " };"
408 "};", Recursive));
409
410 EXPECT_TRUE(matches(
411 "class F {"
412 " class Z {"
413 " class A {};"
414 " class X {};"
415 " class Y {};"
416 " };"
417 " class C {"
418 " class X {};"
419 " class A {};"
420 " class B {};"
421 " };"
422 "};", Recursive));
423
424 EXPECT_TRUE(matches(
425 "class O1 {"
426 " class O2 {"
427 " class F {"
428 " class Z {"
429 " class A {};"
430 " class X {};"
431 " class Y {};"
432 " };"
433 " class C {"
434 " class X {};"
435 " class A {};"
436 " class B {};"
437 " };"
438 " };"
439 " };"
440 "};", Recursive));
441}
442
443TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
444 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000445 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000446 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000447 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000448 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000449 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000450 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000451 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000452 hasName("Y"))),
453 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000454 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000455 anyOf(
456 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000457 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000458 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000459 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000460 hasName("B")))))),
461 hasName("F")));
462
463 EXPECT_TRUE(matches("class F {};", Recursive));
464 EXPECT_TRUE(matches("class Z {};", Recursive));
465 EXPECT_TRUE(matches("class C {};", Recursive));
466 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
467 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
468 EXPECT_TRUE(
469 matches("class O1 { class O2 {"
470 " class M { class N { class B {}; }; }; "
471 "}; };", Recursive));
472}
473
474TEST(DeclarationMatcher, MatchNot) {
475 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000476 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000477 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000478 unless(hasName("X")));
479 EXPECT_TRUE(notMatches("", NotClassX));
480 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
481 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
482 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
483 EXPECT_TRUE(
484 notMatches("class Y {}; class Z {}; class X : public Y {};",
485 NotClassX));
486
487 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000488 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000489 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000490 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000491 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000492 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000493 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
494 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
495 ClassXHasNotClassY));
496}
497
498TEST(DeclarationMatcher, HasDescendant) {
499 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000500 recordDecl(
501 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000502 hasName("Z"));
503 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
504 EXPECT_TRUE(
505 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
506 EXPECT_TRUE(
507 matches("class Z { class A { class Y { class X {}; }; }; };",
508 ZDescendantClassX));
509 EXPECT_TRUE(
510 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
511 ZDescendantClassX));
512 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
513
514 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000515 recordDecl(
516 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000517 hasName("X"))),
518 hasName("Z"));
519 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
520 ZDescendantClassXHasClassY));
521 EXPECT_TRUE(
522 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
523 ZDescendantClassXHasClassY));
524 EXPECT_TRUE(notMatches(
525 "class Z {"
526 " class A {"
527 " class B {"
528 " class X {"
529 " class C {"
530 " class Y {};"
531 " };"
532 " };"
533 " }; "
534 " };"
535 "};", ZDescendantClassXHasClassY));
536
537 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000538 recordDecl(
539 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
540 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000541 hasName("Z"));
542 EXPECT_TRUE(
543 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
544 ZDescendantClassXDescendantClassY));
545 EXPECT_TRUE(matches(
546 "class Z {"
547 " class A {"
548 " class X {"
549 " class B {"
550 " class Y {};"
551 " };"
552 " class Y {};"
553 " };"
554 " };"
555 "};", ZDescendantClassXDescendantClassY));
556}
557
Daniel Jaspera267cf62012-10-29 10:14:44 +0000558// Implements a run method that returns whether BoundNodes contains a
559// Decl bound to Id that can be dynamically cast to T.
560// Optionally checks that the check succeeded a specific number of times.
561template <typename T>
562class VerifyIdIsBoundTo : public BoundNodesCallback {
563public:
564 // Create an object that checks that a node of type \c T was bound to \c Id.
565 // Does not check for a certain number of matches.
566 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
567 : Id(Id), ExpectedCount(-1), Count(0) {}
568
569 // Create an object that checks that a node of type \c T was bound to \c Id.
570 // Checks that there were exactly \c ExpectedCount matches.
571 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
572 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
573
574 // Create an object that checks that a node of type \c T was bound to \c Id.
575 // Checks that there was exactly one match with the name \c ExpectedName.
576 // Note that \c T must be a NamedDecl for this to work.
577 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName)
578 : Id(Id), ExpectedCount(1), Count(0), ExpectedName(ExpectedName) {}
579
580 ~VerifyIdIsBoundTo() {
581 if (ExpectedCount != -1)
582 EXPECT_EQ(ExpectedCount, Count);
583 if (!ExpectedName.empty())
584 EXPECT_EQ(ExpectedName, Name);
585 }
586
587 virtual bool run(const BoundNodes *Nodes) {
588 if (Nodes->getNodeAs<T>(Id)) {
589 ++Count;
590 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
591 Name = Named->getNameAsString();
592 } else if (const NestedNameSpecifier *NNS =
593 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
594 llvm::raw_string_ostream OS(Name);
595 NNS->print(OS, PrintingPolicy(LangOptions()));
596 }
597 return true;
598 }
599 return false;
600 }
601
Daniel Jasper452abbc2012-10-29 10:48:25 +0000602 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
603 return run(Nodes);
604 }
605
Daniel Jaspera267cf62012-10-29 10:14:44 +0000606private:
607 const std::string Id;
608 const int ExpectedCount;
609 int Count;
610 const std::string ExpectedName;
611 std::string Name;
612};
613
614TEST(HasDescendant, MatchesDescendantTypes) {
615 EXPECT_TRUE(matches("void f() { int i = 3; }",
616 decl(hasDescendant(loc(builtinType())))));
617 EXPECT_TRUE(matches("void f() { int i = 3; }",
618 stmt(hasDescendant(builtinType()))));
619
620 EXPECT_TRUE(matches("void f() { int i = 3; }",
621 stmt(hasDescendant(loc(builtinType())))));
622 EXPECT_TRUE(matches("void f() { int i = 3; }",
623 stmt(hasDescendant(qualType(builtinType())))));
624
625 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
626 stmt(hasDescendant(isInteger()))));
627
628 EXPECT_TRUE(matchAndVerifyResultTrue(
629 "void f() { int a; float c; int d; int e; }",
630 functionDecl(forEachDescendant(
631 varDecl(hasDescendant(isInteger())).bind("x"))),
632 new VerifyIdIsBoundTo<Decl>("x", 3)));
633}
634
635TEST(HasDescendant, MatchesDescendantsOfTypes) {
636 EXPECT_TRUE(matches("void f() { int*** i; }",
637 qualType(hasDescendant(builtinType()))));
638 EXPECT_TRUE(matches("void f() { int*** i; }",
639 qualType(hasDescendant(
640 pointerType(pointee(builtinType()))))));
641 EXPECT_TRUE(matches("void f() { int*** i; }",
642 typeLoc(hasDescendant(builtinTypeLoc()))));
643
644 EXPECT_TRUE(matchAndVerifyResultTrue(
645 "void f() { int*** i; }",
646 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
647 new VerifyIdIsBoundTo<Type>("x", 2)));
648}
649
650TEST(Has, MatchesChildrenOfTypes) {
651 EXPECT_TRUE(matches("int i;",
652 varDecl(hasName("i"), has(isInteger()))));
653 EXPECT_TRUE(notMatches("int** i;",
654 varDecl(hasName("i"), has(isInteger()))));
655 EXPECT_TRUE(matchAndVerifyResultTrue(
656 "int (*f)(float, int);",
657 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
658 new VerifyIdIsBoundTo<QualType>("x", 2)));
659}
660
661TEST(Has, MatchesChildTypes) {
662 EXPECT_TRUE(matches(
663 "int* i;",
664 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
665 EXPECT_TRUE(notMatches(
666 "int* i;",
667 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
668}
669
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000670TEST(Enum, DoesNotMatchClasses) {
671 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
672}
673
674TEST(Enum, MatchesEnums) {
675 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
676}
677
678TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000679 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000680 EXPECT_TRUE(matches("enum X{ A };", Matcher));
681 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
682 EXPECT_TRUE(notMatches("enum X {};", Matcher));
683}
684
Manuel Klimek4da21662012-07-06 05:48:52 +0000685TEST(StatementMatcher, Has) {
686 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000687 expr(hasType(pointsTo(recordDecl(hasName("X")))),
688 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000689
690 EXPECT_TRUE(matches(
691 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
692 EXPECT_TRUE(notMatches(
693 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
694}
695
696TEST(StatementMatcher, HasDescendant) {
697 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000698 expr(hasType(pointsTo(recordDecl(hasName("X")))),
699 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000700
701 EXPECT_TRUE(matches(
702 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
703 HasDescendantVariableI));
704 EXPECT_TRUE(notMatches(
705 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
706 HasDescendantVariableI));
707}
708
709TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000710 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000711
712 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
713 EXPECT_TRUE(notMatches("class A {};", TypeA));
714
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000715 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000716
717 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
718 TypeDerivedFromA));
719 EXPECT_TRUE(notMatches("class A {};", TypeA));
720
721 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000722 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000723
724 EXPECT_TRUE(
725 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
726}
727
Manuel Klimek4da21662012-07-06 05:48:52 +0000728TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000729 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000730
731 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000732 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000733
734 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000735 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000736
737 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000738 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000739
740 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
741 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000742 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000743
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000744 StatementMatcher MethodX =
745 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000746
747 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
748 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000749 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000750}
751
752TEST(Matcher, BindTheSameNameInAlternatives) {
753 StatementMatcher matcher = anyOf(
754 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000755 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000756 hasRHS(integerLiteral(equals(0)))),
757 binaryOperator(hasOperatorName("+"),
758 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000759 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000760
761 EXPECT_TRUE(matchAndVerifyResultTrue(
762 // The first branch of the matcher binds x to 0 but then fails.
763 // The second branch binds x to f() and succeeds.
764 "int f() { return 0 + f(); }",
765 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000766 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000767}
768
Manuel Klimek66341c52012-08-30 19:41:06 +0000769TEST(Matcher, BindsIDForMemoizedResults) {
770 // Using the same matcher in two match expressions will make memoization
771 // kick in.
772 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
773 EXPECT_TRUE(matchAndVerifyResultTrue(
774 "class A { class B { class X {}; }; };",
775 DeclarationMatcher(anyOf(
776 recordDecl(hasName("A"), hasDescendant(ClassX)),
777 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000778 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000779}
780
Manuel Klimek4da21662012-07-06 05:48:52 +0000781TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000782 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000783 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000784 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000785 EXPECT_TRUE(
786 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000787 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000788 EXPECT_TRUE(
789 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000790 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000791}
792
793TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000794 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000795 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000796 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000797 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000798 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000799 EXPECT_TRUE(
800 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000801 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000802}
803
804TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000805 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000806 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000807 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000808 EXPECT_TRUE(
809 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000810 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000811}
812
813TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000814 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000815 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000816 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000817 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000818 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000819}
820
821TEST(Matcher, Call) {
822 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000823 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000824 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000825
826 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
827 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
828
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000829 StatementMatcher MethodOnY =
830 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000831
832 EXPECT_TRUE(
833 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
834 MethodOnY));
835 EXPECT_TRUE(
836 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
837 MethodOnY));
838 EXPECT_TRUE(
839 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
840 MethodOnY));
841 EXPECT_TRUE(
842 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
843 MethodOnY));
844 EXPECT_TRUE(
845 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
846 MethodOnY));
847
848 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000849 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000850
851 EXPECT_TRUE(
852 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
853 MethodOnYPointer));
854 EXPECT_TRUE(
855 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
856 MethodOnYPointer));
857 EXPECT_TRUE(
858 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
859 MethodOnYPointer));
860 EXPECT_TRUE(
861 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
862 MethodOnYPointer));
863 EXPECT_TRUE(
864 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
865 MethodOnYPointer));
866}
867
Daniel Jasper31f7c082012-10-01 13:40:41 +0000868TEST(Matcher, Lambda) {
869 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
870 lambdaExpr()));
871}
872
873TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000874 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
875 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000876 forRangeStmt()));
877 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
878 forRangeStmt()));
879}
880
881TEST(Matcher, UserDefinedLiteral) {
882 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
883 " return i + 1;"
884 "}"
885 "char c = 'a'_inc;",
886 userDefinedLiteral()));
887}
888
Daniel Jasperb54b7642012-09-20 14:12:57 +0000889TEST(Matcher, FlowControl) {
890 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
891 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
892 continueStmt()));
893 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
894 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
895 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
896}
897
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000898TEST(HasType, MatchesAsString) {
899 EXPECT_TRUE(
900 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000901 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000902 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000903 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000904 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000905 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000906 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000907 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000908}
909
Manuel Klimek4da21662012-07-06 05:48:52 +0000910TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000911 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000912 // Unary operator
913 EXPECT_TRUE(matches("class Y { }; "
914 "bool operator!(Y x) { return false; }; "
915 "Y y; bool c = !y;", OpCall));
916 // No match -- special operators like "new", "delete"
917 // FIXME: operator new takes size_t, for which we need stddef.h, for which
918 // we need to figure out include paths in the test.
919 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
920 // "class Y { }; "
921 // "void *operator new(size_t size) { return 0; } "
922 // "Y *y = new Y;", OpCall));
923 EXPECT_TRUE(notMatches("class Y { }; "
924 "void operator delete(void *p) { } "
925 "void a() {Y *y = new Y; delete y;}", OpCall));
926 // Binary operator
927 EXPECT_TRUE(matches("class Y { }; "
928 "bool operator&&(Y x, Y y) { return true; }; "
929 "Y a; Y b; bool c = a && b;",
930 OpCall));
931 // No match -- normal operator, not an overloaded one.
932 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
933 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
934}
935
936TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
937 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000938 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000939 EXPECT_TRUE(matches("class Y { }; "
940 "bool operator&&(Y x, Y y) { return true; }; "
941 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
942 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000943 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000944 EXPECT_TRUE(notMatches("class Y { }; "
945 "bool operator&&(Y x, Y y) { return true; }; "
946 "Y a; Y b; bool c = a && b;",
947 OpCallLessLess));
948}
949
950TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +0000951 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000952 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000953
954 EXPECT_TRUE(
955 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
956 MethodOnY));
957 EXPECT_TRUE(
958 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
959 MethodOnY));
960 EXPECT_TRUE(
961 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
962 MethodOnY));
963 EXPECT_TRUE(
964 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
965 MethodOnY));
966 EXPECT_TRUE(
967 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
968 MethodOnY));
969
970 EXPECT_TRUE(matches(
971 "class Y {"
972 " public: virtual void x();"
973 "};"
974 "class X : public Y {"
975 " public: virtual void x();"
976 "};"
977 "void z() { X *x; x->Y::x(); }", MethodOnY));
978}
979
980TEST(Matcher, VariableUsage) {
981 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000982 declRefExpr(to(
983 varDecl(hasInitializer(
984 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000985
986 EXPECT_TRUE(matches(
987 "class Y {"
988 " public:"
989 " bool x() const;"
990 "};"
991 "void z(const Y &y) {"
992 " bool b = y.x();"
993 " if (b) {}"
994 "}", Reference));
995
996 EXPECT_TRUE(notMatches(
997 "class Y {"
998 " public:"
999 " bool x() const;"
1000 "};"
1001 "void z(const Y &y) {"
1002 " bool b = y.x();"
1003 "}", Reference));
1004}
1005
Daniel Jasper9bd28092012-07-30 05:03:25 +00001006TEST(Matcher, FindsVarDeclInFuncitonParameter) {
1007 EXPECT_TRUE(matches(
1008 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001009 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001010}
1011
Manuel Klimek4da21662012-07-06 05:48:52 +00001012TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001013 StatementMatcher CallOnVariableY =
1014 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001015
1016 EXPECT_TRUE(matches(
1017 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1018 EXPECT_TRUE(matches(
1019 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1020 EXPECT_TRUE(matches(
1021 "class Y { public: void x(); };"
1022 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1023 EXPECT_TRUE(matches(
1024 "class Y { public: void x(); };"
1025 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1026 EXPECT_TRUE(notMatches(
1027 "class Y { public: void x(); };"
1028 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1029 CallOnVariableY));
1030}
1031
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001032TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1033 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1034 unaryExprOrTypeTraitExpr()));
1035 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1036 alignOfExpr(anything())));
1037 // FIXME: Uncomment once alignof is enabled.
1038 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1039 // unaryExprOrTypeTraitExpr()));
1040 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1041 // sizeOfExpr()));
1042}
1043
1044TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1045 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1046 hasArgumentOfType(asString("int")))));
1047 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1048 hasArgumentOfType(asString("float")))));
1049 EXPECT_TRUE(matches(
1050 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001051 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001052 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001053 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001054}
1055
Manuel Klimek4da21662012-07-06 05:48:52 +00001056TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001057 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001058}
1059
1060TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001061 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001062}
1063
1064TEST(MemberExpression, MatchesVariable) {
1065 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001066 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001067 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001068 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001069 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001070 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001071}
1072
1073TEST(MemberExpression, MatchesStaticVariable) {
1074 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001075 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001076 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001077 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001078 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001079 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001080}
1081
Daniel Jasper6a124492012-07-12 08:50:38 +00001082TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001083 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1084 EXPECT_TRUE(matches(
1085 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1086 callExpr(hasArgument(0, declRefExpr(
1087 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001088}
1089
1090TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001091 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001092 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001093 callExpr(hasArgument(0, declRefExpr(
1094 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001095}
1096
Manuel Klimek4da21662012-07-06 05:48:52 +00001097TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1098 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001099 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001100 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001101 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001102 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001103 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001104}
1105
1106TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1107 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001108 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001109 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001110 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001111 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001112 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001113}
1114
1115TEST(IsArrow, MatchesMemberCallsViaArrow) {
1116 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001117 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001118 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001119 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001120 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001121 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001122}
1123
1124TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001125 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001126
1127 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1128 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1129}
1130
1131TEST(Callee, MatchesMemberExpressions) {
1132 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001133 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001134 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001135 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001136}
1137
1138TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001139 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001140
1141 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1142 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1143
Manuel Klimeke265c872012-07-10 14:21:30 +00001144#if !defined(_MSC_VER)
1145 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001146 // Dependent contexts, but a non-dependent call.
1147 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1148 CallFunctionF));
1149 EXPECT_TRUE(
1150 matches("void f(); template <int N> struct S { void g() { f(); } };",
1151 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001152#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001153
1154 // Depedent calls don't match.
1155 EXPECT_TRUE(
1156 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1157 CallFunctionF));
1158 EXPECT_TRUE(
1159 notMatches("void f(int);"
1160 "template <typename T> struct S { void g(T t) { f(t); } };",
1161 CallFunctionF));
1162}
1163
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001164TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1165 EXPECT_TRUE(
1166 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001167 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001168}
1169
1170TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1171 EXPECT_TRUE(
1172 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001173 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001174}
1175
1176TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1177 EXPECT_TRUE(
1178 notMatches("void g(); template <typename T> void f(T t) {}"
1179 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001180 functionTemplateDecl(hasName("f"),
1181 hasDescendant(declRefExpr(to(
1182 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001183}
1184
Manuel Klimek4da21662012-07-06 05:48:52 +00001185TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001186 StatementMatcher CallArgumentY = callExpr(
1187 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001188
1189 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1190 EXPECT_TRUE(
1191 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1192 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1193
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001194 StatementMatcher WrongIndex = callExpr(
1195 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001196 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1197}
1198
1199TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001200 StatementMatcher CallArgumentY = callExpr(
1201 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001202 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1203 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1204 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1205}
1206
1207TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001208 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001209
1210 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1211 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1212 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1213}
1214
1215TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001216 DeclarationMatcher ReferenceClassX = varDecl(
1217 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001218 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1219 ReferenceClassX));
1220 EXPECT_TRUE(
1221 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1222 EXPECT_TRUE(
1223 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1224 EXPECT_TRUE(
1225 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1226}
1227
1228TEST(HasParameter, CallsInnerMatcher) {
1229 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001230 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001231 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001232 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001233}
1234
1235TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1236 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001237 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001238}
1239
1240TEST(HasType, MatchesParameterVariableTypesStrictly) {
1241 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001242 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001243 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001244 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001245 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001246 methodDecl(hasParameter(0,
1247 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001248 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001249 methodDecl(hasParameter(0,
1250 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001251}
1252
1253TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1254 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001255 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001256 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001257 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001258}
1259
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001260TEST(Returns, MatchesReturnTypes) {
1261 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001262 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001263 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001264 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001265 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001266 functionDecl(returns(hasDeclaration(
1267 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001268}
1269
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001270TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001271 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1272 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1273 functionDecl(isExternC())));
1274 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001275}
1276
Manuel Klimek4da21662012-07-06 05:48:52 +00001277TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1278 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001279 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001280}
1281
1282TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1283 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001284 methodDecl(hasAnyParameter(hasType(pointsTo(
1285 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001286}
1287
1288TEST(HasName, MatchesParameterVariableDeclartions) {
1289 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001290 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001291 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001292 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001293}
1294
Daniel Jasper371f9392012-08-01 08:40:24 +00001295TEST(Matcher, MatchesClassTemplateSpecialization) {
1296 EXPECT_TRUE(matches("template<typename T> struct A {};"
1297 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001298 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001299 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001300 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001301 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001302 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001303}
1304
1305TEST(Matcher, MatchesTypeTemplateArgument) {
1306 EXPECT_TRUE(matches(
1307 "template<typename T> struct B {};"
1308 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001309 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001310 asString("int"))))));
1311}
1312
1313TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1314 EXPECT_TRUE(matches(
1315 "struct B { int next; };"
1316 "template<int(B::*next_ptr)> struct A {};"
1317 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001318 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1319 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001320
1321 EXPECT_TRUE(notMatches(
1322 "template <typename T> struct A {};"
1323 "A<int> a;",
1324 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1325 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001326}
1327
1328TEST(Matcher, MatchesSpecificArgument) {
1329 EXPECT_TRUE(matches(
1330 "template<typename T, typename U> class A {};"
1331 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001332 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001333 1, refersToType(asString("int"))))));
1334 EXPECT_TRUE(notMatches(
1335 "template<typename T, typename U> class A {};"
1336 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001337 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001338 1, refersToType(asString("int"))))));
1339}
1340
Manuel Klimek4da21662012-07-06 05:48:52 +00001341TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001342 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001343
1344 EXPECT_TRUE(
1345 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1346 EXPECT_TRUE(
1347 matches("class X { public: X(); }; void x() { X x = X(); }",
1348 Constructor));
1349 EXPECT_TRUE(
1350 matches("class X { public: X(int); }; void x() { X x = 0; }",
1351 Constructor));
1352 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1353}
1354
1355TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001356 StatementMatcher Constructor = constructExpr(
1357 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001358
1359 EXPECT_TRUE(
1360 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1361 Constructor));
1362 EXPECT_TRUE(
1363 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1364 Constructor));
1365 EXPECT_TRUE(
1366 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1367 Constructor));
1368 EXPECT_TRUE(
1369 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1370 Constructor));
1371
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001372 StatementMatcher WrongIndex = constructExpr(
1373 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001374 EXPECT_TRUE(
1375 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1376 WrongIndex));
1377}
1378
1379TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001380 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001381
1382 EXPECT_TRUE(
1383 matches("class X { public: X(int); }; void x() { X x(0); }",
1384 Constructor1Arg));
1385 EXPECT_TRUE(
1386 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1387 Constructor1Arg));
1388 EXPECT_TRUE(
1389 matches("class X { public: X(int); }; void x() { X x = 0; }",
1390 Constructor1Arg));
1391 EXPECT_TRUE(
1392 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1393 Constructor1Arg));
1394}
1395
Manuel Klimek70b9db92012-10-23 10:40:50 +00001396TEST(Matcher,ThisExpr) {
1397 EXPECT_TRUE(
1398 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1399 EXPECT_TRUE(
1400 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1401}
1402
Manuel Klimek4da21662012-07-06 05:48:52 +00001403TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001404 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001405
1406 std::string ClassString = "class string { public: string(); ~string(); }; ";
1407
1408 EXPECT_TRUE(
1409 matches(ClassString +
1410 "string GetStringByValue();"
1411 "void FunctionTakesString(string s);"
1412 "void run() { FunctionTakesString(GetStringByValue()); }",
1413 TempExpression));
1414
1415 EXPECT_TRUE(
1416 notMatches(ClassString +
1417 "string* GetStringPointer(); "
1418 "void FunctionTakesStringPtr(string* s);"
1419 "void run() {"
1420 " string* s = GetStringPointer();"
1421 " FunctionTakesStringPtr(GetStringPointer());"
1422 " FunctionTakesStringPtr(s);"
1423 "}",
1424 TempExpression));
1425
1426 EXPECT_TRUE(
1427 notMatches("class no_dtor {};"
1428 "no_dtor GetObjByValue();"
1429 "void ConsumeObj(no_dtor param);"
1430 "void run() { ConsumeObj(GetObjByValue()); }",
1431 TempExpression));
1432}
1433
Sam Panzere16acd32012-08-24 22:04:44 +00001434TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1435 std::string ClassString =
1436 "class string { public: string(); int length(); }; ";
1437
1438 EXPECT_TRUE(
1439 matches(ClassString +
1440 "string GetStringByValue();"
1441 "void FunctionTakesString(string s);"
1442 "void run() { FunctionTakesString(GetStringByValue()); }",
1443 materializeTemporaryExpr()));
1444
1445 EXPECT_TRUE(
1446 notMatches(ClassString +
1447 "string* GetStringPointer(); "
1448 "void FunctionTakesStringPtr(string* s);"
1449 "void run() {"
1450 " string* s = GetStringPointer();"
1451 " FunctionTakesStringPtr(GetStringPointer());"
1452 " FunctionTakesStringPtr(s);"
1453 "}",
1454 materializeTemporaryExpr()));
1455
1456 EXPECT_TRUE(
1457 notMatches(ClassString +
1458 "string GetStringByValue();"
1459 "void run() { int k = GetStringByValue().length(); }",
1460 materializeTemporaryExpr()));
1461
1462 EXPECT_TRUE(
1463 notMatches(ClassString +
1464 "string GetStringByValue();"
1465 "void run() { GetStringByValue(); }",
1466 materializeTemporaryExpr()));
1467}
1468
Manuel Klimek4da21662012-07-06 05:48:52 +00001469TEST(ConstructorDeclaration, SimpleCase) {
1470 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001471 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001472 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001473 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001474}
1475
1476TEST(ConstructorDeclaration, IsImplicit) {
1477 // This one doesn't match because the constructor is not added by the
1478 // compiler (it is not needed).
1479 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001480 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001481 // The compiler added the implicit default constructor.
1482 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001483 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001484 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001485 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001486}
1487
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001488TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1489 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001490 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001491}
1492
1493TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001494 EXPECT_TRUE(notMatches("class Foo {};",
1495 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001496}
1497
Manuel Klimek4da21662012-07-06 05:48:52 +00001498TEST(HasAnyConstructorInitializer, SimpleCase) {
1499 EXPECT_TRUE(notMatches(
1500 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001501 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001502 EXPECT_TRUE(matches(
1503 "class Foo {"
1504 " Foo() : foo_() { }"
1505 " int foo_;"
1506 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001507 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001508}
1509
1510TEST(HasAnyConstructorInitializer, ForField) {
1511 static const char Code[] =
1512 "class Baz { };"
1513 "class Foo {"
1514 " Foo() : foo_() { }"
1515 " Baz foo_;"
1516 " Baz bar_;"
1517 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001518 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1519 forField(hasType(recordDecl(hasName("Baz"))))))));
1520 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001521 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001522 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1523 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001524}
1525
1526TEST(HasAnyConstructorInitializer, WithInitializer) {
1527 static const char Code[] =
1528 "class Foo {"
1529 " Foo() : foo_(0) { }"
1530 " int foo_;"
1531 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001532 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001533 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001534 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001535 withInitializer(integerLiteral(equals(1)))))));
1536}
1537
1538TEST(HasAnyConstructorInitializer, IsWritten) {
1539 static const char Code[] =
1540 "struct Bar { Bar(){} };"
1541 "class Foo {"
1542 " Foo() : foo_() { }"
1543 " Bar foo_;"
1544 " Bar bar_;"
1545 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001546 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001547 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001548 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001549 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001550 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001551 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1552}
1553
1554TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001555 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001556
1557 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1558 EXPECT_TRUE(
1559 matches("class X { public: X(); }; void x() { new X(); }", New));
1560 EXPECT_TRUE(
1561 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1562 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1563}
1564
1565TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001566 StatementMatcher New = constructExpr(
1567 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001568
1569 EXPECT_TRUE(
1570 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1571 New));
1572 EXPECT_TRUE(
1573 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1574 New));
1575 EXPECT_TRUE(
1576 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1577 New));
1578
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001579 StatementMatcher WrongIndex = constructExpr(
1580 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001581 EXPECT_TRUE(
1582 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1583 WrongIndex));
1584}
1585
1586TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001587 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001588
1589 EXPECT_TRUE(
1590 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1591 EXPECT_TRUE(
1592 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1593 New));
1594}
1595
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001596TEST(Matcher, DeleteExpression) {
1597 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001598 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001599}
1600
Manuel Klimek4da21662012-07-06 05:48:52 +00001601TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001602 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001603
1604 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1605 EXPECT_TRUE(
1606 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1607 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1608}
1609
1610TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001611 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001612 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1613 // wide string
1614 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1615 // with escaped characters
1616 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1617 // no matching -- though the data type is the same, there is no string literal
1618 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1619}
1620
1621TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001622 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001623 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1624 // wide character
1625 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1626 // wide character, Hex encoded, NOT MATCHED!
1627 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1628 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1629}
1630
1631TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001632 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001633 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1634 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1635 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1636 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1637
1638 // Non-matching cases (character literals, float and double)
1639 EXPECT_TRUE(notMatches("int i = L'a';",
1640 HasIntLiteral)); // this is actually a character
1641 // literal cast to int
1642 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1643 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1644 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1645}
1646
Daniel Jasper31f7c082012-10-01 13:40:41 +00001647TEST(Matcher, NullPtrLiteral) {
1648 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1649}
1650
Daniel Jasperb54b7642012-09-20 14:12:57 +00001651TEST(Matcher, AsmStatement) {
1652 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1653}
1654
Manuel Klimek4da21662012-07-06 05:48:52 +00001655TEST(Matcher, Conditions) {
1656 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1657
1658 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1659 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1660 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1661 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1662 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1663}
1664
1665TEST(MatchBinaryOperator, HasOperatorName) {
1666 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1667
1668 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1669 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1670}
1671
1672TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1673 StatementMatcher OperatorTrueFalse =
1674 binaryOperator(hasLHS(boolLiteral(equals(true))),
1675 hasRHS(boolLiteral(equals(false))));
1676
1677 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1678 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1679 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1680}
1681
1682TEST(MatchBinaryOperator, HasEitherOperand) {
1683 StatementMatcher HasOperand =
1684 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1685
1686 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1687 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1688 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1689}
1690
1691TEST(Matcher, BinaryOperatorTypes) {
1692 // Integration test that verifies the AST provides all binary operators in
1693 // a way we expect.
1694 // FIXME: Operator ','
1695 EXPECT_TRUE(
1696 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1697 EXPECT_TRUE(
1698 matches("bool b; bool c = (b = true);",
1699 binaryOperator(hasOperatorName("="))));
1700 EXPECT_TRUE(
1701 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1702 EXPECT_TRUE(
1703 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1704 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1705 EXPECT_TRUE(
1706 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1707 EXPECT_TRUE(
1708 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1709 EXPECT_TRUE(
1710 matches("int i = 1; int j = (i <<= 2);",
1711 binaryOperator(hasOperatorName("<<="))));
1712 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1713 EXPECT_TRUE(
1714 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1715 EXPECT_TRUE(
1716 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1717 EXPECT_TRUE(
1718 matches("int i = 1; int j = (i >>= 2);",
1719 binaryOperator(hasOperatorName(">>="))));
1720 EXPECT_TRUE(
1721 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1722 EXPECT_TRUE(
1723 matches("int i = 42; int j = (i ^= 42);",
1724 binaryOperator(hasOperatorName("^="))));
1725 EXPECT_TRUE(
1726 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1727 EXPECT_TRUE(
1728 matches("int i = 42; int j = (i %= 42);",
1729 binaryOperator(hasOperatorName("%="))));
1730 EXPECT_TRUE(
1731 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1732 EXPECT_TRUE(
1733 matches("bool b = true && false;",
1734 binaryOperator(hasOperatorName("&&"))));
1735 EXPECT_TRUE(
1736 matches("bool b = true; bool c = (b &= false);",
1737 binaryOperator(hasOperatorName("&="))));
1738 EXPECT_TRUE(
1739 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1740 EXPECT_TRUE(
1741 matches("bool b = true || false;",
1742 binaryOperator(hasOperatorName("||"))));
1743 EXPECT_TRUE(
1744 matches("bool b = true; bool c = (b |= false);",
1745 binaryOperator(hasOperatorName("|="))));
1746 EXPECT_TRUE(
1747 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1748 EXPECT_TRUE(
1749 matches("int i = 42; int j = (i *= 23);",
1750 binaryOperator(hasOperatorName("*="))));
1751 EXPECT_TRUE(
1752 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1753 EXPECT_TRUE(
1754 matches("int i = 42; int j = (i /= 23);",
1755 binaryOperator(hasOperatorName("/="))));
1756 EXPECT_TRUE(
1757 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1758 EXPECT_TRUE(
1759 matches("int i = 42; int j = (i += 23);",
1760 binaryOperator(hasOperatorName("+="))));
1761 EXPECT_TRUE(
1762 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1763 EXPECT_TRUE(
1764 matches("int i = 42; int j = (i -= 23);",
1765 binaryOperator(hasOperatorName("-="))));
1766 EXPECT_TRUE(
1767 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1768 binaryOperator(hasOperatorName("->*"))));
1769 EXPECT_TRUE(
1770 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1771 binaryOperator(hasOperatorName(".*"))));
1772
1773 // Member expressions as operators are not supported in matches.
1774 EXPECT_TRUE(
1775 notMatches("struct A { void x(A *a) { a->x(this); } };",
1776 binaryOperator(hasOperatorName("->"))));
1777
1778 // Initializer assignments are not represented as operator equals.
1779 EXPECT_TRUE(
1780 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1781
1782 // Array indexing is not represented as operator.
1783 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1784
1785 // Overloaded operators do not match at all.
1786 EXPECT_TRUE(notMatches(
1787 "struct A { bool operator&&(const A &a) const { return false; } };"
1788 "void x() { A a, b; a && b; }",
1789 binaryOperator()));
1790}
1791
1792TEST(MatchUnaryOperator, HasOperatorName) {
1793 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1794
1795 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1796 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1797}
1798
1799TEST(MatchUnaryOperator, HasUnaryOperand) {
1800 StatementMatcher OperatorOnFalse =
1801 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1802
1803 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1804 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1805}
1806
1807TEST(Matcher, UnaryOperatorTypes) {
1808 // Integration test that verifies the AST provides all unary operators in
1809 // a way we expect.
1810 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1811 EXPECT_TRUE(
1812 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1813 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1814 EXPECT_TRUE(
1815 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1816 EXPECT_TRUE(
1817 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1818 EXPECT_TRUE(
1819 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1820 EXPECT_TRUE(
1821 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1822 EXPECT_TRUE(
1823 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1824 EXPECT_TRUE(
1825 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1826 EXPECT_TRUE(
1827 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1828
1829 // We don't match conversion operators.
1830 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1831
1832 // Function calls are not represented as operator.
1833 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1834
1835 // Overloaded operators do not match at all.
1836 // FIXME: We probably want to add that.
1837 EXPECT_TRUE(notMatches(
1838 "struct A { bool operator!() const { return false; } };"
1839 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1840}
1841
1842TEST(Matcher, ConditionalOperator) {
1843 StatementMatcher Conditional = conditionalOperator(
1844 hasCondition(boolLiteral(equals(true))),
1845 hasTrueExpression(boolLiteral(equals(false))));
1846
1847 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1848 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1849 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1850
1851 StatementMatcher ConditionalFalse = conditionalOperator(
1852 hasFalseExpression(boolLiteral(equals(false))));
1853
1854 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1855 EXPECT_TRUE(
1856 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1857}
1858
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001859TEST(ArraySubscriptMatchers, ArraySubscripts) {
1860 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1861 arraySubscriptExpr()));
1862 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1863 arraySubscriptExpr()));
1864}
1865
1866TEST(ArraySubscriptMatchers, ArrayIndex) {
1867 EXPECT_TRUE(matches(
1868 "int i[2]; void f() { i[1] = 1; }",
1869 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1870 EXPECT_TRUE(matches(
1871 "int i[2]; void f() { 1[i] = 1; }",
1872 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1873 EXPECT_TRUE(notMatches(
1874 "int i[2]; void f() { i[1] = 1; }",
1875 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1876}
1877
1878TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1879 EXPECT_TRUE(matches(
1880 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001881 arraySubscriptExpr(hasBase(implicitCastExpr(
1882 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001883}
1884
Manuel Klimek4da21662012-07-06 05:48:52 +00001885TEST(Matcher, HasNameSupportsNamespaces) {
1886 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001887 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001888 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001889 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001890 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001891 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001892 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001893 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001894 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001895 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001896 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001897 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001898 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001899 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001900 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001901 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001902 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001903 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001904 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001905 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001906 EXPECT_TRUE(notMatches("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(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001909 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001910}
1911
1912TEST(Matcher, HasNameSupportsOuterClasses) {
1913 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001914 matches("class A { class B { class C; }; };",
1915 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001916 EXPECT_TRUE(
1917 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001918 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001919 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001920 matches("class A { class B { class C; }; };",
1921 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001922 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001923 matches("class A { class B { class C; }; };",
1924 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001925 EXPECT_TRUE(
1926 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001927 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001928 EXPECT_TRUE(
1929 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001930 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001931 EXPECT_TRUE(
1932 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001933 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001934 EXPECT_TRUE(
1935 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001936 recordDecl(hasName("::C"))));
1937 EXPECT_TRUE(
1938 notMatches("class A { class B { class C; }; };",
1939 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001940 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001941 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001942 EXPECT_TRUE(
1943 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001944 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001945}
1946
1947TEST(Matcher, IsDefinition) {
1948 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001949 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001950 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1951 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1952
1953 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001954 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001955 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1956 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1957
1958 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001959 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001960 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1961 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1962}
1963
1964TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001965 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00001966 ofClass(hasName("X")))));
1967
1968 EXPECT_TRUE(
1969 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
1970 EXPECT_TRUE(
1971 matches("class X { public: X(); }; void x(int) { X x = X(); }",
1972 Constructor));
1973 EXPECT_TRUE(
1974 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
1975 Constructor));
1976}
1977
1978TEST(Matcher, VisitsTemplateInstantiations) {
1979 EXPECT_TRUE(matches(
1980 "class A { public: void x(); };"
1981 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001982 "void f() { B<A> b; b.y(); }",
1983 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001984
1985 EXPECT_TRUE(matches(
1986 "class A { public: void x(); };"
1987 "class C {"
1988 " public:"
1989 " template <typename T> class B { public: void y() { T t; t.x(); } };"
1990 "};"
1991 "void f() {"
1992 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001993 "}",
1994 recordDecl(hasName("C"),
1995 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001996}
1997
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001998TEST(Matcher, HandlesNullQualTypes) {
1999 // FIXME: Add a Type matcher so we can replace uses of this
2000 // variable with Type(True())
2001 const TypeMatcher AnyType = anything();
2002
2003 // We don't really care whether this matcher succeeds; we're testing that
2004 // it completes without crashing.
2005 EXPECT_TRUE(matches(
2006 "struct A { };"
2007 "template <typename T>"
2008 "void f(T t) {"
2009 " T local_t(t /* this becomes a null QualType in the AST */);"
2010 "}"
2011 "void g() {"
2012 " f(0);"
2013 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002014 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002015 anyOf(
2016 TypeMatcher(hasDeclaration(anything())),
2017 pointsTo(AnyType),
2018 references(AnyType)
2019 // Other QualType matchers should go here.
2020 ))))));
2021}
2022
Manuel Klimek4da21662012-07-06 05:48:52 +00002023// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002024AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002025 // Make sure all special variables are used: node, match_finder,
2026 // bound_nodes_builder, and the parameter named 'AMatcher'.
2027 return AMatcher.matches(Node, Finder, Builder);
2028}
2029
2030TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002031 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002032
2033 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002034 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002035
2036 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002037 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002038
2039 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002040 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002041}
2042
2043AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002044 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
2045 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
2046 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00002047 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00002048 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002049 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002050 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2051 ASTMatchFinder::BK_First);
2052}
2053
2054TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002055 DeclarationMatcher HasClassB =
2056 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002057
2058 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002059 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002060
2061 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002062 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002063
2064 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002065 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002066
2067 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002068 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002069
2070 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2071}
2072
2073TEST(For, FindsForLoops) {
2074 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2075 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002076 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2077 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002078 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002079}
2080
Daniel Jasper6a124492012-07-12 08:50:38 +00002081TEST(For, ForLoopInternals) {
2082 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2083 forStmt(hasCondition(anything()))));
2084 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2085 forStmt(hasLoopInit(anything()))));
2086}
2087
2088TEST(For, NegativeForLoopInternals) {
2089 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002090 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002091 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2092 forStmt(hasLoopInit(anything()))));
2093}
2094
Manuel Klimek4da21662012-07-06 05:48:52 +00002095TEST(For, ReportsNoFalsePositives) {
2096 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2097 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2098}
2099
2100TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002101 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2102 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2103 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002104}
2105
2106TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2107 // It's not a compound statement just because there's "{}" in the source
2108 // text. This is an AST search, not grep.
2109 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002110 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002111 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002112 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002113}
2114
Daniel Jasper6a124492012-07-12 08:50:38 +00002115TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002116 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002117 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002118 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002119 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002120 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002121 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002122 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002123 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002124}
2125
2126TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2127 // The simplest case: every compound statement is in a function
2128 // definition, and the function body itself must be a compound
2129 // statement.
2130 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002131 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002132}
2133
2134TEST(HasAnySubstatement, IsNotRecursive) {
2135 // It's really "has any immediate substatement".
2136 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002137 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002138}
2139
2140TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2141 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002142 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002143}
2144
2145TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2146 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002147 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002148}
2149
2150TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2151 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002152 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002153 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002154 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002155}
2156
2157TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2158 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002159 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002160 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002161 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002162 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002163 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002164}
2165
2166TEST(StatementCountIs, WorksWithMultipleStatements) {
2167 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002168 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002169}
2170
2171TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2172 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002173 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002174 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002175 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002176 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002177 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002178 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002179 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002180}
2181
2182TEST(Member, WorksInSimplestCase) {
2183 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002184 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002185}
2186
2187TEST(Member, DoesNotMatchTheBaseExpression) {
2188 // Don't pick out the wrong part of the member expression, this should
2189 // be checking the member (name) only.
2190 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002191 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002192}
2193
2194TEST(Member, MatchesInMemberFunctionCall) {
2195 EXPECT_TRUE(matches("void f() {"
2196 " struct { void first() {}; } s;"
2197 " s.first();"
2198 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002199 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002200}
2201
Daniel Jasperc711af22012-10-23 15:46:39 +00002202TEST(Member, MatchesMember) {
2203 EXPECT_TRUE(matches(
2204 "struct A { int i; }; void f() { A a; a.i = 2; }",
2205 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2206 EXPECT_TRUE(notMatches(
2207 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2208 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2209}
2210
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002211TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002212 // Fails in C++11 mode
2213 EXPECT_TRUE(matchesConditionally(
2214 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2215 "class X { void *operator new(std::size_t); };",
2216 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002217
2218 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002219 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002220
Daniel Jasper31f7c082012-10-01 13:40:41 +00002221 // Fails in C++11 mode
2222 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002223 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2224 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002225 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002226}
2227
Manuel Klimek4da21662012-07-06 05:48:52 +00002228TEST(HasObjectExpression, DoesNotMatchMember) {
2229 EXPECT_TRUE(notMatches(
2230 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002231 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002232}
2233
2234TEST(HasObjectExpression, MatchesBaseOfVariable) {
2235 EXPECT_TRUE(matches(
2236 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002237 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002238 EXPECT_TRUE(matches(
2239 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002240 memberExpr(hasObjectExpression(
2241 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002242}
2243
2244TEST(HasObjectExpression,
2245 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2246 EXPECT_TRUE(matches(
2247 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002248 memberExpr(hasObjectExpression(
2249 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002250 EXPECT_TRUE(matches(
2251 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002252 memberExpr(hasObjectExpression(
2253 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002254}
2255
2256TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002257 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2258 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2259 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2260 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002261}
2262
2263TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002264 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002265}
2266
2267TEST(IsConstQualified, MatchesConstInt) {
2268 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002269 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002270}
2271
2272TEST(IsConstQualified, MatchesConstPointer) {
2273 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002274 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002275}
2276
2277TEST(IsConstQualified, MatchesThroughTypedef) {
2278 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002279 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002280 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002281 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002282}
2283
2284TEST(IsConstQualified, DoesNotMatchInappropriately) {
2285 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002286 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002287 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002288 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002289}
2290
Sam Panzer089e5b32012-08-16 16:58:10 +00002291TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002292 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2293 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2294 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2295 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002296}
2297TEST(CastExpression, MatchesImplicitCasts) {
2298 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002299 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002300 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002301 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002302}
2303
2304TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002305 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2306 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2307 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2308 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002309}
2310
Manuel Klimek4da21662012-07-06 05:48:52 +00002311TEST(ReinterpretCast, MatchesSimpleCase) {
2312 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002313 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002314}
2315
2316TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002317 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002318 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002319 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002320 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002321 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002322 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2323 "B b;"
2324 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002325 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002326}
2327
2328TEST(FunctionalCast, MatchesSimpleCase) {
2329 std::string foo_class = "class Foo { public: Foo(char*); };";
2330 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002331 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002332}
2333
2334TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2335 std::string FooClass = "class Foo { public: Foo(char*); };";
2336 EXPECT_TRUE(
2337 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002338 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002339 EXPECT_TRUE(
2340 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002341 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002342}
2343
2344TEST(DynamicCast, MatchesSimpleCase) {
2345 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2346 "B b;"
2347 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002348 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002349}
2350
2351TEST(StaticCast, MatchesSimpleCase) {
2352 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002353 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002354}
2355
2356TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002357 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002358 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002359 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002360 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002361 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002362 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2363 "B b;"
2364 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002365 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002366}
2367
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002368TEST(CStyleCast, MatchesSimpleCase) {
2369 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2370}
2371
2372TEST(CStyleCast, DoesNotMatchOtherCasts) {
2373 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2374 "char q, *r = const_cast<char*>(&q);"
2375 "void* s = reinterpret_cast<char*>(&s);"
2376 "struct B { virtual ~B() {} }; struct D : B {};"
2377 "B b;"
2378 "D* t = dynamic_cast<D*>(&b);",
2379 cStyleCastExpr()));
2380}
2381
Manuel Klimek4da21662012-07-06 05:48:52 +00002382TEST(HasDestinationType, MatchesSimpleCase) {
2383 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002384 staticCastExpr(hasDestinationType(
2385 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002386}
2387
Sam Panzer089e5b32012-08-16 16:58:10 +00002388TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2389 // This test creates an implicit const cast.
2390 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002391 implicitCastExpr(
2392 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002393 // This test creates an implicit array-to-pointer cast.
2394 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002395 implicitCastExpr(hasImplicitDestinationType(
2396 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002397}
2398
2399TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2400 // This test creates an implicit cast from int to char.
2401 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002402 implicitCastExpr(hasImplicitDestinationType(
2403 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002404 // This test creates an implicit array-to-pointer cast.
2405 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002406 implicitCastExpr(hasImplicitDestinationType(
2407 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002408}
2409
2410TEST(ImplicitCast, MatchesSimpleCase) {
2411 // This test creates an implicit const cast.
2412 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002413 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002414 // This test creates an implicit cast from int to char.
2415 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002416 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002417 // This test creates an implicit array-to-pointer cast.
2418 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002419 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002420}
2421
2422TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002423 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002424 // are present, and that it ignores explicit and paren casts.
2425
2426 // These two test cases have no casts.
2427 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002428 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002429 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002430 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002431
2432 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002433 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002434 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002435 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002436
2437 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002438 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002439}
2440
2441TEST(IgnoringImpCasts, MatchesImpCasts) {
2442 // This test checks that ignoringImpCasts matches when implicit casts are
2443 // present and its inner matcher alone does not match.
2444 // Note that this test creates an implicit const cast.
2445 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002446 varDecl(hasInitializer(ignoringImpCasts(
2447 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002448 // This test creates an implict cast from int to char.
2449 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002450 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002451 integerLiteral(equals(0)))))));
2452}
2453
2454TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2455 // These tests verify that ignoringImpCasts does not match if the inner
2456 // matcher does not match.
2457 // Note that the first test creates an implicit const cast.
2458 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002459 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002460 unless(anything()))))));
2461 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002462 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002463 unless(anything()))))));
2464
2465 // These tests verify that ignoringImplictCasts does not look through explicit
2466 // casts or parentheses.
2467 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002468 varDecl(hasInitializer(ignoringImpCasts(
2469 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002470 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002471 varDecl(hasInitializer(ignoringImpCasts(
2472 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002473 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002474 varDecl(hasInitializer(ignoringImpCasts(
2475 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002476 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002477 varDecl(hasInitializer(ignoringImpCasts(
2478 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002479}
2480
2481TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2482 // This test verifies that expressions that do not have implicit casts
2483 // still match the inner matcher.
2484 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002485 varDecl(hasInitializer(ignoringImpCasts(
2486 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002487}
2488
2489TEST(IgnoringParenCasts, MatchesParenCasts) {
2490 // This test checks that ignoringParenCasts matches when parentheses and/or
2491 // casts are present and its inner matcher alone does not match.
2492 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002493 varDecl(hasInitializer(ignoringParenCasts(
2494 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002495 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002496 varDecl(hasInitializer(ignoringParenCasts(
2497 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002498
2499 // This test creates an implict cast from int to char in addition to the
2500 // parentheses.
2501 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002502 varDecl(hasInitializer(ignoringParenCasts(
2503 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002504
2505 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002506 varDecl(hasInitializer(ignoringParenCasts(
2507 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002508 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002509 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002510 integerLiteral(equals(0)))))));
2511}
2512
2513TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2514 // This test verifies that expressions that do not have any casts still match.
2515 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
2520TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2521 // These tests verify that ignoringImpCasts does not match if the inner
2522 // matcher does not match.
2523 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002524 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002525 unless(anything()))))));
2526
2527 // This test creates an implicit cast from int to char in addition to the
2528 // parentheses.
2529 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002530 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002531 unless(anything()))))));
2532
2533 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002534 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002535 unless(anything()))))));
2536}
2537
2538TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2539 // This test checks that ignoringParenAndImpCasts matches when
2540 // parentheses and/or implicit casts are present and its inner matcher alone
2541 // does not match.
2542 // Note that this test creates an implicit const cast.
2543 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002544 varDecl(hasInitializer(ignoringParenImpCasts(
2545 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002546 // This test creates an implicit cast from int to char.
2547 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002548 varDecl(hasInitializer(ignoringParenImpCasts(
2549 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002550}
2551
2552TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2553 // This test verifies that expressions that do not have parentheses or
2554 // implicit casts still match.
2555 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002556 varDecl(hasInitializer(ignoringParenImpCasts(
2557 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002558 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002559 varDecl(hasInitializer(ignoringParenImpCasts(
2560 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002561}
2562
2563TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2564 // These tests verify that ignoringParenImpCasts does not match if
2565 // the inner matcher does not match.
2566 // This test creates an implicit cast.
2567 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002568 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002569 unless(anything()))))));
2570 // These tests verify that ignoringParenAndImplictCasts does not look
2571 // through explicit casts.
2572 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002573 varDecl(hasInitializer(ignoringParenImpCasts(
2574 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002575 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002576 varDecl(hasInitializer(ignoringParenImpCasts(
2577 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002578 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002579 varDecl(hasInitializer(ignoringParenImpCasts(
2580 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002581}
2582
Manuel Klimek715c9562012-07-25 10:02:02 +00002583TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002584 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2585 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002586 implicitCastExpr(
2587 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002588}
2589
Manuel Klimek715c9562012-07-25 10:02:02 +00002590TEST(HasSourceExpression, MatchesExplicitCasts) {
2591 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002592 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002593 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002594 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002595}
2596
Manuel Klimek4da21662012-07-06 05:48:52 +00002597TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002598 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002599}
2600
2601TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002602 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002603}
2604
2605TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002606 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002607}
2608
2609TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002610 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002611}
2612
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002613TEST(InitListExpression, MatchesInitListExpression) {
2614 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2615 initListExpr(hasType(asString("int [2]")))));
2616 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002617 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002618}
2619
2620TEST(UsingDeclaration, MatchesUsingDeclarations) {
2621 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2622 usingDecl()));
2623}
2624
2625TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2626 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2627 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2628}
2629
2630TEST(UsingDeclaration, MatchesSpecificTarget) {
2631 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2632 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002633 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002634 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2635 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002636 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002637}
2638
2639TEST(UsingDeclaration, ThroughUsingDeclaration) {
2640 EXPECT_TRUE(matches(
2641 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002642 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002643 EXPECT_TRUE(notMatches(
2644 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002645 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002646}
2647
Sam Panzer425f41b2012-08-16 17:20:59 +00002648TEST(SingleDecl, IsSingleDecl) {
2649 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002650 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002651 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2652 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2653 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2654 SingleDeclStmt));
2655}
2656
2657TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002658 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002659
2660 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002661 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002662 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002663 declStmt(containsDeclaration(0, MatchesInit),
2664 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002665 unsigned WrongIndex = 42;
2666 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002667 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002668 MatchesInit))));
2669}
2670
2671TEST(DeclCount, DeclCountIsCorrect) {
2672 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002673 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002674 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002675 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002676 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002677 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002678}
2679
Manuel Klimek4da21662012-07-06 05:48:52 +00002680TEST(While, MatchesWhileLoops) {
2681 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2682 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2683 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2684}
2685
2686TEST(Do, MatchesDoLoops) {
2687 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2688 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2689}
2690
2691TEST(Do, DoesNotMatchWhileLoops) {
2692 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2693}
2694
2695TEST(SwitchCase, MatchesCase) {
2696 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2697 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2698 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2699 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2700}
2701
Daniel Jasperb54b7642012-09-20 14:12:57 +00002702TEST(SwitchCase, MatchesSwitch) {
2703 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2704 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2705 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2706 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2707}
2708
2709TEST(ExceptionHandling, SimpleCases) {
2710 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2711 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2712 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2713 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2714 throwExpr()));
2715 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2716 throwExpr()));
2717}
2718
Manuel Klimek4da21662012-07-06 05:48:52 +00002719TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2720 EXPECT_TRUE(notMatches(
2721 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002722 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002723 EXPECT_TRUE(notMatches(
2724 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002725 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002726}
2727
2728TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2729 EXPECT_TRUE(matches(
2730 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002731 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002732}
2733
2734TEST(ForEach, BindsOneNode) {
2735 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002736 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002737 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002738}
2739
2740TEST(ForEach, BindsMultipleNodes) {
2741 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002742 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002743 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002744}
2745
2746TEST(ForEach, BindsRecursiveCombinations) {
2747 EXPECT_TRUE(matchAndVerifyResultTrue(
2748 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002749 recordDecl(hasName("C"),
2750 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002751 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002752}
2753
2754TEST(ForEachDescendant, BindsOneNode) {
2755 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002756 recordDecl(hasName("C"),
2757 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002758 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002759}
2760
2761TEST(ForEachDescendant, BindsMultipleNodes) {
2762 EXPECT_TRUE(matchAndVerifyResultTrue(
2763 "class C { class D { int x; int y; }; "
2764 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002765 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002766 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002767}
2768
2769TEST(ForEachDescendant, BindsRecursiveCombinations) {
2770 EXPECT_TRUE(matchAndVerifyResultTrue(
2771 "class C { class D { "
2772 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002773 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2774 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002775 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002776}
2777
2778
2779TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2780 // Make sure that we can both match the class by name (::X) and by the type
2781 // the template was instantiated with (via a field).
2782
2783 EXPECT_TRUE(matches(
2784 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002785 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002786
2787 EXPECT_TRUE(matches(
2788 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002789 recordDecl(isTemplateInstantiation(), hasDescendant(
2790 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002791}
2792
2793TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2794 EXPECT_TRUE(matches(
2795 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002796 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002797 isTemplateInstantiation())));
2798}
2799
2800TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2801 EXPECT_TRUE(matches(
2802 "template <typename T> class X { T t; }; class A {};"
2803 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002804 recordDecl(isTemplateInstantiation(), hasDescendant(
2805 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002806}
2807
2808TEST(IsTemplateInstantiation,
2809 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2810 EXPECT_TRUE(matches(
2811 "template <typename T> class X {};"
2812 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002813 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002814}
2815
2816TEST(IsTemplateInstantiation,
2817 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2818 EXPECT_TRUE(matches(
2819 "class A {};"
2820 "class X {"
2821 " template <typename U> class Y { U u; };"
2822 " Y<A> y;"
2823 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002824 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002825}
2826
2827TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2828 // FIXME: Figure out whether this makes sense. It doesn't affect the
2829 // normal use case as long as the uppermost instantiation always is marked
2830 // as template instantiation, but it might be confusing as a predicate.
2831 EXPECT_TRUE(matches(
2832 "class A {};"
2833 "template <typename T> class X {"
2834 " template <typename U> class Y { U u; };"
2835 " Y<T> y;"
2836 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002837 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002838}
2839
2840TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2841 EXPECT_TRUE(notMatches(
2842 "template <typename T> class X {}; class A {};"
2843 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002844 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002845}
2846
2847TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2848 EXPECT_TRUE(notMatches(
2849 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002850 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002851}
2852
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002853TEST(IsExplicitTemplateSpecialization,
2854 DoesNotMatchPrimaryTemplate) {
2855 EXPECT_TRUE(notMatches(
2856 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002857 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002858 EXPECT_TRUE(notMatches(
2859 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002860 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002861}
2862
2863TEST(IsExplicitTemplateSpecialization,
2864 DoesNotMatchExplicitTemplateInstantiations) {
2865 EXPECT_TRUE(notMatches(
2866 "template <typename T> class X {};"
2867 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002868 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002869 EXPECT_TRUE(notMatches(
2870 "template <typename T> void f(T t) {}"
2871 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002872 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002873}
2874
2875TEST(IsExplicitTemplateSpecialization,
2876 DoesNotMatchImplicitTemplateInstantiations) {
2877 EXPECT_TRUE(notMatches(
2878 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002879 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002880 EXPECT_TRUE(notMatches(
2881 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002882 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002883}
2884
2885TEST(IsExplicitTemplateSpecialization,
2886 MatchesExplicitTemplateSpecializations) {
2887 EXPECT_TRUE(matches(
2888 "template <typename T> class X {};"
2889 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002890 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002891 EXPECT_TRUE(matches(
2892 "template <typename T> void f(T t) {}"
2893 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002894 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002895}
2896
Manuel Klimek579b1202012-09-07 09:26:10 +00002897TEST(HasAncenstor, MatchesDeclarationAncestors) {
2898 EXPECT_TRUE(matches(
2899 "class A { class B { class C {}; }; };",
2900 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2901}
2902
2903TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2904 EXPECT_TRUE(notMatches(
2905 "class A { class B { class C {}; }; };",
2906 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2907}
2908
2909TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2910 EXPECT_TRUE(matches(
2911 "class A { class B { void f() { C c; } class C {}; }; };",
2912 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2913 hasAncestor(recordDecl(hasName("A"))))))));
2914}
2915
2916TEST(HasAncenstor, MatchesStatementAncestors) {
2917 EXPECT_TRUE(matches(
2918 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002919 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002920}
2921
2922TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2923 EXPECT_TRUE(matches(
2924 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002925 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002926}
2927
2928TEST(HasAncestor, BindsRecursiveCombinations) {
2929 EXPECT_TRUE(matchAndVerifyResultTrue(
2930 "class C { class D { class E { class F { int y; }; }; }; };",
2931 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002932 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00002933}
2934
2935TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
2936 EXPECT_TRUE(matchAndVerifyResultTrue(
2937 "class C { class D { class E { class F { int y; }; }; }; };",
2938 fieldDecl(hasAncestor(
2939 decl(
2940 hasDescendant(recordDecl(isDefinition(),
2941 hasAncestor(recordDecl())))
2942 ).bind("d")
2943 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00002944 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00002945}
2946
2947TEST(HasAncestor, MatchesInTemplateInstantiations) {
2948 EXPECT_TRUE(matches(
2949 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
2950 "A<int>::B::C a;",
2951 fieldDecl(hasType(asString("int")),
2952 hasAncestor(recordDecl(hasName("A"))))));
2953}
2954
2955TEST(HasAncestor, MatchesInImplicitCode) {
2956 EXPECT_TRUE(matches(
2957 "struct X {}; struct A { A() {} X x; };",
2958 constructorDecl(
2959 hasAnyConstructorInitializer(withInitializer(expr(
2960 hasAncestor(recordDecl(hasName("A")))))))));
2961}
2962
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00002963TEST(HasParent, MatchesOnlyParent) {
2964 EXPECT_TRUE(matches(
2965 "void f() { if (true) { int x = 42; } }",
2966 compoundStmt(hasParent(ifStmt()))));
2967 EXPECT_TRUE(notMatches(
2968 "void f() { for (;;) { int x = 42; } }",
2969 compoundStmt(hasParent(ifStmt()))));
2970 EXPECT_TRUE(notMatches(
2971 "void f() { if (true) for (;;) { int x = 42; } }",
2972 compoundStmt(hasParent(ifStmt()))));
2973}
2974
Daniel Jasperce620072012-10-17 08:52:59 +00002975TEST(TypeMatching, MatchesTypes) {
2976 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
2977}
2978
2979TEST(TypeMatching, MatchesArrayTypes) {
2980 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
2981 EXPECT_TRUE(matches("int a[42];", arrayType()));
2982 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
2983
2984 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
2985 arrayType(hasElementType(builtinType()))));
2986
2987 EXPECT_TRUE(matches(
2988 "int const a[] = { 2, 3 };",
2989 qualType(arrayType(hasElementType(builtinType())))));
2990 EXPECT_TRUE(matches(
2991 "int const a[] = { 2, 3 };",
2992 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
2993 EXPECT_TRUE(matches(
2994 "typedef const int T; T x[] = { 1, 2 };",
2995 qualType(isConstQualified(), arrayType())));
2996
2997 EXPECT_TRUE(notMatches(
2998 "int a[] = { 2, 3 };",
2999 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3000 EXPECT_TRUE(notMatches(
3001 "int a[] = { 2, 3 };",
3002 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3003 EXPECT_TRUE(notMatches(
3004 "int const a[] = { 2, 3 };",
3005 qualType(arrayType(hasElementType(builtinType())),
3006 unless(isConstQualified()))));
3007
3008 EXPECT_TRUE(matches("int a[2];",
3009 constantArrayType(hasElementType(builtinType()))));
3010 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3011}
3012
3013TEST(TypeMatching, MatchesComplexTypes) {
3014 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3015 EXPECT_TRUE(matches(
3016 "_Complex float f;",
3017 complexType(hasElementType(builtinType()))));
3018 EXPECT_TRUE(notMatches(
3019 "_Complex float f;",
3020 complexType(hasElementType(isInteger()))));
3021}
3022
3023TEST(TypeMatching, MatchesConstantArrayTypes) {
3024 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3025 EXPECT_TRUE(notMatches(
3026 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3027 constantArrayType(hasElementType(builtinType()))));
3028
3029 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3030 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3031 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3032}
3033
3034TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3035 EXPECT_TRUE(matches(
3036 "template <typename T, int Size> class array { T data[Size]; };",
3037 dependentSizedArrayType()));
3038 EXPECT_TRUE(notMatches(
3039 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3040 dependentSizedArrayType()));
3041}
3042
3043TEST(TypeMatching, MatchesIncompleteArrayType) {
3044 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3045 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3046
3047 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3048 incompleteArrayType()));
3049}
3050
3051TEST(TypeMatching, MatchesVariableArrayType) {
3052 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3053 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3054
3055 EXPECT_TRUE(matches(
3056 "void f(int b) { int a[b]; }",
3057 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3058 varDecl(hasName("b")))))))));
3059}
3060
3061TEST(TypeMatching, MatchesAtomicTypes) {
3062 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3063
3064 EXPECT_TRUE(matches("_Atomic(int) i;",
3065 atomicType(hasValueType(isInteger()))));
3066 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3067 atomicType(hasValueType(isInteger()))));
3068}
3069
3070TEST(TypeMatching, MatchesAutoTypes) {
3071 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3072 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3073 autoType()));
3074
3075 EXPECT_TRUE(matches("auto a = 1;",
3076 autoType(hasDeducedType(isInteger()))));
3077 EXPECT_TRUE(notMatches("auto b = 2.0;",
3078 autoType(hasDeducedType(isInteger()))));
3079}
3080
Daniel Jaspera267cf62012-10-29 10:14:44 +00003081TEST(TypeMatching, MatchesFunctionTypes) {
3082 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3083 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3084}
3085
Daniel Jasperce620072012-10-17 08:52:59 +00003086TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003087 // FIXME: Reactive when these tests can be more specific (not matching
3088 // implicit code on certain platforms), likely when we have hasDescendant for
3089 // Types/TypeLocs.
3090 //EXPECT_TRUE(matchAndVerifyResultTrue(
3091 // "int* a;",
3092 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3093 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3094 //EXPECT_TRUE(matchAndVerifyResultTrue(
3095 // "int* a;",
3096 // pointerTypeLoc().bind("loc"),
3097 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003098 EXPECT_TRUE(matches(
3099 "int** a;",
3100 pointerTypeLoc(pointeeLoc(loc(qualType())))));
3101 EXPECT_TRUE(matches(
3102 "int** a;",
3103 loc(pointerType(pointee(pointerType())))));
3104 EXPECT_TRUE(matches(
3105 "int* b; int* * const a = &b;",
3106 loc(qualType(isConstQualified(), pointerType()))));
3107
3108 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003109 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3110 hasType(blockPointerType()))));
3111 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3112 hasType(memberPointerType()))));
3113 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3114 hasType(pointerType()))));
3115 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3116 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003117
Daniel Jasper1802daf2012-10-17 13:35:36 +00003118 Fragment = "int *ptr;";
3119 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3120 hasType(blockPointerType()))));
3121 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3122 hasType(memberPointerType()))));
3123 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3124 hasType(pointerType()))));
3125 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3126 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003127
Daniel Jasper1802daf2012-10-17 13:35:36 +00003128 Fragment = "int a; int &ref = a;";
3129 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3130 hasType(blockPointerType()))));
3131 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3132 hasType(memberPointerType()))));
3133 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3134 hasType(pointerType()))));
3135 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3136 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003137}
3138
3139TEST(TypeMatching, PointeeTypes) {
3140 EXPECT_TRUE(matches("int b; int &a = b;",
3141 referenceType(pointee(builtinType()))));
3142 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3143
3144 EXPECT_TRUE(matches("int *a;",
3145 pointerTypeLoc(pointeeLoc(loc(builtinType())))));
3146
3147 EXPECT_TRUE(matches(
3148 "int const *A;",
3149 pointerType(pointee(isConstQualified(), builtinType()))));
3150 EXPECT_TRUE(notMatches(
3151 "int *A;",
3152 pointerType(pointee(isConstQualified(), builtinType()))));
3153}
3154
3155TEST(TypeMatching, MatchesPointersToConstTypes) {
3156 EXPECT_TRUE(matches("int b; int * const a = &b;",
3157 loc(pointerType())));
3158 EXPECT_TRUE(matches("int b; int * const a = &b;",
3159 pointerTypeLoc()));
3160 EXPECT_TRUE(matches(
3161 "int b; const int * a = &b;",
3162 pointerTypeLoc(pointeeLoc(builtinTypeLoc()))));
3163 EXPECT_TRUE(matches(
3164 "int b; const int * a = &b;",
3165 pointerType(pointee(builtinType()))));
3166}
3167
3168TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003169 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3170 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003171
Daniel Jasper1802daf2012-10-17 13:35:36 +00003172 EXPECT_TRUE(matches("typedef int X; X a;",
3173 varDecl(hasName("a"),
3174 hasType(typedefType(hasDecl(decl()))))));
Daniel Jasperce620072012-10-17 08:52:59 +00003175}
3176
Daniel Jaspera7564432012-09-13 13:11:25 +00003177TEST(NNS, MatchesNestedNameSpecifiers) {
3178 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3179 nestedNameSpecifier()));
3180 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3181 nestedNameSpecifier()));
3182 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3183 nestedNameSpecifier()));
3184
3185 EXPECT_TRUE(matches(
3186 "struct A { static void f() {} }; void g() { A::f(); }",
3187 nestedNameSpecifier()));
3188 EXPECT_TRUE(notMatches(
3189 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3190 nestedNameSpecifier()));
3191}
3192
Daniel Jasperb54b7642012-09-20 14:12:57 +00003193TEST(NullStatement, SimpleCases) {
3194 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3195 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3196}
3197
Daniel Jaspera7564432012-09-13 13:11:25 +00003198TEST(NNS, MatchesTypes) {
3199 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3200 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3201 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3202 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3203 Matcher));
3204 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3205}
3206
3207TEST(NNS, MatchesNamespaceDecls) {
3208 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3209 specifiesNamespace(hasName("ns")));
3210 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3211 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3212 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3213}
3214
3215TEST(NNS, BindsNestedNameSpecifiers) {
3216 EXPECT_TRUE(matchAndVerifyResultTrue(
3217 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3218 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3219 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3220}
3221
3222TEST(NNS, BindsNestedNameSpecifierLocs) {
3223 EXPECT_TRUE(matchAndVerifyResultTrue(
3224 "namespace ns { struct B {}; } ns::B b;",
3225 loc(nestedNameSpecifier()).bind("loc"),
3226 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3227}
3228
3229TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3230 EXPECT_TRUE(matches(
3231 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3232 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3233 EXPECT_TRUE(matches(
3234 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003235 nestedNameSpecifierLoc(hasPrefix(
3236 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003237}
3238
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003239template <typename T>
3240class VerifyRecursiveMatch : public BoundNodesCallback {
3241public:
3242 explicit VerifyRecursiveMatch(StringRef Id,
3243 const internal::Matcher<T> &InnerMatcher)
3244 : Id(Id), InnerMatcher(InnerMatcher) {}
Daniel Jasper452abbc2012-10-29 10:48:25 +00003245
3246 virtual bool run(const BoundNodes *Nodes) {
3247 return false;
3248 }
3249
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003250 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3251 const T *Node = Nodes->getNodeAs<T>(Id);
3252 bool Found = false;
3253 MatchFinder Finder;
3254 Finder.addMatcher(InnerMatcher, new VerifyMatch(0, &Found));
3255 Finder.findAll(*Node, *Context);
3256 return Found;
3257 }
3258private:
3259 std::string Id;
3260 internal::Matcher<T> InnerMatcher;
3261};
3262
3263TEST(MatchFinder, CanMatchDeclarationsRecursively) {
3264 EXPECT_TRUE(matchAndVerifyResultTrue("class X { class Y {}; };",
3265 recordDecl(hasName("::X")).bind("X"),
3266 new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Y")))));
3267 EXPECT_TRUE(matchAndVerifyResultFalse("class X { class Y {}; };",
3268 recordDecl(hasName("::X")).bind("X"),
3269 new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Z")))));
3270}
3271
3272TEST(MatchFinder, CanMatchStatementsRecursively) {
3273 EXPECT_TRUE(matchAndVerifyResultTrue("void f() { if (1) { for (;;) { } } }",
3274 ifStmt().bind("if"),
3275 new VerifyRecursiveMatch<clang::Stmt>("if", forStmt())));
3276 EXPECT_TRUE(matchAndVerifyResultFalse("void f() { if (1) { for (;;) { } } }",
3277 ifStmt().bind("if"),
3278 new VerifyRecursiveMatch<clang::Stmt>("if", declStmt())));
3279}
3280
Manuel Klimek4da21662012-07-06 05:48:52 +00003281} // end namespace ast_matchers
3282} // end namespace clang