blob: 5060e807c6721461ffaeaa31c7f7a4c665cfbd09 [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
Daniel Jasperc711af22012-10-23 15:46:39 +00002142TEST(Member, MatchesMember) {
2143 EXPECT_TRUE(matches(
2144 "struct A { int i; }; void f() { A a; a.i = 2; }",
2145 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2146 EXPECT_TRUE(notMatches(
2147 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2148 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2149}
2150
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002151TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002152 // Fails in C++11 mode
2153 EXPECT_TRUE(matchesConditionally(
2154 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2155 "class X { void *operator new(std::size_t); };",
2156 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002157
2158 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002159 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002160
Daniel Jasper31f7c082012-10-01 13:40:41 +00002161 // Fails in C++11 mode
2162 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002163 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2164 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002165 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002166}
2167
Manuel Klimek4da21662012-07-06 05:48:52 +00002168TEST(HasObjectExpression, DoesNotMatchMember) {
2169 EXPECT_TRUE(notMatches(
2170 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002171 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002172}
2173
2174TEST(HasObjectExpression, MatchesBaseOfVariable) {
2175 EXPECT_TRUE(matches(
2176 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002177 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002178 EXPECT_TRUE(matches(
2179 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002180 memberExpr(hasObjectExpression(
2181 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002182}
2183
2184TEST(HasObjectExpression,
2185 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2186 EXPECT_TRUE(matches(
2187 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002188 memberExpr(hasObjectExpression(
2189 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002190 EXPECT_TRUE(matches(
2191 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002192 memberExpr(hasObjectExpression(
2193 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002194}
2195
2196TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002197 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2198 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2199 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2200 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002201}
2202
2203TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002204 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002205}
2206
2207TEST(IsConstQualified, MatchesConstInt) {
2208 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002209 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002210}
2211
2212TEST(IsConstQualified, MatchesConstPointer) {
2213 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002214 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002215}
2216
2217TEST(IsConstQualified, MatchesThroughTypedef) {
2218 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002219 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002220 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002221 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002222}
2223
2224TEST(IsConstQualified, DoesNotMatchInappropriately) {
2225 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002226 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002227 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002228 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002229}
2230
Sam Panzer089e5b32012-08-16 16:58:10 +00002231TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002232 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2233 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2234 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2235 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002236}
2237TEST(CastExpression, MatchesImplicitCasts) {
2238 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002239 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002240 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002241 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002242}
2243
2244TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002245 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2246 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2247 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2248 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002249}
2250
Manuel Klimek4da21662012-07-06 05:48:52 +00002251TEST(ReinterpretCast, MatchesSimpleCase) {
2252 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002253 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002254}
2255
2256TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002257 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002258 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002259 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002260 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002261 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002262 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2263 "B b;"
2264 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002265 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002266}
2267
2268TEST(FunctionalCast, MatchesSimpleCase) {
2269 std::string foo_class = "class Foo { public: Foo(char*); };";
2270 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002271 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002272}
2273
2274TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2275 std::string FooClass = "class Foo { public: Foo(char*); };";
2276 EXPECT_TRUE(
2277 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002278 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002279 EXPECT_TRUE(
2280 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002281 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002282}
2283
2284TEST(DynamicCast, MatchesSimpleCase) {
2285 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2286 "B b;"
2287 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002288 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002289}
2290
2291TEST(StaticCast, MatchesSimpleCase) {
2292 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002293 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002294}
2295
2296TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002297 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002298 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002299 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002300 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002301 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002302 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2303 "B b;"
2304 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002305 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002306}
2307
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002308TEST(CStyleCast, MatchesSimpleCase) {
2309 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2310}
2311
2312TEST(CStyleCast, DoesNotMatchOtherCasts) {
2313 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2314 "char q, *r = const_cast<char*>(&q);"
2315 "void* s = reinterpret_cast<char*>(&s);"
2316 "struct B { virtual ~B() {} }; struct D : B {};"
2317 "B b;"
2318 "D* t = dynamic_cast<D*>(&b);",
2319 cStyleCastExpr()));
2320}
2321
Manuel Klimek4da21662012-07-06 05:48:52 +00002322TEST(HasDestinationType, MatchesSimpleCase) {
2323 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002324 staticCastExpr(hasDestinationType(
2325 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002326}
2327
Sam Panzer089e5b32012-08-16 16:58:10 +00002328TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2329 // This test creates an implicit const cast.
2330 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002331 implicitCastExpr(
2332 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002333 // This test creates an implicit array-to-pointer cast.
2334 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002335 implicitCastExpr(hasImplicitDestinationType(
2336 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002337}
2338
2339TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2340 // This test creates an implicit cast from int to char.
2341 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002342 implicitCastExpr(hasImplicitDestinationType(
2343 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002344 // This test creates an implicit array-to-pointer cast.
2345 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002346 implicitCastExpr(hasImplicitDestinationType(
2347 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002348}
2349
2350TEST(ImplicitCast, MatchesSimpleCase) {
2351 // This test creates an implicit const cast.
2352 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002353 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002354 // This test creates an implicit cast from int to char.
2355 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002356 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002357 // This test creates an implicit array-to-pointer cast.
2358 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002359 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002360}
2361
2362TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002363 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002364 // are present, and that it ignores explicit and paren casts.
2365
2366 // These two test cases have no casts.
2367 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002368 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002369 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002370 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002371
2372 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002373 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002374 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002375 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002376
2377 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002378 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002379}
2380
2381TEST(IgnoringImpCasts, MatchesImpCasts) {
2382 // This test checks that ignoringImpCasts matches when implicit casts are
2383 // present and its inner matcher alone does not match.
2384 // Note that this test creates an implicit const cast.
2385 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002386 varDecl(hasInitializer(ignoringImpCasts(
2387 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002388 // This test creates an implict cast from int to char.
2389 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002390 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002391 integerLiteral(equals(0)))))));
2392}
2393
2394TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2395 // These tests verify that ignoringImpCasts does not match if the inner
2396 // matcher does not match.
2397 // Note that the first test creates an implicit const cast.
2398 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002399 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002400 unless(anything()))))));
2401 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002402 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002403 unless(anything()))))));
2404
2405 // These tests verify that ignoringImplictCasts does not look through explicit
2406 // casts or parentheses.
2407 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002408 varDecl(hasInitializer(ignoringImpCasts(
2409 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002410 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002411 varDecl(hasInitializer(ignoringImpCasts(
2412 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002413 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002414 varDecl(hasInitializer(ignoringImpCasts(
2415 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002416 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002417 varDecl(hasInitializer(ignoringImpCasts(
2418 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002419}
2420
2421TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2422 // This test verifies that expressions that do not have implicit casts
2423 // still match the inner matcher.
2424 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002425 varDecl(hasInitializer(ignoringImpCasts(
2426 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002427}
2428
2429TEST(IgnoringParenCasts, MatchesParenCasts) {
2430 // This test checks that ignoringParenCasts matches when parentheses and/or
2431 // casts are present and its inner matcher alone does not match.
2432 EXPECT_TRUE(matches("int 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 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002436 varDecl(hasInitializer(ignoringParenCasts(
2437 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002438
2439 // This test creates an implict cast from int to char in addition to the
2440 // parentheses.
2441 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002442 varDecl(hasInitializer(ignoringParenCasts(
2443 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002444
2445 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002446 varDecl(hasInitializer(ignoringParenCasts(
2447 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002448 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002449 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002450 integerLiteral(equals(0)))))));
2451}
2452
2453TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2454 // This test verifies that expressions that do not have any casts still match.
2455 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002456 varDecl(hasInitializer(ignoringParenCasts(
2457 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002458}
2459
2460TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2461 // These tests verify that ignoringImpCasts does not match if the inner
2462 // matcher does not match.
2463 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002464 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002465 unless(anything()))))));
2466
2467 // This test creates an implicit cast from int to char in addition to the
2468 // parentheses.
2469 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002470 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002471 unless(anything()))))));
2472
2473 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002474 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002475 unless(anything()))))));
2476}
2477
2478TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2479 // This test checks that ignoringParenAndImpCasts matches when
2480 // parentheses and/or implicit casts are present and its inner matcher alone
2481 // does not match.
2482 // Note that this test creates an implicit const cast.
2483 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002484 varDecl(hasInitializer(ignoringParenImpCasts(
2485 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002486 // This test creates an implicit cast from int to char.
2487 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002488 varDecl(hasInitializer(ignoringParenImpCasts(
2489 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002490}
2491
2492TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2493 // This test verifies that expressions that do not have parentheses or
2494 // implicit casts still match.
2495 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002496 varDecl(hasInitializer(ignoringParenImpCasts(
2497 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002498 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002499 varDecl(hasInitializer(ignoringParenImpCasts(
2500 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002501}
2502
2503TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2504 // These tests verify that ignoringParenImpCasts does not match if
2505 // the inner matcher does not match.
2506 // This test creates an implicit cast.
2507 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002508 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002509 unless(anything()))))));
2510 // These tests verify that ignoringParenAndImplictCasts does not look
2511 // through explicit casts.
2512 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002513 varDecl(hasInitializer(ignoringParenImpCasts(
2514 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002515 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002516 varDecl(hasInitializer(ignoringParenImpCasts(
2517 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002518 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002519 varDecl(hasInitializer(ignoringParenImpCasts(
2520 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002521}
2522
Manuel Klimek715c9562012-07-25 10:02:02 +00002523TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002524 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2525 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002526 implicitCastExpr(
2527 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002528}
2529
Manuel Klimek715c9562012-07-25 10:02:02 +00002530TEST(HasSourceExpression, MatchesExplicitCasts) {
2531 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002532 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002533 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002534 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002535}
2536
Manuel Klimek4da21662012-07-06 05:48:52 +00002537TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002538 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002539}
2540
2541TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002542 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002543}
2544
2545TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002546 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002547}
2548
2549TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002550 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002551}
2552
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002553TEST(InitListExpression, MatchesInitListExpression) {
2554 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2555 initListExpr(hasType(asString("int [2]")))));
2556 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002557 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002558}
2559
2560TEST(UsingDeclaration, MatchesUsingDeclarations) {
2561 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2562 usingDecl()));
2563}
2564
2565TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2566 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2567 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2568}
2569
2570TEST(UsingDeclaration, MatchesSpecificTarget) {
2571 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2572 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002573 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002574 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2575 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002576 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002577}
2578
2579TEST(UsingDeclaration, ThroughUsingDeclaration) {
2580 EXPECT_TRUE(matches(
2581 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002582 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002583 EXPECT_TRUE(notMatches(
2584 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002585 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002586}
2587
Sam Panzer425f41b2012-08-16 17:20:59 +00002588TEST(SingleDecl, IsSingleDecl) {
2589 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002590 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002591 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2592 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2593 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2594 SingleDeclStmt));
2595}
2596
2597TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002598 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002599
2600 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002601 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002602 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002603 declStmt(containsDeclaration(0, MatchesInit),
2604 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002605 unsigned WrongIndex = 42;
2606 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002607 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002608 MatchesInit))));
2609}
2610
2611TEST(DeclCount, DeclCountIsCorrect) {
2612 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002613 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002614 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002615 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002616 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002617 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002618}
2619
Manuel Klimek4da21662012-07-06 05:48:52 +00002620TEST(While, MatchesWhileLoops) {
2621 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2622 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2623 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2624}
2625
2626TEST(Do, MatchesDoLoops) {
2627 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2628 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2629}
2630
2631TEST(Do, DoesNotMatchWhileLoops) {
2632 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2633}
2634
2635TEST(SwitchCase, MatchesCase) {
2636 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2637 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2638 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2639 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2640}
2641
Daniel Jasperb54b7642012-09-20 14:12:57 +00002642TEST(SwitchCase, MatchesSwitch) {
2643 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2644 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2645 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2646 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2647}
2648
2649TEST(ExceptionHandling, SimpleCases) {
2650 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2651 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2652 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2653 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2654 throwExpr()));
2655 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2656 throwExpr()));
2657}
2658
Manuel Klimek4da21662012-07-06 05:48:52 +00002659TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2660 EXPECT_TRUE(notMatches(
2661 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002662 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002663 EXPECT_TRUE(notMatches(
2664 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002665 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002666}
2667
2668TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2669 EXPECT_TRUE(matches(
2670 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002671 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002672}
2673
2674TEST(ForEach, BindsOneNode) {
2675 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002676 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002677 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002678}
2679
2680TEST(ForEach, BindsMultipleNodes) {
2681 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002682 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002683 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002684}
2685
2686TEST(ForEach, BindsRecursiveCombinations) {
2687 EXPECT_TRUE(matchAndVerifyResultTrue(
2688 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002689 recordDecl(hasName("C"),
2690 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002691 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002692}
2693
2694TEST(ForEachDescendant, BindsOneNode) {
2695 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002696 recordDecl(hasName("C"),
2697 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002698 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002699}
2700
2701TEST(ForEachDescendant, BindsMultipleNodes) {
2702 EXPECT_TRUE(matchAndVerifyResultTrue(
2703 "class C { class D { int x; int y; }; "
2704 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002705 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002706 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002707}
2708
2709TEST(ForEachDescendant, BindsRecursiveCombinations) {
2710 EXPECT_TRUE(matchAndVerifyResultTrue(
2711 "class C { class D { "
2712 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002713 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2714 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002715 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002716}
2717
2718
2719TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2720 // Make sure that we can both match the class by name (::X) and by the type
2721 // the template was instantiated with (via a field).
2722
2723 EXPECT_TRUE(matches(
2724 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002725 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002726
2727 EXPECT_TRUE(matches(
2728 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002729 recordDecl(isTemplateInstantiation(), hasDescendant(
2730 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002731}
2732
2733TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2734 EXPECT_TRUE(matches(
2735 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002736 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002737 isTemplateInstantiation())));
2738}
2739
2740TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2741 EXPECT_TRUE(matches(
2742 "template <typename T> class X { T t; }; class A {};"
2743 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002744 recordDecl(isTemplateInstantiation(), hasDescendant(
2745 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002746}
2747
2748TEST(IsTemplateInstantiation,
2749 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2750 EXPECT_TRUE(matches(
2751 "template <typename T> class X {};"
2752 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002753 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002754}
2755
2756TEST(IsTemplateInstantiation,
2757 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2758 EXPECT_TRUE(matches(
2759 "class A {};"
2760 "class X {"
2761 " template <typename U> class Y { U u; };"
2762 " Y<A> y;"
2763 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002764 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002765}
2766
2767TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2768 // FIXME: Figure out whether this makes sense. It doesn't affect the
2769 // normal use case as long as the uppermost instantiation always is marked
2770 // as template instantiation, but it might be confusing as a predicate.
2771 EXPECT_TRUE(matches(
2772 "class A {};"
2773 "template <typename T> class X {"
2774 " template <typename U> class Y { U u; };"
2775 " Y<T> y;"
2776 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002777 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002778}
2779
2780TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2781 EXPECT_TRUE(notMatches(
2782 "template <typename T> class X {}; class A {};"
2783 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002784 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002785}
2786
2787TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2788 EXPECT_TRUE(notMatches(
2789 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002790 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002791}
2792
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002793TEST(IsExplicitTemplateSpecialization,
2794 DoesNotMatchPrimaryTemplate) {
2795 EXPECT_TRUE(notMatches(
2796 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002797 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002798 EXPECT_TRUE(notMatches(
2799 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002800 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002801}
2802
2803TEST(IsExplicitTemplateSpecialization,
2804 DoesNotMatchExplicitTemplateInstantiations) {
2805 EXPECT_TRUE(notMatches(
2806 "template <typename T> class X {};"
2807 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002808 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002809 EXPECT_TRUE(notMatches(
2810 "template <typename T> void f(T t) {}"
2811 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002812 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002813}
2814
2815TEST(IsExplicitTemplateSpecialization,
2816 DoesNotMatchImplicitTemplateInstantiations) {
2817 EXPECT_TRUE(notMatches(
2818 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002819 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002820 EXPECT_TRUE(notMatches(
2821 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002822 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002823}
2824
2825TEST(IsExplicitTemplateSpecialization,
2826 MatchesExplicitTemplateSpecializations) {
2827 EXPECT_TRUE(matches(
2828 "template <typename T> class X {};"
2829 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002830 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002831 EXPECT_TRUE(matches(
2832 "template <typename T> void f(T t) {}"
2833 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002834 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002835}
2836
Manuel Klimek579b1202012-09-07 09:26:10 +00002837TEST(HasAncenstor, MatchesDeclarationAncestors) {
2838 EXPECT_TRUE(matches(
2839 "class A { class B { class C {}; }; };",
2840 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2841}
2842
2843TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2844 EXPECT_TRUE(notMatches(
2845 "class A { class B { class C {}; }; };",
2846 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2847}
2848
2849TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2850 EXPECT_TRUE(matches(
2851 "class A { class B { void f() { C c; } class C {}; }; };",
2852 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2853 hasAncestor(recordDecl(hasName("A"))))))));
2854}
2855
2856TEST(HasAncenstor, MatchesStatementAncestors) {
2857 EXPECT_TRUE(matches(
2858 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002859 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002860}
2861
2862TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2863 EXPECT_TRUE(matches(
2864 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002865 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002866}
2867
2868TEST(HasAncestor, BindsRecursiveCombinations) {
2869 EXPECT_TRUE(matchAndVerifyResultTrue(
2870 "class C { class D { class E { class F { int y; }; }; }; };",
2871 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002872 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00002873}
2874
2875TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
2876 EXPECT_TRUE(matchAndVerifyResultTrue(
2877 "class C { class D { class E { class F { int y; }; }; }; };",
2878 fieldDecl(hasAncestor(
2879 decl(
2880 hasDescendant(recordDecl(isDefinition(),
2881 hasAncestor(recordDecl())))
2882 ).bind("d")
2883 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00002884 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00002885}
2886
2887TEST(HasAncestor, MatchesInTemplateInstantiations) {
2888 EXPECT_TRUE(matches(
2889 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
2890 "A<int>::B::C a;",
2891 fieldDecl(hasType(asString("int")),
2892 hasAncestor(recordDecl(hasName("A"))))));
2893}
2894
2895TEST(HasAncestor, MatchesInImplicitCode) {
2896 EXPECT_TRUE(matches(
2897 "struct X {}; struct A { A() {} X x; };",
2898 constructorDecl(
2899 hasAnyConstructorInitializer(withInitializer(expr(
2900 hasAncestor(recordDecl(hasName("A")))))))));
2901}
2902
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00002903TEST(HasParent, MatchesOnlyParent) {
2904 EXPECT_TRUE(matches(
2905 "void f() { if (true) { int x = 42; } }",
2906 compoundStmt(hasParent(ifStmt()))));
2907 EXPECT_TRUE(notMatches(
2908 "void f() { for (;;) { int x = 42; } }",
2909 compoundStmt(hasParent(ifStmt()))));
2910 EXPECT_TRUE(notMatches(
2911 "void f() { if (true) for (;;) { int x = 42; } }",
2912 compoundStmt(hasParent(ifStmt()))));
2913}
2914
Daniel Jasperce620072012-10-17 08:52:59 +00002915TEST(TypeMatching, MatchesTypes) {
2916 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
2917}
2918
2919TEST(TypeMatching, MatchesArrayTypes) {
2920 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
2921 EXPECT_TRUE(matches("int a[42];", arrayType()));
2922 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
2923
2924 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
2925 arrayType(hasElementType(builtinType()))));
2926
2927 EXPECT_TRUE(matches(
2928 "int const a[] = { 2, 3 };",
2929 qualType(arrayType(hasElementType(builtinType())))));
2930 EXPECT_TRUE(matches(
2931 "int const a[] = { 2, 3 };",
2932 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
2933 EXPECT_TRUE(matches(
2934 "typedef const int T; T x[] = { 1, 2 };",
2935 qualType(isConstQualified(), arrayType())));
2936
2937 EXPECT_TRUE(notMatches(
2938 "int a[] = { 2, 3 };",
2939 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
2940 EXPECT_TRUE(notMatches(
2941 "int a[] = { 2, 3 };",
2942 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
2943 EXPECT_TRUE(notMatches(
2944 "int const a[] = { 2, 3 };",
2945 qualType(arrayType(hasElementType(builtinType())),
2946 unless(isConstQualified()))));
2947
2948 EXPECT_TRUE(matches("int a[2];",
2949 constantArrayType(hasElementType(builtinType()))));
2950 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
2951}
2952
2953TEST(TypeMatching, MatchesComplexTypes) {
2954 EXPECT_TRUE(matches("_Complex float f;", complexType()));
2955 EXPECT_TRUE(matches(
2956 "_Complex float f;",
2957 complexType(hasElementType(builtinType()))));
2958 EXPECT_TRUE(notMatches(
2959 "_Complex float f;",
2960 complexType(hasElementType(isInteger()))));
2961}
2962
2963TEST(TypeMatching, MatchesConstantArrayTypes) {
2964 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
2965 EXPECT_TRUE(notMatches(
2966 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
2967 constantArrayType(hasElementType(builtinType()))));
2968
2969 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
2970 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
2971 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
2972}
2973
2974TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
2975 EXPECT_TRUE(matches(
2976 "template <typename T, int Size> class array { T data[Size]; };",
2977 dependentSizedArrayType()));
2978 EXPECT_TRUE(notMatches(
2979 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
2980 dependentSizedArrayType()));
2981}
2982
2983TEST(TypeMatching, MatchesIncompleteArrayType) {
2984 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
2985 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
2986
2987 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
2988 incompleteArrayType()));
2989}
2990
2991TEST(TypeMatching, MatchesVariableArrayType) {
2992 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
2993 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
2994
2995 EXPECT_TRUE(matches(
2996 "void f(int b) { int a[b]; }",
2997 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
2998 varDecl(hasName("b")))))))));
2999}
3000
3001TEST(TypeMatching, MatchesAtomicTypes) {
3002 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3003
3004 EXPECT_TRUE(matches("_Atomic(int) i;",
3005 atomicType(hasValueType(isInteger()))));
3006 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3007 atomicType(hasValueType(isInteger()))));
3008}
3009
3010TEST(TypeMatching, MatchesAutoTypes) {
3011 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3012 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3013 autoType()));
3014
3015 EXPECT_TRUE(matches("auto a = 1;",
3016 autoType(hasDeducedType(isInteger()))));
3017 EXPECT_TRUE(notMatches("auto b = 2.0;",
3018 autoType(hasDeducedType(isInteger()))));
3019}
3020
3021TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003022 // FIXME: Reactive when these tests can be more specific (not matching
3023 // implicit code on certain platforms), likely when we have hasDescendant for
3024 // Types/TypeLocs.
3025 //EXPECT_TRUE(matchAndVerifyResultTrue(
3026 // "int* a;",
3027 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3028 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3029 //EXPECT_TRUE(matchAndVerifyResultTrue(
3030 // "int* a;",
3031 // pointerTypeLoc().bind("loc"),
3032 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003033 EXPECT_TRUE(matches(
3034 "int** a;",
3035 pointerTypeLoc(pointeeLoc(loc(qualType())))));
3036 EXPECT_TRUE(matches(
3037 "int** a;",
3038 loc(pointerType(pointee(pointerType())))));
3039 EXPECT_TRUE(matches(
3040 "int* b; int* * const a = &b;",
3041 loc(qualType(isConstQualified(), pointerType()))));
3042
3043 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003044 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3045 hasType(blockPointerType()))));
3046 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3047 hasType(memberPointerType()))));
3048 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3049 hasType(pointerType()))));
3050 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3051 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003052
Daniel Jasper1802daf2012-10-17 13:35:36 +00003053 Fragment = "int *ptr;";
3054 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3055 hasType(blockPointerType()))));
3056 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3057 hasType(memberPointerType()))));
3058 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3059 hasType(pointerType()))));
3060 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3061 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003062
Daniel Jasper1802daf2012-10-17 13:35:36 +00003063 Fragment = "int a; int &ref = a;";
3064 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3065 hasType(blockPointerType()))));
3066 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3067 hasType(memberPointerType()))));
3068 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3069 hasType(pointerType()))));
3070 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3071 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003072}
3073
3074TEST(TypeMatching, PointeeTypes) {
3075 EXPECT_TRUE(matches("int b; int &a = b;",
3076 referenceType(pointee(builtinType()))));
3077 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3078
3079 EXPECT_TRUE(matches("int *a;",
3080 pointerTypeLoc(pointeeLoc(loc(builtinType())))));
3081
3082 EXPECT_TRUE(matches(
3083 "int const *A;",
3084 pointerType(pointee(isConstQualified(), builtinType()))));
3085 EXPECT_TRUE(notMatches(
3086 "int *A;",
3087 pointerType(pointee(isConstQualified(), builtinType()))));
3088}
3089
3090TEST(TypeMatching, MatchesPointersToConstTypes) {
3091 EXPECT_TRUE(matches("int b; int * const a = &b;",
3092 loc(pointerType())));
3093 EXPECT_TRUE(matches("int b; int * const a = &b;",
3094 pointerTypeLoc()));
3095 EXPECT_TRUE(matches(
3096 "int b; const int * a = &b;",
3097 pointerTypeLoc(pointeeLoc(builtinTypeLoc()))));
3098 EXPECT_TRUE(matches(
3099 "int b; const int * a = &b;",
3100 pointerType(pointee(builtinType()))));
3101}
3102
3103TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003104 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3105 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003106
Daniel Jasper1802daf2012-10-17 13:35:36 +00003107 EXPECT_TRUE(matches("typedef int X; X a;",
3108 varDecl(hasName("a"),
3109 hasType(typedefType(hasDecl(decl()))))));
Daniel Jasperce620072012-10-17 08:52:59 +00003110}
3111
Daniel Jaspera7564432012-09-13 13:11:25 +00003112TEST(NNS, MatchesNestedNameSpecifiers) {
3113 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3114 nestedNameSpecifier()));
3115 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3116 nestedNameSpecifier()));
3117 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3118 nestedNameSpecifier()));
3119
3120 EXPECT_TRUE(matches(
3121 "struct A { static void f() {} }; void g() { A::f(); }",
3122 nestedNameSpecifier()));
3123 EXPECT_TRUE(notMatches(
3124 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3125 nestedNameSpecifier()));
3126}
3127
Daniel Jasperb54b7642012-09-20 14:12:57 +00003128TEST(NullStatement, SimpleCases) {
3129 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3130 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3131}
3132
Daniel Jaspera7564432012-09-13 13:11:25 +00003133TEST(NNS, MatchesTypes) {
3134 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3135 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3136 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3137 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3138 Matcher));
3139 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3140}
3141
3142TEST(NNS, MatchesNamespaceDecls) {
3143 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3144 specifiesNamespace(hasName("ns")));
3145 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3146 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3147 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3148}
3149
3150TEST(NNS, BindsNestedNameSpecifiers) {
3151 EXPECT_TRUE(matchAndVerifyResultTrue(
3152 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3153 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3154 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3155}
3156
3157TEST(NNS, BindsNestedNameSpecifierLocs) {
3158 EXPECT_TRUE(matchAndVerifyResultTrue(
3159 "namespace ns { struct B {}; } ns::B b;",
3160 loc(nestedNameSpecifier()).bind("loc"),
3161 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3162}
3163
3164TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3165 EXPECT_TRUE(matches(
3166 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3167 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3168 EXPECT_TRUE(matches(
3169 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003170 nestedNameSpecifierLoc(hasPrefix(
3171 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003172}
3173
Manuel Klimek4da21662012-07-06 05:48:52 +00003174} // end namespace ast_matchers
3175} // end namespace clang