blob: 6556444bc24b0a4806de40782b356ee3b390fd05 [file] [log] [blame]
Manuel Klimek4da21662012-07-06 05:48:52 +00001//===- unittest/Tooling/ASTMatchersTest.cpp - AST matcher unit tests ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ASTMatchersTest.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Tooling/Tooling.h"
14#include "gtest/gtest.h"
15
16namespace clang {
17namespace ast_matchers {
18
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000019#if GTEST_HAS_DEATH_TEST
Manuel Klimek4da21662012-07-06 05:48:52 +000020TEST(HasNameDeathTest, DiesOnEmptyName) {
21 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000022 DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000023 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
24 }, "");
25}
26
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000027TEST(HasNameDeathTest, DiesOnEmptyPattern) {
28 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000029 DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000030 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
31 }, "");
32}
33
Manuel Klimek4da21662012-07-06 05:48:52 +000034TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
35 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000036 DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000037 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
38 }, "");
39}
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000040#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000041
Manuel Klimek715c9562012-07-25 10:02:02 +000042TEST(Decl, MatchesDeclarations) {
43 EXPECT_TRUE(notMatches("", decl(usingDecl())));
44 EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
45 decl(usingDecl())));
46}
47
Manuel Klimek4da21662012-07-06 05:48:52 +000048TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000049 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +000050 EXPECT_TRUE(matches("typedef int X;", NamedX));
51 EXPECT_TRUE(matches("int X;", NamedX));
52 EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
53 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
54 EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
55 EXPECT_TRUE(matches("namespace X { }", NamedX));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000056 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek4da21662012-07-06 05:48:52 +000057
58 EXPECT_TRUE(notMatches("#define X 1", NamedX));
59}
60
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000061TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000062 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000063 EXPECT_TRUE(matches("typedef int Xa;", NamedX));
64 EXPECT_TRUE(matches("int Xb;", NamedX));
65 EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
66 EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
67 EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
68 EXPECT_TRUE(matches("namespace Xij { }", NamedX));
69 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
70
71 EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
72
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000073 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000074 EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
75 EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
76
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000077 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000078 EXPECT_TRUE(matches("int abc;", Abc));
79 EXPECT_TRUE(matches("int aFOObBARc;", Abc));
80 EXPECT_TRUE(notMatches("int cab;", Abc));
81 EXPECT_TRUE(matches("int cabc;", Abc));
82}
83
Manuel Klimek4da21662012-07-06 05:48:52 +000084TEST(DeclarationMatcher, MatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000085 DeclarationMatcher ClassMatcher(recordDecl());
Manuel Klimeke265c872012-07-10 14:21:30 +000086#if !defined(_MSC_VER)
Manuel Klimek4da21662012-07-06 05:48:52 +000087 EXPECT_FALSE(matches("", ClassMatcher));
Manuel Klimeke265c872012-07-10 14:21:30 +000088#else
89 // Matches class type_info.
90 EXPECT_TRUE(matches("", ClassMatcher));
91#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000092
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000093 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +000094 EXPECT_TRUE(matches("class X;", ClassX));
95 EXPECT_TRUE(matches("class X {};", ClassX));
96 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
97 EXPECT_TRUE(notMatches("", ClassX));
98}
99
100TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000101 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000102
103 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000104 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
105 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek4da21662012-07-06 05:48:52 +0000106 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
107 EXPECT_TRUE(notMatches("", IsDerivedFromX));
108
Daniel Jasper63d88722012-09-12 21:14:15 +0000109 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000110
111 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
112 EXPECT_TRUE(matches("class X {};", IsAX));
113 EXPECT_TRUE(matches("class X;", IsAX));
114 EXPECT_TRUE(notMatches("class Y;", IsAX));
115 EXPECT_TRUE(notMatches("", IsAX));
116
Manuel Klimek4da21662012-07-06 05:48:52 +0000117 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000118 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000119 EXPECT_TRUE(
120 matches("class X {}; class Y : public X {}; class Z : public Y {};",
121 ZIsDerivedFromX));
122 EXPECT_TRUE(
123 matches("class X {};"
124 "template<class T> class Y : public X {};"
125 "class Z : public Y<int> {};", ZIsDerivedFromX));
126 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
127 ZIsDerivedFromX));
128 EXPECT_TRUE(
129 matches("template<class T> class X {}; "
130 "template<class T> class Z : public X<T> {};",
131 ZIsDerivedFromX));
132 EXPECT_TRUE(
133 matches("template<class T, class U=T> class X {}; "
134 "template<class T> class Z : public X<T> {};",
135 ZIsDerivedFromX));
136 EXPECT_TRUE(
137 notMatches("template<class X> class A { class Z : public X {}; };",
138 ZIsDerivedFromX));
139 EXPECT_TRUE(
140 matches("template<class X> class A { public: class Z : public X {}; }; "
141 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
142 EXPECT_TRUE(
143 matches("template <class T> class X {}; "
144 "template<class Y> class A { class Z : public X<Y> {}; };",
145 ZIsDerivedFromX));
146 EXPECT_TRUE(
147 notMatches("template<template<class T> class X> class A { "
148 " class Z : public X<int> {}; };", ZIsDerivedFromX));
149 EXPECT_TRUE(
150 matches("template<template<class T> class X> class A { "
151 " public: class Z : public X<int> {}; }; "
152 "template<class T> class X {}; void y() { A<X>::Z z; }",
153 ZIsDerivedFromX));
154 EXPECT_TRUE(
155 notMatches("template<class X> class A { class Z : public X::D {}; };",
156 ZIsDerivedFromX));
157 EXPECT_TRUE(
158 matches("template<class X> class A { public: "
159 " class Z : public X::D {}; }; "
160 "class Y { public: class X {}; typedef X D; }; "
161 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
162 EXPECT_TRUE(
163 matches("class X {}; typedef X Y; class Z : public Y {};",
164 ZIsDerivedFromX));
165 EXPECT_TRUE(
166 matches("template<class T> class Y { typedef typename T::U X; "
167 " class Z : public X {}; };", ZIsDerivedFromX));
168 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
169 ZIsDerivedFromX));
170 EXPECT_TRUE(
171 notMatches("template<class T> class X {}; "
172 "template<class T> class A { class Z : public X<T>::D {}; };",
173 ZIsDerivedFromX));
174 EXPECT_TRUE(
175 matches("template<class T> class X { public: typedef X<T> D; }; "
176 "template<class T> class A { public: "
177 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
178 ZIsDerivedFromX));
179 EXPECT_TRUE(
180 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
181 ZIsDerivedFromX));
182 EXPECT_TRUE(
183 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
184 ZIsDerivedFromX));
185 EXPECT_TRUE(
186 matches("class X {}; class Y : public X {}; "
187 "typedef Y V; typedef V W; class Z : public W {};",
188 ZIsDerivedFromX));
189 EXPECT_TRUE(
190 matches("template<class T, class U> class X {}; "
191 "template<class T> class A { class Z : public X<T, int> {}; };",
192 ZIsDerivedFromX));
193 EXPECT_TRUE(
194 notMatches("template<class X> class D { typedef X A; typedef A B; "
195 " typedef B C; class Z : public C {}; };",
196 ZIsDerivedFromX));
197 EXPECT_TRUE(
198 matches("class X {}; typedef X A; typedef A B; "
199 "class Z : public B {};", ZIsDerivedFromX));
200 EXPECT_TRUE(
201 matches("class X {}; typedef X A; typedef A B; typedef B C; "
202 "class Z : public C {};", ZIsDerivedFromX));
203 EXPECT_TRUE(
204 matches("class U {}; typedef U X; typedef X V; "
205 "class Z : public V {};", ZIsDerivedFromX));
206 EXPECT_TRUE(
207 matches("class Base {}; typedef Base X; "
208 "class Z : public Base {};", ZIsDerivedFromX));
209 EXPECT_TRUE(
210 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
211 "class Z : public Base {};", ZIsDerivedFromX));
212 EXPECT_TRUE(
213 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
214 "class Z : public Base {};", ZIsDerivedFromX));
215 EXPECT_TRUE(
216 matches("class A {}; typedef A X; typedef A Y; "
217 "class Z : public Y {};", ZIsDerivedFromX));
218 EXPECT_TRUE(
219 notMatches("template <typename T> class Z;"
220 "template <> class Z<void> {};"
221 "template <typename T> class Z : public Z<void> {};",
222 IsDerivedFromX));
223 EXPECT_TRUE(
224 matches("template <typename T> class X;"
225 "template <> class X<void> {};"
226 "template <typename T> class X : public X<void> {};",
227 IsDerivedFromX));
228 EXPECT_TRUE(matches(
229 "class X {};"
230 "template <typename T> class Z;"
231 "template <> class Z<void> {};"
232 "template <typename T> class Z : public Z<void>, public X {};",
233 ZIsDerivedFromX));
234
235 // FIXME: Once we have better matchers for template type matching,
236 // get rid of the Variable(...) matching and match the right template
237 // declarations directly.
238 const char *RecursiveTemplateOneParameter =
239 "class Base1 {}; class Base2 {};"
240 "template <typename T> class Z;"
241 "template <> class Z<void> : public Base1 {};"
242 "template <> class Z<int> : public Base2 {};"
243 "template <> class Z<float> : public Z<void> {};"
244 "template <> class Z<double> : public Z<int> {};"
245 "template <typename T> class Z : public Z<float>, public Z<double> {};"
246 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
247 EXPECT_TRUE(matches(
248 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000249 varDecl(hasName("z_float"),
250 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000251 EXPECT_TRUE(notMatches(
252 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000253 varDecl(hasName("z_float"),
254 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000255 EXPECT_TRUE(matches(
256 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000257 varDecl(hasName("z_char"),
258 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
259 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000260
261 const char *RecursiveTemplateTwoParameters =
262 "class Base1 {}; class Base2 {};"
263 "template <typename T1, typename T2> class Z;"
264 "template <typename T> class Z<void, T> : public Base1 {};"
265 "template <typename T> class Z<int, T> : public Base2 {};"
266 "template <typename T> class Z<float, T> : public Z<void, T> {};"
267 "template <typename T> class Z<double, T> : public Z<int, T> {};"
268 "template <typename T1, typename T2> class Z : "
269 " public Z<float, T2>, public Z<double, T2> {};"
270 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
271 " Z<char, void> z_char; }";
272 EXPECT_TRUE(matches(
273 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000274 varDecl(hasName("z_float"),
275 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000276 EXPECT_TRUE(notMatches(
277 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000278 varDecl(hasName("z_float"),
279 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000280 EXPECT_TRUE(matches(
281 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000282 varDecl(hasName("z_char"),
283 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
284 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000285 EXPECT_TRUE(matches(
286 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000287 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000288 EXPECT_TRUE(notMatches(
289 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000290 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000291
292 EXPECT_TRUE(matches(
293 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000294 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000295}
296
Daniel Jasper08f0c532012-09-18 14:17:42 +0000297TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
298 EXPECT_TRUE(matches(
299 "template <typename T> struct A {"
300 " template <typename T2> struct F {};"
301 "};"
302 "template <typename T> struct B : A<T>::template F<T> {};"
303 "B<int> b;",
304 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
305}
306
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000307TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000308 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000309 EXPECT_TRUE(notMatches("class X;", ClassX));
310 EXPECT_TRUE(notMatches("class X {};", ClassX));
311}
312
313TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000314 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000315 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
316 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
317}
318
319TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
320 EXPECT_TRUE(notMatches("template<typename T> class X { };"
321 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000322 classTemplateDecl(hasName("X"),
323 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000324}
325
326TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
327 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
328 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000329 classTemplateDecl(hasName("X"),
330 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000331}
332
Daniel Jasper6a124492012-07-12 08:50:38 +0000333TEST(AllOf, AllOverloadsWork) {
334 const char Program[] =
335 "struct T { }; int f(int, T*); void g(int x) { T t; f(x, &t); }";
336 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000337 callExpr(allOf(callee(functionDecl(hasName("f"))),
338 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000339 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000340 callExpr(allOf(callee(functionDecl(hasName("f"))),
341 hasArgument(0, declRefExpr(to(varDecl()))),
342 hasArgument(1, hasType(pointsTo(
343 recordDecl(hasName("T")))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000344}
345
Manuel Klimek4da21662012-07-06 05:48:52 +0000346TEST(DeclarationMatcher, MatchAnyOf) {
347 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000348 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000349 EXPECT_TRUE(
350 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
351 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
352 EXPECT_TRUE(
353 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
354 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
355
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000356 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000357 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000358 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
359 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
360
Manuel Klimek4da21662012-07-06 05:48:52 +0000361 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000362 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
363 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000364 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
365 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
366 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
367 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
368 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
369 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
370}
371
372TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000373 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000374 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
375 EXPECT_TRUE(matches("class X {};", HasClassX));
376
377 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000378 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000379 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
380 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
381 EXPECT_TRUE(
382 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
383}
384
385TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
386 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000387 recordDecl(
388 has(recordDecl(
389 has(recordDecl(hasName("X"))),
390 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000391 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000392 has(recordDecl(
393 has(recordDecl(hasName("A"))),
394 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000395 hasName("C"))),
396 hasName("F"));
397
398 EXPECT_TRUE(matches(
399 "class F {"
400 " class Z {"
401 " class X {};"
402 " class Y {};"
403 " };"
404 " class C {"
405 " class A {};"
406 " class B {};"
407 " };"
408 "};", Recursive));
409
410 EXPECT_TRUE(matches(
411 "class F {"
412 " class Z {"
413 " class A {};"
414 " class X {};"
415 " class Y {};"
416 " };"
417 " class C {"
418 " class X {};"
419 " class A {};"
420 " class B {};"
421 " };"
422 "};", Recursive));
423
424 EXPECT_TRUE(matches(
425 "class O1 {"
426 " class O2 {"
427 " class F {"
428 " class Z {"
429 " class A {};"
430 " class X {};"
431 " class Y {};"
432 " };"
433 " class C {"
434 " class X {};"
435 " class A {};"
436 " class B {};"
437 " };"
438 " };"
439 " };"
440 "};", Recursive));
441}
442
443TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
444 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000445 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000446 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000447 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000448 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000449 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000450 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000451 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000452 hasName("Y"))),
453 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000454 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000455 anyOf(
456 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000457 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000458 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000459 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000460 hasName("B")))))),
461 hasName("F")));
462
463 EXPECT_TRUE(matches("class F {};", Recursive));
464 EXPECT_TRUE(matches("class Z {};", Recursive));
465 EXPECT_TRUE(matches("class C {};", Recursive));
466 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
467 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
468 EXPECT_TRUE(
469 matches("class O1 { class O2 {"
470 " class M { class N { class B {}; }; }; "
471 "}; };", Recursive));
472}
473
474TEST(DeclarationMatcher, MatchNot) {
475 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000476 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000477 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000478 unless(hasName("X")));
479 EXPECT_TRUE(notMatches("", NotClassX));
480 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
481 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
482 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
483 EXPECT_TRUE(
484 notMatches("class Y {}; class Z {}; class X : public Y {};",
485 NotClassX));
486
487 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000488 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000489 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000490 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000491 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000492 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000493 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
494 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
495 ClassXHasNotClassY));
496}
497
498TEST(DeclarationMatcher, HasDescendant) {
499 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000500 recordDecl(
501 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000502 hasName("Z"));
503 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
504 EXPECT_TRUE(
505 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
506 EXPECT_TRUE(
507 matches("class Z { class A { class Y { class X {}; }; }; };",
508 ZDescendantClassX));
509 EXPECT_TRUE(
510 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
511 ZDescendantClassX));
512 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
513
514 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000515 recordDecl(
516 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000517 hasName("X"))),
518 hasName("Z"));
519 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
520 ZDescendantClassXHasClassY));
521 EXPECT_TRUE(
522 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
523 ZDescendantClassXHasClassY));
524 EXPECT_TRUE(notMatches(
525 "class Z {"
526 " class A {"
527 " class B {"
528 " class X {"
529 " class C {"
530 " class Y {};"
531 " };"
532 " };"
533 " }; "
534 " };"
535 "};", ZDescendantClassXHasClassY));
536
537 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000538 recordDecl(
539 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
540 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000541 hasName("Z"));
542 EXPECT_TRUE(
543 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
544 ZDescendantClassXDescendantClassY));
545 EXPECT_TRUE(matches(
546 "class Z {"
547 " class A {"
548 " class X {"
549 " class B {"
550 " class Y {};"
551 " };"
552 " class Y {};"
553 " };"
554 " };"
555 "};", ZDescendantClassXDescendantClassY));
556}
557
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000558TEST(Enum, DoesNotMatchClasses) {
559 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
560}
561
562TEST(Enum, MatchesEnums) {
563 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
564}
565
566TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000567 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000568 EXPECT_TRUE(matches("enum X{ A };", Matcher));
569 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
570 EXPECT_TRUE(notMatches("enum X {};", Matcher));
571}
572
Manuel Klimek4da21662012-07-06 05:48:52 +0000573TEST(StatementMatcher, Has) {
574 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000575 expr(hasType(pointsTo(recordDecl(hasName("X")))),
576 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000577
578 EXPECT_TRUE(matches(
579 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
580 EXPECT_TRUE(notMatches(
581 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
582}
583
584TEST(StatementMatcher, HasDescendant) {
585 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000586 expr(hasType(pointsTo(recordDecl(hasName("X")))),
587 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000588
589 EXPECT_TRUE(matches(
590 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
591 HasDescendantVariableI));
592 EXPECT_TRUE(notMatches(
593 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
594 HasDescendantVariableI));
595}
596
597TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000598 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000599
600 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
601 EXPECT_TRUE(notMatches("class A {};", TypeA));
602
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000603 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000604
605 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
606 TypeDerivedFromA));
607 EXPECT_TRUE(notMatches("class A {};", TypeA));
608
609 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000610 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000611
612 EXPECT_TRUE(
613 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
614}
615
Manuel Klimek579b1202012-09-07 09:26:10 +0000616// Implements a run method that returns whether BoundNodes contains a
617// Decl bound to Id that can be dynamically cast to T.
Manuel Klimek4da21662012-07-06 05:48:52 +0000618// Optionally checks that the check succeeded a specific number of times.
619template <typename T>
Daniel Jaspera7564432012-09-13 13:11:25 +0000620class VerifyIdIsBoundTo : public BoundNodesCallback {
Manuel Klimek4da21662012-07-06 05:48:52 +0000621public:
Manuel Klimek579b1202012-09-07 09:26:10 +0000622 // Create an object that checks that a node of type \c T was bound to \c Id.
Manuel Klimek4da21662012-07-06 05:48:52 +0000623 // Does not check for a certain number of matches.
Daniel Jaspera7564432012-09-13 13:11:25 +0000624 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
Manuel Klimek4da21662012-07-06 05:48:52 +0000625 : Id(Id), ExpectedCount(-1), Count(0) {}
626
Manuel Klimek579b1202012-09-07 09:26:10 +0000627 // Create an object that checks that a node of type \c T was bound to \c Id.
628 // Checks that there were exactly \c ExpectedCount matches.
Daniel Jaspera7564432012-09-13 13:11:25 +0000629 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
Manuel Klimek4da21662012-07-06 05:48:52 +0000630 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
631
Manuel Klimek579b1202012-09-07 09:26:10 +0000632 // Create an object that checks that a node of type \c T was bound to \c Id.
Daniel Jaspera7564432012-09-13 13:11:25 +0000633 // Checks that there was exactly one match with the name \c ExpectedName.
Manuel Klimek579b1202012-09-07 09:26:10 +0000634 // Note that \c T must be a NamedDecl for this to work.
Daniel Jaspera7564432012-09-13 13:11:25 +0000635 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName)
636 : Id(Id), ExpectedCount(1), Count(0), ExpectedName(ExpectedName) {}
Manuel Klimek579b1202012-09-07 09:26:10 +0000637
Daniel Jaspera7564432012-09-13 13:11:25 +0000638 ~VerifyIdIsBoundTo() {
Manuel Klimek579b1202012-09-07 09:26:10 +0000639 if (ExpectedCount != -1)
Manuel Klimek4da21662012-07-06 05:48:52 +0000640 EXPECT_EQ(ExpectedCount, Count);
Daniel Jaspera7564432012-09-13 13:11:25 +0000641 if (!ExpectedName.empty())
642 EXPECT_EQ(ExpectedName, Name);
Manuel Klimek4da21662012-07-06 05:48:52 +0000643 }
644
645 virtual bool run(const BoundNodes *Nodes) {
Daniel Jaspera7564432012-09-13 13:11:25 +0000646 if (Nodes->getNodeAs<T>(Id)) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000647 ++Count;
Daniel Jaspera7564432012-09-13 13:11:25 +0000648 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
649 Name = Named->getNameAsString();
650 } else if (const NestedNameSpecifier *NNS =
651 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
652 llvm::raw_string_ostream OS(Name);
653 NNS->print(OS, PrintingPolicy(LangOptions()));
Manuel Klimek579b1202012-09-07 09:26:10 +0000654 }
Manuel Klimek4da21662012-07-06 05:48:52 +0000655 return true;
656 }
657 return false;
658 }
659
660private:
661 const std::string Id;
662 const int ExpectedCount;
663 int Count;
Daniel Jaspera7564432012-09-13 13:11:25 +0000664 const std::string ExpectedName;
665 std::string Name;
Manuel Klimek4da21662012-07-06 05:48:52 +0000666};
667
668TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000669 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000670
671 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000672 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000673
674 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000675 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000676
677 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000678 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000679
680 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
681 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000682 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000683
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000684 StatementMatcher MethodX =
685 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000686
687 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
688 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000689 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000690}
691
692TEST(Matcher, BindTheSameNameInAlternatives) {
693 StatementMatcher matcher = anyOf(
694 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000695 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000696 hasRHS(integerLiteral(equals(0)))),
697 binaryOperator(hasOperatorName("+"),
698 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000699 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000700
701 EXPECT_TRUE(matchAndVerifyResultTrue(
702 // The first branch of the matcher binds x to 0 but then fails.
703 // The second branch binds x to f() and succeeds.
704 "int f() { return 0 + f(); }",
705 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000706 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000707}
708
Manuel Klimek66341c52012-08-30 19:41:06 +0000709TEST(Matcher, BindsIDForMemoizedResults) {
710 // Using the same matcher in two match expressions will make memoization
711 // kick in.
712 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
713 EXPECT_TRUE(matchAndVerifyResultTrue(
714 "class A { class B { class X {}; }; };",
715 DeclarationMatcher(anyOf(
716 recordDecl(hasName("A"), hasDescendant(ClassX)),
717 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000718 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000719}
720
Manuel Klimek4da21662012-07-06 05:48:52 +0000721TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000722 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000723 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000724 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000725 EXPECT_TRUE(
726 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000727 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000728 EXPECT_TRUE(
729 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000730 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000731}
732
733TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000734 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000735 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000736 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000737 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000738 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000739 EXPECT_TRUE(
740 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000741 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000742}
743
744TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000745 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000746 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000747 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000748 EXPECT_TRUE(
749 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000750 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000751}
752
753TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000754 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000755 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000756 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000757 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000758 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000759}
760
761TEST(Matcher, Call) {
762 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000763 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000764 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000765
766 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
767 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
768
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000769 StatementMatcher MethodOnY =
770 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000771
772 EXPECT_TRUE(
773 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
774 MethodOnY));
775 EXPECT_TRUE(
776 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
777 MethodOnY));
778 EXPECT_TRUE(
779 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
780 MethodOnY));
781 EXPECT_TRUE(
782 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
783 MethodOnY));
784 EXPECT_TRUE(
785 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
786 MethodOnY));
787
788 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000789 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000790
791 EXPECT_TRUE(
792 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
793 MethodOnYPointer));
794 EXPECT_TRUE(
795 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
796 MethodOnYPointer));
797 EXPECT_TRUE(
798 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
799 MethodOnYPointer));
800 EXPECT_TRUE(
801 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
802 MethodOnYPointer));
803 EXPECT_TRUE(
804 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
805 MethodOnYPointer));
806}
807
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000808TEST(HasType, MatchesAsString) {
809 EXPECT_TRUE(
810 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000811 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000812 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000813 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000814 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000815 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000816 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000817 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000818}
819
Manuel Klimek4da21662012-07-06 05:48:52 +0000820TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000821 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000822 // Unary operator
823 EXPECT_TRUE(matches("class Y { }; "
824 "bool operator!(Y x) { return false; }; "
825 "Y y; bool c = !y;", OpCall));
826 // No match -- special operators like "new", "delete"
827 // FIXME: operator new takes size_t, for which we need stddef.h, for which
828 // we need to figure out include paths in the test.
829 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
830 // "class Y { }; "
831 // "void *operator new(size_t size) { return 0; } "
832 // "Y *y = new Y;", OpCall));
833 EXPECT_TRUE(notMatches("class Y { }; "
834 "void operator delete(void *p) { } "
835 "void a() {Y *y = new Y; delete y;}", OpCall));
836 // Binary operator
837 EXPECT_TRUE(matches("class Y { }; "
838 "bool operator&&(Y x, Y y) { return true; }; "
839 "Y a; Y b; bool c = a && b;",
840 OpCall));
841 // No match -- normal operator, not an overloaded one.
842 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
843 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
844}
845
846TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
847 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000848 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000849 EXPECT_TRUE(matches("class Y { }; "
850 "bool operator&&(Y x, Y y) { return true; }; "
851 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
852 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000853 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000854 EXPECT_TRUE(notMatches("class Y { }; "
855 "bool operator&&(Y x, Y y) { return true; }; "
856 "Y a; Y b; bool c = a && b;",
857 OpCallLessLess));
858}
859
860TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +0000861 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000862 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000863
864 EXPECT_TRUE(
865 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
866 MethodOnY));
867 EXPECT_TRUE(
868 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
869 MethodOnY));
870 EXPECT_TRUE(
871 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
872 MethodOnY));
873 EXPECT_TRUE(
874 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
875 MethodOnY));
876 EXPECT_TRUE(
877 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
878 MethodOnY));
879
880 EXPECT_TRUE(matches(
881 "class Y {"
882 " public: virtual void x();"
883 "};"
884 "class X : public Y {"
885 " public: virtual void x();"
886 "};"
887 "void z() { X *x; x->Y::x(); }", MethodOnY));
888}
889
890TEST(Matcher, VariableUsage) {
891 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000892 declRefExpr(to(
893 varDecl(hasInitializer(
894 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000895
896 EXPECT_TRUE(matches(
897 "class Y {"
898 " public:"
899 " bool x() const;"
900 "};"
901 "void z(const Y &y) {"
902 " bool b = y.x();"
903 " if (b) {}"
904 "}", Reference));
905
906 EXPECT_TRUE(notMatches(
907 "class Y {"
908 " public:"
909 " bool x() const;"
910 "};"
911 "void z(const Y &y) {"
912 " bool b = y.x();"
913 "}", Reference));
914}
915
Daniel Jasper9bd28092012-07-30 05:03:25 +0000916TEST(Matcher, FindsVarDeclInFuncitonParameter) {
917 EXPECT_TRUE(matches(
918 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000919 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +0000920}
921
Manuel Klimek4da21662012-07-06 05:48:52 +0000922TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +0000923 StatementMatcher CallOnVariableY =
924 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000925
926 EXPECT_TRUE(matches(
927 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
928 EXPECT_TRUE(matches(
929 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
930 EXPECT_TRUE(matches(
931 "class Y { public: void x(); };"
932 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
933 EXPECT_TRUE(matches(
934 "class Y { public: void x(); };"
935 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
936 EXPECT_TRUE(notMatches(
937 "class Y { public: void x(); };"
938 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
939 CallOnVariableY));
940}
941
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000942TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
943 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
944 unaryExprOrTypeTraitExpr()));
945 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
946 alignOfExpr(anything())));
947 // FIXME: Uncomment once alignof is enabled.
948 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
949 // unaryExprOrTypeTraitExpr()));
950 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
951 // sizeOfExpr()));
952}
953
954TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
955 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
956 hasArgumentOfType(asString("int")))));
957 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
958 hasArgumentOfType(asString("float")))));
959 EXPECT_TRUE(matches(
960 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000961 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000962 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000963 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000964}
965
Manuel Klimek4da21662012-07-06 05:48:52 +0000966TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000967 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000968}
969
970TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000971 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000972}
973
974TEST(MemberExpression, MatchesVariable) {
975 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000976 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000977 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000978 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000979 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000980 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000981}
982
983TEST(MemberExpression, MatchesStaticVariable) {
984 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000985 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000986 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000987 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000988 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000989 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000990}
991
Daniel Jasper6a124492012-07-12 08:50:38 +0000992TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000993 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
994 EXPECT_TRUE(matches(
995 "long long i = 0; void f(long long) { }; void g() {f(i);}",
996 callExpr(hasArgument(0, declRefExpr(
997 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000998}
999
1000TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001001 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001002 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001003 callExpr(hasArgument(0, declRefExpr(
1004 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001005}
1006
Manuel Klimek4da21662012-07-06 05:48:52 +00001007TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1008 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001009 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001010 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001011 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001012 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001013 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001014}
1015
1016TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1017 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001018 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001019 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001020 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001021 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001022 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001023}
1024
1025TEST(IsArrow, MatchesMemberCallsViaArrow) {
1026 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001027 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001028 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001029 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001030 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001031 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001032}
1033
1034TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001035 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001036
1037 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1038 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1039}
1040
1041TEST(Callee, MatchesMemberExpressions) {
1042 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001043 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001044 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001045 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001046}
1047
1048TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001049 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001050
1051 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1052 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1053
Manuel Klimeke265c872012-07-10 14:21:30 +00001054#if !defined(_MSC_VER)
1055 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001056 // Dependent contexts, but a non-dependent call.
1057 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1058 CallFunctionF));
1059 EXPECT_TRUE(
1060 matches("void f(); template <int N> struct S { void g() { f(); } };",
1061 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001062#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001063
1064 // Depedent calls don't match.
1065 EXPECT_TRUE(
1066 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1067 CallFunctionF));
1068 EXPECT_TRUE(
1069 notMatches("void f(int);"
1070 "template <typename T> struct S { void g(T t) { f(t); } };",
1071 CallFunctionF));
1072}
1073
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001074TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1075 EXPECT_TRUE(
1076 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001077 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001078}
1079
1080TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1081 EXPECT_TRUE(
1082 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001083 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001084}
1085
1086TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1087 EXPECT_TRUE(
1088 notMatches("void g(); template <typename T> void f(T t) {}"
1089 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001090 functionTemplateDecl(hasName("f"),
1091 hasDescendant(declRefExpr(to(
1092 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001093}
1094
Manuel Klimek4da21662012-07-06 05:48:52 +00001095TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001096 StatementMatcher CallArgumentY = callExpr(
1097 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001098
1099 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1100 EXPECT_TRUE(
1101 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1102 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1103
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001104 StatementMatcher WrongIndex = callExpr(
1105 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001106 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1107}
1108
1109TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001110 StatementMatcher CallArgumentY = callExpr(
1111 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001112 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1113 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1114 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1115}
1116
1117TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001118 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001119
1120 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1121 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1122 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1123}
1124
1125TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001126 DeclarationMatcher ReferenceClassX = varDecl(
1127 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001128 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1129 ReferenceClassX));
1130 EXPECT_TRUE(
1131 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1132 EXPECT_TRUE(
1133 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1134 EXPECT_TRUE(
1135 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1136}
1137
1138TEST(HasParameter, CallsInnerMatcher) {
1139 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001140 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001141 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001142 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001143}
1144
1145TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1146 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001147 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001148}
1149
1150TEST(HasType, MatchesParameterVariableTypesStrictly) {
1151 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001152 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001153 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001154 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001155 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001156 methodDecl(hasParameter(0,
1157 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001158 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001159 methodDecl(hasParameter(0,
1160 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001161}
1162
1163TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1164 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001165 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001166 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001167 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001168}
1169
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001170TEST(Returns, MatchesReturnTypes) {
1171 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001172 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001173 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001174 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001175 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001176 functionDecl(returns(hasDeclaration(
1177 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001178}
1179
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001180TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001181 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1182 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1183 functionDecl(isExternC())));
1184 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001185}
1186
Manuel Klimek4da21662012-07-06 05:48:52 +00001187TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1188 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001189 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001190}
1191
1192TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1193 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001194 methodDecl(hasAnyParameter(hasType(pointsTo(
1195 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001196}
1197
1198TEST(HasName, MatchesParameterVariableDeclartions) {
1199 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001200 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001201 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001202 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001203}
1204
Daniel Jasper371f9392012-08-01 08:40:24 +00001205TEST(Matcher, MatchesClassTemplateSpecialization) {
1206 EXPECT_TRUE(matches("template<typename T> struct A {};"
1207 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001208 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001209 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001210 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001211 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001212 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001213}
1214
1215TEST(Matcher, MatchesTypeTemplateArgument) {
1216 EXPECT_TRUE(matches(
1217 "template<typename T> struct B {};"
1218 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001219 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001220 asString("int"))))));
1221}
1222
1223TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1224 EXPECT_TRUE(matches(
1225 "struct B { int next; };"
1226 "template<int(B::*next_ptr)> struct A {};"
1227 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001228 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1229 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001230}
1231
1232TEST(Matcher, MatchesSpecificArgument) {
1233 EXPECT_TRUE(matches(
1234 "template<typename T, typename U> class A {};"
1235 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001236 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001237 1, refersToType(asString("int"))))));
1238 EXPECT_TRUE(notMatches(
1239 "template<typename T, typename U> class A {};"
1240 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001241 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001242 1, refersToType(asString("int"))))));
1243}
1244
Manuel Klimek4da21662012-07-06 05:48:52 +00001245TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001246 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001247
1248 EXPECT_TRUE(
1249 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1250 EXPECT_TRUE(
1251 matches("class X { public: X(); }; void x() { X x = X(); }",
1252 Constructor));
1253 EXPECT_TRUE(
1254 matches("class X { public: X(int); }; void x() { X x = 0; }",
1255 Constructor));
1256 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1257}
1258
1259TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001260 StatementMatcher Constructor = constructExpr(
1261 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001262
1263 EXPECT_TRUE(
1264 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1265 Constructor));
1266 EXPECT_TRUE(
1267 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1268 Constructor));
1269 EXPECT_TRUE(
1270 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1271 Constructor));
1272 EXPECT_TRUE(
1273 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1274 Constructor));
1275
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001276 StatementMatcher WrongIndex = constructExpr(
1277 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001278 EXPECT_TRUE(
1279 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1280 WrongIndex));
1281}
1282
1283TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001284 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001285
1286 EXPECT_TRUE(
1287 matches("class X { public: X(int); }; void x() { X x(0); }",
1288 Constructor1Arg));
1289 EXPECT_TRUE(
1290 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1291 Constructor1Arg));
1292 EXPECT_TRUE(
1293 matches("class X { public: X(int); }; void x() { X x = 0; }",
1294 Constructor1Arg));
1295 EXPECT_TRUE(
1296 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1297 Constructor1Arg));
1298}
1299
1300TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001301 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001302
1303 std::string ClassString = "class string { public: string(); ~string(); }; ";
1304
1305 EXPECT_TRUE(
1306 matches(ClassString +
1307 "string GetStringByValue();"
1308 "void FunctionTakesString(string s);"
1309 "void run() { FunctionTakesString(GetStringByValue()); }",
1310 TempExpression));
1311
1312 EXPECT_TRUE(
1313 notMatches(ClassString +
1314 "string* GetStringPointer(); "
1315 "void FunctionTakesStringPtr(string* s);"
1316 "void run() {"
1317 " string* s = GetStringPointer();"
1318 " FunctionTakesStringPtr(GetStringPointer());"
1319 " FunctionTakesStringPtr(s);"
1320 "}",
1321 TempExpression));
1322
1323 EXPECT_TRUE(
1324 notMatches("class no_dtor {};"
1325 "no_dtor GetObjByValue();"
1326 "void ConsumeObj(no_dtor param);"
1327 "void run() { ConsumeObj(GetObjByValue()); }",
1328 TempExpression));
1329}
1330
Sam Panzere16acd32012-08-24 22:04:44 +00001331TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1332 std::string ClassString =
1333 "class string { public: string(); int length(); }; ";
1334
1335 EXPECT_TRUE(
1336 matches(ClassString +
1337 "string GetStringByValue();"
1338 "void FunctionTakesString(string s);"
1339 "void run() { FunctionTakesString(GetStringByValue()); }",
1340 materializeTemporaryExpr()));
1341
1342 EXPECT_TRUE(
1343 notMatches(ClassString +
1344 "string* GetStringPointer(); "
1345 "void FunctionTakesStringPtr(string* s);"
1346 "void run() {"
1347 " string* s = GetStringPointer();"
1348 " FunctionTakesStringPtr(GetStringPointer());"
1349 " FunctionTakesStringPtr(s);"
1350 "}",
1351 materializeTemporaryExpr()));
1352
1353 EXPECT_TRUE(
1354 notMatches(ClassString +
1355 "string GetStringByValue();"
1356 "void run() { int k = GetStringByValue().length(); }",
1357 materializeTemporaryExpr()));
1358
1359 EXPECT_TRUE(
1360 notMatches(ClassString +
1361 "string GetStringByValue();"
1362 "void run() { GetStringByValue(); }",
1363 materializeTemporaryExpr()));
1364}
1365
Manuel Klimek4da21662012-07-06 05:48:52 +00001366TEST(ConstructorDeclaration, SimpleCase) {
1367 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001368 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001369 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001370 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001371}
1372
1373TEST(ConstructorDeclaration, IsImplicit) {
1374 // This one doesn't match because the constructor is not added by the
1375 // compiler (it is not needed).
1376 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001377 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001378 // The compiler added the implicit default constructor.
1379 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001380 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001381 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001382 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001383}
1384
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001385TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1386 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001387 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001388}
1389
1390TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001391 EXPECT_TRUE(notMatches("class Foo {};",
1392 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001393}
1394
Manuel Klimek4da21662012-07-06 05:48:52 +00001395TEST(HasAnyConstructorInitializer, SimpleCase) {
1396 EXPECT_TRUE(notMatches(
1397 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001398 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001399 EXPECT_TRUE(matches(
1400 "class Foo {"
1401 " Foo() : foo_() { }"
1402 " int foo_;"
1403 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001404 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001405}
1406
1407TEST(HasAnyConstructorInitializer, ForField) {
1408 static const char Code[] =
1409 "class Baz { };"
1410 "class Foo {"
1411 " Foo() : foo_() { }"
1412 " Baz foo_;"
1413 " Baz bar_;"
1414 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001415 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1416 forField(hasType(recordDecl(hasName("Baz"))))))));
1417 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001418 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001419 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1420 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001421}
1422
1423TEST(HasAnyConstructorInitializer, WithInitializer) {
1424 static const char Code[] =
1425 "class Foo {"
1426 " Foo() : foo_(0) { }"
1427 " int foo_;"
1428 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001429 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001430 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001431 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001432 withInitializer(integerLiteral(equals(1)))))));
1433}
1434
1435TEST(HasAnyConstructorInitializer, IsWritten) {
1436 static const char Code[] =
1437 "struct Bar { Bar(){} };"
1438 "class Foo {"
1439 " Foo() : foo_() { }"
1440 " Bar foo_;"
1441 " Bar bar_;"
1442 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001443 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001444 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001445 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001446 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001447 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001448 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1449}
1450
1451TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001452 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001453
1454 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1455 EXPECT_TRUE(
1456 matches("class X { public: X(); }; void x() { new X(); }", New));
1457 EXPECT_TRUE(
1458 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1459 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1460}
1461
1462TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001463 StatementMatcher New = constructExpr(
1464 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001465
1466 EXPECT_TRUE(
1467 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1468 New));
1469 EXPECT_TRUE(
1470 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1471 New));
1472 EXPECT_TRUE(
1473 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1474 New));
1475
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001476 StatementMatcher WrongIndex = constructExpr(
1477 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001478 EXPECT_TRUE(
1479 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1480 WrongIndex));
1481}
1482
1483TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001484 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001485
1486 EXPECT_TRUE(
1487 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1488 EXPECT_TRUE(
1489 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1490 New));
1491}
1492
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001493TEST(Matcher, DeleteExpression) {
1494 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001495 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001496}
1497
Manuel Klimek4da21662012-07-06 05:48:52 +00001498TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001499 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001500
1501 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1502 EXPECT_TRUE(
1503 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1504 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1505}
1506
1507TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001508 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001509 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1510 // wide string
1511 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1512 // with escaped characters
1513 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1514 // no matching -- though the data type is the same, there is no string literal
1515 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1516}
1517
1518TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001519 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001520 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1521 // wide character
1522 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1523 // wide character, Hex encoded, NOT MATCHED!
1524 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1525 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1526}
1527
1528TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001529 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001530 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1531 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1532 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1533 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1534
1535 // Non-matching cases (character literals, float and double)
1536 EXPECT_TRUE(notMatches("int i = L'a';",
1537 HasIntLiteral)); // this is actually a character
1538 // literal cast to int
1539 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1540 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1541 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1542}
1543
1544TEST(Matcher, Conditions) {
1545 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1546
1547 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1548 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1549 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1550 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1551 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1552}
1553
1554TEST(MatchBinaryOperator, HasOperatorName) {
1555 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1556
1557 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1558 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1559}
1560
1561TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1562 StatementMatcher OperatorTrueFalse =
1563 binaryOperator(hasLHS(boolLiteral(equals(true))),
1564 hasRHS(boolLiteral(equals(false))));
1565
1566 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1567 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1568 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1569}
1570
1571TEST(MatchBinaryOperator, HasEitherOperand) {
1572 StatementMatcher HasOperand =
1573 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1574
1575 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1576 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1577 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1578}
1579
1580TEST(Matcher, BinaryOperatorTypes) {
1581 // Integration test that verifies the AST provides all binary operators in
1582 // a way we expect.
1583 // FIXME: Operator ','
1584 EXPECT_TRUE(
1585 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1586 EXPECT_TRUE(
1587 matches("bool b; bool c = (b = true);",
1588 binaryOperator(hasOperatorName("="))));
1589 EXPECT_TRUE(
1590 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1591 EXPECT_TRUE(
1592 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1593 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1594 EXPECT_TRUE(
1595 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1596 EXPECT_TRUE(
1597 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1598 EXPECT_TRUE(
1599 matches("int i = 1; int j = (i <<= 2);",
1600 binaryOperator(hasOperatorName("<<="))));
1601 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1602 EXPECT_TRUE(
1603 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1604 EXPECT_TRUE(
1605 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1606 EXPECT_TRUE(
1607 matches("int i = 1; int j = (i >>= 2);",
1608 binaryOperator(hasOperatorName(">>="))));
1609 EXPECT_TRUE(
1610 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1611 EXPECT_TRUE(
1612 matches("int i = 42; int j = (i ^= 42);",
1613 binaryOperator(hasOperatorName("^="))));
1614 EXPECT_TRUE(
1615 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1616 EXPECT_TRUE(
1617 matches("int i = 42; int j = (i %= 42);",
1618 binaryOperator(hasOperatorName("%="))));
1619 EXPECT_TRUE(
1620 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1621 EXPECT_TRUE(
1622 matches("bool b = true && false;",
1623 binaryOperator(hasOperatorName("&&"))));
1624 EXPECT_TRUE(
1625 matches("bool b = true; bool c = (b &= false);",
1626 binaryOperator(hasOperatorName("&="))));
1627 EXPECT_TRUE(
1628 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1629 EXPECT_TRUE(
1630 matches("bool b = true || false;",
1631 binaryOperator(hasOperatorName("||"))));
1632 EXPECT_TRUE(
1633 matches("bool b = true; bool c = (b |= false);",
1634 binaryOperator(hasOperatorName("|="))));
1635 EXPECT_TRUE(
1636 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1637 EXPECT_TRUE(
1638 matches("int i = 42; int j = (i *= 23);",
1639 binaryOperator(hasOperatorName("*="))));
1640 EXPECT_TRUE(
1641 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1642 EXPECT_TRUE(
1643 matches("int i = 42; int j = (i /= 23);",
1644 binaryOperator(hasOperatorName("/="))));
1645 EXPECT_TRUE(
1646 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1647 EXPECT_TRUE(
1648 matches("int i = 42; int j = (i += 23);",
1649 binaryOperator(hasOperatorName("+="))));
1650 EXPECT_TRUE(
1651 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1652 EXPECT_TRUE(
1653 matches("int i = 42; int j = (i -= 23);",
1654 binaryOperator(hasOperatorName("-="))));
1655 EXPECT_TRUE(
1656 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1657 binaryOperator(hasOperatorName("->*"))));
1658 EXPECT_TRUE(
1659 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1660 binaryOperator(hasOperatorName(".*"))));
1661
1662 // Member expressions as operators are not supported in matches.
1663 EXPECT_TRUE(
1664 notMatches("struct A { void x(A *a) { a->x(this); } };",
1665 binaryOperator(hasOperatorName("->"))));
1666
1667 // Initializer assignments are not represented as operator equals.
1668 EXPECT_TRUE(
1669 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1670
1671 // Array indexing is not represented as operator.
1672 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1673
1674 // Overloaded operators do not match at all.
1675 EXPECT_TRUE(notMatches(
1676 "struct A { bool operator&&(const A &a) const { return false; } };"
1677 "void x() { A a, b; a && b; }",
1678 binaryOperator()));
1679}
1680
1681TEST(MatchUnaryOperator, HasOperatorName) {
1682 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1683
1684 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1685 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1686}
1687
1688TEST(MatchUnaryOperator, HasUnaryOperand) {
1689 StatementMatcher OperatorOnFalse =
1690 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1691
1692 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1693 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1694}
1695
1696TEST(Matcher, UnaryOperatorTypes) {
1697 // Integration test that verifies the AST provides all unary operators in
1698 // a way we expect.
1699 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1700 EXPECT_TRUE(
1701 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1702 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1703 EXPECT_TRUE(
1704 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1705 EXPECT_TRUE(
1706 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1707 EXPECT_TRUE(
1708 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1709 EXPECT_TRUE(
1710 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1711 EXPECT_TRUE(
1712 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1713 EXPECT_TRUE(
1714 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1715 EXPECT_TRUE(
1716 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1717
1718 // We don't match conversion operators.
1719 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1720
1721 // Function calls are not represented as operator.
1722 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1723
1724 // Overloaded operators do not match at all.
1725 // FIXME: We probably want to add that.
1726 EXPECT_TRUE(notMatches(
1727 "struct A { bool operator!() const { return false; } };"
1728 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1729}
1730
1731TEST(Matcher, ConditionalOperator) {
1732 StatementMatcher Conditional = conditionalOperator(
1733 hasCondition(boolLiteral(equals(true))),
1734 hasTrueExpression(boolLiteral(equals(false))));
1735
1736 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1737 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1738 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1739
1740 StatementMatcher ConditionalFalse = conditionalOperator(
1741 hasFalseExpression(boolLiteral(equals(false))));
1742
1743 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1744 EXPECT_TRUE(
1745 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1746}
1747
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001748TEST(ArraySubscriptMatchers, ArraySubscripts) {
1749 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1750 arraySubscriptExpr()));
1751 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1752 arraySubscriptExpr()));
1753}
1754
1755TEST(ArraySubscriptMatchers, ArrayIndex) {
1756 EXPECT_TRUE(matches(
1757 "int i[2]; void f() { i[1] = 1; }",
1758 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1759 EXPECT_TRUE(matches(
1760 "int i[2]; void f() { 1[i] = 1; }",
1761 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1762 EXPECT_TRUE(notMatches(
1763 "int i[2]; void f() { i[1] = 1; }",
1764 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1765}
1766
1767TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1768 EXPECT_TRUE(matches(
1769 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001770 arraySubscriptExpr(hasBase(implicitCastExpr(
1771 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001772}
1773
Manuel Klimek4da21662012-07-06 05:48:52 +00001774TEST(Matcher, HasNameSupportsNamespaces) {
1775 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001776 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001777 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001778 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001779 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001780 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001781 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001782 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001783 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001784 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001785 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001786 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001787 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001788 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001789 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001790 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001791 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001792 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001793 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001794 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001795 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001796 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001797 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001798 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001799}
1800
1801TEST(Matcher, HasNameSupportsOuterClasses) {
1802 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001803 matches("class A { class B { class C; }; };",
1804 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001805 EXPECT_TRUE(
1806 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001807 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001808 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001809 matches("class A { class B { class C; }; };",
1810 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001811 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001812 matches("class A { class B { class C; }; };",
1813 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001814 EXPECT_TRUE(
1815 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001816 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001817 EXPECT_TRUE(
1818 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001819 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001820 EXPECT_TRUE(
1821 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001822 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001823 EXPECT_TRUE(
1824 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001825 recordDecl(hasName("::C"))));
1826 EXPECT_TRUE(
1827 notMatches("class A { class B { class C; }; };",
1828 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001829 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001830 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001831 EXPECT_TRUE(
1832 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001833 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001834}
1835
1836TEST(Matcher, IsDefinition) {
1837 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001838 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001839 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1840 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1841
1842 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001843 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001844 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1845 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1846
1847 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001848 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001849 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1850 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1851}
1852
1853TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001854 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00001855 ofClass(hasName("X")))));
1856
1857 EXPECT_TRUE(
1858 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
1859 EXPECT_TRUE(
1860 matches("class X { public: X(); }; void x(int) { X x = X(); }",
1861 Constructor));
1862 EXPECT_TRUE(
1863 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
1864 Constructor));
1865}
1866
1867TEST(Matcher, VisitsTemplateInstantiations) {
1868 EXPECT_TRUE(matches(
1869 "class A { public: void x(); };"
1870 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001871 "void f() { B<A> b; b.y(); }",
1872 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001873
1874 EXPECT_TRUE(matches(
1875 "class A { public: void x(); };"
1876 "class C {"
1877 " public:"
1878 " template <typename T> class B { public: void y() { T t; t.x(); } };"
1879 "};"
1880 "void f() {"
1881 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001882 "}",
1883 recordDecl(hasName("C"),
1884 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001885}
1886
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001887TEST(Matcher, HandlesNullQualTypes) {
1888 // FIXME: Add a Type matcher so we can replace uses of this
1889 // variable with Type(True())
1890 const TypeMatcher AnyType = anything();
1891
1892 // We don't really care whether this matcher succeeds; we're testing that
1893 // it completes without crashing.
1894 EXPECT_TRUE(matches(
1895 "struct A { };"
1896 "template <typename T>"
1897 "void f(T t) {"
1898 " T local_t(t /* this becomes a null QualType in the AST */);"
1899 "}"
1900 "void g() {"
1901 " f(0);"
1902 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001903 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001904 anyOf(
1905 TypeMatcher(hasDeclaration(anything())),
1906 pointsTo(AnyType),
1907 references(AnyType)
1908 // Other QualType matchers should go here.
1909 ))))));
1910}
1911
Manuel Klimek4da21662012-07-06 05:48:52 +00001912// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001913AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00001914 // Make sure all special variables are used: node, match_finder,
1915 // bound_nodes_builder, and the parameter named 'AMatcher'.
1916 return AMatcher.matches(Node, Finder, Builder);
1917}
1918
1919TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001920 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001921
1922 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001923 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001924
1925 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001926 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001927
1928 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001929 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001930}
1931
1932AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001933 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
1934 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
1935 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00001936 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00001937 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00001938 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00001939 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
1940 ASTMatchFinder::BK_First);
1941}
1942
1943TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001944 DeclarationMatcher HasClassB =
1945 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001946
1947 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001948 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001949
1950 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001951 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001952
1953 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001954 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001955
1956 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001957 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001958
1959 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
1960}
1961
1962TEST(For, FindsForLoops) {
1963 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
1964 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
1965}
1966
Daniel Jasper6a124492012-07-12 08:50:38 +00001967TEST(For, ForLoopInternals) {
1968 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
1969 forStmt(hasCondition(anything()))));
1970 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
1971 forStmt(hasLoopInit(anything()))));
1972}
1973
1974TEST(For, NegativeForLoopInternals) {
1975 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001976 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001977 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
1978 forStmt(hasLoopInit(anything()))));
1979}
1980
Manuel Klimek4da21662012-07-06 05:48:52 +00001981TEST(For, ReportsNoFalsePositives) {
1982 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
1983 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
1984}
1985
1986TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001987 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
1988 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
1989 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001990}
1991
1992TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
1993 // It's not a compound statement just because there's "{}" in the source
1994 // text. This is an AST search, not grep.
1995 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001996 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001997 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001998 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001999}
2000
Daniel Jasper6a124492012-07-12 08:50:38 +00002001TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002002 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002003 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002004 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002005 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002006 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002007 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002008 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002009 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002010}
2011
2012TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2013 // The simplest case: every compound statement is in a function
2014 // definition, and the function body itself must be a compound
2015 // statement.
2016 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002017 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002018}
2019
2020TEST(HasAnySubstatement, IsNotRecursive) {
2021 // It's really "has any immediate substatement".
2022 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002023 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002024}
2025
2026TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2027 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002028 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002029}
2030
2031TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2032 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002033 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002034}
2035
2036TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2037 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002038 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002039 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002040 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002041}
2042
2043TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2044 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002045 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002046 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002047 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002048 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002049 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002050}
2051
2052TEST(StatementCountIs, WorksWithMultipleStatements) {
2053 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002054 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002055}
2056
2057TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2058 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002059 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002060 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002061 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002062 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002063 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002064 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002065 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002066}
2067
2068TEST(Member, WorksInSimplestCase) {
2069 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002070 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002071}
2072
2073TEST(Member, DoesNotMatchTheBaseExpression) {
2074 // Don't pick out the wrong part of the member expression, this should
2075 // be checking the member (name) only.
2076 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002077 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002078}
2079
2080TEST(Member, MatchesInMemberFunctionCall) {
2081 EXPECT_TRUE(matches("void f() {"
2082 " struct { void first() {}; } s;"
2083 " s.first();"
2084 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002085 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002086}
2087
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002088TEST(Member, MatchesMemberAllocationFunction) {
Dmitri Gribenko02ed37f2012-08-18 00:41:04 +00002089 EXPECT_TRUE(matches("namespace std { typedef typeof(sizeof(int)) size_t; }"
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002090 "class X { void *operator new(std::size_t); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002091 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002092
2093 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002094 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002095
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002096 EXPECT_TRUE(matches(
2097 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2098 "class X { void operator delete[](void*, std::size_t); };",
2099 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002100}
2101
Manuel Klimek4da21662012-07-06 05:48:52 +00002102TEST(HasObjectExpression, DoesNotMatchMember) {
2103 EXPECT_TRUE(notMatches(
2104 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002105 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002106}
2107
2108TEST(HasObjectExpression, MatchesBaseOfVariable) {
2109 EXPECT_TRUE(matches(
2110 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002111 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002112 EXPECT_TRUE(matches(
2113 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002114 memberExpr(hasObjectExpression(
2115 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002116}
2117
2118TEST(HasObjectExpression,
2119 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2120 EXPECT_TRUE(matches(
2121 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002122 memberExpr(hasObjectExpression(
2123 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002124 EXPECT_TRUE(matches(
2125 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002126 memberExpr(hasObjectExpression(
2127 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002128}
2129
2130TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002131 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2132 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2133 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2134 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002135}
2136
2137TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002138 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002139}
2140
2141TEST(IsConstQualified, MatchesConstInt) {
2142 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002143 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002144}
2145
2146TEST(IsConstQualified, MatchesConstPointer) {
2147 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002148 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002149}
2150
2151TEST(IsConstQualified, MatchesThroughTypedef) {
2152 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002153 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002154 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002155 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002156}
2157
2158TEST(IsConstQualified, DoesNotMatchInappropriately) {
2159 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002160 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002161 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002162 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002163}
2164
Sam Panzer089e5b32012-08-16 16:58:10 +00002165TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002166 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2167 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2168 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2169 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002170}
2171TEST(CastExpression, MatchesImplicitCasts) {
2172 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002173 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002174 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002175 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002176}
2177
2178TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002179 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2180 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2181 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2182 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002183}
2184
Manuel Klimek4da21662012-07-06 05:48:52 +00002185TEST(ReinterpretCast, MatchesSimpleCase) {
2186 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002187 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002188}
2189
2190TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002191 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002192 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002193 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002194 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002195 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002196 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2197 "B b;"
2198 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002199 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002200}
2201
2202TEST(FunctionalCast, MatchesSimpleCase) {
2203 std::string foo_class = "class Foo { public: Foo(char*); };";
2204 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002205 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002206}
2207
2208TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2209 std::string FooClass = "class Foo { public: Foo(char*); };";
2210 EXPECT_TRUE(
2211 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002212 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002213 EXPECT_TRUE(
2214 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002215 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002216}
2217
2218TEST(DynamicCast, MatchesSimpleCase) {
2219 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2220 "B b;"
2221 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002222 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002223}
2224
2225TEST(StaticCast, MatchesSimpleCase) {
2226 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002227 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002228}
2229
2230TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002231 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002232 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002233 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002234 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002235 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002236 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2237 "B b;"
2238 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002239 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002240}
2241
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002242TEST(CStyleCast, MatchesSimpleCase) {
2243 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2244}
2245
2246TEST(CStyleCast, DoesNotMatchOtherCasts) {
2247 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2248 "char q, *r = const_cast<char*>(&q);"
2249 "void* s = reinterpret_cast<char*>(&s);"
2250 "struct B { virtual ~B() {} }; struct D : B {};"
2251 "B b;"
2252 "D* t = dynamic_cast<D*>(&b);",
2253 cStyleCastExpr()));
2254}
2255
Manuel Klimek4da21662012-07-06 05:48:52 +00002256TEST(HasDestinationType, MatchesSimpleCase) {
2257 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002258 staticCastExpr(hasDestinationType(
2259 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002260}
2261
Sam Panzer089e5b32012-08-16 16:58:10 +00002262TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2263 // This test creates an implicit const cast.
2264 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002265 implicitCastExpr(
2266 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002267 // This test creates an implicit array-to-pointer cast.
2268 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002269 implicitCastExpr(hasImplicitDestinationType(
2270 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002271}
2272
2273TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2274 // This test creates an implicit cast from int to char.
2275 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002276 implicitCastExpr(hasImplicitDestinationType(
2277 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002278 // This test creates an implicit array-to-pointer cast.
2279 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002280 implicitCastExpr(hasImplicitDestinationType(
2281 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002282}
2283
2284TEST(ImplicitCast, MatchesSimpleCase) {
2285 // This test creates an implicit const cast.
2286 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002287 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002288 // This test creates an implicit cast from int to char.
2289 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002290 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002291 // This test creates an implicit array-to-pointer cast.
2292 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002293 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002294}
2295
2296TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002297 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002298 // are present, and that it ignores explicit and paren casts.
2299
2300 // These two test cases have no casts.
2301 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002302 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002303 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002304 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002305
2306 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002307 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002308 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002309 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002310
2311 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002312 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002313}
2314
2315TEST(IgnoringImpCasts, MatchesImpCasts) {
2316 // This test checks that ignoringImpCasts matches when implicit casts are
2317 // present and its inner matcher alone does not match.
2318 // Note that this test creates an implicit const cast.
2319 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002320 varDecl(hasInitializer(ignoringImpCasts(
2321 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002322 // This test creates an implict cast from int to char.
2323 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002324 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002325 integerLiteral(equals(0)))))));
2326}
2327
2328TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2329 // These tests verify that ignoringImpCasts does not match if the inner
2330 // matcher does not match.
2331 // Note that the first test creates an implicit const cast.
2332 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002333 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002334 unless(anything()))))));
2335 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002336 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002337 unless(anything()))))));
2338
2339 // These tests verify that ignoringImplictCasts does not look through explicit
2340 // casts or parentheses.
2341 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002342 varDecl(hasInitializer(ignoringImpCasts(
2343 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002344 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002345 varDecl(hasInitializer(ignoringImpCasts(
2346 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002347 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002348 varDecl(hasInitializer(ignoringImpCasts(
2349 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002350 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002351 varDecl(hasInitializer(ignoringImpCasts(
2352 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002353}
2354
2355TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2356 // This test verifies that expressions that do not have implicit casts
2357 // still match the inner matcher.
2358 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002359 varDecl(hasInitializer(ignoringImpCasts(
2360 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002361}
2362
2363TEST(IgnoringParenCasts, MatchesParenCasts) {
2364 // This test checks that ignoringParenCasts matches when parentheses and/or
2365 // casts are present and its inner matcher alone does not match.
2366 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002367 varDecl(hasInitializer(ignoringParenCasts(
2368 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002369 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002370 varDecl(hasInitializer(ignoringParenCasts(
2371 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002372
2373 // This test creates an implict cast from int to char in addition to the
2374 // parentheses.
2375 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002376 varDecl(hasInitializer(ignoringParenCasts(
2377 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002378
2379 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002380 varDecl(hasInitializer(ignoringParenCasts(
2381 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002382 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002383 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002384 integerLiteral(equals(0)))))));
2385}
2386
2387TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2388 // This test verifies that expressions that do not have any casts still match.
2389 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002390 varDecl(hasInitializer(ignoringParenCasts(
2391 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002392}
2393
2394TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2395 // These tests verify that ignoringImpCasts does not match if the inner
2396 // matcher does not match.
2397 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002398 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002399 unless(anything()))))));
2400
2401 // This test creates an implicit cast from int to char in addition to the
2402 // parentheses.
2403 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002404 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002405 unless(anything()))))));
2406
2407 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002408 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002409 unless(anything()))))));
2410}
2411
2412TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2413 // This test checks that ignoringParenAndImpCasts matches when
2414 // parentheses and/or implicit casts are present and its inner matcher alone
2415 // does not match.
2416 // Note that this test creates an implicit const cast.
2417 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002418 varDecl(hasInitializer(ignoringParenImpCasts(
2419 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002420 // This test creates an implicit cast from int to char.
2421 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002422 varDecl(hasInitializer(ignoringParenImpCasts(
2423 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002424}
2425
2426TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2427 // This test verifies that expressions that do not have parentheses or
2428 // implicit casts still match.
2429 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002430 varDecl(hasInitializer(ignoringParenImpCasts(
2431 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002432 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002433 varDecl(hasInitializer(ignoringParenImpCasts(
2434 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002435}
2436
2437TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2438 // These tests verify that ignoringParenImpCasts does not match if
2439 // the inner matcher does not match.
2440 // This test creates an implicit cast.
2441 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002442 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002443 unless(anything()))))));
2444 // These tests verify that ignoringParenAndImplictCasts does not look
2445 // through explicit casts.
2446 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002447 varDecl(hasInitializer(ignoringParenImpCasts(
2448 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002449 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002450 varDecl(hasInitializer(ignoringParenImpCasts(
2451 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002452 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002453 varDecl(hasInitializer(ignoringParenImpCasts(
2454 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002455}
2456
Manuel Klimek715c9562012-07-25 10:02:02 +00002457TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002458 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2459 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002460 implicitCastExpr(
2461 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002462}
2463
Manuel Klimek715c9562012-07-25 10:02:02 +00002464TEST(HasSourceExpression, MatchesExplicitCasts) {
2465 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002466 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002467 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002468 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002469}
2470
Manuel Klimek4da21662012-07-06 05:48:52 +00002471TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002472 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002473}
2474
2475TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002476 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002477}
2478
2479TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002480 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002481}
2482
2483TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002484 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002485}
2486
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002487TEST(InitListExpression, MatchesInitListExpression) {
2488 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2489 initListExpr(hasType(asString("int [2]")))));
2490 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002491 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002492}
2493
2494TEST(UsingDeclaration, MatchesUsingDeclarations) {
2495 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2496 usingDecl()));
2497}
2498
2499TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2500 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2501 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2502}
2503
2504TEST(UsingDeclaration, MatchesSpecificTarget) {
2505 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2506 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002507 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002508 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2509 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002510 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002511}
2512
2513TEST(UsingDeclaration, ThroughUsingDeclaration) {
2514 EXPECT_TRUE(matches(
2515 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002516 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002517 EXPECT_TRUE(notMatches(
2518 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002519 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002520}
2521
Sam Panzer425f41b2012-08-16 17:20:59 +00002522TEST(SingleDecl, IsSingleDecl) {
2523 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002524 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002525 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2526 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2527 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2528 SingleDeclStmt));
2529}
2530
2531TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002532 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002533
2534 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002535 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002536 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002537 declStmt(containsDeclaration(0, MatchesInit),
2538 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002539 unsigned WrongIndex = 42;
2540 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002541 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002542 MatchesInit))));
2543}
2544
2545TEST(DeclCount, DeclCountIsCorrect) {
2546 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002547 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002548 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002549 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002550 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002551 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002552}
2553
Manuel Klimek4da21662012-07-06 05:48:52 +00002554TEST(While, MatchesWhileLoops) {
2555 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2556 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2557 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2558}
2559
2560TEST(Do, MatchesDoLoops) {
2561 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2562 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2563}
2564
2565TEST(Do, DoesNotMatchWhileLoops) {
2566 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2567}
2568
2569TEST(SwitchCase, MatchesCase) {
2570 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2571 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2572 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2573 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2574}
2575
2576TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2577 EXPECT_TRUE(notMatches(
2578 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002579 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002580 EXPECT_TRUE(notMatches(
2581 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002582 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002583}
2584
2585TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2586 EXPECT_TRUE(matches(
2587 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002588 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002589}
2590
2591TEST(ForEach, BindsOneNode) {
2592 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002593 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002594 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002595}
2596
2597TEST(ForEach, BindsMultipleNodes) {
2598 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002599 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002600 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002601}
2602
2603TEST(ForEach, BindsRecursiveCombinations) {
2604 EXPECT_TRUE(matchAndVerifyResultTrue(
2605 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002606 recordDecl(hasName("C"),
2607 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002608 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002609}
2610
2611TEST(ForEachDescendant, BindsOneNode) {
2612 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002613 recordDecl(hasName("C"),
2614 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002615 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002616}
2617
2618TEST(ForEachDescendant, BindsMultipleNodes) {
2619 EXPECT_TRUE(matchAndVerifyResultTrue(
2620 "class C { class D { int x; int y; }; "
2621 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002622 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002623 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002624}
2625
2626TEST(ForEachDescendant, BindsRecursiveCombinations) {
2627 EXPECT_TRUE(matchAndVerifyResultTrue(
2628 "class C { class D { "
2629 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002630 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2631 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002632 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002633}
2634
2635
2636TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2637 // Make sure that we can both match the class by name (::X) and by the type
2638 // the template was instantiated with (via a field).
2639
2640 EXPECT_TRUE(matches(
2641 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002642 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002643
2644 EXPECT_TRUE(matches(
2645 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002646 recordDecl(isTemplateInstantiation(), hasDescendant(
2647 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002648}
2649
2650TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2651 EXPECT_TRUE(matches(
2652 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002653 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002654 isTemplateInstantiation())));
2655}
2656
2657TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2658 EXPECT_TRUE(matches(
2659 "template <typename T> class X { T t; }; class A {};"
2660 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002661 recordDecl(isTemplateInstantiation(), hasDescendant(
2662 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002663}
2664
2665TEST(IsTemplateInstantiation,
2666 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2667 EXPECT_TRUE(matches(
2668 "template <typename T> class X {};"
2669 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002670 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002671}
2672
2673TEST(IsTemplateInstantiation,
2674 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2675 EXPECT_TRUE(matches(
2676 "class A {};"
2677 "class X {"
2678 " template <typename U> class Y { U u; };"
2679 " Y<A> y;"
2680 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002681 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002682}
2683
2684TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2685 // FIXME: Figure out whether this makes sense. It doesn't affect the
2686 // normal use case as long as the uppermost instantiation always is marked
2687 // as template instantiation, but it might be confusing as a predicate.
2688 EXPECT_TRUE(matches(
2689 "class A {};"
2690 "template <typename T> class X {"
2691 " template <typename U> class Y { U u; };"
2692 " Y<T> y;"
2693 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002694 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002695}
2696
2697TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2698 EXPECT_TRUE(notMatches(
2699 "template <typename T> class X {}; class A {};"
2700 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002701 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002702}
2703
2704TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2705 EXPECT_TRUE(notMatches(
2706 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002707 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002708}
2709
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002710TEST(IsExplicitTemplateSpecialization,
2711 DoesNotMatchPrimaryTemplate) {
2712 EXPECT_TRUE(notMatches(
2713 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002714 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002715 EXPECT_TRUE(notMatches(
2716 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002717 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002718}
2719
2720TEST(IsExplicitTemplateSpecialization,
2721 DoesNotMatchExplicitTemplateInstantiations) {
2722 EXPECT_TRUE(notMatches(
2723 "template <typename T> class X {};"
2724 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002725 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002726 EXPECT_TRUE(notMatches(
2727 "template <typename T> void f(T t) {}"
2728 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002729 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002730}
2731
2732TEST(IsExplicitTemplateSpecialization,
2733 DoesNotMatchImplicitTemplateInstantiations) {
2734 EXPECT_TRUE(notMatches(
2735 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002736 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002737 EXPECT_TRUE(notMatches(
2738 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002739 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002740}
2741
2742TEST(IsExplicitTemplateSpecialization,
2743 MatchesExplicitTemplateSpecializations) {
2744 EXPECT_TRUE(matches(
2745 "template <typename T> class X {};"
2746 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002747 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002748 EXPECT_TRUE(matches(
2749 "template <typename T> void f(T t) {}"
2750 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002751 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002752}
2753
Manuel Klimek579b1202012-09-07 09:26:10 +00002754TEST(HasAncenstor, MatchesDeclarationAncestors) {
2755 EXPECT_TRUE(matches(
2756 "class A { class B { class C {}; }; };",
2757 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2758}
2759
2760TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2761 EXPECT_TRUE(notMatches(
2762 "class A { class B { class C {}; }; };",
2763 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2764}
2765
2766TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2767 EXPECT_TRUE(matches(
2768 "class A { class B { void f() { C c; } class C {}; }; };",
2769 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2770 hasAncestor(recordDecl(hasName("A"))))))));
2771}
2772
2773TEST(HasAncenstor, MatchesStatementAncestors) {
2774 EXPECT_TRUE(matches(
2775 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002776 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002777}
2778
2779TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2780 EXPECT_TRUE(matches(
2781 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002782 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002783}
2784
2785TEST(HasAncestor, BindsRecursiveCombinations) {
2786 EXPECT_TRUE(matchAndVerifyResultTrue(
2787 "class C { class D { class E { class F { int y; }; }; }; };",
2788 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002789 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00002790}
2791
2792TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
2793 EXPECT_TRUE(matchAndVerifyResultTrue(
2794 "class C { class D { class E { class F { int y; }; }; }; };",
2795 fieldDecl(hasAncestor(
2796 decl(
2797 hasDescendant(recordDecl(isDefinition(),
2798 hasAncestor(recordDecl())))
2799 ).bind("d")
2800 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00002801 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00002802}
2803
2804TEST(HasAncestor, MatchesInTemplateInstantiations) {
2805 EXPECT_TRUE(matches(
2806 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
2807 "A<int>::B::C a;",
2808 fieldDecl(hasType(asString("int")),
2809 hasAncestor(recordDecl(hasName("A"))))));
2810}
2811
2812TEST(HasAncestor, MatchesInImplicitCode) {
2813 EXPECT_TRUE(matches(
2814 "struct X {}; struct A { A() {} X x; };",
2815 constructorDecl(
2816 hasAnyConstructorInitializer(withInitializer(expr(
2817 hasAncestor(recordDecl(hasName("A")))))))));
2818}
2819
Daniel Jaspera7564432012-09-13 13:11:25 +00002820TEST(NNS, MatchesNestedNameSpecifiers) {
2821 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
2822 nestedNameSpecifier()));
2823 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
2824 nestedNameSpecifier()));
2825 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
2826 nestedNameSpecifier()));
2827
2828 EXPECT_TRUE(matches(
2829 "struct A { static void f() {} }; void g() { A::f(); }",
2830 nestedNameSpecifier()));
2831 EXPECT_TRUE(notMatches(
2832 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
2833 nestedNameSpecifier()));
2834}
2835
2836TEST(NNS, MatchesTypes) {
2837 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
2838 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
2839 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
2840 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
2841 Matcher));
2842 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
2843}
2844
2845TEST(NNS, MatchesNamespaceDecls) {
2846 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
2847 specifiesNamespace(hasName("ns")));
2848 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
2849 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
2850 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
2851}
2852
2853TEST(NNS, BindsNestedNameSpecifiers) {
2854 EXPECT_TRUE(matchAndVerifyResultTrue(
2855 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
2856 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
2857 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
2858}
2859
2860TEST(NNS, BindsNestedNameSpecifierLocs) {
2861 EXPECT_TRUE(matchAndVerifyResultTrue(
2862 "namespace ns { struct B {}; } ns::B b;",
2863 loc(nestedNameSpecifier()).bind("loc"),
2864 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
2865}
2866
2867TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
2868 EXPECT_TRUE(matches(
2869 "struct A { struct B { struct C {}; }; }; A::B::C c;",
2870 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
2871 EXPECT_TRUE(matches(
2872 "struct A { struct B { struct C {}; }; }; A::B::C c;",
2873 nestedNameSpecifierLoc(hasPrefix(loc(
2874 specifiesType(asString("struct A")))))));
2875}
2876
Manuel Klimek4da21662012-07-06 05:48:52 +00002877} // end namespace ast_matchers
2878} // end namespace clang