blob: 2602e87840eb325a0d098e7b7eea41681060ca3e [file] [log] [blame]
Manuel Klimek4da21662012-07-06 05:48:52 +00001//===- unittest/Tooling/ASTMatchersTest.cpp - AST matcher unit tests ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ASTMatchersTest.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Tooling/Tooling.h"
14#include "gtest/gtest.h"
15
16namespace clang {
17namespace ast_matchers {
18
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000019#if GTEST_HAS_DEATH_TEST
Manuel Klimek4da21662012-07-06 05:48:52 +000020TEST(HasNameDeathTest, DiesOnEmptyName) {
21 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000022 DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000023 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
24 }, "");
25}
26
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000027TEST(HasNameDeathTest, DiesOnEmptyPattern) {
28 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000029 DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000030 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
31 }, "");
32}
33
Manuel Klimek4da21662012-07-06 05:48:52 +000034TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
35 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000036 DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000037 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
38 }, "");
39}
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000040#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000041
Manuel Klimek715c9562012-07-25 10:02:02 +000042TEST(Decl, MatchesDeclarations) {
43 EXPECT_TRUE(notMatches("", decl(usingDecl())));
44 EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
45 decl(usingDecl())));
46}
47
Manuel Klimek4da21662012-07-06 05:48:52 +000048TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000049 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +000050 EXPECT_TRUE(matches("typedef int X;", NamedX));
51 EXPECT_TRUE(matches("int X;", NamedX));
52 EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
53 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
54 EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
55 EXPECT_TRUE(matches("namespace X { }", NamedX));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000056 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek4da21662012-07-06 05:48:52 +000057
58 EXPECT_TRUE(notMatches("#define X 1", NamedX));
59}
60
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000061TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000062 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000063 EXPECT_TRUE(matches("typedef int Xa;", NamedX));
64 EXPECT_TRUE(matches("int Xb;", NamedX));
65 EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
66 EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
67 EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
68 EXPECT_TRUE(matches("namespace Xij { }", NamedX));
69 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
70
71 EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
72
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000073 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000074 EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
75 EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
76
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000077 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000078 EXPECT_TRUE(matches("int abc;", Abc));
79 EXPECT_TRUE(matches("int aFOObBARc;", Abc));
80 EXPECT_TRUE(notMatches("int cab;", Abc));
81 EXPECT_TRUE(matches("int cabc;", Abc));
82}
83
Manuel Klimek4da21662012-07-06 05:48:52 +000084TEST(DeclarationMatcher, MatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000085 DeclarationMatcher ClassMatcher(recordDecl());
Manuel Klimeke265c872012-07-10 14:21:30 +000086#if !defined(_MSC_VER)
Manuel Klimek4da21662012-07-06 05:48:52 +000087 EXPECT_FALSE(matches("", ClassMatcher));
Manuel Klimeke265c872012-07-10 14:21:30 +000088#else
89 // Matches class type_info.
90 EXPECT_TRUE(matches("", ClassMatcher));
91#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000092
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000093 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +000094 EXPECT_TRUE(matches("class X;", ClassX));
95 EXPECT_TRUE(matches("class X {};", ClassX));
96 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
97 EXPECT_TRUE(notMatches("", ClassX));
98}
99
100TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000101 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000102
103 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000104 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
105 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek4da21662012-07-06 05:48:52 +0000106 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
107 EXPECT_TRUE(notMatches("", IsDerivedFromX));
108
Daniel Jasper63d88722012-09-12 21:14:15 +0000109 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000110
111 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
112 EXPECT_TRUE(matches("class X {};", IsAX));
113 EXPECT_TRUE(matches("class X;", IsAX));
114 EXPECT_TRUE(notMatches("class Y;", IsAX));
115 EXPECT_TRUE(notMatches("", IsAX));
116
Manuel Klimek4da21662012-07-06 05:48:52 +0000117 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000118 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000119 EXPECT_TRUE(
120 matches("class X {}; class Y : public X {}; class Z : public Y {};",
121 ZIsDerivedFromX));
122 EXPECT_TRUE(
123 matches("class X {};"
124 "template<class T> class Y : public X {};"
125 "class Z : public Y<int> {};", ZIsDerivedFromX));
126 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
127 ZIsDerivedFromX));
128 EXPECT_TRUE(
129 matches("template<class T> class X {}; "
130 "template<class T> class Z : public X<T> {};",
131 ZIsDerivedFromX));
132 EXPECT_TRUE(
133 matches("template<class T, class U=T> class X {}; "
134 "template<class T> class Z : public X<T> {};",
135 ZIsDerivedFromX));
136 EXPECT_TRUE(
137 notMatches("template<class X> class A { class Z : public X {}; };",
138 ZIsDerivedFromX));
139 EXPECT_TRUE(
140 matches("template<class X> class A { public: class Z : public X {}; }; "
141 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
142 EXPECT_TRUE(
143 matches("template <class T> class X {}; "
144 "template<class Y> class A { class Z : public X<Y> {}; };",
145 ZIsDerivedFromX));
146 EXPECT_TRUE(
147 notMatches("template<template<class T> class X> class A { "
148 " class Z : public X<int> {}; };", ZIsDerivedFromX));
149 EXPECT_TRUE(
150 matches("template<template<class T> class X> class A { "
151 " public: class Z : public X<int> {}; }; "
152 "template<class T> class X {}; void y() { A<X>::Z z; }",
153 ZIsDerivedFromX));
154 EXPECT_TRUE(
155 notMatches("template<class X> class A { class Z : public X::D {}; };",
156 ZIsDerivedFromX));
157 EXPECT_TRUE(
158 matches("template<class X> class A { public: "
159 " class Z : public X::D {}; }; "
160 "class Y { public: class X {}; typedef X D; }; "
161 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
162 EXPECT_TRUE(
163 matches("class X {}; typedef X Y; class Z : public Y {};",
164 ZIsDerivedFromX));
165 EXPECT_TRUE(
166 matches("template<class T> class Y { typedef typename T::U X; "
167 " class Z : public X {}; };", ZIsDerivedFromX));
168 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
169 ZIsDerivedFromX));
170 EXPECT_TRUE(
171 notMatches("template<class T> class X {}; "
172 "template<class T> class A { class Z : public X<T>::D {}; };",
173 ZIsDerivedFromX));
174 EXPECT_TRUE(
175 matches("template<class T> class X { public: typedef X<T> D; }; "
176 "template<class T> class A { public: "
177 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
178 ZIsDerivedFromX));
179 EXPECT_TRUE(
180 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
181 ZIsDerivedFromX));
182 EXPECT_TRUE(
183 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
184 ZIsDerivedFromX));
185 EXPECT_TRUE(
186 matches("class X {}; class Y : public X {}; "
187 "typedef Y V; typedef V W; class Z : public W {};",
188 ZIsDerivedFromX));
189 EXPECT_TRUE(
190 matches("template<class T, class U> class X {}; "
191 "template<class T> class A { class Z : public X<T, int> {}; };",
192 ZIsDerivedFromX));
193 EXPECT_TRUE(
194 notMatches("template<class X> class D { typedef X A; typedef A B; "
195 " typedef B C; class Z : public C {}; };",
196 ZIsDerivedFromX));
197 EXPECT_TRUE(
198 matches("class X {}; typedef X A; typedef A B; "
199 "class Z : public B {};", ZIsDerivedFromX));
200 EXPECT_TRUE(
201 matches("class X {}; typedef X A; typedef A B; typedef B C; "
202 "class Z : public C {};", ZIsDerivedFromX));
203 EXPECT_TRUE(
204 matches("class U {}; typedef U X; typedef X V; "
205 "class Z : public V {};", ZIsDerivedFromX));
206 EXPECT_TRUE(
207 matches("class Base {}; typedef Base X; "
208 "class Z : public Base {};", ZIsDerivedFromX));
209 EXPECT_TRUE(
210 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
211 "class Z : public Base {};", ZIsDerivedFromX));
212 EXPECT_TRUE(
213 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
214 "class Z : public Base {};", ZIsDerivedFromX));
215 EXPECT_TRUE(
216 matches("class A {}; typedef A X; typedef A Y; "
217 "class Z : public Y {};", ZIsDerivedFromX));
218 EXPECT_TRUE(
219 notMatches("template <typename T> class Z;"
220 "template <> class Z<void> {};"
221 "template <typename T> class Z : public Z<void> {};",
222 IsDerivedFromX));
223 EXPECT_TRUE(
224 matches("template <typename T> class X;"
225 "template <> class X<void> {};"
226 "template <typename T> class X : public X<void> {};",
227 IsDerivedFromX));
228 EXPECT_TRUE(matches(
229 "class X {};"
230 "template <typename T> class Z;"
231 "template <> class Z<void> {};"
232 "template <typename T> class Z : public Z<void>, public X {};",
233 ZIsDerivedFromX));
234
235 // FIXME: Once we have better matchers for template type matching,
236 // get rid of the Variable(...) matching and match the right template
237 // declarations directly.
238 const char *RecursiveTemplateOneParameter =
239 "class Base1 {}; class Base2 {};"
240 "template <typename T> class Z;"
241 "template <> class Z<void> : public Base1 {};"
242 "template <> class Z<int> : public Base2 {};"
243 "template <> class Z<float> : public Z<void> {};"
244 "template <> class Z<double> : public Z<int> {};"
245 "template <typename T> class Z : public Z<float>, public Z<double> {};"
246 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
247 EXPECT_TRUE(matches(
248 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000249 varDecl(hasName("z_float"),
250 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000251 EXPECT_TRUE(notMatches(
252 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000253 varDecl(hasName("z_float"),
254 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000255 EXPECT_TRUE(matches(
256 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000257 varDecl(hasName("z_char"),
258 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
259 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000260
261 const char *RecursiveTemplateTwoParameters =
262 "class Base1 {}; class Base2 {};"
263 "template <typename T1, typename T2> class Z;"
264 "template <typename T> class Z<void, T> : public Base1 {};"
265 "template <typename T> class Z<int, T> : public Base2 {};"
266 "template <typename T> class Z<float, T> : public Z<void, T> {};"
267 "template <typename T> class Z<double, T> : public Z<int, T> {};"
268 "template <typename T1, typename T2> class Z : "
269 " public Z<float, T2>, public Z<double, T2> {};"
270 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
271 " Z<char, void> z_char; }";
272 EXPECT_TRUE(matches(
273 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000274 varDecl(hasName("z_float"),
275 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000276 EXPECT_TRUE(notMatches(
277 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000278 varDecl(hasName("z_float"),
279 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000280 EXPECT_TRUE(matches(
281 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000282 varDecl(hasName("z_char"),
283 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
284 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000285 EXPECT_TRUE(matches(
286 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000287 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000288 EXPECT_TRUE(notMatches(
289 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000290 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000291
292 EXPECT_TRUE(matches(
293 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000294 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000295}
296
Daniel Jasper08f0c532012-09-18 14:17:42 +0000297TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
298 EXPECT_TRUE(matches(
299 "template <typename T> struct A {"
300 " template <typename T2> struct F {};"
301 "};"
302 "template <typename T> struct B : A<T>::template F<T> {};"
303 "B<int> b;",
304 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
305}
306
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000307TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000308 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000309 EXPECT_TRUE(notMatches("class X;", ClassX));
310 EXPECT_TRUE(notMatches("class X {};", ClassX));
311}
312
313TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000314 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000315 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
316 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
317}
318
319TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
320 EXPECT_TRUE(notMatches("template<typename T> class X { };"
321 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000322 classTemplateDecl(hasName("X"),
323 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000324}
325
326TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
327 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
328 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000329 classTemplateDecl(hasName("X"),
330 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000331}
332
Daniel Jasper6a124492012-07-12 08:50:38 +0000333TEST(AllOf, AllOverloadsWork) {
334 const char Program[] =
335 "struct T { }; int f(int, T*); void g(int x) { T t; f(x, &t); }";
336 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000337 callExpr(allOf(callee(functionDecl(hasName("f"))),
338 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000339 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000340 callExpr(allOf(callee(functionDecl(hasName("f"))),
341 hasArgument(0, declRefExpr(to(varDecl()))),
342 hasArgument(1, hasType(pointsTo(
343 recordDecl(hasName("T")))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000344}
345
Manuel Klimek4da21662012-07-06 05:48:52 +0000346TEST(DeclarationMatcher, MatchAnyOf) {
347 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000348 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000349 EXPECT_TRUE(
350 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
351 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
352 EXPECT_TRUE(
353 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
354 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
355
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000356 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000357 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000358 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
359 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
360
Manuel Klimek4da21662012-07-06 05:48:52 +0000361 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000362 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
363 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000364 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
365 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
366 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
367 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
368 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
369 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
370}
371
372TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000373 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000374 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
375 EXPECT_TRUE(matches("class X {};", HasClassX));
376
377 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000378 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000379 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
380 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
381 EXPECT_TRUE(
382 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
383}
384
385TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
386 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000387 recordDecl(
388 has(recordDecl(
389 has(recordDecl(hasName("X"))),
390 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000391 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000392 has(recordDecl(
393 has(recordDecl(hasName("A"))),
394 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000395 hasName("C"))),
396 hasName("F"));
397
398 EXPECT_TRUE(matches(
399 "class F {"
400 " class Z {"
401 " class X {};"
402 " class Y {};"
403 " };"
404 " class C {"
405 " class A {};"
406 " class B {};"
407 " };"
408 "};", Recursive));
409
410 EXPECT_TRUE(matches(
411 "class F {"
412 " class Z {"
413 " class A {};"
414 " class X {};"
415 " class Y {};"
416 " };"
417 " class C {"
418 " class X {};"
419 " class A {};"
420 " class B {};"
421 " };"
422 "};", Recursive));
423
424 EXPECT_TRUE(matches(
425 "class O1 {"
426 " class O2 {"
427 " class F {"
428 " class Z {"
429 " class A {};"
430 " class X {};"
431 " class Y {};"
432 " };"
433 " class C {"
434 " class X {};"
435 " class A {};"
436 " class B {};"
437 " };"
438 " };"
439 " };"
440 "};", Recursive));
441}
442
443TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
444 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000445 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000446 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000447 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000448 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000449 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000450 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000451 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000452 hasName("Y"))),
453 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000454 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000455 anyOf(
456 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000457 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000458 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000459 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000460 hasName("B")))))),
461 hasName("F")));
462
463 EXPECT_TRUE(matches("class F {};", Recursive));
464 EXPECT_TRUE(matches("class Z {};", Recursive));
465 EXPECT_TRUE(matches("class C {};", Recursive));
466 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
467 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
468 EXPECT_TRUE(
469 matches("class O1 { class O2 {"
470 " class M { class N { class B {}; }; }; "
471 "}; };", Recursive));
472}
473
474TEST(DeclarationMatcher, MatchNot) {
475 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000476 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000477 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000478 unless(hasName("X")));
479 EXPECT_TRUE(notMatches("", NotClassX));
480 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
481 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
482 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
483 EXPECT_TRUE(
484 notMatches("class Y {}; class Z {}; class X : public Y {};",
485 NotClassX));
486
487 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000488 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000489 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000490 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000491 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000492 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000493 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
494 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
495 ClassXHasNotClassY));
496}
497
498TEST(DeclarationMatcher, HasDescendant) {
499 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000500 recordDecl(
501 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000502 hasName("Z"));
503 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
504 EXPECT_TRUE(
505 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
506 EXPECT_TRUE(
507 matches("class Z { class A { class Y { class X {}; }; }; };",
508 ZDescendantClassX));
509 EXPECT_TRUE(
510 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
511 ZDescendantClassX));
512 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
513
514 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000515 recordDecl(
516 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000517 hasName("X"))),
518 hasName("Z"));
519 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
520 ZDescendantClassXHasClassY));
521 EXPECT_TRUE(
522 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
523 ZDescendantClassXHasClassY));
524 EXPECT_TRUE(notMatches(
525 "class Z {"
526 " class A {"
527 " class B {"
528 " class X {"
529 " class C {"
530 " class Y {};"
531 " };"
532 " };"
533 " }; "
534 " };"
535 "};", ZDescendantClassXHasClassY));
536
537 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000538 recordDecl(
539 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
540 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000541 hasName("Z"));
542 EXPECT_TRUE(
543 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
544 ZDescendantClassXDescendantClassY));
545 EXPECT_TRUE(matches(
546 "class Z {"
547 " class A {"
548 " class X {"
549 " class B {"
550 " class Y {};"
551 " };"
552 " class Y {};"
553 " };"
554 " };"
555 "};", ZDescendantClassXDescendantClassY));
556}
557
Daniel Jaspera267cf62012-10-29 10:14:44 +0000558// Implements a run method that returns whether BoundNodes contains a
559// Decl bound to Id that can be dynamically cast to T.
560// Optionally checks that the check succeeded a specific number of times.
561template <typename T>
562class VerifyIdIsBoundTo : public BoundNodesCallback {
563public:
564 // Create an object that checks that a node of type \c T was bound to \c Id.
565 // Does not check for a certain number of matches.
566 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
567 : Id(Id), ExpectedCount(-1), Count(0) {}
568
569 // Create an object that checks that a node of type \c T was bound to \c Id.
570 // Checks that there were exactly \c ExpectedCount matches.
571 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
572 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
573
574 // Create an object that checks that a node of type \c T was bound to \c Id.
575 // Checks that there was exactly one match with the name \c ExpectedName.
576 // Note that \c T must be a NamedDecl for this to work.
577 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName)
578 : Id(Id), ExpectedCount(1), Count(0), ExpectedName(ExpectedName) {}
579
580 ~VerifyIdIsBoundTo() {
581 if (ExpectedCount != -1)
582 EXPECT_EQ(ExpectedCount, Count);
583 if (!ExpectedName.empty())
584 EXPECT_EQ(ExpectedName, Name);
585 }
586
587 virtual bool run(const BoundNodes *Nodes) {
588 if (Nodes->getNodeAs<T>(Id)) {
589 ++Count;
590 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
591 Name = Named->getNameAsString();
592 } else if (const NestedNameSpecifier *NNS =
593 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
594 llvm::raw_string_ostream OS(Name);
595 NNS->print(OS, PrintingPolicy(LangOptions()));
596 }
597 return true;
598 }
599 return false;
600 }
601
602private:
603 const std::string Id;
604 const int ExpectedCount;
605 int Count;
606 const std::string ExpectedName;
607 std::string Name;
608};
609
610TEST(HasDescendant, MatchesDescendantTypes) {
611 EXPECT_TRUE(matches("void f() { int i = 3; }",
612 decl(hasDescendant(loc(builtinType())))));
613 EXPECT_TRUE(matches("void f() { int i = 3; }",
614 stmt(hasDescendant(builtinType()))));
615
616 EXPECT_TRUE(matches("void f() { int i = 3; }",
617 stmt(hasDescendant(loc(builtinType())))));
618 EXPECT_TRUE(matches("void f() { int i = 3; }",
619 stmt(hasDescendant(qualType(builtinType())))));
620
621 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
622 stmt(hasDescendant(isInteger()))));
623
624 EXPECT_TRUE(matchAndVerifyResultTrue(
625 "void f() { int a; float c; int d; int e; }",
626 functionDecl(forEachDescendant(
627 varDecl(hasDescendant(isInteger())).bind("x"))),
628 new VerifyIdIsBoundTo<Decl>("x", 3)));
629}
630
631TEST(HasDescendant, MatchesDescendantsOfTypes) {
632 EXPECT_TRUE(matches("void f() { int*** i; }",
633 qualType(hasDescendant(builtinType()))));
634 EXPECT_TRUE(matches("void f() { int*** i; }",
635 qualType(hasDescendant(
636 pointerType(pointee(builtinType()))))));
637 EXPECT_TRUE(matches("void f() { int*** i; }",
638 typeLoc(hasDescendant(builtinTypeLoc()))));
639
640 EXPECT_TRUE(matchAndVerifyResultTrue(
641 "void f() { int*** i; }",
642 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
643 new VerifyIdIsBoundTo<Type>("x", 2)));
644}
645
646TEST(Has, MatchesChildrenOfTypes) {
647 EXPECT_TRUE(matches("int i;",
648 varDecl(hasName("i"), has(isInteger()))));
649 EXPECT_TRUE(notMatches("int** i;",
650 varDecl(hasName("i"), has(isInteger()))));
651 EXPECT_TRUE(matchAndVerifyResultTrue(
652 "int (*f)(float, int);",
653 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
654 new VerifyIdIsBoundTo<QualType>("x", 2)));
655}
656
657TEST(Has, MatchesChildTypes) {
658 EXPECT_TRUE(matches(
659 "int* i;",
660 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
661 EXPECT_TRUE(notMatches(
662 "int* i;",
663 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
664}
665
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000666TEST(Enum, DoesNotMatchClasses) {
667 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
668}
669
670TEST(Enum, MatchesEnums) {
671 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
672}
673
674TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000675 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000676 EXPECT_TRUE(matches("enum X{ A };", Matcher));
677 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
678 EXPECT_TRUE(notMatches("enum X {};", Matcher));
679}
680
Manuel Klimek4da21662012-07-06 05:48:52 +0000681TEST(StatementMatcher, Has) {
682 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000683 expr(hasType(pointsTo(recordDecl(hasName("X")))),
684 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000685
686 EXPECT_TRUE(matches(
687 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
688 EXPECT_TRUE(notMatches(
689 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
690}
691
692TEST(StatementMatcher, HasDescendant) {
693 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000694 expr(hasType(pointsTo(recordDecl(hasName("X")))),
695 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000696
697 EXPECT_TRUE(matches(
698 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
699 HasDescendantVariableI));
700 EXPECT_TRUE(notMatches(
701 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
702 HasDescendantVariableI));
703}
704
705TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000706 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000707
708 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
709 EXPECT_TRUE(notMatches("class A {};", TypeA));
710
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000711 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000712
713 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
714 TypeDerivedFromA));
715 EXPECT_TRUE(notMatches("class A {};", TypeA));
716
717 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000718 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000719
720 EXPECT_TRUE(
721 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
722}
723
Manuel Klimek4da21662012-07-06 05:48:52 +0000724TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000725 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000726
727 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000728 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000729
730 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000731 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000732
733 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000734 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000735
736 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
737 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000738 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000739
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000740 StatementMatcher MethodX =
741 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000742
743 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
744 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000745 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000746}
747
748TEST(Matcher, BindTheSameNameInAlternatives) {
749 StatementMatcher matcher = anyOf(
750 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000751 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000752 hasRHS(integerLiteral(equals(0)))),
753 binaryOperator(hasOperatorName("+"),
754 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000755 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000756
757 EXPECT_TRUE(matchAndVerifyResultTrue(
758 // The first branch of the matcher binds x to 0 but then fails.
759 // The second branch binds x to f() and succeeds.
760 "int f() { return 0 + f(); }",
761 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000762 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000763}
764
Manuel Klimek66341c52012-08-30 19:41:06 +0000765TEST(Matcher, BindsIDForMemoizedResults) {
766 // Using the same matcher in two match expressions will make memoization
767 // kick in.
768 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
769 EXPECT_TRUE(matchAndVerifyResultTrue(
770 "class A { class B { class X {}; }; };",
771 DeclarationMatcher(anyOf(
772 recordDecl(hasName("A"), hasDescendant(ClassX)),
773 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000774 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000775}
776
Manuel Klimek4da21662012-07-06 05:48:52 +0000777TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000778 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000779 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000780 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000781 EXPECT_TRUE(
782 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000783 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000784 EXPECT_TRUE(
785 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000786 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000787}
788
789TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000790 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000791 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000792 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000793 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000794 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000795 EXPECT_TRUE(
796 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000797 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000798}
799
800TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000801 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000802 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000803 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000804 EXPECT_TRUE(
805 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000806 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000807}
808
809TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000810 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000811 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000812 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000813 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000814 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000815}
816
817TEST(Matcher, Call) {
818 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000819 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000820 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000821
822 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
823 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
824
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000825 StatementMatcher MethodOnY =
826 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000827
828 EXPECT_TRUE(
829 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
830 MethodOnY));
831 EXPECT_TRUE(
832 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
833 MethodOnY));
834 EXPECT_TRUE(
835 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
836 MethodOnY));
837 EXPECT_TRUE(
838 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
839 MethodOnY));
840 EXPECT_TRUE(
841 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
842 MethodOnY));
843
844 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000845 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000846
847 EXPECT_TRUE(
848 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
849 MethodOnYPointer));
850 EXPECT_TRUE(
851 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
852 MethodOnYPointer));
853 EXPECT_TRUE(
854 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
855 MethodOnYPointer));
856 EXPECT_TRUE(
857 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
858 MethodOnYPointer));
859 EXPECT_TRUE(
860 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
861 MethodOnYPointer));
862}
863
Daniel Jasper31f7c082012-10-01 13:40:41 +0000864TEST(Matcher, Lambda) {
865 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
866 lambdaExpr()));
867}
868
869TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000870 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
871 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000872 forRangeStmt()));
873 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
874 forRangeStmt()));
875}
876
877TEST(Matcher, UserDefinedLiteral) {
878 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
879 " return i + 1;"
880 "}"
881 "char c = 'a'_inc;",
882 userDefinedLiteral()));
883}
884
Daniel Jasperb54b7642012-09-20 14:12:57 +0000885TEST(Matcher, FlowControl) {
886 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
887 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
888 continueStmt()));
889 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
890 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
891 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
892}
893
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000894TEST(HasType, MatchesAsString) {
895 EXPECT_TRUE(
896 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000897 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000898 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000899 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000900 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000901 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000902 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000903 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000904}
905
Manuel Klimek4da21662012-07-06 05:48:52 +0000906TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000907 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000908 // Unary operator
909 EXPECT_TRUE(matches("class Y { }; "
910 "bool operator!(Y x) { return false; }; "
911 "Y y; bool c = !y;", OpCall));
912 // No match -- special operators like "new", "delete"
913 // FIXME: operator new takes size_t, for which we need stddef.h, for which
914 // we need to figure out include paths in the test.
915 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
916 // "class Y { }; "
917 // "void *operator new(size_t size) { return 0; } "
918 // "Y *y = new Y;", OpCall));
919 EXPECT_TRUE(notMatches("class Y { }; "
920 "void operator delete(void *p) { } "
921 "void a() {Y *y = new Y; delete y;}", OpCall));
922 // Binary operator
923 EXPECT_TRUE(matches("class Y { }; "
924 "bool operator&&(Y x, Y y) { return true; }; "
925 "Y a; Y b; bool c = a && b;",
926 OpCall));
927 // No match -- normal operator, not an overloaded one.
928 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
929 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
930}
931
932TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
933 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000934 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000935 EXPECT_TRUE(matches("class Y { }; "
936 "bool operator&&(Y x, Y y) { return true; }; "
937 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
938 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000939 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000940 EXPECT_TRUE(notMatches("class Y { }; "
941 "bool operator&&(Y x, Y y) { return true; }; "
942 "Y a; Y b; bool c = a && b;",
943 OpCallLessLess));
944}
945
946TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +0000947 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000948 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000949
950 EXPECT_TRUE(
951 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
952 MethodOnY));
953 EXPECT_TRUE(
954 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
955 MethodOnY));
956 EXPECT_TRUE(
957 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
958 MethodOnY));
959 EXPECT_TRUE(
960 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
961 MethodOnY));
962 EXPECT_TRUE(
963 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
964 MethodOnY));
965
966 EXPECT_TRUE(matches(
967 "class Y {"
968 " public: virtual void x();"
969 "};"
970 "class X : public Y {"
971 " public: virtual void x();"
972 "};"
973 "void z() { X *x; x->Y::x(); }", MethodOnY));
974}
975
976TEST(Matcher, VariableUsage) {
977 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000978 declRefExpr(to(
979 varDecl(hasInitializer(
980 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000981
982 EXPECT_TRUE(matches(
983 "class Y {"
984 " public:"
985 " bool x() const;"
986 "};"
987 "void z(const Y &y) {"
988 " bool b = y.x();"
989 " if (b) {}"
990 "}", Reference));
991
992 EXPECT_TRUE(notMatches(
993 "class Y {"
994 " public:"
995 " bool x() const;"
996 "};"
997 "void z(const Y &y) {"
998 " bool b = y.x();"
999 "}", Reference));
1000}
1001
Daniel Jasper9bd28092012-07-30 05:03:25 +00001002TEST(Matcher, FindsVarDeclInFuncitonParameter) {
1003 EXPECT_TRUE(matches(
1004 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001005 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001006}
1007
Manuel Klimek4da21662012-07-06 05:48:52 +00001008TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001009 StatementMatcher CallOnVariableY =
1010 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001011
1012 EXPECT_TRUE(matches(
1013 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1014 EXPECT_TRUE(matches(
1015 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1016 EXPECT_TRUE(matches(
1017 "class Y { public: void x(); };"
1018 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1019 EXPECT_TRUE(matches(
1020 "class Y { public: void x(); };"
1021 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1022 EXPECT_TRUE(notMatches(
1023 "class Y { public: void x(); };"
1024 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1025 CallOnVariableY));
1026}
1027
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001028TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1029 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1030 unaryExprOrTypeTraitExpr()));
1031 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1032 alignOfExpr(anything())));
1033 // FIXME: Uncomment once alignof is enabled.
1034 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1035 // unaryExprOrTypeTraitExpr()));
1036 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1037 // sizeOfExpr()));
1038}
1039
1040TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1041 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1042 hasArgumentOfType(asString("int")))));
1043 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1044 hasArgumentOfType(asString("float")))));
1045 EXPECT_TRUE(matches(
1046 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001047 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001048 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001049 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001050}
1051
Manuel Klimek4da21662012-07-06 05:48:52 +00001052TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001053 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001054}
1055
1056TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001057 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001058}
1059
1060TEST(MemberExpression, MatchesVariable) {
1061 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001062 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001063 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001064 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001065 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001066 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001067}
1068
1069TEST(MemberExpression, MatchesStaticVariable) {
1070 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001071 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001072 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001073 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001074 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001075 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001076}
1077
Daniel Jasper6a124492012-07-12 08:50:38 +00001078TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001079 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1080 EXPECT_TRUE(matches(
1081 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1082 callExpr(hasArgument(0, declRefExpr(
1083 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001084}
1085
1086TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001087 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001088 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001089 callExpr(hasArgument(0, declRefExpr(
1090 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001091}
1092
Manuel Klimek4da21662012-07-06 05:48:52 +00001093TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1094 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001095 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001096 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001097 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001098 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001099 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001100}
1101
1102TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1103 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001104 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001105 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001106 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001107 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001108 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001109}
1110
1111TEST(IsArrow, MatchesMemberCallsViaArrow) {
1112 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001113 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001114 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001115 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001116 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001117 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001118}
1119
1120TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001121 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001122
1123 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1124 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1125}
1126
1127TEST(Callee, MatchesMemberExpressions) {
1128 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001129 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001130 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001131 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001132}
1133
1134TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001135 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001136
1137 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1138 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1139
Manuel Klimeke265c872012-07-10 14:21:30 +00001140#if !defined(_MSC_VER)
1141 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001142 // Dependent contexts, but a non-dependent call.
1143 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1144 CallFunctionF));
1145 EXPECT_TRUE(
1146 matches("void f(); template <int N> struct S { void g() { f(); } };",
1147 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001148#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001149
1150 // Depedent calls don't match.
1151 EXPECT_TRUE(
1152 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1153 CallFunctionF));
1154 EXPECT_TRUE(
1155 notMatches("void f(int);"
1156 "template <typename T> struct S { void g(T t) { f(t); } };",
1157 CallFunctionF));
1158}
1159
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001160TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1161 EXPECT_TRUE(
1162 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001163 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001164}
1165
1166TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1167 EXPECT_TRUE(
1168 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001169 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001170}
1171
1172TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1173 EXPECT_TRUE(
1174 notMatches("void g(); template <typename T> void f(T t) {}"
1175 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001176 functionTemplateDecl(hasName("f"),
1177 hasDescendant(declRefExpr(to(
1178 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001179}
1180
Manuel Klimek4da21662012-07-06 05:48:52 +00001181TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001182 StatementMatcher CallArgumentY = callExpr(
1183 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001184
1185 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1186 EXPECT_TRUE(
1187 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1188 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1189
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001190 StatementMatcher WrongIndex = callExpr(
1191 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001192 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1193}
1194
1195TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001196 StatementMatcher CallArgumentY = callExpr(
1197 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001198 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1199 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1200 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1201}
1202
1203TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001204 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001205
1206 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1207 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1208 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1209}
1210
1211TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001212 DeclarationMatcher ReferenceClassX = varDecl(
1213 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001214 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1215 ReferenceClassX));
1216 EXPECT_TRUE(
1217 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1218 EXPECT_TRUE(
1219 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1220 EXPECT_TRUE(
1221 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1222}
1223
1224TEST(HasParameter, CallsInnerMatcher) {
1225 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001226 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001227 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001228 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001229}
1230
1231TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1232 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001233 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001234}
1235
1236TEST(HasType, MatchesParameterVariableTypesStrictly) {
1237 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001238 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001239 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001240 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001241 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001242 methodDecl(hasParameter(0,
1243 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001244 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001245 methodDecl(hasParameter(0,
1246 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001247}
1248
1249TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1250 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001251 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001252 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001253 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001254}
1255
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001256TEST(Returns, MatchesReturnTypes) {
1257 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001258 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001259 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001260 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001261 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001262 functionDecl(returns(hasDeclaration(
1263 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001264}
1265
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001266TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001267 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1268 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1269 functionDecl(isExternC())));
1270 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001271}
1272
Manuel Klimek4da21662012-07-06 05:48:52 +00001273TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1274 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001275 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001276}
1277
1278TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1279 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001280 methodDecl(hasAnyParameter(hasType(pointsTo(
1281 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001282}
1283
1284TEST(HasName, MatchesParameterVariableDeclartions) {
1285 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001286 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001287 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001288 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001289}
1290
Daniel Jasper371f9392012-08-01 08:40:24 +00001291TEST(Matcher, MatchesClassTemplateSpecialization) {
1292 EXPECT_TRUE(matches("template<typename T> struct A {};"
1293 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001294 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001295 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001296 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001297 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001298 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001299}
1300
1301TEST(Matcher, MatchesTypeTemplateArgument) {
1302 EXPECT_TRUE(matches(
1303 "template<typename T> struct B {};"
1304 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001305 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001306 asString("int"))))));
1307}
1308
1309TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1310 EXPECT_TRUE(matches(
1311 "struct B { int next; };"
1312 "template<int(B::*next_ptr)> struct A {};"
1313 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001314 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1315 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001316
1317 EXPECT_TRUE(notMatches(
1318 "template <typename T> struct A {};"
1319 "A<int> a;",
1320 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1321 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001322}
1323
1324TEST(Matcher, MatchesSpecificArgument) {
1325 EXPECT_TRUE(matches(
1326 "template<typename T, typename U> class A {};"
1327 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001328 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001329 1, refersToType(asString("int"))))));
1330 EXPECT_TRUE(notMatches(
1331 "template<typename T, typename U> class A {};"
1332 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001333 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001334 1, refersToType(asString("int"))))));
1335}
1336
Manuel Klimek4da21662012-07-06 05:48:52 +00001337TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001338 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001339
1340 EXPECT_TRUE(
1341 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1342 EXPECT_TRUE(
1343 matches("class X { public: X(); }; void x() { X x = X(); }",
1344 Constructor));
1345 EXPECT_TRUE(
1346 matches("class X { public: X(int); }; void x() { X x = 0; }",
1347 Constructor));
1348 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1349}
1350
1351TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001352 StatementMatcher Constructor = constructExpr(
1353 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001354
1355 EXPECT_TRUE(
1356 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1357 Constructor));
1358 EXPECT_TRUE(
1359 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1360 Constructor));
1361 EXPECT_TRUE(
1362 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1363 Constructor));
1364 EXPECT_TRUE(
1365 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1366 Constructor));
1367
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001368 StatementMatcher WrongIndex = constructExpr(
1369 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001370 EXPECT_TRUE(
1371 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1372 WrongIndex));
1373}
1374
1375TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001376 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001377
1378 EXPECT_TRUE(
1379 matches("class X { public: X(int); }; void x() { X x(0); }",
1380 Constructor1Arg));
1381 EXPECT_TRUE(
1382 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1383 Constructor1Arg));
1384 EXPECT_TRUE(
1385 matches("class X { public: X(int); }; void x() { X x = 0; }",
1386 Constructor1Arg));
1387 EXPECT_TRUE(
1388 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1389 Constructor1Arg));
1390}
1391
Manuel Klimek70b9db92012-10-23 10:40:50 +00001392TEST(Matcher,ThisExpr) {
1393 EXPECT_TRUE(
1394 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1395 EXPECT_TRUE(
1396 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1397}
1398
Manuel Klimek4da21662012-07-06 05:48:52 +00001399TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001400 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001401
1402 std::string ClassString = "class string { public: string(); ~string(); }; ";
1403
1404 EXPECT_TRUE(
1405 matches(ClassString +
1406 "string GetStringByValue();"
1407 "void FunctionTakesString(string s);"
1408 "void run() { FunctionTakesString(GetStringByValue()); }",
1409 TempExpression));
1410
1411 EXPECT_TRUE(
1412 notMatches(ClassString +
1413 "string* GetStringPointer(); "
1414 "void FunctionTakesStringPtr(string* s);"
1415 "void run() {"
1416 " string* s = GetStringPointer();"
1417 " FunctionTakesStringPtr(GetStringPointer());"
1418 " FunctionTakesStringPtr(s);"
1419 "}",
1420 TempExpression));
1421
1422 EXPECT_TRUE(
1423 notMatches("class no_dtor {};"
1424 "no_dtor GetObjByValue();"
1425 "void ConsumeObj(no_dtor param);"
1426 "void run() { ConsumeObj(GetObjByValue()); }",
1427 TempExpression));
1428}
1429
Sam Panzere16acd32012-08-24 22:04:44 +00001430TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1431 std::string ClassString =
1432 "class string { public: string(); int length(); }; ";
1433
1434 EXPECT_TRUE(
1435 matches(ClassString +
1436 "string GetStringByValue();"
1437 "void FunctionTakesString(string s);"
1438 "void run() { FunctionTakesString(GetStringByValue()); }",
1439 materializeTemporaryExpr()));
1440
1441 EXPECT_TRUE(
1442 notMatches(ClassString +
1443 "string* GetStringPointer(); "
1444 "void FunctionTakesStringPtr(string* s);"
1445 "void run() {"
1446 " string* s = GetStringPointer();"
1447 " FunctionTakesStringPtr(GetStringPointer());"
1448 " FunctionTakesStringPtr(s);"
1449 "}",
1450 materializeTemporaryExpr()));
1451
1452 EXPECT_TRUE(
1453 notMatches(ClassString +
1454 "string GetStringByValue();"
1455 "void run() { int k = GetStringByValue().length(); }",
1456 materializeTemporaryExpr()));
1457
1458 EXPECT_TRUE(
1459 notMatches(ClassString +
1460 "string GetStringByValue();"
1461 "void run() { GetStringByValue(); }",
1462 materializeTemporaryExpr()));
1463}
1464
Manuel Klimek4da21662012-07-06 05:48:52 +00001465TEST(ConstructorDeclaration, SimpleCase) {
1466 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001467 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001468 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001469 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001470}
1471
1472TEST(ConstructorDeclaration, IsImplicit) {
1473 // This one doesn't match because the constructor is not added by the
1474 // compiler (it is not needed).
1475 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001476 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001477 // The compiler added the implicit default constructor.
1478 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001479 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001480 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001481 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001482}
1483
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001484TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1485 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001486 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001487}
1488
1489TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001490 EXPECT_TRUE(notMatches("class Foo {};",
1491 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001492}
1493
Manuel Klimek4da21662012-07-06 05:48:52 +00001494TEST(HasAnyConstructorInitializer, SimpleCase) {
1495 EXPECT_TRUE(notMatches(
1496 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001497 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001498 EXPECT_TRUE(matches(
1499 "class Foo {"
1500 " Foo() : foo_() { }"
1501 " int foo_;"
1502 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001503 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001504}
1505
1506TEST(HasAnyConstructorInitializer, ForField) {
1507 static const char Code[] =
1508 "class Baz { };"
1509 "class Foo {"
1510 " Foo() : foo_() { }"
1511 " Baz foo_;"
1512 " Baz bar_;"
1513 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001514 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1515 forField(hasType(recordDecl(hasName("Baz"))))))));
1516 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001517 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001518 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1519 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001520}
1521
1522TEST(HasAnyConstructorInitializer, WithInitializer) {
1523 static const char Code[] =
1524 "class Foo {"
1525 " Foo() : foo_(0) { }"
1526 " int foo_;"
1527 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001528 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001529 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001530 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001531 withInitializer(integerLiteral(equals(1)))))));
1532}
1533
1534TEST(HasAnyConstructorInitializer, IsWritten) {
1535 static const char Code[] =
1536 "struct Bar { Bar(){} };"
1537 "class Foo {"
1538 " Foo() : foo_() { }"
1539 " Bar foo_;"
1540 " Bar bar_;"
1541 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001542 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001543 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001544 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001545 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001546 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001547 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1548}
1549
1550TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001551 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001552
1553 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1554 EXPECT_TRUE(
1555 matches("class X { public: X(); }; void x() { new X(); }", New));
1556 EXPECT_TRUE(
1557 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1558 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1559}
1560
1561TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001562 StatementMatcher New = constructExpr(
1563 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001564
1565 EXPECT_TRUE(
1566 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1567 New));
1568 EXPECT_TRUE(
1569 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1570 New));
1571 EXPECT_TRUE(
1572 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1573 New));
1574
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001575 StatementMatcher WrongIndex = constructExpr(
1576 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001577 EXPECT_TRUE(
1578 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1579 WrongIndex));
1580}
1581
1582TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001583 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001584
1585 EXPECT_TRUE(
1586 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1587 EXPECT_TRUE(
1588 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1589 New));
1590}
1591
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001592TEST(Matcher, DeleteExpression) {
1593 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001594 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001595}
1596
Manuel Klimek4da21662012-07-06 05:48:52 +00001597TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001598 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001599
1600 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1601 EXPECT_TRUE(
1602 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1603 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1604}
1605
1606TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001607 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001608 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1609 // wide string
1610 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1611 // with escaped characters
1612 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1613 // no matching -- though the data type is the same, there is no string literal
1614 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1615}
1616
1617TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001618 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001619 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1620 // wide character
1621 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1622 // wide character, Hex encoded, NOT MATCHED!
1623 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1624 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1625}
1626
1627TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001628 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001629 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1630 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1631 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1632 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1633
1634 // Non-matching cases (character literals, float and double)
1635 EXPECT_TRUE(notMatches("int i = L'a';",
1636 HasIntLiteral)); // this is actually a character
1637 // literal cast to int
1638 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1639 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1640 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1641}
1642
Daniel Jasper31f7c082012-10-01 13:40:41 +00001643TEST(Matcher, NullPtrLiteral) {
1644 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1645}
1646
Daniel Jasperb54b7642012-09-20 14:12:57 +00001647TEST(Matcher, AsmStatement) {
1648 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1649}
1650
Manuel Klimek4da21662012-07-06 05:48:52 +00001651TEST(Matcher, Conditions) {
1652 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1653
1654 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1655 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1656 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1657 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1658 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1659}
1660
1661TEST(MatchBinaryOperator, HasOperatorName) {
1662 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1663
1664 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1665 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1666}
1667
1668TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1669 StatementMatcher OperatorTrueFalse =
1670 binaryOperator(hasLHS(boolLiteral(equals(true))),
1671 hasRHS(boolLiteral(equals(false))));
1672
1673 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1674 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1675 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1676}
1677
1678TEST(MatchBinaryOperator, HasEitherOperand) {
1679 StatementMatcher HasOperand =
1680 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1681
1682 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1683 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1684 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1685}
1686
1687TEST(Matcher, BinaryOperatorTypes) {
1688 // Integration test that verifies the AST provides all binary operators in
1689 // a way we expect.
1690 // FIXME: Operator ','
1691 EXPECT_TRUE(
1692 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1693 EXPECT_TRUE(
1694 matches("bool b; bool c = (b = true);",
1695 binaryOperator(hasOperatorName("="))));
1696 EXPECT_TRUE(
1697 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1698 EXPECT_TRUE(
1699 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1700 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1701 EXPECT_TRUE(
1702 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1703 EXPECT_TRUE(
1704 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1705 EXPECT_TRUE(
1706 matches("int i = 1; int j = (i <<= 2);",
1707 binaryOperator(hasOperatorName("<<="))));
1708 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1709 EXPECT_TRUE(
1710 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1711 EXPECT_TRUE(
1712 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1713 EXPECT_TRUE(
1714 matches("int i = 1; int j = (i >>= 2);",
1715 binaryOperator(hasOperatorName(">>="))));
1716 EXPECT_TRUE(
1717 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1718 EXPECT_TRUE(
1719 matches("int i = 42; int j = (i ^= 42);",
1720 binaryOperator(hasOperatorName("^="))));
1721 EXPECT_TRUE(
1722 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1723 EXPECT_TRUE(
1724 matches("int i = 42; int j = (i %= 42);",
1725 binaryOperator(hasOperatorName("%="))));
1726 EXPECT_TRUE(
1727 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1728 EXPECT_TRUE(
1729 matches("bool b = true && false;",
1730 binaryOperator(hasOperatorName("&&"))));
1731 EXPECT_TRUE(
1732 matches("bool b = true; bool c = (b &= false);",
1733 binaryOperator(hasOperatorName("&="))));
1734 EXPECT_TRUE(
1735 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1736 EXPECT_TRUE(
1737 matches("bool b = true || false;",
1738 binaryOperator(hasOperatorName("||"))));
1739 EXPECT_TRUE(
1740 matches("bool b = true; bool c = (b |= false);",
1741 binaryOperator(hasOperatorName("|="))));
1742 EXPECT_TRUE(
1743 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1744 EXPECT_TRUE(
1745 matches("int i = 42; int j = (i *= 23);",
1746 binaryOperator(hasOperatorName("*="))));
1747 EXPECT_TRUE(
1748 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1749 EXPECT_TRUE(
1750 matches("int i = 42; int j = (i /= 23);",
1751 binaryOperator(hasOperatorName("/="))));
1752 EXPECT_TRUE(
1753 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1754 EXPECT_TRUE(
1755 matches("int i = 42; int j = (i += 23);",
1756 binaryOperator(hasOperatorName("+="))));
1757 EXPECT_TRUE(
1758 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1759 EXPECT_TRUE(
1760 matches("int i = 42; int j = (i -= 23);",
1761 binaryOperator(hasOperatorName("-="))));
1762 EXPECT_TRUE(
1763 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1764 binaryOperator(hasOperatorName("->*"))));
1765 EXPECT_TRUE(
1766 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1767 binaryOperator(hasOperatorName(".*"))));
1768
1769 // Member expressions as operators are not supported in matches.
1770 EXPECT_TRUE(
1771 notMatches("struct A { void x(A *a) { a->x(this); } };",
1772 binaryOperator(hasOperatorName("->"))));
1773
1774 // Initializer assignments are not represented as operator equals.
1775 EXPECT_TRUE(
1776 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1777
1778 // Array indexing is not represented as operator.
1779 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1780
1781 // Overloaded operators do not match at all.
1782 EXPECT_TRUE(notMatches(
1783 "struct A { bool operator&&(const A &a) const { return false; } };"
1784 "void x() { A a, b; a && b; }",
1785 binaryOperator()));
1786}
1787
1788TEST(MatchUnaryOperator, HasOperatorName) {
1789 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1790
1791 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1792 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1793}
1794
1795TEST(MatchUnaryOperator, HasUnaryOperand) {
1796 StatementMatcher OperatorOnFalse =
1797 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1798
1799 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1800 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1801}
1802
1803TEST(Matcher, UnaryOperatorTypes) {
1804 // Integration test that verifies the AST provides all unary operators in
1805 // a way we expect.
1806 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1807 EXPECT_TRUE(
1808 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1809 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1810 EXPECT_TRUE(
1811 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1812 EXPECT_TRUE(
1813 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1814 EXPECT_TRUE(
1815 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1816 EXPECT_TRUE(
1817 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1818 EXPECT_TRUE(
1819 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1820 EXPECT_TRUE(
1821 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1822 EXPECT_TRUE(
1823 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1824
1825 // We don't match conversion operators.
1826 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1827
1828 // Function calls are not represented as operator.
1829 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1830
1831 // Overloaded operators do not match at all.
1832 // FIXME: We probably want to add that.
1833 EXPECT_TRUE(notMatches(
1834 "struct A { bool operator!() const { return false; } };"
1835 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1836}
1837
1838TEST(Matcher, ConditionalOperator) {
1839 StatementMatcher Conditional = conditionalOperator(
1840 hasCondition(boolLiteral(equals(true))),
1841 hasTrueExpression(boolLiteral(equals(false))));
1842
1843 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1844 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1845 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1846
1847 StatementMatcher ConditionalFalse = conditionalOperator(
1848 hasFalseExpression(boolLiteral(equals(false))));
1849
1850 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1851 EXPECT_TRUE(
1852 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1853}
1854
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001855TEST(ArraySubscriptMatchers, ArraySubscripts) {
1856 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1857 arraySubscriptExpr()));
1858 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1859 arraySubscriptExpr()));
1860}
1861
1862TEST(ArraySubscriptMatchers, ArrayIndex) {
1863 EXPECT_TRUE(matches(
1864 "int i[2]; void f() { i[1] = 1; }",
1865 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1866 EXPECT_TRUE(matches(
1867 "int i[2]; void f() { 1[i] = 1; }",
1868 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1869 EXPECT_TRUE(notMatches(
1870 "int i[2]; void f() { i[1] = 1; }",
1871 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1872}
1873
1874TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1875 EXPECT_TRUE(matches(
1876 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001877 arraySubscriptExpr(hasBase(implicitCastExpr(
1878 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001879}
1880
Manuel Klimek4da21662012-07-06 05:48:52 +00001881TEST(Matcher, HasNameSupportsNamespaces) {
1882 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001883 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001884 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001885 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001886 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001887 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001888 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001889 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001890 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001891 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001892 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001893 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001894 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001895 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001896 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001897 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001898 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001899 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001900 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001901 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001902 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001903 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001904 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001905 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001906}
1907
1908TEST(Matcher, HasNameSupportsOuterClasses) {
1909 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001910 matches("class A { class B { class C; }; };",
1911 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001912 EXPECT_TRUE(
1913 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001914 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001915 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001916 matches("class A { class B { class C; }; };",
1917 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001918 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001919 matches("class A { class B { class C; }; };",
1920 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001921 EXPECT_TRUE(
1922 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001923 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001924 EXPECT_TRUE(
1925 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001926 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001927 EXPECT_TRUE(
1928 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001929 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001930 EXPECT_TRUE(
1931 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001932 recordDecl(hasName("::C"))));
1933 EXPECT_TRUE(
1934 notMatches("class A { class B { class C; }; };",
1935 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001936 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001937 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001938 EXPECT_TRUE(
1939 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001940 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001941}
1942
1943TEST(Matcher, IsDefinition) {
1944 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001945 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001946 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1947 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1948
1949 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001950 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001951 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1952 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1953
1954 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001955 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001956 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1957 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1958}
1959
1960TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001961 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00001962 ofClass(hasName("X")))));
1963
1964 EXPECT_TRUE(
1965 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
1966 EXPECT_TRUE(
1967 matches("class X { public: X(); }; void x(int) { X x = X(); }",
1968 Constructor));
1969 EXPECT_TRUE(
1970 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
1971 Constructor));
1972}
1973
1974TEST(Matcher, VisitsTemplateInstantiations) {
1975 EXPECT_TRUE(matches(
1976 "class A { public: void x(); };"
1977 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001978 "void f() { B<A> b; b.y(); }",
1979 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001980
1981 EXPECT_TRUE(matches(
1982 "class A { public: void x(); };"
1983 "class C {"
1984 " public:"
1985 " template <typename T> class B { public: void y() { T t; t.x(); } };"
1986 "};"
1987 "void f() {"
1988 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001989 "}",
1990 recordDecl(hasName("C"),
1991 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001992}
1993
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001994TEST(Matcher, HandlesNullQualTypes) {
1995 // FIXME: Add a Type matcher so we can replace uses of this
1996 // variable with Type(True())
1997 const TypeMatcher AnyType = anything();
1998
1999 // We don't really care whether this matcher succeeds; we're testing that
2000 // it completes without crashing.
2001 EXPECT_TRUE(matches(
2002 "struct A { };"
2003 "template <typename T>"
2004 "void f(T t) {"
2005 " T local_t(t /* this becomes a null QualType in the AST */);"
2006 "}"
2007 "void g() {"
2008 " f(0);"
2009 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002010 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002011 anyOf(
2012 TypeMatcher(hasDeclaration(anything())),
2013 pointsTo(AnyType),
2014 references(AnyType)
2015 // Other QualType matchers should go here.
2016 ))))));
2017}
2018
Manuel Klimek4da21662012-07-06 05:48:52 +00002019// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002020AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002021 // Make sure all special variables are used: node, match_finder,
2022 // bound_nodes_builder, and the parameter named 'AMatcher'.
2023 return AMatcher.matches(Node, Finder, Builder);
2024}
2025
2026TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002027 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002028
2029 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002030 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002031
2032 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002033 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002034
2035 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002036 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002037}
2038
2039AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002040 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
2041 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
2042 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00002043 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00002044 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002045 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002046 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2047 ASTMatchFinder::BK_First);
2048}
2049
2050TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002051 DeclarationMatcher HasClassB =
2052 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002053
2054 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002055 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002056
2057 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002058 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002059
2060 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002061 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002062
2063 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002064 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002065
2066 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2067}
2068
2069TEST(For, FindsForLoops) {
2070 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2071 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002072 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2073 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002074 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002075}
2076
Daniel Jasper6a124492012-07-12 08:50:38 +00002077TEST(For, ForLoopInternals) {
2078 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2079 forStmt(hasCondition(anything()))));
2080 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2081 forStmt(hasLoopInit(anything()))));
2082}
2083
2084TEST(For, NegativeForLoopInternals) {
2085 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002086 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002087 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2088 forStmt(hasLoopInit(anything()))));
2089}
2090
Manuel Klimek4da21662012-07-06 05:48:52 +00002091TEST(For, ReportsNoFalsePositives) {
2092 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2093 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2094}
2095
2096TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002097 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2098 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2099 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002100}
2101
2102TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2103 // It's not a compound statement just because there's "{}" in the source
2104 // text. This is an AST search, not grep.
2105 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002106 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002107 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002108 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002109}
2110
Daniel Jasper6a124492012-07-12 08:50:38 +00002111TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002112 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002113 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002114 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002115 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002116 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002117 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002118 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002119 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002120}
2121
2122TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2123 // The simplest case: every compound statement is in a function
2124 // definition, and the function body itself must be a compound
2125 // statement.
2126 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002127 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002128}
2129
2130TEST(HasAnySubstatement, IsNotRecursive) {
2131 // It's really "has any immediate substatement".
2132 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002133 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002134}
2135
2136TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2137 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002138 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002139}
2140
2141TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2142 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002143 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002144}
2145
2146TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2147 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002148 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002149 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002150 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002151}
2152
2153TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2154 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002155 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002156 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002157 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002158 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002159 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002160}
2161
2162TEST(StatementCountIs, WorksWithMultipleStatements) {
2163 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002164 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002165}
2166
2167TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2168 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002169 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002170 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002171 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002172 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002173 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002174 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002175 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002176}
2177
2178TEST(Member, WorksInSimplestCase) {
2179 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002180 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002181}
2182
2183TEST(Member, DoesNotMatchTheBaseExpression) {
2184 // Don't pick out the wrong part of the member expression, this should
2185 // be checking the member (name) only.
2186 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002187 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002188}
2189
2190TEST(Member, MatchesInMemberFunctionCall) {
2191 EXPECT_TRUE(matches("void f() {"
2192 " struct { void first() {}; } s;"
2193 " s.first();"
2194 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002195 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002196}
2197
Daniel Jasperc711af22012-10-23 15:46:39 +00002198TEST(Member, MatchesMember) {
2199 EXPECT_TRUE(matches(
2200 "struct A { int i; }; void f() { A a; a.i = 2; }",
2201 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2202 EXPECT_TRUE(notMatches(
2203 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2204 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2205}
2206
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002207TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002208 // Fails in C++11 mode
2209 EXPECT_TRUE(matchesConditionally(
2210 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2211 "class X { void *operator new(std::size_t); };",
2212 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002213
2214 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002215 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002216
Daniel Jasper31f7c082012-10-01 13:40:41 +00002217 // Fails in C++11 mode
2218 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002219 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2220 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002221 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002222}
2223
Manuel Klimek4da21662012-07-06 05:48:52 +00002224TEST(HasObjectExpression, DoesNotMatchMember) {
2225 EXPECT_TRUE(notMatches(
2226 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002227 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002228}
2229
2230TEST(HasObjectExpression, MatchesBaseOfVariable) {
2231 EXPECT_TRUE(matches(
2232 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002233 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002234 EXPECT_TRUE(matches(
2235 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002236 memberExpr(hasObjectExpression(
2237 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002238}
2239
2240TEST(HasObjectExpression,
2241 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2242 EXPECT_TRUE(matches(
2243 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002244 memberExpr(hasObjectExpression(
2245 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002246 EXPECT_TRUE(matches(
2247 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002248 memberExpr(hasObjectExpression(
2249 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002250}
2251
2252TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002253 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2254 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2255 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2256 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002257}
2258
2259TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002260 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002261}
2262
2263TEST(IsConstQualified, MatchesConstInt) {
2264 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002265 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002266}
2267
2268TEST(IsConstQualified, MatchesConstPointer) {
2269 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002270 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002271}
2272
2273TEST(IsConstQualified, MatchesThroughTypedef) {
2274 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002275 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002276 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002277 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002278}
2279
2280TEST(IsConstQualified, DoesNotMatchInappropriately) {
2281 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002282 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002283 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002284 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002285}
2286
Sam Panzer089e5b32012-08-16 16:58:10 +00002287TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002288 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2289 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2290 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2291 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002292}
2293TEST(CastExpression, MatchesImplicitCasts) {
2294 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002295 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002296 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002297 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002298}
2299
2300TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002301 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2302 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2303 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2304 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002305}
2306
Manuel Klimek4da21662012-07-06 05:48:52 +00002307TEST(ReinterpretCast, MatchesSimpleCase) {
2308 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002309 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002310}
2311
2312TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002313 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002314 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002315 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002316 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002317 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002318 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2319 "B b;"
2320 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002321 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002322}
2323
2324TEST(FunctionalCast, MatchesSimpleCase) {
2325 std::string foo_class = "class Foo { public: Foo(char*); };";
2326 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002327 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002328}
2329
2330TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2331 std::string FooClass = "class Foo { public: Foo(char*); };";
2332 EXPECT_TRUE(
2333 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002334 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002335 EXPECT_TRUE(
2336 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002337 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002338}
2339
2340TEST(DynamicCast, MatchesSimpleCase) {
2341 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2342 "B b;"
2343 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002344 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002345}
2346
2347TEST(StaticCast, MatchesSimpleCase) {
2348 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002349 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002350}
2351
2352TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002353 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002354 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002355 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002356 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002357 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002358 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2359 "B b;"
2360 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002361 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002362}
2363
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002364TEST(CStyleCast, MatchesSimpleCase) {
2365 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2366}
2367
2368TEST(CStyleCast, DoesNotMatchOtherCasts) {
2369 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2370 "char q, *r = const_cast<char*>(&q);"
2371 "void* s = reinterpret_cast<char*>(&s);"
2372 "struct B { virtual ~B() {} }; struct D : B {};"
2373 "B b;"
2374 "D* t = dynamic_cast<D*>(&b);",
2375 cStyleCastExpr()));
2376}
2377
Manuel Klimek4da21662012-07-06 05:48:52 +00002378TEST(HasDestinationType, MatchesSimpleCase) {
2379 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002380 staticCastExpr(hasDestinationType(
2381 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002382}
2383
Sam Panzer089e5b32012-08-16 16:58:10 +00002384TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2385 // This test creates an implicit const cast.
2386 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002387 implicitCastExpr(
2388 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002389 // This test creates an implicit array-to-pointer cast.
2390 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002391 implicitCastExpr(hasImplicitDestinationType(
2392 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002393}
2394
2395TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2396 // This test creates an implicit cast from int to char.
2397 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002398 implicitCastExpr(hasImplicitDestinationType(
2399 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002400 // This test creates an implicit array-to-pointer cast.
2401 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002402 implicitCastExpr(hasImplicitDestinationType(
2403 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002404}
2405
2406TEST(ImplicitCast, MatchesSimpleCase) {
2407 // This test creates an implicit const cast.
2408 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002409 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002410 // This test creates an implicit cast from int to char.
2411 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002412 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002413 // This test creates an implicit array-to-pointer cast.
2414 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002415 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002416}
2417
2418TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002419 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002420 // are present, and that it ignores explicit and paren casts.
2421
2422 // These two test cases have no casts.
2423 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002424 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002425 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002426 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002427
2428 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002429 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002430 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002431 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002432
2433 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002434 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002435}
2436
2437TEST(IgnoringImpCasts, MatchesImpCasts) {
2438 // This test checks that ignoringImpCasts matches when implicit casts are
2439 // present and its inner matcher alone does not match.
2440 // Note that this test creates an implicit const cast.
2441 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002442 varDecl(hasInitializer(ignoringImpCasts(
2443 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002444 // This test creates an implict cast from int to char.
2445 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002446 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002447 integerLiteral(equals(0)))))));
2448}
2449
2450TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2451 // These tests verify that ignoringImpCasts does not match if the inner
2452 // matcher does not match.
2453 // Note that the first test creates an implicit const cast.
2454 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002455 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002456 unless(anything()))))));
2457 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002458 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002459 unless(anything()))))));
2460
2461 // These tests verify that ignoringImplictCasts does not look through explicit
2462 // casts or parentheses.
2463 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002464 varDecl(hasInitializer(ignoringImpCasts(
2465 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002466 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002467 varDecl(hasInitializer(ignoringImpCasts(
2468 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002469 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002470 varDecl(hasInitializer(ignoringImpCasts(
2471 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002472 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002473 varDecl(hasInitializer(ignoringImpCasts(
2474 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002475}
2476
2477TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2478 // This test verifies that expressions that do not have implicit casts
2479 // still match the inner matcher.
2480 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002481 varDecl(hasInitializer(ignoringImpCasts(
2482 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002483}
2484
2485TEST(IgnoringParenCasts, MatchesParenCasts) {
2486 // This test checks that ignoringParenCasts matches when parentheses and/or
2487 // casts are present and its inner matcher alone does not match.
2488 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002489 varDecl(hasInitializer(ignoringParenCasts(
2490 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002491 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002492 varDecl(hasInitializer(ignoringParenCasts(
2493 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002494
2495 // This test creates an implict cast from int to char in addition to the
2496 // parentheses.
2497 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002498 varDecl(hasInitializer(ignoringParenCasts(
2499 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002500
2501 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002502 varDecl(hasInitializer(ignoringParenCasts(
2503 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002504 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002505 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002506 integerLiteral(equals(0)))))));
2507}
2508
2509TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2510 // This test verifies that expressions that do not have any casts still match.
2511 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002512 varDecl(hasInitializer(ignoringParenCasts(
2513 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002514}
2515
2516TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2517 // These tests verify that ignoringImpCasts does not match if the inner
2518 // matcher does not match.
2519 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002520 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002521 unless(anything()))))));
2522
2523 // This test creates an implicit cast from int to char in addition to the
2524 // parentheses.
2525 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002526 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002527 unless(anything()))))));
2528
2529 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002530 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002531 unless(anything()))))));
2532}
2533
2534TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2535 // This test checks that ignoringParenAndImpCasts matches when
2536 // parentheses and/or implicit casts are present and its inner matcher alone
2537 // does not match.
2538 // Note that this test creates an implicit const cast.
2539 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002540 varDecl(hasInitializer(ignoringParenImpCasts(
2541 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002542 // This test creates an implicit cast from int to char.
2543 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002544 varDecl(hasInitializer(ignoringParenImpCasts(
2545 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002546}
2547
2548TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2549 // This test verifies that expressions that do not have parentheses or
2550 // implicit casts still match.
2551 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002552 varDecl(hasInitializer(ignoringParenImpCasts(
2553 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002554 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002555 varDecl(hasInitializer(ignoringParenImpCasts(
2556 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002557}
2558
2559TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2560 // These tests verify that ignoringParenImpCasts does not match if
2561 // the inner matcher does not match.
2562 // This test creates an implicit cast.
2563 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002564 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002565 unless(anything()))))));
2566 // These tests verify that ignoringParenAndImplictCasts does not look
2567 // through explicit casts.
2568 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002569 varDecl(hasInitializer(ignoringParenImpCasts(
2570 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002571 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002572 varDecl(hasInitializer(ignoringParenImpCasts(
2573 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002574 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002575 varDecl(hasInitializer(ignoringParenImpCasts(
2576 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002577}
2578
Manuel Klimek715c9562012-07-25 10:02:02 +00002579TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002580 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2581 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002582 implicitCastExpr(
2583 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002584}
2585
Manuel Klimek715c9562012-07-25 10:02:02 +00002586TEST(HasSourceExpression, MatchesExplicitCasts) {
2587 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002588 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002589 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002590 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002591}
2592
Manuel Klimek4da21662012-07-06 05:48:52 +00002593TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002594 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002595}
2596
2597TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002598 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002599}
2600
2601TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002602 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002603}
2604
2605TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002606 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002607}
2608
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002609TEST(InitListExpression, MatchesInitListExpression) {
2610 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2611 initListExpr(hasType(asString("int [2]")))));
2612 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002613 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002614}
2615
2616TEST(UsingDeclaration, MatchesUsingDeclarations) {
2617 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2618 usingDecl()));
2619}
2620
2621TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2622 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2623 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2624}
2625
2626TEST(UsingDeclaration, MatchesSpecificTarget) {
2627 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2628 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002629 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002630 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2631 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002632 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002633}
2634
2635TEST(UsingDeclaration, ThroughUsingDeclaration) {
2636 EXPECT_TRUE(matches(
2637 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002638 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002639 EXPECT_TRUE(notMatches(
2640 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002641 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002642}
2643
Sam Panzer425f41b2012-08-16 17:20:59 +00002644TEST(SingleDecl, IsSingleDecl) {
2645 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002646 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002647 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2648 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2649 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2650 SingleDeclStmt));
2651}
2652
2653TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002654 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002655
2656 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002657 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002658 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002659 declStmt(containsDeclaration(0, MatchesInit),
2660 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002661 unsigned WrongIndex = 42;
2662 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002663 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002664 MatchesInit))));
2665}
2666
2667TEST(DeclCount, DeclCountIsCorrect) {
2668 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002669 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002670 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002671 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002672 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002673 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002674}
2675
Manuel Klimek4da21662012-07-06 05:48:52 +00002676TEST(While, MatchesWhileLoops) {
2677 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2678 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2679 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2680}
2681
2682TEST(Do, MatchesDoLoops) {
2683 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2684 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2685}
2686
2687TEST(Do, DoesNotMatchWhileLoops) {
2688 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2689}
2690
2691TEST(SwitchCase, MatchesCase) {
2692 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2693 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2694 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2695 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2696}
2697
Daniel Jasperb54b7642012-09-20 14:12:57 +00002698TEST(SwitchCase, MatchesSwitch) {
2699 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2700 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2701 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2702 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2703}
2704
2705TEST(ExceptionHandling, SimpleCases) {
2706 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2707 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2708 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2709 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2710 throwExpr()));
2711 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2712 throwExpr()));
2713}
2714
Manuel Klimek4da21662012-07-06 05:48:52 +00002715TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2716 EXPECT_TRUE(notMatches(
2717 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002718 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002719 EXPECT_TRUE(notMatches(
2720 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002721 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002722}
2723
2724TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2725 EXPECT_TRUE(matches(
2726 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002727 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002728}
2729
2730TEST(ForEach, BindsOneNode) {
2731 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002732 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002733 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002734}
2735
2736TEST(ForEach, BindsMultipleNodes) {
2737 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002738 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002739 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002740}
2741
2742TEST(ForEach, BindsRecursiveCombinations) {
2743 EXPECT_TRUE(matchAndVerifyResultTrue(
2744 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002745 recordDecl(hasName("C"),
2746 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002747 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002748}
2749
2750TEST(ForEachDescendant, BindsOneNode) {
2751 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002752 recordDecl(hasName("C"),
2753 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002754 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002755}
2756
2757TEST(ForEachDescendant, BindsMultipleNodes) {
2758 EXPECT_TRUE(matchAndVerifyResultTrue(
2759 "class C { class D { int x; int y; }; "
2760 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002761 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002762 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002763}
2764
2765TEST(ForEachDescendant, BindsRecursiveCombinations) {
2766 EXPECT_TRUE(matchAndVerifyResultTrue(
2767 "class C { class D { "
2768 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002769 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2770 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002771 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002772}
2773
2774
2775TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2776 // Make sure that we can both match the class by name (::X) and by the type
2777 // the template was instantiated with (via a field).
2778
2779 EXPECT_TRUE(matches(
2780 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002781 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002782
2783 EXPECT_TRUE(matches(
2784 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002785 recordDecl(isTemplateInstantiation(), hasDescendant(
2786 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002787}
2788
2789TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2790 EXPECT_TRUE(matches(
2791 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002792 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002793 isTemplateInstantiation())));
2794}
2795
2796TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2797 EXPECT_TRUE(matches(
2798 "template <typename T> class X { T t; }; class A {};"
2799 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002800 recordDecl(isTemplateInstantiation(), hasDescendant(
2801 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002802}
2803
2804TEST(IsTemplateInstantiation,
2805 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2806 EXPECT_TRUE(matches(
2807 "template <typename T> class X {};"
2808 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002809 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002810}
2811
2812TEST(IsTemplateInstantiation,
2813 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2814 EXPECT_TRUE(matches(
2815 "class A {};"
2816 "class X {"
2817 " template <typename U> class Y { U u; };"
2818 " Y<A> y;"
2819 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002820 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002821}
2822
2823TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2824 // FIXME: Figure out whether this makes sense. It doesn't affect the
2825 // normal use case as long as the uppermost instantiation always is marked
2826 // as template instantiation, but it might be confusing as a predicate.
2827 EXPECT_TRUE(matches(
2828 "class A {};"
2829 "template <typename T> class X {"
2830 " template <typename U> class Y { U u; };"
2831 " Y<T> y;"
2832 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002833 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002834}
2835
2836TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2837 EXPECT_TRUE(notMatches(
2838 "template <typename T> class X {}; class A {};"
2839 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002840 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002841}
2842
2843TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2844 EXPECT_TRUE(notMatches(
2845 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002846 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002847}
2848
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002849TEST(IsExplicitTemplateSpecialization,
2850 DoesNotMatchPrimaryTemplate) {
2851 EXPECT_TRUE(notMatches(
2852 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002853 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002854 EXPECT_TRUE(notMatches(
2855 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002856 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002857}
2858
2859TEST(IsExplicitTemplateSpecialization,
2860 DoesNotMatchExplicitTemplateInstantiations) {
2861 EXPECT_TRUE(notMatches(
2862 "template <typename T> class X {};"
2863 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002864 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002865 EXPECT_TRUE(notMatches(
2866 "template <typename T> void f(T t) {}"
2867 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002868 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002869}
2870
2871TEST(IsExplicitTemplateSpecialization,
2872 DoesNotMatchImplicitTemplateInstantiations) {
2873 EXPECT_TRUE(notMatches(
2874 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002875 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002876 EXPECT_TRUE(notMatches(
2877 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002878 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002879}
2880
2881TEST(IsExplicitTemplateSpecialization,
2882 MatchesExplicitTemplateSpecializations) {
2883 EXPECT_TRUE(matches(
2884 "template <typename T> class X {};"
2885 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002886 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002887 EXPECT_TRUE(matches(
2888 "template <typename T> void f(T t) {}"
2889 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002890 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002891}
2892
Manuel Klimek579b1202012-09-07 09:26:10 +00002893TEST(HasAncenstor, MatchesDeclarationAncestors) {
2894 EXPECT_TRUE(matches(
2895 "class A { class B { class C {}; }; };",
2896 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2897}
2898
2899TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2900 EXPECT_TRUE(notMatches(
2901 "class A { class B { class C {}; }; };",
2902 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2903}
2904
2905TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2906 EXPECT_TRUE(matches(
2907 "class A { class B { void f() { C c; } class C {}; }; };",
2908 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2909 hasAncestor(recordDecl(hasName("A"))))))));
2910}
2911
2912TEST(HasAncenstor, MatchesStatementAncestors) {
2913 EXPECT_TRUE(matches(
2914 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002915 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002916}
2917
2918TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2919 EXPECT_TRUE(matches(
2920 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002921 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002922}
2923
2924TEST(HasAncestor, BindsRecursiveCombinations) {
2925 EXPECT_TRUE(matchAndVerifyResultTrue(
2926 "class C { class D { class E { class F { int y; }; }; }; };",
2927 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002928 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00002929}
2930
2931TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
2932 EXPECT_TRUE(matchAndVerifyResultTrue(
2933 "class C { class D { class E { class F { int y; }; }; }; };",
2934 fieldDecl(hasAncestor(
2935 decl(
2936 hasDescendant(recordDecl(isDefinition(),
2937 hasAncestor(recordDecl())))
2938 ).bind("d")
2939 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00002940 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00002941}
2942
2943TEST(HasAncestor, MatchesInTemplateInstantiations) {
2944 EXPECT_TRUE(matches(
2945 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
2946 "A<int>::B::C a;",
2947 fieldDecl(hasType(asString("int")),
2948 hasAncestor(recordDecl(hasName("A"))))));
2949}
2950
2951TEST(HasAncestor, MatchesInImplicitCode) {
2952 EXPECT_TRUE(matches(
2953 "struct X {}; struct A { A() {} X x; };",
2954 constructorDecl(
2955 hasAnyConstructorInitializer(withInitializer(expr(
2956 hasAncestor(recordDecl(hasName("A")))))))));
2957}
2958
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00002959TEST(HasParent, MatchesOnlyParent) {
2960 EXPECT_TRUE(matches(
2961 "void f() { if (true) { int x = 42; } }",
2962 compoundStmt(hasParent(ifStmt()))));
2963 EXPECT_TRUE(notMatches(
2964 "void f() { for (;;) { int x = 42; } }",
2965 compoundStmt(hasParent(ifStmt()))));
2966 EXPECT_TRUE(notMatches(
2967 "void f() { if (true) for (;;) { int x = 42; } }",
2968 compoundStmt(hasParent(ifStmt()))));
2969}
2970
Daniel Jasperce620072012-10-17 08:52:59 +00002971TEST(TypeMatching, MatchesTypes) {
2972 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
2973}
2974
2975TEST(TypeMatching, MatchesArrayTypes) {
2976 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
2977 EXPECT_TRUE(matches("int a[42];", arrayType()));
2978 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
2979
2980 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
2981 arrayType(hasElementType(builtinType()))));
2982
2983 EXPECT_TRUE(matches(
2984 "int const a[] = { 2, 3 };",
2985 qualType(arrayType(hasElementType(builtinType())))));
2986 EXPECT_TRUE(matches(
2987 "int const a[] = { 2, 3 };",
2988 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
2989 EXPECT_TRUE(matches(
2990 "typedef const int T; T x[] = { 1, 2 };",
2991 qualType(isConstQualified(), arrayType())));
2992
2993 EXPECT_TRUE(notMatches(
2994 "int a[] = { 2, 3 };",
2995 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
2996 EXPECT_TRUE(notMatches(
2997 "int a[] = { 2, 3 };",
2998 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
2999 EXPECT_TRUE(notMatches(
3000 "int const a[] = { 2, 3 };",
3001 qualType(arrayType(hasElementType(builtinType())),
3002 unless(isConstQualified()))));
3003
3004 EXPECT_TRUE(matches("int a[2];",
3005 constantArrayType(hasElementType(builtinType()))));
3006 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3007}
3008
3009TEST(TypeMatching, MatchesComplexTypes) {
3010 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3011 EXPECT_TRUE(matches(
3012 "_Complex float f;",
3013 complexType(hasElementType(builtinType()))));
3014 EXPECT_TRUE(notMatches(
3015 "_Complex float f;",
3016 complexType(hasElementType(isInteger()))));
3017}
3018
3019TEST(TypeMatching, MatchesConstantArrayTypes) {
3020 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3021 EXPECT_TRUE(notMatches(
3022 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3023 constantArrayType(hasElementType(builtinType()))));
3024
3025 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3026 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3027 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3028}
3029
3030TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3031 EXPECT_TRUE(matches(
3032 "template <typename T, int Size> class array { T data[Size]; };",
3033 dependentSizedArrayType()));
3034 EXPECT_TRUE(notMatches(
3035 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3036 dependentSizedArrayType()));
3037}
3038
3039TEST(TypeMatching, MatchesIncompleteArrayType) {
3040 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3041 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3042
3043 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3044 incompleteArrayType()));
3045}
3046
3047TEST(TypeMatching, MatchesVariableArrayType) {
3048 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3049 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3050
3051 EXPECT_TRUE(matches(
3052 "void f(int b) { int a[b]; }",
3053 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3054 varDecl(hasName("b")))))))));
3055}
3056
3057TEST(TypeMatching, MatchesAtomicTypes) {
3058 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3059
3060 EXPECT_TRUE(matches("_Atomic(int) i;",
3061 atomicType(hasValueType(isInteger()))));
3062 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3063 atomicType(hasValueType(isInteger()))));
3064}
3065
3066TEST(TypeMatching, MatchesAutoTypes) {
3067 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3068 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3069 autoType()));
3070
3071 EXPECT_TRUE(matches("auto a = 1;",
3072 autoType(hasDeducedType(isInteger()))));
3073 EXPECT_TRUE(notMatches("auto b = 2.0;",
3074 autoType(hasDeducedType(isInteger()))));
3075}
3076
Daniel Jaspera267cf62012-10-29 10:14:44 +00003077TEST(TypeMatching, MatchesFunctionTypes) {
3078 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3079 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3080}
3081
Daniel Jasperce620072012-10-17 08:52:59 +00003082TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003083 // FIXME: Reactive when these tests can be more specific (not matching
3084 // implicit code on certain platforms), likely when we have hasDescendant for
3085 // Types/TypeLocs.
3086 //EXPECT_TRUE(matchAndVerifyResultTrue(
3087 // "int* a;",
3088 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3089 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3090 //EXPECT_TRUE(matchAndVerifyResultTrue(
3091 // "int* a;",
3092 // pointerTypeLoc().bind("loc"),
3093 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003094 EXPECT_TRUE(matches(
3095 "int** a;",
3096 pointerTypeLoc(pointeeLoc(loc(qualType())))));
3097 EXPECT_TRUE(matches(
3098 "int** a;",
3099 loc(pointerType(pointee(pointerType())))));
3100 EXPECT_TRUE(matches(
3101 "int* b; int* * const a = &b;",
3102 loc(qualType(isConstQualified(), pointerType()))));
3103
3104 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003105 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3106 hasType(blockPointerType()))));
3107 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3108 hasType(memberPointerType()))));
3109 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3110 hasType(pointerType()))));
3111 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3112 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003113
Daniel Jasper1802daf2012-10-17 13:35:36 +00003114 Fragment = "int *ptr;";
3115 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3116 hasType(blockPointerType()))));
3117 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3118 hasType(memberPointerType()))));
3119 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3120 hasType(pointerType()))));
3121 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3122 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003123
Daniel Jasper1802daf2012-10-17 13:35:36 +00003124 Fragment = "int a; int &ref = a;";
3125 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3126 hasType(blockPointerType()))));
3127 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3128 hasType(memberPointerType()))));
3129 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3130 hasType(pointerType()))));
3131 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3132 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003133}
3134
3135TEST(TypeMatching, PointeeTypes) {
3136 EXPECT_TRUE(matches("int b; int &a = b;",
3137 referenceType(pointee(builtinType()))));
3138 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3139
3140 EXPECT_TRUE(matches("int *a;",
3141 pointerTypeLoc(pointeeLoc(loc(builtinType())))));
3142
3143 EXPECT_TRUE(matches(
3144 "int const *A;",
3145 pointerType(pointee(isConstQualified(), builtinType()))));
3146 EXPECT_TRUE(notMatches(
3147 "int *A;",
3148 pointerType(pointee(isConstQualified(), builtinType()))));
3149}
3150
3151TEST(TypeMatching, MatchesPointersToConstTypes) {
3152 EXPECT_TRUE(matches("int b; int * const a = &b;",
3153 loc(pointerType())));
3154 EXPECT_TRUE(matches("int b; int * const a = &b;",
3155 pointerTypeLoc()));
3156 EXPECT_TRUE(matches(
3157 "int b; const int * a = &b;",
3158 pointerTypeLoc(pointeeLoc(builtinTypeLoc()))));
3159 EXPECT_TRUE(matches(
3160 "int b; const int * a = &b;",
3161 pointerType(pointee(builtinType()))));
3162}
3163
3164TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003165 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3166 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003167
Daniel Jasper1802daf2012-10-17 13:35:36 +00003168 EXPECT_TRUE(matches("typedef int X; X a;",
3169 varDecl(hasName("a"),
3170 hasType(typedefType(hasDecl(decl()))))));
Daniel Jasperce620072012-10-17 08:52:59 +00003171}
3172
Daniel Jaspera7564432012-09-13 13:11:25 +00003173TEST(NNS, MatchesNestedNameSpecifiers) {
3174 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3175 nestedNameSpecifier()));
3176 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3177 nestedNameSpecifier()));
3178 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3179 nestedNameSpecifier()));
3180
3181 EXPECT_TRUE(matches(
3182 "struct A { static void f() {} }; void g() { A::f(); }",
3183 nestedNameSpecifier()));
3184 EXPECT_TRUE(notMatches(
3185 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3186 nestedNameSpecifier()));
3187}
3188
Daniel Jasperb54b7642012-09-20 14:12:57 +00003189TEST(NullStatement, SimpleCases) {
3190 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3191 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3192}
3193
Daniel Jaspera7564432012-09-13 13:11:25 +00003194TEST(NNS, MatchesTypes) {
3195 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3196 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3197 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3198 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3199 Matcher));
3200 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3201}
3202
3203TEST(NNS, MatchesNamespaceDecls) {
3204 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3205 specifiesNamespace(hasName("ns")));
3206 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3207 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3208 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3209}
3210
3211TEST(NNS, BindsNestedNameSpecifiers) {
3212 EXPECT_TRUE(matchAndVerifyResultTrue(
3213 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3214 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3215 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3216}
3217
3218TEST(NNS, BindsNestedNameSpecifierLocs) {
3219 EXPECT_TRUE(matchAndVerifyResultTrue(
3220 "namespace ns { struct B {}; } ns::B b;",
3221 loc(nestedNameSpecifier()).bind("loc"),
3222 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3223}
3224
3225TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3226 EXPECT_TRUE(matches(
3227 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3228 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3229 EXPECT_TRUE(matches(
3230 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003231 nestedNameSpecifierLoc(hasPrefix(
3232 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003233}
3234
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003235template <typename T>
3236class VerifyRecursiveMatch : public BoundNodesCallback {
3237public:
3238 explicit VerifyRecursiveMatch(StringRef Id,
3239 const internal::Matcher<T> &InnerMatcher)
3240 : Id(Id), InnerMatcher(InnerMatcher) {}
3241 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3242 const T *Node = Nodes->getNodeAs<T>(Id);
3243 bool Found = false;
3244 MatchFinder Finder;
3245 Finder.addMatcher(InnerMatcher, new VerifyMatch(0, &Found));
3246 Finder.findAll(*Node, *Context);
3247 return Found;
3248 }
3249private:
3250 std::string Id;
3251 internal::Matcher<T> InnerMatcher;
3252};
3253
3254TEST(MatchFinder, CanMatchDeclarationsRecursively) {
3255 EXPECT_TRUE(matchAndVerifyResultTrue("class X { class Y {}; };",
3256 recordDecl(hasName("::X")).bind("X"),
3257 new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Y")))));
3258 EXPECT_TRUE(matchAndVerifyResultFalse("class X { class Y {}; };",
3259 recordDecl(hasName("::X")).bind("X"),
3260 new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Z")))));
3261}
3262
3263TEST(MatchFinder, CanMatchStatementsRecursively) {
3264 EXPECT_TRUE(matchAndVerifyResultTrue("void f() { if (1) { for (;;) { } } }",
3265 ifStmt().bind("if"),
3266 new VerifyRecursiveMatch<clang::Stmt>("if", forStmt())));
3267 EXPECT_TRUE(matchAndVerifyResultFalse("void f() { if (1) { for (;;) { } } }",
3268 ifStmt().bind("if"),
3269 new VerifyRecursiveMatch<clang::Stmt>("if", declStmt())));
3270}
3271
Manuel Klimek4da21662012-07-06 05:48:52 +00003272} // end namespace ast_matchers
3273} // end namespace clang