blob: a97e90d74c5151c96c712d9cbd6ec1d51ce70814 [file] [log] [blame]
Manuel Klimek4da21662012-07-06 05:48:52 +00001//===- unittest/Tooling/ASTMatchersTest.cpp - AST matcher unit tests ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ASTMatchersTest.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Tooling/Tooling.h"
14#include "gtest/gtest.h"
15
16namespace clang {
17namespace ast_matchers {
18
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000019#if GTEST_HAS_DEATH_TEST
Manuel Klimek4da21662012-07-06 05:48:52 +000020TEST(HasNameDeathTest, DiesOnEmptyName) {
21 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000022 DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000023 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
24 }, "");
25}
26
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000027TEST(HasNameDeathTest, DiesOnEmptyPattern) {
28 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000029 DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000030 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
31 }, "");
32}
33
Manuel Klimek4da21662012-07-06 05:48:52 +000034TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
35 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000036 DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000037 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
38 }, "");
39}
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000040#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000041
Manuel Klimek715c9562012-07-25 10:02:02 +000042TEST(Decl, MatchesDeclarations) {
43 EXPECT_TRUE(notMatches("", decl(usingDecl())));
44 EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
45 decl(usingDecl())));
46}
47
Manuel Klimek4da21662012-07-06 05:48:52 +000048TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000049 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +000050 EXPECT_TRUE(matches("typedef int X;", NamedX));
51 EXPECT_TRUE(matches("int X;", NamedX));
52 EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
53 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
54 EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
55 EXPECT_TRUE(matches("namespace X { }", NamedX));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000056 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek4da21662012-07-06 05:48:52 +000057
58 EXPECT_TRUE(notMatches("#define X 1", NamedX));
59}
60
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000061TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000062 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000063 EXPECT_TRUE(matches("typedef int Xa;", NamedX));
64 EXPECT_TRUE(matches("int Xb;", NamedX));
65 EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
66 EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
67 EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
68 EXPECT_TRUE(matches("namespace Xij { }", NamedX));
69 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
70
71 EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
72
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000073 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000074 EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
75 EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
76
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000077 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000078 EXPECT_TRUE(matches("int abc;", Abc));
79 EXPECT_TRUE(matches("int aFOObBARc;", Abc));
80 EXPECT_TRUE(notMatches("int cab;", Abc));
81 EXPECT_TRUE(matches("int cabc;", Abc));
82}
83
Manuel Klimek4da21662012-07-06 05:48:52 +000084TEST(DeclarationMatcher, MatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000085 DeclarationMatcher ClassMatcher(recordDecl());
Manuel Klimeke265c872012-07-10 14:21:30 +000086#if !defined(_MSC_VER)
Manuel Klimek4da21662012-07-06 05:48:52 +000087 EXPECT_FALSE(matches("", ClassMatcher));
Manuel Klimeke265c872012-07-10 14:21:30 +000088#else
89 // Matches class type_info.
90 EXPECT_TRUE(matches("", ClassMatcher));
91#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000092
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000093 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +000094 EXPECT_TRUE(matches("class X;", ClassX));
95 EXPECT_TRUE(matches("class X {};", ClassX));
96 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
97 EXPECT_TRUE(notMatches("", ClassX));
98}
99
100TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000101 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000102
103 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000104 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
105 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek4da21662012-07-06 05:48:52 +0000106 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
107 EXPECT_TRUE(notMatches("", IsDerivedFromX));
108
Daniel Jasper63d88722012-09-12 21:14:15 +0000109 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000110
111 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
112 EXPECT_TRUE(matches("class X {};", IsAX));
113 EXPECT_TRUE(matches("class X;", IsAX));
114 EXPECT_TRUE(notMatches("class Y;", IsAX));
115 EXPECT_TRUE(notMatches("", IsAX));
116
Manuel Klimek4da21662012-07-06 05:48:52 +0000117 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000118 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000119 EXPECT_TRUE(
120 matches("class X {}; class Y : public X {}; class Z : public Y {};",
121 ZIsDerivedFromX));
122 EXPECT_TRUE(
123 matches("class X {};"
124 "template<class T> class Y : public X {};"
125 "class Z : public Y<int> {};", ZIsDerivedFromX));
126 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
127 ZIsDerivedFromX));
128 EXPECT_TRUE(
129 matches("template<class T> class X {}; "
130 "template<class T> class Z : public X<T> {};",
131 ZIsDerivedFromX));
132 EXPECT_TRUE(
133 matches("template<class T, class U=T> class X {}; "
134 "template<class T> class Z : public X<T> {};",
135 ZIsDerivedFromX));
136 EXPECT_TRUE(
137 notMatches("template<class X> class A { class Z : public X {}; };",
138 ZIsDerivedFromX));
139 EXPECT_TRUE(
140 matches("template<class X> class A { public: class Z : public X {}; }; "
141 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
142 EXPECT_TRUE(
143 matches("template <class T> class X {}; "
144 "template<class Y> class A { class Z : public X<Y> {}; };",
145 ZIsDerivedFromX));
146 EXPECT_TRUE(
147 notMatches("template<template<class T> class X> class A { "
148 " class Z : public X<int> {}; };", ZIsDerivedFromX));
149 EXPECT_TRUE(
150 matches("template<template<class T> class X> class A { "
151 " public: class Z : public X<int> {}; }; "
152 "template<class T> class X {}; void y() { A<X>::Z z; }",
153 ZIsDerivedFromX));
154 EXPECT_TRUE(
155 notMatches("template<class X> class A { class Z : public X::D {}; };",
156 ZIsDerivedFromX));
157 EXPECT_TRUE(
158 matches("template<class X> class A { public: "
159 " class Z : public X::D {}; }; "
160 "class Y { public: class X {}; typedef X D; }; "
161 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
162 EXPECT_TRUE(
163 matches("class X {}; typedef X Y; class Z : public Y {};",
164 ZIsDerivedFromX));
165 EXPECT_TRUE(
166 matches("template<class T> class Y { typedef typename T::U X; "
167 " class Z : public X {}; };", ZIsDerivedFromX));
168 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
169 ZIsDerivedFromX));
170 EXPECT_TRUE(
171 notMatches("template<class T> class X {}; "
172 "template<class T> class A { class Z : public X<T>::D {}; };",
173 ZIsDerivedFromX));
174 EXPECT_TRUE(
175 matches("template<class T> class X { public: typedef X<T> D; }; "
176 "template<class T> class A { public: "
177 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
178 ZIsDerivedFromX));
179 EXPECT_TRUE(
180 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
181 ZIsDerivedFromX));
182 EXPECT_TRUE(
183 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
184 ZIsDerivedFromX));
185 EXPECT_TRUE(
186 matches("class X {}; class Y : public X {}; "
187 "typedef Y V; typedef V W; class Z : public W {};",
188 ZIsDerivedFromX));
189 EXPECT_TRUE(
190 matches("template<class T, class U> class X {}; "
191 "template<class T> class A { class Z : public X<T, int> {}; };",
192 ZIsDerivedFromX));
193 EXPECT_TRUE(
194 notMatches("template<class X> class D { typedef X A; typedef A B; "
195 " typedef B C; class Z : public C {}; };",
196 ZIsDerivedFromX));
197 EXPECT_TRUE(
198 matches("class X {}; typedef X A; typedef A B; "
199 "class Z : public B {};", ZIsDerivedFromX));
200 EXPECT_TRUE(
201 matches("class X {}; typedef X A; typedef A B; typedef B C; "
202 "class Z : public C {};", ZIsDerivedFromX));
203 EXPECT_TRUE(
204 matches("class U {}; typedef U X; typedef X V; "
205 "class Z : public V {};", ZIsDerivedFromX));
206 EXPECT_TRUE(
207 matches("class Base {}; typedef Base X; "
208 "class Z : public Base {};", ZIsDerivedFromX));
209 EXPECT_TRUE(
210 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
211 "class Z : public Base {};", ZIsDerivedFromX));
212 EXPECT_TRUE(
213 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
214 "class Z : public Base {};", ZIsDerivedFromX));
215 EXPECT_TRUE(
216 matches("class A {}; typedef A X; typedef A Y; "
217 "class Z : public Y {};", ZIsDerivedFromX));
218 EXPECT_TRUE(
219 notMatches("template <typename T> class Z;"
220 "template <> class Z<void> {};"
221 "template <typename T> class Z : public Z<void> {};",
222 IsDerivedFromX));
223 EXPECT_TRUE(
224 matches("template <typename T> class X;"
225 "template <> class X<void> {};"
226 "template <typename T> class X : public X<void> {};",
227 IsDerivedFromX));
228 EXPECT_TRUE(matches(
229 "class X {};"
230 "template <typename T> class Z;"
231 "template <> class Z<void> {};"
232 "template <typename T> class Z : public Z<void>, public X {};",
233 ZIsDerivedFromX));
234
235 // FIXME: Once we have better matchers for template type matching,
236 // get rid of the Variable(...) matching and match the right template
237 // declarations directly.
238 const char *RecursiveTemplateOneParameter =
239 "class Base1 {}; class Base2 {};"
240 "template <typename T> class Z;"
241 "template <> class Z<void> : public Base1 {};"
242 "template <> class Z<int> : public Base2 {};"
243 "template <> class Z<float> : public Z<void> {};"
244 "template <> class Z<double> : public Z<int> {};"
245 "template <typename T> class Z : public Z<float>, public Z<double> {};"
246 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
247 EXPECT_TRUE(matches(
248 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000249 varDecl(hasName("z_float"),
250 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000251 EXPECT_TRUE(notMatches(
252 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000253 varDecl(hasName("z_float"),
254 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000255 EXPECT_TRUE(matches(
256 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000257 varDecl(hasName("z_char"),
258 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
259 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000260
261 const char *RecursiveTemplateTwoParameters =
262 "class Base1 {}; class Base2 {};"
263 "template <typename T1, typename T2> class Z;"
264 "template <typename T> class Z<void, T> : public Base1 {};"
265 "template <typename T> class Z<int, T> : public Base2 {};"
266 "template <typename T> class Z<float, T> : public Z<void, T> {};"
267 "template <typename T> class Z<double, T> : public Z<int, T> {};"
268 "template <typename T1, typename T2> class Z : "
269 " public Z<float, T2>, public Z<double, T2> {};"
270 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
271 " Z<char, void> z_char; }";
272 EXPECT_TRUE(matches(
273 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000274 varDecl(hasName("z_float"),
275 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000276 EXPECT_TRUE(notMatches(
277 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000278 varDecl(hasName("z_float"),
279 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000280 EXPECT_TRUE(matches(
281 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000282 varDecl(hasName("z_char"),
283 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
284 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000285 EXPECT_TRUE(matches(
286 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000287 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000288 EXPECT_TRUE(notMatches(
289 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000290 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000291
292 EXPECT_TRUE(matches(
293 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000294 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000295}
296
Daniel Jasper08f0c532012-09-18 14:17:42 +0000297TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
298 EXPECT_TRUE(matches(
299 "template <typename T> struct A {"
300 " template <typename T2> struct F {};"
301 "};"
302 "template <typename T> struct B : A<T>::template F<T> {};"
303 "B<int> b;",
304 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
305}
306
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000307TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000308 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000309 EXPECT_TRUE(notMatches("class X;", ClassX));
310 EXPECT_TRUE(notMatches("class X {};", ClassX));
311}
312
313TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000314 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000315 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
316 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
317}
318
319TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
320 EXPECT_TRUE(notMatches("template<typename T> class X { };"
321 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000322 classTemplateDecl(hasName("X"),
323 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000324}
325
326TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
327 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
328 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000329 classTemplateDecl(hasName("X"),
330 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000331}
332
Daniel Jasper6a124492012-07-12 08:50:38 +0000333TEST(AllOf, AllOverloadsWork) {
334 const char Program[] =
335 "struct T { }; int f(int, T*); void g(int x) { T t; f(x, &t); }";
336 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000337 callExpr(allOf(callee(functionDecl(hasName("f"))),
338 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000339 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000340 callExpr(allOf(callee(functionDecl(hasName("f"))),
341 hasArgument(0, declRefExpr(to(varDecl()))),
342 hasArgument(1, hasType(pointsTo(
343 recordDecl(hasName("T")))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000344}
345
Manuel Klimek4da21662012-07-06 05:48:52 +0000346TEST(DeclarationMatcher, MatchAnyOf) {
347 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000348 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000349 EXPECT_TRUE(
350 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
351 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
352 EXPECT_TRUE(
353 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
354 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
355
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000356 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000357 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000358 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
359 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
360
Manuel Klimek4da21662012-07-06 05:48:52 +0000361 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000362 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
363 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000364 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
365 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
366 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
367 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
368 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
369 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
370}
371
372TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000373 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000374 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
375 EXPECT_TRUE(matches("class X {};", HasClassX));
376
377 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000378 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000379 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
380 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
381 EXPECT_TRUE(
382 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
383}
384
385TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
386 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000387 recordDecl(
388 has(recordDecl(
389 has(recordDecl(hasName("X"))),
390 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000391 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000392 has(recordDecl(
393 has(recordDecl(hasName("A"))),
394 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000395 hasName("C"))),
396 hasName("F"));
397
398 EXPECT_TRUE(matches(
399 "class F {"
400 " class Z {"
401 " class X {};"
402 " class Y {};"
403 " };"
404 " class C {"
405 " class A {};"
406 " class B {};"
407 " };"
408 "};", Recursive));
409
410 EXPECT_TRUE(matches(
411 "class F {"
412 " class Z {"
413 " class A {};"
414 " class X {};"
415 " class Y {};"
416 " };"
417 " class C {"
418 " class X {};"
419 " class A {};"
420 " class B {};"
421 " };"
422 "};", Recursive));
423
424 EXPECT_TRUE(matches(
425 "class O1 {"
426 " class O2 {"
427 " class F {"
428 " class Z {"
429 " class A {};"
430 " class X {};"
431 " class Y {};"
432 " };"
433 " class C {"
434 " class X {};"
435 " class A {};"
436 " class B {};"
437 " };"
438 " };"
439 " };"
440 "};", Recursive));
441}
442
443TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
444 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000445 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000446 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000447 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000448 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000449 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000450 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000451 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000452 hasName("Y"))),
453 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000454 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000455 anyOf(
456 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000457 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000458 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000459 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000460 hasName("B")))))),
461 hasName("F")));
462
463 EXPECT_TRUE(matches("class F {};", Recursive));
464 EXPECT_TRUE(matches("class Z {};", Recursive));
465 EXPECT_TRUE(matches("class C {};", Recursive));
466 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
467 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
468 EXPECT_TRUE(
469 matches("class O1 { class O2 {"
470 " class M { class N { class B {}; }; }; "
471 "}; };", Recursive));
472}
473
474TEST(DeclarationMatcher, MatchNot) {
475 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000476 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000477 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000478 unless(hasName("X")));
479 EXPECT_TRUE(notMatches("", NotClassX));
480 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
481 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
482 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
483 EXPECT_TRUE(
484 notMatches("class Y {}; class Z {}; class X : public Y {};",
485 NotClassX));
486
487 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000488 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000489 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000490 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000491 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000492 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000493 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
494 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
495 ClassXHasNotClassY));
496}
497
498TEST(DeclarationMatcher, HasDescendant) {
499 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000500 recordDecl(
501 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000502 hasName("Z"));
503 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
504 EXPECT_TRUE(
505 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
506 EXPECT_TRUE(
507 matches("class Z { class A { class Y { class X {}; }; }; };",
508 ZDescendantClassX));
509 EXPECT_TRUE(
510 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
511 ZDescendantClassX));
512 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
513
514 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000515 recordDecl(
516 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000517 hasName("X"))),
518 hasName("Z"));
519 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
520 ZDescendantClassXHasClassY));
521 EXPECT_TRUE(
522 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
523 ZDescendantClassXHasClassY));
524 EXPECT_TRUE(notMatches(
525 "class Z {"
526 " class A {"
527 " class B {"
528 " class X {"
529 " class C {"
530 " class Y {};"
531 " };"
532 " };"
533 " }; "
534 " };"
535 "};", ZDescendantClassXHasClassY));
536
537 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000538 recordDecl(
539 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
540 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000541 hasName("Z"));
542 EXPECT_TRUE(
543 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
544 ZDescendantClassXDescendantClassY));
545 EXPECT_TRUE(matches(
546 "class Z {"
547 " class A {"
548 " class X {"
549 " class B {"
550 " class Y {};"
551 " };"
552 " class Y {};"
553 " };"
554 " };"
555 "};", ZDescendantClassXDescendantClassY));
556}
557
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000558TEST(Enum, DoesNotMatchClasses) {
559 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
560}
561
562TEST(Enum, MatchesEnums) {
563 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
564}
565
566TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000567 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000568 EXPECT_TRUE(matches("enum X{ A };", Matcher));
569 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
570 EXPECT_TRUE(notMatches("enum X {};", Matcher));
571}
572
Manuel Klimek4da21662012-07-06 05:48:52 +0000573TEST(StatementMatcher, Has) {
574 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000575 expr(hasType(pointsTo(recordDecl(hasName("X")))),
576 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000577
578 EXPECT_TRUE(matches(
579 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
580 EXPECT_TRUE(notMatches(
581 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
582}
583
584TEST(StatementMatcher, HasDescendant) {
585 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000586 expr(hasType(pointsTo(recordDecl(hasName("X")))),
587 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000588
589 EXPECT_TRUE(matches(
590 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
591 HasDescendantVariableI));
592 EXPECT_TRUE(notMatches(
593 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
594 HasDescendantVariableI));
595}
596
597TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000598 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000599
600 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
601 EXPECT_TRUE(notMatches("class A {};", TypeA));
602
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000603 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000604
605 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
606 TypeDerivedFromA));
607 EXPECT_TRUE(notMatches("class A {};", TypeA));
608
609 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000610 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000611
612 EXPECT_TRUE(
613 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
614}
615
Manuel Klimek579b1202012-09-07 09:26:10 +0000616// Implements a run method that returns whether BoundNodes contains a
617// Decl bound to Id that can be dynamically cast to T.
Manuel Klimek4da21662012-07-06 05:48:52 +0000618// Optionally checks that the check succeeded a specific number of times.
619template <typename T>
Daniel Jaspera7564432012-09-13 13:11:25 +0000620class VerifyIdIsBoundTo : public BoundNodesCallback {
Manuel Klimek4da21662012-07-06 05:48:52 +0000621public:
Manuel Klimek579b1202012-09-07 09:26:10 +0000622 // Create an object that checks that a node of type \c T was bound to \c Id.
Manuel Klimek4da21662012-07-06 05:48:52 +0000623 // Does not check for a certain number of matches.
Daniel Jaspera7564432012-09-13 13:11:25 +0000624 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
Manuel Klimek4da21662012-07-06 05:48:52 +0000625 : Id(Id), ExpectedCount(-1), Count(0) {}
626
Manuel Klimek579b1202012-09-07 09:26:10 +0000627 // Create an object that checks that a node of type \c T was bound to \c Id.
628 // Checks that there were exactly \c ExpectedCount matches.
Daniel Jaspera7564432012-09-13 13:11:25 +0000629 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
Manuel Klimek4da21662012-07-06 05:48:52 +0000630 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
631
Manuel Klimek579b1202012-09-07 09:26:10 +0000632 // Create an object that checks that a node of type \c T was bound to \c Id.
Daniel Jaspera7564432012-09-13 13:11:25 +0000633 // Checks that there was exactly one match with the name \c ExpectedName.
Manuel Klimek579b1202012-09-07 09:26:10 +0000634 // Note that \c T must be a NamedDecl for this to work.
Daniel Jaspera7564432012-09-13 13:11:25 +0000635 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName)
636 : Id(Id), ExpectedCount(1), Count(0), ExpectedName(ExpectedName) {}
Manuel Klimek579b1202012-09-07 09:26:10 +0000637
Daniel Jaspera7564432012-09-13 13:11:25 +0000638 ~VerifyIdIsBoundTo() {
Manuel Klimek579b1202012-09-07 09:26:10 +0000639 if (ExpectedCount != -1)
Manuel Klimek4da21662012-07-06 05:48:52 +0000640 EXPECT_EQ(ExpectedCount, Count);
Daniel Jaspera7564432012-09-13 13:11:25 +0000641 if (!ExpectedName.empty())
642 EXPECT_EQ(ExpectedName, Name);
Manuel Klimek4da21662012-07-06 05:48:52 +0000643 }
644
645 virtual bool run(const BoundNodes *Nodes) {
Daniel Jaspera7564432012-09-13 13:11:25 +0000646 if (Nodes->getNodeAs<T>(Id)) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000647 ++Count;
Daniel Jaspera7564432012-09-13 13:11:25 +0000648 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
649 Name = Named->getNameAsString();
650 } else if (const NestedNameSpecifier *NNS =
651 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
652 llvm::raw_string_ostream OS(Name);
653 NNS->print(OS, PrintingPolicy(LangOptions()));
Manuel Klimek579b1202012-09-07 09:26:10 +0000654 }
Manuel Klimek4da21662012-07-06 05:48:52 +0000655 return true;
656 }
657 return false;
658 }
659
660private:
661 const std::string Id;
662 const int ExpectedCount;
663 int Count;
Daniel Jaspera7564432012-09-13 13:11:25 +0000664 const std::string ExpectedName;
665 std::string Name;
Manuel Klimek4da21662012-07-06 05:48:52 +0000666};
667
668TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000669 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000670
671 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000672 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000673
674 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000675 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000676
677 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000678 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000679
680 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
681 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000682 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000683
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000684 StatementMatcher MethodX =
685 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000686
687 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
688 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000689 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000690}
691
692TEST(Matcher, BindTheSameNameInAlternatives) {
693 StatementMatcher matcher = anyOf(
694 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000695 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000696 hasRHS(integerLiteral(equals(0)))),
697 binaryOperator(hasOperatorName("+"),
698 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000699 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000700
701 EXPECT_TRUE(matchAndVerifyResultTrue(
702 // The first branch of the matcher binds x to 0 but then fails.
703 // The second branch binds x to f() and succeeds.
704 "int f() { return 0 + f(); }",
705 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000706 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000707}
708
Manuel Klimek66341c52012-08-30 19:41:06 +0000709TEST(Matcher, BindsIDForMemoizedResults) {
710 // Using the same matcher in two match expressions will make memoization
711 // kick in.
712 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
713 EXPECT_TRUE(matchAndVerifyResultTrue(
714 "class A { class B { class X {}; }; };",
715 DeclarationMatcher(anyOf(
716 recordDecl(hasName("A"), hasDescendant(ClassX)),
717 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000718 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000719}
720
Manuel Klimek4da21662012-07-06 05:48:52 +0000721TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000722 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000723 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000724 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000725 EXPECT_TRUE(
726 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000727 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000728 EXPECT_TRUE(
729 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000730 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000731}
732
733TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000734 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000735 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000736 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000737 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000738 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000739 EXPECT_TRUE(
740 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000741 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000742}
743
744TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000745 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000746 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000747 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000748 EXPECT_TRUE(
749 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000750 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000751}
752
753TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000754 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000755 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000756 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000757 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000758 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000759}
760
761TEST(Matcher, Call) {
762 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000763 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000764 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000765
766 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
767 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
768
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000769 StatementMatcher MethodOnY =
770 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000771
772 EXPECT_TRUE(
773 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
774 MethodOnY));
775 EXPECT_TRUE(
776 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
777 MethodOnY));
778 EXPECT_TRUE(
779 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
780 MethodOnY));
781 EXPECT_TRUE(
782 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
783 MethodOnY));
784 EXPECT_TRUE(
785 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
786 MethodOnY));
787
788 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000789 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000790
791 EXPECT_TRUE(
792 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
793 MethodOnYPointer));
794 EXPECT_TRUE(
795 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
796 MethodOnYPointer));
797 EXPECT_TRUE(
798 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
799 MethodOnYPointer));
800 EXPECT_TRUE(
801 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
802 MethodOnYPointer));
803 EXPECT_TRUE(
804 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
805 MethodOnYPointer));
806}
807
Daniel Jasper31f7c082012-10-01 13:40:41 +0000808TEST(Matcher, Lambda) {
809 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
810 lambdaExpr()));
811}
812
813TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000814 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
815 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000816 forRangeStmt()));
817 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
818 forRangeStmt()));
819}
820
821TEST(Matcher, UserDefinedLiteral) {
822 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
823 " return i + 1;"
824 "}"
825 "char c = 'a'_inc;",
826 userDefinedLiteral()));
827}
828
Daniel Jasperb54b7642012-09-20 14:12:57 +0000829TEST(Matcher, FlowControl) {
830 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
831 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
832 continueStmt()));
833 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
834 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
835 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
836}
837
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000838TEST(HasType, MatchesAsString) {
839 EXPECT_TRUE(
840 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000841 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000842 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000843 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000844 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000845 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000846 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000847 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000848}
849
Manuel Klimek4da21662012-07-06 05:48:52 +0000850TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000851 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000852 // Unary operator
853 EXPECT_TRUE(matches("class Y { }; "
854 "bool operator!(Y x) { return false; }; "
855 "Y y; bool c = !y;", OpCall));
856 // No match -- special operators like "new", "delete"
857 // FIXME: operator new takes size_t, for which we need stddef.h, for which
858 // we need to figure out include paths in the test.
859 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
860 // "class Y { }; "
861 // "void *operator new(size_t size) { return 0; } "
862 // "Y *y = new Y;", OpCall));
863 EXPECT_TRUE(notMatches("class Y { }; "
864 "void operator delete(void *p) { } "
865 "void a() {Y *y = new Y; delete y;}", OpCall));
866 // Binary operator
867 EXPECT_TRUE(matches("class Y { }; "
868 "bool operator&&(Y x, Y y) { return true; }; "
869 "Y a; Y b; bool c = a && b;",
870 OpCall));
871 // No match -- normal operator, not an overloaded one.
872 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
873 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
874}
875
876TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
877 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000878 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000879 EXPECT_TRUE(matches("class Y { }; "
880 "bool operator&&(Y x, Y y) { return true; }; "
881 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
882 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000883 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000884 EXPECT_TRUE(notMatches("class Y { }; "
885 "bool operator&&(Y x, Y y) { return true; }; "
886 "Y a; Y b; bool c = a && b;",
887 OpCallLessLess));
888}
889
890TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +0000891 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000892 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000893
894 EXPECT_TRUE(
895 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
896 MethodOnY));
897 EXPECT_TRUE(
898 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
899 MethodOnY));
900 EXPECT_TRUE(
901 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
902 MethodOnY));
903 EXPECT_TRUE(
904 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
905 MethodOnY));
906 EXPECT_TRUE(
907 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
908 MethodOnY));
909
910 EXPECT_TRUE(matches(
911 "class Y {"
912 " public: virtual void x();"
913 "};"
914 "class X : public Y {"
915 " public: virtual void x();"
916 "};"
917 "void z() { X *x; x->Y::x(); }", MethodOnY));
918}
919
920TEST(Matcher, VariableUsage) {
921 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000922 declRefExpr(to(
923 varDecl(hasInitializer(
924 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000925
926 EXPECT_TRUE(matches(
927 "class Y {"
928 " public:"
929 " bool x() const;"
930 "};"
931 "void z(const Y &y) {"
932 " bool b = y.x();"
933 " if (b) {}"
934 "}", Reference));
935
936 EXPECT_TRUE(notMatches(
937 "class Y {"
938 " public:"
939 " bool x() const;"
940 "};"
941 "void z(const Y &y) {"
942 " bool b = y.x();"
943 "}", Reference));
944}
945
Daniel Jasper9bd28092012-07-30 05:03:25 +0000946TEST(Matcher, FindsVarDeclInFuncitonParameter) {
947 EXPECT_TRUE(matches(
948 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000949 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +0000950}
951
Manuel Klimek4da21662012-07-06 05:48:52 +0000952TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +0000953 StatementMatcher CallOnVariableY =
954 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000955
956 EXPECT_TRUE(matches(
957 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
958 EXPECT_TRUE(matches(
959 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
960 EXPECT_TRUE(matches(
961 "class Y { public: void x(); };"
962 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
963 EXPECT_TRUE(matches(
964 "class Y { public: void x(); };"
965 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
966 EXPECT_TRUE(notMatches(
967 "class Y { public: void x(); };"
968 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
969 CallOnVariableY));
970}
971
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000972TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
973 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
974 unaryExprOrTypeTraitExpr()));
975 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
976 alignOfExpr(anything())));
977 // FIXME: Uncomment once alignof is enabled.
978 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
979 // unaryExprOrTypeTraitExpr()));
980 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
981 // sizeOfExpr()));
982}
983
984TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
985 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
986 hasArgumentOfType(asString("int")))));
987 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
988 hasArgumentOfType(asString("float")))));
989 EXPECT_TRUE(matches(
990 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000991 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000992 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000993 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000994}
995
Manuel Klimek4da21662012-07-06 05:48:52 +0000996TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000997 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +0000998}
999
1000TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001001 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001002}
1003
1004TEST(MemberExpression, MatchesVariable) {
1005 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001006 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001007 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001008 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001009 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001010 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001011}
1012
1013TEST(MemberExpression, MatchesStaticVariable) {
1014 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001015 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001016 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001017 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001018 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001019 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001020}
1021
Daniel Jasper6a124492012-07-12 08:50:38 +00001022TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001023 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1024 EXPECT_TRUE(matches(
1025 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1026 callExpr(hasArgument(0, declRefExpr(
1027 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001028}
1029
1030TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001031 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001032 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001033 callExpr(hasArgument(0, declRefExpr(
1034 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001035}
1036
Manuel Klimek4da21662012-07-06 05:48:52 +00001037TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1038 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001039 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001040 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001041 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001042 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001043 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001044}
1045
1046TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1047 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001048 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001049 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001050 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001051 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001052 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001053}
1054
1055TEST(IsArrow, MatchesMemberCallsViaArrow) {
1056 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001057 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001058 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001059 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001060 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001061 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001062}
1063
1064TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001065 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001066
1067 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1068 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1069}
1070
1071TEST(Callee, MatchesMemberExpressions) {
1072 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001073 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001074 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001075 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001076}
1077
1078TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001079 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001080
1081 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1082 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1083
Manuel Klimeke265c872012-07-10 14:21:30 +00001084#if !defined(_MSC_VER)
1085 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001086 // Dependent contexts, but a non-dependent call.
1087 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1088 CallFunctionF));
1089 EXPECT_TRUE(
1090 matches("void f(); template <int N> struct S { void g() { f(); } };",
1091 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001092#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001093
1094 // Depedent calls don't match.
1095 EXPECT_TRUE(
1096 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1097 CallFunctionF));
1098 EXPECT_TRUE(
1099 notMatches("void f(int);"
1100 "template <typename T> struct S { void g(T t) { f(t); } };",
1101 CallFunctionF));
1102}
1103
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001104TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1105 EXPECT_TRUE(
1106 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001107 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001108}
1109
1110TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1111 EXPECT_TRUE(
1112 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001113 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001114}
1115
1116TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1117 EXPECT_TRUE(
1118 notMatches("void g(); template <typename T> void f(T t) {}"
1119 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001120 functionTemplateDecl(hasName("f"),
1121 hasDescendant(declRefExpr(to(
1122 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001123}
1124
Manuel Klimek4da21662012-07-06 05:48:52 +00001125TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001126 StatementMatcher CallArgumentY = callExpr(
1127 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001128
1129 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1130 EXPECT_TRUE(
1131 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1132 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1133
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001134 StatementMatcher WrongIndex = callExpr(
1135 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001136 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1137}
1138
1139TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001140 StatementMatcher CallArgumentY = callExpr(
1141 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001142 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1143 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1144 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1145}
1146
1147TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001148 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001149
1150 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1151 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1152 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1153}
1154
1155TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001156 DeclarationMatcher ReferenceClassX = varDecl(
1157 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001158 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1159 ReferenceClassX));
1160 EXPECT_TRUE(
1161 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1162 EXPECT_TRUE(
1163 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1164 EXPECT_TRUE(
1165 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1166}
1167
1168TEST(HasParameter, CallsInnerMatcher) {
1169 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001170 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001171 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001172 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001173}
1174
1175TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1176 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001177 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001178}
1179
1180TEST(HasType, MatchesParameterVariableTypesStrictly) {
1181 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001182 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001183 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001184 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001185 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001186 methodDecl(hasParameter(0,
1187 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001188 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001189 methodDecl(hasParameter(0,
1190 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001191}
1192
1193TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1194 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001195 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001196 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001197 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001198}
1199
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001200TEST(Returns, MatchesReturnTypes) {
1201 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001202 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001203 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001204 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001205 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001206 functionDecl(returns(hasDeclaration(
1207 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001208}
1209
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001210TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001211 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1212 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1213 functionDecl(isExternC())));
1214 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001215}
1216
Manuel Klimek4da21662012-07-06 05:48:52 +00001217TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1218 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001219 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001220}
1221
1222TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1223 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001224 methodDecl(hasAnyParameter(hasType(pointsTo(
1225 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001226}
1227
1228TEST(HasName, MatchesParameterVariableDeclartions) {
1229 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001230 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001231 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001232 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001233}
1234
Daniel Jasper371f9392012-08-01 08:40:24 +00001235TEST(Matcher, MatchesClassTemplateSpecialization) {
1236 EXPECT_TRUE(matches("template<typename T> struct A {};"
1237 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001238 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001239 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001240 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001241 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001242 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001243}
1244
1245TEST(Matcher, MatchesTypeTemplateArgument) {
1246 EXPECT_TRUE(matches(
1247 "template<typename T> struct B {};"
1248 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001249 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001250 asString("int"))))));
1251}
1252
1253TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1254 EXPECT_TRUE(matches(
1255 "struct B { int next; };"
1256 "template<int(B::*next_ptr)> struct A {};"
1257 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001258 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1259 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001260
1261 EXPECT_TRUE(notMatches(
1262 "template <typename T> struct A {};"
1263 "A<int> a;",
1264 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1265 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001266}
1267
1268TEST(Matcher, MatchesSpecificArgument) {
1269 EXPECT_TRUE(matches(
1270 "template<typename T, typename U> class A {};"
1271 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001272 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001273 1, refersToType(asString("int"))))));
1274 EXPECT_TRUE(notMatches(
1275 "template<typename T, typename U> class A {};"
1276 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001277 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001278 1, refersToType(asString("int"))))));
1279}
1280
Manuel Klimek4da21662012-07-06 05:48:52 +00001281TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001282 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001283
1284 EXPECT_TRUE(
1285 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1286 EXPECT_TRUE(
1287 matches("class X { public: X(); }; void x() { X x = X(); }",
1288 Constructor));
1289 EXPECT_TRUE(
1290 matches("class X { public: X(int); }; void x() { X x = 0; }",
1291 Constructor));
1292 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1293}
1294
1295TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001296 StatementMatcher Constructor = constructExpr(
1297 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001298
1299 EXPECT_TRUE(
1300 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1301 Constructor));
1302 EXPECT_TRUE(
1303 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1304 Constructor));
1305 EXPECT_TRUE(
1306 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1307 Constructor));
1308 EXPECT_TRUE(
1309 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1310 Constructor));
1311
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001312 StatementMatcher WrongIndex = constructExpr(
1313 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001314 EXPECT_TRUE(
1315 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1316 WrongIndex));
1317}
1318
1319TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001320 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001321
1322 EXPECT_TRUE(
1323 matches("class X { public: X(int); }; void x() { X x(0); }",
1324 Constructor1Arg));
1325 EXPECT_TRUE(
1326 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1327 Constructor1Arg));
1328 EXPECT_TRUE(
1329 matches("class X { public: X(int); }; void x() { X x = 0; }",
1330 Constructor1Arg));
1331 EXPECT_TRUE(
1332 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1333 Constructor1Arg));
1334}
1335
Manuel Klimek70b9db92012-10-23 10:40:50 +00001336TEST(Matcher,ThisExpr) {
1337 EXPECT_TRUE(
1338 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1339 EXPECT_TRUE(
1340 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1341}
1342
Manuel Klimek4da21662012-07-06 05:48:52 +00001343TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001344 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001345
1346 std::string ClassString = "class string { public: string(); ~string(); }; ";
1347
1348 EXPECT_TRUE(
1349 matches(ClassString +
1350 "string GetStringByValue();"
1351 "void FunctionTakesString(string s);"
1352 "void run() { FunctionTakesString(GetStringByValue()); }",
1353 TempExpression));
1354
1355 EXPECT_TRUE(
1356 notMatches(ClassString +
1357 "string* GetStringPointer(); "
1358 "void FunctionTakesStringPtr(string* s);"
1359 "void run() {"
1360 " string* s = GetStringPointer();"
1361 " FunctionTakesStringPtr(GetStringPointer());"
1362 " FunctionTakesStringPtr(s);"
1363 "}",
1364 TempExpression));
1365
1366 EXPECT_TRUE(
1367 notMatches("class no_dtor {};"
1368 "no_dtor GetObjByValue();"
1369 "void ConsumeObj(no_dtor param);"
1370 "void run() { ConsumeObj(GetObjByValue()); }",
1371 TempExpression));
1372}
1373
Sam Panzere16acd32012-08-24 22:04:44 +00001374TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1375 std::string ClassString =
1376 "class string { public: string(); int length(); }; ";
1377
1378 EXPECT_TRUE(
1379 matches(ClassString +
1380 "string GetStringByValue();"
1381 "void FunctionTakesString(string s);"
1382 "void run() { FunctionTakesString(GetStringByValue()); }",
1383 materializeTemporaryExpr()));
1384
1385 EXPECT_TRUE(
1386 notMatches(ClassString +
1387 "string* GetStringPointer(); "
1388 "void FunctionTakesStringPtr(string* s);"
1389 "void run() {"
1390 " string* s = GetStringPointer();"
1391 " FunctionTakesStringPtr(GetStringPointer());"
1392 " FunctionTakesStringPtr(s);"
1393 "}",
1394 materializeTemporaryExpr()));
1395
1396 EXPECT_TRUE(
1397 notMatches(ClassString +
1398 "string GetStringByValue();"
1399 "void run() { int k = GetStringByValue().length(); }",
1400 materializeTemporaryExpr()));
1401
1402 EXPECT_TRUE(
1403 notMatches(ClassString +
1404 "string GetStringByValue();"
1405 "void run() { GetStringByValue(); }",
1406 materializeTemporaryExpr()));
1407}
1408
Manuel Klimek4da21662012-07-06 05:48:52 +00001409TEST(ConstructorDeclaration, SimpleCase) {
1410 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001411 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001412 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001413 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001414}
1415
1416TEST(ConstructorDeclaration, IsImplicit) {
1417 // This one doesn't match because the constructor is not added by the
1418 // compiler (it is not needed).
1419 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001420 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001421 // The compiler added the implicit default constructor.
1422 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001423 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001424 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001425 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001426}
1427
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001428TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1429 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001430 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001431}
1432
1433TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001434 EXPECT_TRUE(notMatches("class Foo {};",
1435 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001436}
1437
Manuel Klimek4da21662012-07-06 05:48:52 +00001438TEST(HasAnyConstructorInitializer, SimpleCase) {
1439 EXPECT_TRUE(notMatches(
1440 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001441 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001442 EXPECT_TRUE(matches(
1443 "class Foo {"
1444 " Foo() : foo_() { }"
1445 " int foo_;"
1446 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001447 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001448}
1449
1450TEST(HasAnyConstructorInitializer, ForField) {
1451 static const char Code[] =
1452 "class Baz { };"
1453 "class Foo {"
1454 " Foo() : foo_() { }"
1455 " Baz foo_;"
1456 " Baz bar_;"
1457 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001458 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1459 forField(hasType(recordDecl(hasName("Baz"))))))));
1460 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001461 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001462 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1463 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001464}
1465
1466TEST(HasAnyConstructorInitializer, WithInitializer) {
1467 static const char Code[] =
1468 "class Foo {"
1469 " Foo() : foo_(0) { }"
1470 " int foo_;"
1471 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001472 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001473 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001474 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001475 withInitializer(integerLiteral(equals(1)))))));
1476}
1477
1478TEST(HasAnyConstructorInitializer, IsWritten) {
1479 static const char Code[] =
1480 "struct Bar { Bar(){} };"
1481 "class Foo {"
1482 " Foo() : foo_() { }"
1483 " Bar foo_;"
1484 " Bar bar_;"
1485 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001486 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001487 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001488 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001489 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001490 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001491 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1492}
1493
1494TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001495 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001496
1497 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1498 EXPECT_TRUE(
1499 matches("class X { public: X(); }; void x() { new X(); }", New));
1500 EXPECT_TRUE(
1501 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1502 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1503}
1504
1505TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001506 StatementMatcher New = constructExpr(
1507 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001508
1509 EXPECT_TRUE(
1510 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1511 New));
1512 EXPECT_TRUE(
1513 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1514 New));
1515 EXPECT_TRUE(
1516 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1517 New));
1518
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001519 StatementMatcher WrongIndex = constructExpr(
1520 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001521 EXPECT_TRUE(
1522 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1523 WrongIndex));
1524}
1525
1526TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001527 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001528
1529 EXPECT_TRUE(
1530 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1531 EXPECT_TRUE(
1532 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1533 New));
1534}
1535
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001536TEST(Matcher, DeleteExpression) {
1537 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001538 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001539}
1540
Manuel Klimek4da21662012-07-06 05:48:52 +00001541TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001542 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001543
1544 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1545 EXPECT_TRUE(
1546 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1547 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1548}
1549
1550TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001551 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001552 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1553 // wide string
1554 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1555 // with escaped characters
1556 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1557 // no matching -- though the data type is the same, there is no string literal
1558 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1559}
1560
1561TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001562 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001563 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1564 // wide character
1565 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1566 // wide character, Hex encoded, NOT MATCHED!
1567 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1568 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1569}
1570
1571TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001572 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001573 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1574 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1575 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1576 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1577
1578 // Non-matching cases (character literals, float and double)
1579 EXPECT_TRUE(notMatches("int i = L'a';",
1580 HasIntLiteral)); // this is actually a character
1581 // literal cast to int
1582 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1583 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1584 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1585}
1586
Daniel Jasper31f7c082012-10-01 13:40:41 +00001587TEST(Matcher, NullPtrLiteral) {
1588 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1589}
1590
Daniel Jasperb54b7642012-09-20 14:12:57 +00001591TEST(Matcher, AsmStatement) {
1592 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1593}
1594
Manuel Klimek4da21662012-07-06 05:48:52 +00001595TEST(Matcher, Conditions) {
1596 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1597
1598 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1599 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1600 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1601 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1602 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1603}
1604
1605TEST(MatchBinaryOperator, HasOperatorName) {
1606 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1607
1608 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1609 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1610}
1611
1612TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1613 StatementMatcher OperatorTrueFalse =
1614 binaryOperator(hasLHS(boolLiteral(equals(true))),
1615 hasRHS(boolLiteral(equals(false))));
1616
1617 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1618 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1619 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1620}
1621
1622TEST(MatchBinaryOperator, HasEitherOperand) {
1623 StatementMatcher HasOperand =
1624 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1625
1626 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1627 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1628 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1629}
1630
1631TEST(Matcher, BinaryOperatorTypes) {
1632 // Integration test that verifies the AST provides all binary operators in
1633 // a way we expect.
1634 // FIXME: Operator ','
1635 EXPECT_TRUE(
1636 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1637 EXPECT_TRUE(
1638 matches("bool b; bool c = (b = true);",
1639 binaryOperator(hasOperatorName("="))));
1640 EXPECT_TRUE(
1641 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1642 EXPECT_TRUE(
1643 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1644 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1645 EXPECT_TRUE(
1646 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1647 EXPECT_TRUE(
1648 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1649 EXPECT_TRUE(
1650 matches("int i = 1; int j = (i <<= 2);",
1651 binaryOperator(hasOperatorName("<<="))));
1652 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1653 EXPECT_TRUE(
1654 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1655 EXPECT_TRUE(
1656 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1657 EXPECT_TRUE(
1658 matches("int i = 1; int j = (i >>= 2);",
1659 binaryOperator(hasOperatorName(">>="))));
1660 EXPECT_TRUE(
1661 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1662 EXPECT_TRUE(
1663 matches("int i = 42; int j = (i ^= 42);",
1664 binaryOperator(hasOperatorName("^="))));
1665 EXPECT_TRUE(
1666 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1667 EXPECT_TRUE(
1668 matches("int i = 42; int j = (i %= 42);",
1669 binaryOperator(hasOperatorName("%="))));
1670 EXPECT_TRUE(
1671 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1672 EXPECT_TRUE(
1673 matches("bool b = true && false;",
1674 binaryOperator(hasOperatorName("&&"))));
1675 EXPECT_TRUE(
1676 matches("bool b = true; bool c = (b &= false);",
1677 binaryOperator(hasOperatorName("&="))));
1678 EXPECT_TRUE(
1679 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1680 EXPECT_TRUE(
1681 matches("bool b = true || false;",
1682 binaryOperator(hasOperatorName("||"))));
1683 EXPECT_TRUE(
1684 matches("bool b = true; bool c = (b |= false);",
1685 binaryOperator(hasOperatorName("|="))));
1686 EXPECT_TRUE(
1687 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1688 EXPECT_TRUE(
1689 matches("int i = 42; int j = (i *= 23);",
1690 binaryOperator(hasOperatorName("*="))));
1691 EXPECT_TRUE(
1692 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1693 EXPECT_TRUE(
1694 matches("int i = 42; int j = (i /= 23);",
1695 binaryOperator(hasOperatorName("/="))));
1696 EXPECT_TRUE(
1697 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1698 EXPECT_TRUE(
1699 matches("int i = 42; int j = (i += 23);",
1700 binaryOperator(hasOperatorName("+="))));
1701 EXPECT_TRUE(
1702 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1703 EXPECT_TRUE(
1704 matches("int i = 42; int j = (i -= 23);",
1705 binaryOperator(hasOperatorName("-="))));
1706 EXPECT_TRUE(
1707 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1708 binaryOperator(hasOperatorName("->*"))));
1709 EXPECT_TRUE(
1710 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1711 binaryOperator(hasOperatorName(".*"))));
1712
1713 // Member expressions as operators are not supported in matches.
1714 EXPECT_TRUE(
1715 notMatches("struct A { void x(A *a) { a->x(this); } };",
1716 binaryOperator(hasOperatorName("->"))));
1717
1718 // Initializer assignments are not represented as operator equals.
1719 EXPECT_TRUE(
1720 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1721
1722 // Array indexing is not represented as operator.
1723 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1724
1725 // Overloaded operators do not match at all.
1726 EXPECT_TRUE(notMatches(
1727 "struct A { bool operator&&(const A &a) const { return false; } };"
1728 "void x() { A a, b; a && b; }",
1729 binaryOperator()));
1730}
1731
1732TEST(MatchUnaryOperator, HasOperatorName) {
1733 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1734
1735 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1736 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1737}
1738
1739TEST(MatchUnaryOperator, HasUnaryOperand) {
1740 StatementMatcher OperatorOnFalse =
1741 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1742
1743 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1744 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1745}
1746
1747TEST(Matcher, UnaryOperatorTypes) {
1748 // Integration test that verifies the AST provides all unary operators in
1749 // a way we expect.
1750 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1751 EXPECT_TRUE(
1752 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1753 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1754 EXPECT_TRUE(
1755 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1756 EXPECT_TRUE(
1757 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1758 EXPECT_TRUE(
1759 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1760 EXPECT_TRUE(
1761 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1762 EXPECT_TRUE(
1763 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1764 EXPECT_TRUE(
1765 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1766 EXPECT_TRUE(
1767 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1768
1769 // We don't match conversion operators.
1770 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1771
1772 // Function calls are not represented as operator.
1773 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1774
1775 // Overloaded operators do not match at all.
1776 // FIXME: We probably want to add that.
1777 EXPECT_TRUE(notMatches(
1778 "struct A { bool operator!() const { return false; } };"
1779 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1780}
1781
1782TEST(Matcher, ConditionalOperator) {
1783 StatementMatcher Conditional = conditionalOperator(
1784 hasCondition(boolLiteral(equals(true))),
1785 hasTrueExpression(boolLiteral(equals(false))));
1786
1787 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1788 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1789 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1790
1791 StatementMatcher ConditionalFalse = conditionalOperator(
1792 hasFalseExpression(boolLiteral(equals(false))));
1793
1794 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1795 EXPECT_TRUE(
1796 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1797}
1798
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001799TEST(ArraySubscriptMatchers, ArraySubscripts) {
1800 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1801 arraySubscriptExpr()));
1802 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1803 arraySubscriptExpr()));
1804}
1805
1806TEST(ArraySubscriptMatchers, ArrayIndex) {
1807 EXPECT_TRUE(matches(
1808 "int i[2]; void f() { i[1] = 1; }",
1809 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1810 EXPECT_TRUE(matches(
1811 "int i[2]; void f() { 1[i] = 1; }",
1812 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1813 EXPECT_TRUE(notMatches(
1814 "int i[2]; void f() { i[1] = 1; }",
1815 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1816}
1817
1818TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1819 EXPECT_TRUE(matches(
1820 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001821 arraySubscriptExpr(hasBase(implicitCastExpr(
1822 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001823}
1824
Manuel Klimek4da21662012-07-06 05:48:52 +00001825TEST(Matcher, HasNameSupportsNamespaces) {
1826 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001827 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001828 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001829 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001830 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001831 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001832 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001833 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001834 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001835 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001836 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001837 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001838 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001839 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001840 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001841 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001842 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001843 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001844 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001845 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001846 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001847 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001848 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001849 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001850}
1851
1852TEST(Matcher, HasNameSupportsOuterClasses) {
1853 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001854 matches("class A { class B { class C; }; };",
1855 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001856 EXPECT_TRUE(
1857 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001858 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001859 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001860 matches("class A { class B { class C; }; };",
1861 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001862 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001863 matches("class A { class B { class C; }; };",
1864 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001865 EXPECT_TRUE(
1866 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001867 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001868 EXPECT_TRUE(
1869 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001870 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001871 EXPECT_TRUE(
1872 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001873 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001874 EXPECT_TRUE(
1875 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001876 recordDecl(hasName("::C"))));
1877 EXPECT_TRUE(
1878 notMatches("class A { class B { class C; }; };",
1879 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001880 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001881 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001882 EXPECT_TRUE(
1883 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001884 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001885}
1886
1887TEST(Matcher, IsDefinition) {
1888 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001889 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001890 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1891 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1892
1893 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001894 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001895 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1896 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1897
1898 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001899 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001900 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1901 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1902}
1903
1904TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001905 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00001906 ofClass(hasName("X")))));
1907
1908 EXPECT_TRUE(
1909 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
1910 EXPECT_TRUE(
1911 matches("class X { public: X(); }; void x(int) { X x = X(); }",
1912 Constructor));
1913 EXPECT_TRUE(
1914 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
1915 Constructor));
1916}
1917
1918TEST(Matcher, VisitsTemplateInstantiations) {
1919 EXPECT_TRUE(matches(
1920 "class A { public: void x(); };"
1921 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001922 "void f() { B<A> b; b.y(); }",
1923 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001924
1925 EXPECT_TRUE(matches(
1926 "class A { public: void x(); };"
1927 "class C {"
1928 " public:"
1929 " template <typename T> class B { public: void y() { T t; t.x(); } };"
1930 "};"
1931 "void f() {"
1932 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001933 "}",
1934 recordDecl(hasName("C"),
1935 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001936}
1937
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001938TEST(Matcher, HandlesNullQualTypes) {
1939 // FIXME: Add a Type matcher so we can replace uses of this
1940 // variable with Type(True())
1941 const TypeMatcher AnyType = anything();
1942
1943 // We don't really care whether this matcher succeeds; we're testing that
1944 // it completes without crashing.
1945 EXPECT_TRUE(matches(
1946 "struct A { };"
1947 "template <typename T>"
1948 "void f(T t) {"
1949 " T local_t(t /* this becomes a null QualType in the AST */);"
1950 "}"
1951 "void g() {"
1952 " f(0);"
1953 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001954 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001955 anyOf(
1956 TypeMatcher(hasDeclaration(anything())),
1957 pointsTo(AnyType),
1958 references(AnyType)
1959 // Other QualType matchers should go here.
1960 ))))));
1961}
1962
Manuel Klimek4da21662012-07-06 05:48:52 +00001963// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001964AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00001965 // Make sure all special variables are used: node, match_finder,
1966 // bound_nodes_builder, and the parameter named 'AMatcher'.
1967 return AMatcher.matches(Node, Finder, Builder);
1968}
1969
1970TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001971 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001972
1973 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001974 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001975
1976 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001977 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001978
1979 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001980 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001981}
1982
1983AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001984 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
1985 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
1986 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00001987 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00001988 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00001989 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00001990 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
1991 ASTMatchFinder::BK_First);
1992}
1993
1994TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001995 DeclarationMatcher HasClassB =
1996 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001997
1998 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001999 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002000
2001 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002002 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002003
2004 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002005 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002006
2007 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002008 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002009
2010 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2011}
2012
2013TEST(For, FindsForLoops) {
2014 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2015 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002016 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2017 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002018 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002019}
2020
Daniel Jasper6a124492012-07-12 08:50:38 +00002021TEST(For, ForLoopInternals) {
2022 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2023 forStmt(hasCondition(anything()))));
2024 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2025 forStmt(hasLoopInit(anything()))));
2026}
2027
2028TEST(For, NegativeForLoopInternals) {
2029 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002030 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002031 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2032 forStmt(hasLoopInit(anything()))));
2033}
2034
Manuel Klimek4da21662012-07-06 05:48:52 +00002035TEST(For, ReportsNoFalsePositives) {
2036 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2037 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2038}
2039
2040TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002041 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2042 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2043 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002044}
2045
2046TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2047 // It's not a compound statement just because there's "{}" in the source
2048 // text. This is an AST search, not grep.
2049 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002050 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002051 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002052 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002053}
2054
Daniel Jasper6a124492012-07-12 08:50:38 +00002055TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002056 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002057 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002058 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002059 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002060 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002061 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002062 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002063 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002064}
2065
2066TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2067 // The simplest case: every compound statement is in a function
2068 // definition, and the function body itself must be a compound
2069 // statement.
2070 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002071 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002072}
2073
2074TEST(HasAnySubstatement, IsNotRecursive) {
2075 // It's really "has any immediate substatement".
2076 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002077 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002078}
2079
2080TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2081 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002082 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002083}
2084
2085TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2086 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002087 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002088}
2089
2090TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2091 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002092 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002093 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002094 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002095}
2096
2097TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2098 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002099 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002100 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002101 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002102 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002103 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002104}
2105
2106TEST(StatementCountIs, WorksWithMultipleStatements) {
2107 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002108 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002109}
2110
2111TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2112 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002113 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002114 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002115 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002116 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002117 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002118 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002119 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002120}
2121
2122TEST(Member, WorksInSimplestCase) {
2123 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002124 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002125}
2126
2127TEST(Member, DoesNotMatchTheBaseExpression) {
2128 // Don't pick out the wrong part of the member expression, this should
2129 // be checking the member (name) only.
2130 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002131 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002132}
2133
2134TEST(Member, MatchesInMemberFunctionCall) {
2135 EXPECT_TRUE(matches("void f() {"
2136 " struct { void first() {}; } s;"
2137 " s.first();"
2138 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002139 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002140}
2141
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002142TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002143 // Fails in C++11 mode
2144 EXPECT_TRUE(matchesConditionally(
2145 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2146 "class X { void *operator new(std::size_t); };",
2147 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002148
2149 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002150 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002151
Daniel Jasper31f7c082012-10-01 13:40:41 +00002152 // Fails in C++11 mode
2153 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002154 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2155 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002156 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002157}
2158
Manuel Klimek4da21662012-07-06 05:48:52 +00002159TEST(HasObjectExpression, DoesNotMatchMember) {
2160 EXPECT_TRUE(notMatches(
2161 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002162 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002163}
2164
2165TEST(HasObjectExpression, MatchesBaseOfVariable) {
2166 EXPECT_TRUE(matches(
2167 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002168 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002169 EXPECT_TRUE(matches(
2170 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002171 memberExpr(hasObjectExpression(
2172 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002173}
2174
2175TEST(HasObjectExpression,
2176 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2177 EXPECT_TRUE(matches(
2178 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002179 memberExpr(hasObjectExpression(
2180 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002181 EXPECT_TRUE(matches(
2182 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002183 memberExpr(hasObjectExpression(
2184 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002185}
2186
2187TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002188 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2189 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2190 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2191 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002192}
2193
2194TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002195 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002196}
2197
2198TEST(IsConstQualified, MatchesConstInt) {
2199 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002200 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002201}
2202
2203TEST(IsConstQualified, MatchesConstPointer) {
2204 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002205 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002206}
2207
2208TEST(IsConstQualified, MatchesThroughTypedef) {
2209 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002210 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002211 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002212 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002213}
2214
2215TEST(IsConstQualified, DoesNotMatchInappropriately) {
2216 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002217 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002218 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002219 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002220}
2221
Sam Panzer089e5b32012-08-16 16:58:10 +00002222TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002223 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2224 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2225 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2226 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002227}
2228TEST(CastExpression, MatchesImplicitCasts) {
2229 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002230 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002231 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002232 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002233}
2234
2235TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002236 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2237 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2238 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2239 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002240}
2241
Manuel Klimek4da21662012-07-06 05:48:52 +00002242TEST(ReinterpretCast, MatchesSimpleCase) {
2243 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002244 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002245}
2246
2247TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002248 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002249 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002250 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002251 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002252 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002253 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2254 "B b;"
2255 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002256 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002257}
2258
2259TEST(FunctionalCast, MatchesSimpleCase) {
2260 std::string foo_class = "class Foo { public: Foo(char*); };";
2261 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002262 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002263}
2264
2265TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2266 std::string FooClass = "class Foo { public: Foo(char*); };";
2267 EXPECT_TRUE(
2268 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002269 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002270 EXPECT_TRUE(
2271 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002272 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002273}
2274
2275TEST(DynamicCast, MatchesSimpleCase) {
2276 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2277 "B b;"
2278 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002279 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002280}
2281
2282TEST(StaticCast, MatchesSimpleCase) {
2283 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002284 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002285}
2286
2287TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002288 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002289 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002290 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002291 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002292 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002293 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2294 "B b;"
2295 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002296 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002297}
2298
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002299TEST(CStyleCast, MatchesSimpleCase) {
2300 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2301}
2302
2303TEST(CStyleCast, DoesNotMatchOtherCasts) {
2304 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2305 "char q, *r = const_cast<char*>(&q);"
2306 "void* s = reinterpret_cast<char*>(&s);"
2307 "struct B { virtual ~B() {} }; struct D : B {};"
2308 "B b;"
2309 "D* t = dynamic_cast<D*>(&b);",
2310 cStyleCastExpr()));
2311}
2312
Manuel Klimek4da21662012-07-06 05:48:52 +00002313TEST(HasDestinationType, MatchesSimpleCase) {
2314 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002315 staticCastExpr(hasDestinationType(
2316 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002317}
2318
Sam Panzer089e5b32012-08-16 16:58:10 +00002319TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2320 // This test creates an implicit const cast.
2321 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002322 implicitCastExpr(
2323 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002324 // This test creates an implicit array-to-pointer cast.
2325 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002326 implicitCastExpr(hasImplicitDestinationType(
2327 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002328}
2329
2330TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2331 // This test creates an implicit cast from int to char.
2332 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002333 implicitCastExpr(hasImplicitDestinationType(
2334 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002335 // This test creates an implicit array-to-pointer cast.
2336 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002337 implicitCastExpr(hasImplicitDestinationType(
2338 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002339}
2340
2341TEST(ImplicitCast, MatchesSimpleCase) {
2342 // This test creates an implicit const cast.
2343 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002344 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002345 // This test creates an implicit cast from int to char.
2346 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002347 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002348 // This test creates an implicit array-to-pointer cast.
2349 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002350 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002351}
2352
2353TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002354 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002355 // are present, and that it ignores explicit and paren casts.
2356
2357 // These two test cases have no casts.
2358 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002359 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002360 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002361 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002362
2363 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002364 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002365 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002366 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002367
2368 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002369 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002370}
2371
2372TEST(IgnoringImpCasts, MatchesImpCasts) {
2373 // This test checks that ignoringImpCasts matches when implicit casts are
2374 // present and its inner matcher alone does not match.
2375 // Note that this test creates an implicit const cast.
2376 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002377 varDecl(hasInitializer(ignoringImpCasts(
2378 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002379 // This test creates an implict cast from int to char.
2380 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002381 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002382 integerLiteral(equals(0)))))));
2383}
2384
2385TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2386 // These tests verify that ignoringImpCasts does not match if the inner
2387 // matcher does not match.
2388 // Note that the first test creates an implicit const cast.
2389 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002390 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002391 unless(anything()))))));
2392 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002393 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002394 unless(anything()))))));
2395
2396 // These tests verify that ignoringImplictCasts does not look through explicit
2397 // casts or parentheses.
2398 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002399 varDecl(hasInitializer(ignoringImpCasts(
2400 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002401 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002402 varDecl(hasInitializer(ignoringImpCasts(
2403 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002404 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002405 varDecl(hasInitializer(ignoringImpCasts(
2406 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002407 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002408 varDecl(hasInitializer(ignoringImpCasts(
2409 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002410}
2411
2412TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2413 // This test verifies that expressions that do not have implicit casts
2414 // still match the inner matcher.
2415 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002416 varDecl(hasInitializer(ignoringImpCasts(
2417 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002418}
2419
2420TEST(IgnoringParenCasts, MatchesParenCasts) {
2421 // This test checks that ignoringParenCasts matches when parentheses and/or
2422 // casts are present and its inner matcher alone does not match.
2423 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002424 varDecl(hasInitializer(ignoringParenCasts(
2425 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002426 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002427 varDecl(hasInitializer(ignoringParenCasts(
2428 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002429
2430 // This test creates an implict cast from int to char in addition to the
2431 // parentheses.
2432 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002433 varDecl(hasInitializer(ignoringParenCasts(
2434 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002435
2436 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002437 varDecl(hasInitializer(ignoringParenCasts(
2438 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002439 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002440 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002441 integerLiteral(equals(0)))))));
2442}
2443
2444TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2445 // This test verifies that expressions that do not have any casts still match.
2446 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002447 varDecl(hasInitializer(ignoringParenCasts(
2448 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002449}
2450
2451TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2452 // These tests verify that ignoringImpCasts does not match if the inner
2453 // matcher does not match.
2454 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002455 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002456 unless(anything()))))));
2457
2458 // This test creates an implicit cast from int to char in addition to the
2459 // parentheses.
2460 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002461 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002462 unless(anything()))))));
2463
2464 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002465 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002466 unless(anything()))))));
2467}
2468
2469TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2470 // This test checks that ignoringParenAndImpCasts matches when
2471 // parentheses and/or implicit casts are present and its inner matcher alone
2472 // does not match.
2473 // Note that this test creates an implicit const cast.
2474 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002475 varDecl(hasInitializer(ignoringParenImpCasts(
2476 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002477 // This test creates an implicit cast from int to char.
2478 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002479 varDecl(hasInitializer(ignoringParenImpCasts(
2480 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002481}
2482
2483TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2484 // This test verifies that expressions that do not have parentheses or
2485 // implicit casts still match.
2486 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002487 varDecl(hasInitializer(ignoringParenImpCasts(
2488 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002489 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002490 varDecl(hasInitializer(ignoringParenImpCasts(
2491 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002492}
2493
2494TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2495 // These tests verify that ignoringParenImpCasts does not match if
2496 // the inner matcher does not match.
2497 // This test creates an implicit cast.
2498 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002499 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002500 unless(anything()))))));
2501 // These tests verify that ignoringParenAndImplictCasts does not look
2502 // through explicit casts.
2503 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002504 varDecl(hasInitializer(ignoringParenImpCasts(
2505 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002506 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002507 varDecl(hasInitializer(ignoringParenImpCasts(
2508 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002509 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002510 varDecl(hasInitializer(ignoringParenImpCasts(
2511 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002512}
2513
Manuel Klimek715c9562012-07-25 10:02:02 +00002514TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002515 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2516 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002517 implicitCastExpr(
2518 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002519}
2520
Manuel Klimek715c9562012-07-25 10:02:02 +00002521TEST(HasSourceExpression, MatchesExplicitCasts) {
2522 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002523 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002524 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002525 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002526}
2527
Manuel Klimek4da21662012-07-06 05:48:52 +00002528TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002529 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002530}
2531
2532TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002533 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002534}
2535
2536TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002537 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002538}
2539
2540TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002541 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002542}
2543
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002544TEST(InitListExpression, MatchesInitListExpression) {
2545 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2546 initListExpr(hasType(asString("int [2]")))));
2547 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002548 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002549}
2550
2551TEST(UsingDeclaration, MatchesUsingDeclarations) {
2552 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2553 usingDecl()));
2554}
2555
2556TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2557 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2558 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2559}
2560
2561TEST(UsingDeclaration, MatchesSpecificTarget) {
2562 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2563 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002564 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002565 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2566 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002567 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002568}
2569
2570TEST(UsingDeclaration, ThroughUsingDeclaration) {
2571 EXPECT_TRUE(matches(
2572 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002573 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002574 EXPECT_TRUE(notMatches(
2575 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002576 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002577}
2578
Sam Panzer425f41b2012-08-16 17:20:59 +00002579TEST(SingleDecl, IsSingleDecl) {
2580 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002581 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002582 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2583 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2584 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2585 SingleDeclStmt));
2586}
2587
2588TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002589 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002590
2591 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002592 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002593 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002594 declStmt(containsDeclaration(0, MatchesInit),
2595 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002596 unsigned WrongIndex = 42;
2597 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002598 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002599 MatchesInit))));
2600}
2601
2602TEST(DeclCount, DeclCountIsCorrect) {
2603 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002604 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002605 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002606 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002607 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002608 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002609}
2610
Manuel Klimek4da21662012-07-06 05:48:52 +00002611TEST(While, MatchesWhileLoops) {
2612 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2613 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2614 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2615}
2616
2617TEST(Do, MatchesDoLoops) {
2618 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2619 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2620}
2621
2622TEST(Do, DoesNotMatchWhileLoops) {
2623 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2624}
2625
2626TEST(SwitchCase, MatchesCase) {
2627 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2628 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2629 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2630 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2631}
2632
Daniel Jasperb54b7642012-09-20 14:12:57 +00002633TEST(SwitchCase, MatchesSwitch) {
2634 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2635 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2636 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2637 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2638}
2639
2640TEST(ExceptionHandling, SimpleCases) {
2641 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2642 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2643 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2644 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2645 throwExpr()));
2646 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2647 throwExpr()));
2648}
2649
Manuel Klimek4da21662012-07-06 05:48:52 +00002650TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2651 EXPECT_TRUE(notMatches(
2652 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002653 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002654 EXPECT_TRUE(notMatches(
2655 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002656 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002657}
2658
2659TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2660 EXPECT_TRUE(matches(
2661 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002662 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002663}
2664
2665TEST(ForEach, BindsOneNode) {
2666 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002667 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002668 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002669}
2670
2671TEST(ForEach, BindsMultipleNodes) {
2672 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002673 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002674 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002675}
2676
2677TEST(ForEach, BindsRecursiveCombinations) {
2678 EXPECT_TRUE(matchAndVerifyResultTrue(
2679 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002680 recordDecl(hasName("C"),
2681 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002682 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002683}
2684
2685TEST(ForEachDescendant, BindsOneNode) {
2686 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002687 recordDecl(hasName("C"),
2688 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002689 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002690}
2691
2692TEST(ForEachDescendant, BindsMultipleNodes) {
2693 EXPECT_TRUE(matchAndVerifyResultTrue(
2694 "class C { class D { int x; int y; }; "
2695 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002696 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002697 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002698}
2699
2700TEST(ForEachDescendant, BindsRecursiveCombinations) {
2701 EXPECT_TRUE(matchAndVerifyResultTrue(
2702 "class C { class D { "
2703 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002704 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2705 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002706 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002707}
2708
2709
2710TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2711 // Make sure that we can both match the class by name (::X) and by the type
2712 // the template was instantiated with (via a field).
2713
2714 EXPECT_TRUE(matches(
2715 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002716 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002717
2718 EXPECT_TRUE(matches(
2719 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002720 recordDecl(isTemplateInstantiation(), hasDescendant(
2721 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002722}
2723
2724TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2725 EXPECT_TRUE(matches(
2726 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002727 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002728 isTemplateInstantiation())));
2729}
2730
2731TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2732 EXPECT_TRUE(matches(
2733 "template <typename T> class X { T t; }; class A {};"
2734 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002735 recordDecl(isTemplateInstantiation(), hasDescendant(
2736 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002737}
2738
2739TEST(IsTemplateInstantiation,
2740 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2741 EXPECT_TRUE(matches(
2742 "template <typename T> class X {};"
2743 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002744 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002745}
2746
2747TEST(IsTemplateInstantiation,
2748 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2749 EXPECT_TRUE(matches(
2750 "class A {};"
2751 "class X {"
2752 " template <typename U> class Y { U u; };"
2753 " Y<A> y;"
2754 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002755 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002756}
2757
2758TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2759 // FIXME: Figure out whether this makes sense. It doesn't affect the
2760 // normal use case as long as the uppermost instantiation always is marked
2761 // as template instantiation, but it might be confusing as a predicate.
2762 EXPECT_TRUE(matches(
2763 "class A {};"
2764 "template <typename T> class X {"
2765 " template <typename U> class Y { U u; };"
2766 " Y<T> y;"
2767 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002768 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002769}
2770
2771TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2772 EXPECT_TRUE(notMatches(
2773 "template <typename T> class X {}; class A {};"
2774 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002775 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002776}
2777
2778TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2779 EXPECT_TRUE(notMatches(
2780 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002781 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002782}
2783
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002784TEST(IsExplicitTemplateSpecialization,
2785 DoesNotMatchPrimaryTemplate) {
2786 EXPECT_TRUE(notMatches(
2787 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002788 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002789 EXPECT_TRUE(notMatches(
2790 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002791 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002792}
2793
2794TEST(IsExplicitTemplateSpecialization,
2795 DoesNotMatchExplicitTemplateInstantiations) {
2796 EXPECT_TRUE(notMatches(
2797 "template <typename T> class X {};"
2798 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002799 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002800 EXPECT_TRUE(notMatches(
2801 "template <typename T> void f(T t) {}"
2802 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002803 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002804}
2805
2806TEST(IsExplicitTemplateSpecialization,
2807 DoesNotMatchImplicitTemplateInstantiations) {
2808 EXPECT_TRUE(notMatches(
2809 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002810 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002811 EXPECT_TRUE(notMatches(
2812 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002813 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002814}
2815
2816TEST(IsExplicitTemplateSpecialization,
2817 MatchesExplicitTemplateSpecializations) {
2818 EXPECT_TRUE(matches(
2819 "template <typename T> class X {};"
2820 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002821 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002822 EXPECT_TRUE(matches(
2823 "template <typename T> void f(T t) {}"
2824 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002825 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002826}
2827
Manuel Klimek579b1202012-09-07 09:26:10 +00002828TEST(HasAncenstor, MatchesDeclarationAncestors) {
2829 EXPECT_TRUE(matches(
2830 "class A { class B { class C {}; }; };",
2831 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2832}
2833
2834TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2835 EXPECT_TRUE(notMatches(
2836 "class A { class B { class C {}; }; };",
2837 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2838}
2839
2840TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2841 EXPECT_TRUE(matches(
2842 "class A { class B { void f() { C c; } class C {}; }; };",
2843 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2844 hasAncestor(recordDecl(hasName("A"))))))));
2845}
2846
2847TEST(HasAncenstor, MatchesStatementAncestors) {
2848 EXPECT_TRUE(matches(
2849 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002850 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002851}
2852
2853TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2854 EXPECT_TRUE(matches(
2855 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002856 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002857}
2858
2859TEST(HasAncestor, BindsRecursiveCombinations) {
2860 EXPECT_TRUE(matchAndVerifyResultTrue(
2861 "class C { class D { class E { class F { int y; }; }; }; };",
2862 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002863 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00002864}
2865
2866TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
2867 EXPECT_TRUE(matchAndVerifyResultTrue(
2868 "class C { class D { class E { class F { int y; }; }; }; };",
2869 fieldDecl(hasAncestor(
2870 decl(
2871 hasDescendant(recordDecl(isDefinition(),
2872 hasAncestor(recordDecl())))
2873 ).bind("d")
2874 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00002875 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00002876}
2877
2878TEST(HasAncestor, MatchesInTemplateInstantiations) {
2879 EXPECT_TRUE(matches(
2880 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
2881 "A<int>::B::C a;",
2882 fieldDecl(hasType(asString("int")),
2883 hasAncestor(recordDecl(hasName("A"))))));
2884}
2885
2886TEST(HasAncestor, MatchesInImplicitCode) {
2887 EXPECT_TRUE(matches(
2888 "struct X {}; struct A { A() {} X x; };",
2889 constructorDecl(
2890 hasAnyConstructorInitializer(withInitializer(expr(
2891 hasAncestor(recordDecl(hasName("A")))))))));
2892}
2893
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00002894TEST(HasParent, MatchesOnlyParent) {
2895 EXPECT_TRUE(matches(
2896 "void f() { if (true) { int x = 42; } }",
2897 compoundStmt(hasParent(ifStmt()))));
2898 EXPECT_TRUE(notMatches(
2899 "void f() { for (;;) { int x = 42; } }",
2900 compoundStmt(hasParent(ifStmt()))));
2901 EXPECT_TRUE(notMatches(
2902 "void f() { if (true) for (;;) { int x = 42; } }",
2903 compoundStmt(hasParent(ifStmt()))));
2904}
2905
Daniel Jasperce620072012-10-17 08:52:59 +00002906TEST(TypeMatching, MatchesTypes) {
2907 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
2908}
2909
2910TEST(TypeMatching, MatchesArrayTypes) {
2911 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
2912 EXPECT_TRUE(matches("int a[42];", arrayType()));
2913 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
2914
2915 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
2916 arrayType(hasElementType(builtinType()))));
2917
2918 EXPECT_TRUE(matches(
2919 "int const a[] = { 2, 3 };",
2920 qualType(arrayType(hasElementType(builtinType())))));
2921 EXPECT_TRUE(matches(
2922 "int const a[] = { 2, 3 };",
2923 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
2924 EXPECT_TRUE(matches(
2925 "typedef const int T; T x[] = { 1, 2 };",
2926 qualType(isConstQualified(), arrayType())));
2927
2928 EXPECT_TRUE(notMatches(
2929 "int a[] = { 2, 3 };",
2930 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
2931 EXPECT_TRUE(notMatches(
2932 "int a[] = { 2, 3 };",
2933 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
2934 EXPECT_TRUE(notMatches(
2935 "int const a[] = { 2, 3 };",
2936 qualType(arrayType(hasElementType(builtinType())),
2937 unless(isConstQualified()))));
2938
2939 EXPECT_TRUE(matches("int a[2];",
2940 constantArrayType(hasElementType(builtinType()))));
2941 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
2942}
2943
2944TEST(TypeMatching, MatchesComplexTypes) {
2945 EXPECT_TRUE(matches("_Complex float f;", complexType()));
2946 EXPECT_TRUE(matches(
2947 "_Complex float f;",
2948 complexType(hasElementType(builtinType()))));
2949 EXPECT_TRUE(notMatches(
2950 "_Complex float f;",
2951 complexType(hasElementType(isInteger()))));
2952}
2953
2954TEST(TypeMatching, MatchesConstantArrayTypes) {
2955 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
2956 EXPECT_TRUE(notMatches(
2957 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
2958 constantArrayType(hasElementType(builtinType()))));
2959
2960 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
2961 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
2962 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
2963}
2964
2965TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
2966 EXPECT_TRUE(matches(
2967 "template <typename T, int Size> class array { T data[Size]; };",
2968 dependentSizedArrayType()));
2969 EXPECT_TRUE(notMatches(
2970 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
2971 dependentSizedArrayType()));
2972}
2973
2974TEST(TypeMatching, MatchesIncompleteArrayType) {
2975 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
2976 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
2977
2978 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
2979 incompleteArrayType()));
2980}
2981
2982TEST(TypeMatching, MatchesVariableArrayType) {
2983 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
2984 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
2985
2986 EXPECT_TRUE(matches(
2987 "void f(int b) { int a[b]; }",
2988 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
2989 varDecl(hasName("b")))))))));
2990}
2991
2992TEST(TypeMatching, MatchesAtomicTypes) {
2993 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
2994
2995 EXPECT_TRUE(matches("_Atomic(int) i;",
2996 atomicType(hasValueType(isInteger()))));
2997 EXPECT_TRUE(notMatches("_Atomic(float) f;",
2998 atomicType(hasValueType(isInteger()))));
2999}
3000
3001TEST(TypeMatching, MatchesAutoTypes) {
3002 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3003 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3004 autoType()));
3005
3006 EXPECT_TRUE(matches("auto a = 1;",
3007 autoType(hasDeducedType(isInteger()))));
3008 EXPECT_TRUE(notMatches("auto b = 2.0;",
3009 autoType(hasDeducedType(isInteger()))));
3010}
3011
3012TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003013 // FIXME: Reactive when these tests can be more specific (not matching
3014 // implicit code on certain platforms), likely when we have hasDescendant for
3015 // Types/TypeLocs.
3016 //EXPECT_TRUE(matchAndVerifyResultTrue(
3017 // "int* a;",
3018 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3019 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3020 //EXPECT_TRUE(matchAndVerifyResultTrue(
3021 // "int* a;",
3022 // pointerTypeLoc().bind("loc"),
3023 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003024 EXPECT_TRUE(matches(
3025 "int** a;",
3026 pointerTypeLoc(pointeeLoc(loc(qualType())))));
3027 EXPECT_TRUE(matches(
3028 "int** a;",
3029 loc(pointerType(pointee(pointerType())))));
3030 EXPECT_TRUE(matches(
3031 "int* b; int* * const a = &b;",
3032 loc(qualType(isConstQualified(), pointerType()))));
3033
3034 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003035 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3036 hasType(blockPointerType()))));
3037 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3038 hasType(memberPointerType()))));
3039 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3040 hasType(pointerType()))));
3041 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3042 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003043
Daniel Jasper1802daf2012-10-17 13:35:36 +00003044 Fragment = "int *ptr;";
3045 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3046 hasType(blockPointerType()))));
3047 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3048 hasType(memberPointerType()))));
3049 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3050 hasType(pointerType()))));
3051 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3052 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003053
Daniel Jasper1802daf2012-10-17 13:35:36 +00003054 Fragment = "int a; int &ref = a;";
3055 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3056 hasType(blockPointerType()))));
3057 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3058 hasType(memberPointerType()))));
3059 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3060 hasType(pointerType()))));
3061 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3062 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003063}
3064
3065TEST(TypeMatching, PointeeTypes) {
3066 EXPECT_TRUE(matches("int b; int &a = b;",
3067 referenceType(pointee(builtinType()))));
3068 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3069
3070 EXPECT_TRUE(matches("int *a;",
3071 pointerTypeLoc(pointeeLoc(loc(builtinType())))));
3072
3073 EXPECT_TRUE(matches(
3074 "int const *A;",
3075 pointerType(pointee(isConstQualified(), builtinType()))));
3076 EXPECT_TRUE(notMatches(
3077 "int *A;",
3078 pointerType(pointee(isConstQualified(), builtinType()))));
3079}
3080
3081TEST(TypeMatching, MatchesPointersToConstTypes) {
3082 EXPECT_TRUE(matches("int b; int * const a = &b;",
3083 loc(pointerType())));
3084 EXPECT_TRUE(matches("int b; int * const a = &b;",
3085 pointerTypeLoc()));
3086 EXPECT_TRUE(matches(
3087 "int b; const int * a = &b;",
3088 pointerTypeLoc(pointeeLoc(builtinTypeLoc()))));
3089 EXPECT_TRUE(matches(
3090 "int b; const int * a = &b;",
3091 pointerType(pointee(builtinType()))));
3092}
3093
3094TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003095 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3096 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003097
Daniel Jasper1802daf2012-10-17 13:35:36 +00003098 EXPECT_TRUE(matches("typedef int X; X a;",
3099 varDecl(hasName("a"),
3100 hasType(typedefType(hasDecl(decl()))))));
Daniel Jasperce620072012-10-17 08:52:59 +00003101}
3102
Daniel Jaspera7564432012-09-13 13:11:25 +00003103TEST(NNS, MatchesNestedNameSpecifiers) {
3104 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3105 nestedNameSpecifier()));
3106 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3107 nestedNameSpecifier()));
3108 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3109 nestedNameSpecifier()));
3110
3111 EXPECT_TRUE(matches(
3112 "struct A { static void f() {} }; void g() { A::f(); }",
3113 nestedNameSpecifier()));
3114 EXPECT_TRUE(notMatches(
3115 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3116 nestedNameSpecifier()));
3117}
3118
Daniel Jasperb54b7642012-09-20 14:12:57 +00003119TEST(NullStatement, SimpleCases) {
3120 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3121 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3122}
3123
Daniel Jaspera7564432012-09-13 13:11:25 +00003124TEST(NNS, MatchesTypes) {
3125 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3126 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3127 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3128 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3129 Matcher));
3130 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3131}
3132
3133TEST(NNS, MatchesNamespaceDecls) {
3134 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3135 specifiesNamespace(hasName("ns")));
3136 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3137 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3138 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3139}
3140
3141TEST(NNS, BindsNestedNameSpecifiers) {
3142 EXPECT_TRUE(matchAndVerifyResultTrue(
3143 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3144 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3145 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3146}
3147
3148TEST(NNS, BindsNestedNameSpecifierLocs) {
3149 EXPECT_TRUE(matchAndVerifyResultTrue(
3150 "namespace ns { struct B {}; } ns::B b;",
3151 loc(nestedNameSpecifier()).bind("loc"),
3152 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3153}
3154
3155TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3156 EXPECT_TRUE(matches(
3157 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3158 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3159 EXPECT_TRUE(matches(
3160 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003161 nestedNameSpecifierLoc(hasPrefix(
3162 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003163}
3164
Manuel Klimek4da21662012-07-06 05:48:52 +00003165} // end namespace ast_matchers
3166} // end namespace clang