blob: d8f5511d6daddff13fcca000a2967c1f7b6c233d [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
1336TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001337 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001338
1339 std::string ClassString = "class string { public: string(); ~string(); }; ";
1340
1341 EXPECT_TRUE(
1342 matches(ClassString +
1343 "string GetStringByValue();"
1344 "void FunctionTakesString(string s);"
1345 "void run() { FunctionTakesString(GetStringByValue()); }",
1346 TempExpression));
1347
1348 EXPECT_TRUE(
1349 notMatches(ClassString +
1350 "string* GetStringPointer(); "
1351 "void FunctionTakesStringPtr(string* s);"
1352 "void run() {"
1353 " string* s = GetStringPointer();"
1354 " FunctionTakesStringPtr(GetStringPointer());"
1355 " FunctionTakesStringPtr(s);"
1356 "}",
1357 TempExpression));
1358
1359 EXPECT_TRUE(
1360 notMatches("class no_dtor {};"
1361 "no_dtor GetObjByValue();"
1362 "void ConsumeObj(no_dtor param);"
1363 "void run() { ConsumeObj(GetObjByValue()); }",
1364 TempExpression));
1365}
1366
Sam Panzere16acd32012-08-24 22:04:44 +00001367TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1368 std::string ClassString =
1369 "class string { public: string(); int length(); }; ";
1370
1371 EXPECT_TRUE(
1372 matches(ClassString +
1373 "string GetStringByValue();"
1374 "void FunctionTakesString(string s);"
1375 "void run() { FunctionTakesString(GetStringByValue()); }",
1376 materializeTemporaryExpr()));
1377
1378 EXPECT_TRUE(
1379 notMatches(ClassString +
1380 "string* GetStringPointer(); "
1381 "void FunctionTakesStringPtr(string* s);"
1382 "void run() {"
1383 " string* s = GetStringPointer();"
1384 " FunctionTakesStringPtr(GetStringPointer());"
1385 " FunctionTakesStringPtr(s);"
1386 "}",
1387 materializeTemporaryExpr()));
1388
1389 EXPECT_TRUE(
1390 notMatches(ClassString +
1391 "string GetStringByValue();"
1392 "void run() { int k = GetStringByValue().length(); }",
1393 materializeTemporaryExpr()));
1394
1395 EXPECT_TRUE(
1396 notMatches(ClassString +
1397 "string GetStringByValue();"
1398 "void run() { GetStringByValue(); }",
1399 materializeTemporaryExpr()));
1400}
1401
Manuel Klimek4da21662012-07-06 05:48:52 +00001402TEST(ConstructorDeclaration, SimpleCase) {
1403 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001404 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001405 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001406 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001407}
1408
1409TEST(ConstructorDeclaration, IsImplicit) {
1410 // This one doesn't match because the constructor is not added by the
1411 // compiler (it is not needed).
1412 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001413 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001414 // The compiler added the implicit default constructor.
1415 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001416 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001417 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001418 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001419}
1420
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001421TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1422 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001423 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001424}
1425
1426TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001427 EXPECT_TRUE(notMatches("class Foo {};",
1428 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001429}
1430
Manuel Klimek4da21662012-07-06 05:48:52 +00001431TEST(HasAnyConstructorInitializer, SimpleCase) {
1432 EXPECT_TRUE(notMatches(
1433 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001434 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001435 EXPECT_TRUE(matches(
1436 "class Foo {"
1437 " Foo() : foo_() { }"
1438 " int foo_;"
1439 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001440 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001441}
1442
1443TEST(HasAnyConstructorInitializer, ForField) {
1444 static const char Code[] =
1445 "class Baz { };"
1446 "class Foo {"
1447 " Foo() : foo_() { }"
1448 " Baz foo_;"
1449 " Baz bar_;"
1450 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001451 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1452 forField(hasType(recordDecl(hasName("Baz"))))))));
1453 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001454 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001455 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1456 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001457}
1458
1459TEST(HasAnyConstructorInitializer, WithInitializer) {
1460 static const char Code[] =
1461 "class Foo {"
1462 " Foo() : foo_(0) { }"
1463 " int foo_;"
1464 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001465 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001466 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001467 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001468 withInitializer(integerLiteral(equals(1)))))));
1469}
1470
1471TEST(HasAnyConstructorInitializer, IsWritten) {
1472 static const char Code[] =
1473 "struct Bar { Bar(){} };"
1474 "class Foo {"
1475 " Foo() : foo_() { }"
1476 " Bar foo_;"
1477 " Bar bar_;"
1478 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001479 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001480 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001481 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001482 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001483 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001484 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1485}
1486
1487TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001488 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001489
1490 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1491 EXPECT_TRUE(
1492 matches("class X { public: X(); }; void x() { new X(); }", New));
1493 EXPECT_TRUE(
1494 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1495 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1496}
1497
1498TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001499 StatementMatcher New = constructExpr(
1500 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001501
1502 EXPECT_TRUE(
1503 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1504 New));
1505 EXPECT_TRUE(
1506 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1507 New));
1508 EXPECT_TRUE(
1509 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1510 New));
1511
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001512 StatementMatcher WrongIndex = constructExpr(
1513 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001514 EXPECT_TRUE(
1515 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1516 WrongIndex));
1517}
1518
1519TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001520 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001521
1522 EXPECT_TRUE(
1523 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1524 EXPECT_TRUE(
1525 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1526 New));
1527}
1528
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001529TEST(Matcher, DeleteExpression) {
1530 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001531 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001532}
1533
Manuel Klimek4da21662012-07-06 05:48:52 +00001534TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001535 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001536
1537 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1538 EXPECT_TRUE(
1539 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1540 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1541}
1542
1543TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001544 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001545 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1546 // wide string
1547 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1548 // with escaped characters
1549 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1550 // no matching -- though the data type is the same, there is no string literal
1551 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1552}
1553
1554TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001555 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001556 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1557 // wide character
1558 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1559 // wide character, Hex encoded, NOT MATCHED!
1560 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1561 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1562}
1563
1564TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001565 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001566 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1567 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1568 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1569 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1570
1571 // Non-matching cases (character literals, float and double)
1572 EXPECT_TRUE(notMatches("int i = L'a';",
1573 HasIntLiteral)); // this is actually a character
1574 // literal cast to int
1575 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1576 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1577 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1578}
1579
Daniel Jasper31f7c082012-10-01 13:40:41 +00001580TEST(Matcher, NullPtrLiteral) {
1581 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1582}
1583
Daniel Jasperb54b7642012-09-20 14:12:57 +00001584TEST(Matcher, AsmStatement) {
1585 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1586}
1587
Manuel Klimek4da21662012-07-06 05:48:52 +00001588TEST(Matcher, Conditions) {
1589 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1590
1591 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1592 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1593 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1594 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1595 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1596}
1597
1598TEST(MatchBinaryOperator, HasOperatorName) {
1599 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1600
1601 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1602 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1603}
1604
1605TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1606 StatementMatcher OperatorTrueFalse =
1607 binaryOperator(hasLHS(boolLiteral(equals(true))),
1608 hasRHS(boolLiteral(equals(false))));
1609
1610 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1611 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1612 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1613}
1614
1615TEST(MatchBinaryOperator, HasEitherOperand) {
1616 StatementMatcher HasOperand =
1617 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1618
1619 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1620 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1621 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1622}
1623
1624TEST(Matcher, BinaryOperatorTypes) {
1625 // Integration test that verifies the AST provides all binary operators in
1626 // a way we expect.
1627 // FIXME: Operator ','
1628 EXPECT_TRUE(
1629 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1630 EXPECT_TRUE(
1631 matches("bool b; bool c = (b = true);",
1632 binaryOperator(hasOperatorName("="))));
1633 EXPECT_TRUE(
1634 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1635 EXPECT_TRUE(
1636 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1637 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1638 EXPECT_TRUE(
1639 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1640 EXPECT_TRUE(
1641 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1642 EXPECT_TRUE(
1643 matches("int i = 1; int j = (i <<= 2);",
1644 binaryOperator(hasOperatorName("<<="))));
1645 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1646 EXPECT_TRUE(
1647 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1648 EXPECT_TRUE(
1649 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1650 EXPECT_TRUE(
1651 matches("int i = 1; int j = (i >>= 2);",
1652 binaryOperator(hasOperatorName(">>="))));
1653 EXPECT_TRUE(
1654 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1655 EXPECT_TRUE(
1656 matches("int i = 42; int j = (i ^= 42);",
1657 binaryOperator(hasOperatorName("^="))));
1658 EXPECT_TRUE(
1659 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1660 EXPECT_TRUE(
1661 matches("int i = 42; int j = (i %= 42);",
1662 binaryOperator(hasOperatorName("%="))));
1663 EXPECT_TRUE(
1664 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1665 EXPECT_TRUE(
1666 matches("bool b = true && false;",
1667 binaryOperator(hasOperatorName("&&"))));
1668 EXPECT_TRUE(
1669 matches("bool b = true; bool c = (b &= false);",
1670 binaryOperator(hasOperatorName("&="))));
1671 EXPECT_TRUE(
1672 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1673 EXPECT_TRUE(
1674 matches("bool b = true || false;",
1675 binaryOperator(hasOperatorName("||"))));
1676 EXPECT_TRUE(
1677 matches("bool b = true; bool c = (b |= false);",
1678 binaryOperator(hasOperatorName("|="))));
1679 EXPECT_TRUE(
1680 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1681 EXPECT_TRUE(
1682 matches("int i = 42; int j = (i *= 23);",
1683 binaryOperator(hasOperatorName("*="))));
1684 EXPECT_TRUE(
1685 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1686 EXPECT_TRUE(
1687 matches("int i = 42; int j = (i /= 23);",
1688 binaryOperator(hasOperatorName("/="))));
1689 EXPECT_TRUE(
1690 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1691 EXPECT_TRUE(
1692 matches("int i = 42; int j = (i += 23);",
1693 binaryOperator(hasOperatorName("+="))));
1694 EXPECT_TRUE(
1695 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1696 EXPECT_TRUE(
1697 matches("int i = 42; int j = (i -= 23);",
1698 binaryOperator(hasOperatorName("-="))));
1699 EXPECT_TRUE(
1700 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1701 binaryOperator(hasOperatorName("->*"))));
1702 EXPECT_TRUE(
1703 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1704 binaryOperator(hasOperatorName(".*"))));
1705
1706 // Member expressions as operators are not supported in matches.
1707 EXPECT_TRUE(
1708 notMatches("struct A { void x(A *a) { a->x(this); } };",
1709 binaryOperator(hasOperatorName("->"))));
1710
1711 // Initializer assignments are not represented as operator equals.
1712 EXPECT_TRUE(
1713 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1714
1715 // Array indexing is not represented as operator.
1716 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1717
1718 // Overloaded operators do not match at all.
1719 EXPECT_TRUE(notMatches(
1720 "struct A { bool operator&&(const A &a) const { return false; } };"
1721 "void x() { A a, b; a && b; }",
1722 binaryOperator()));
1723}
1724
1725TEST(MatchUnaryOperator, HasOperatorName) {
1726 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1727
1728 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1729 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1730}
1731
1732TEST(MatchUnaryOperator, HasUnaryOperand) {
1733 StatementMatcher OperatorOnFalse =
1734 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1735
1736 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1737 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1738}
1739
1740TEST(Matcher, UnaryOperatorTypes) {
1741 // Integration test that verifies the AST provides all unary operators in
1742 // a way we expect.
1743 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1744 EXPECT_TRUE(
1745 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1746 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1747 EXPECT_TRUE(
1748 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1749 EXPECT_TRUE(
1750 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1751 EXPECT_TRUE(
1752 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1753 EXPECT_TRUE(
1754 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1755 EXPECT_TRUE(
1756 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1757 EXPECT_TRUE(
1758 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1759 EXPECT_TRUE(
1760 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1761
1762 // We don't match conversion operators.
1763 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1764
1765 // Function calls are not represented as operator.
1766 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1767
1768 // Overloaded operators do not match at all.
1769 // FIXME: We probably want to add that.
1770 EXPECT_TRUE(notMatches(
1771 "struct A { bool operator!() const { return false; } };"
1772 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1773}
1774
1775TEST(Matcher, ConditionalOperator) {
1776 StatementMatcher Conditional = conditionalOperator(
1777 hasCondition(boolLiteral(equals(true))),
1778 hasTrueExpression(boolLiteral(equals(false))));
1779
1780 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1781 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1782 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1783
1784 StatementMatcher ConditionalFalse = conditionalOperator(
1785 hasFalseExpression(boolLiteral(equals(false))));
1786
1787 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1788 EXPECT_TRUE(
1789 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1790}
1791
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001792TEST(ArraySubscriptMatchers, ArraySubscripts) {
1793 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1794 arraySubscriptExpr()));
1795 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1796 arraySubscriptExpr()));
1797}
1798
1799TEST(ArraySubscriptMatchers, ArrayIndex) {
1800 EXPECT_TRUE(matches(
1801 "int i[2]; void f() { i[1] = 1; }",
1802 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1803 EXPECT_TRUE(matches(
1804 "int i[2]; void f() { 1[i] = 1; }",
1805 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1806 EXPECT_TRUE(notMatches(
1807 "int i[2]; void f() { i[1] = 1; }",
1808 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1809}
1810
1811TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1812 EXPECT_TRUE(matches(
1813 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001814 arraySubscriptExpr(hasBase(implicitCastExpr(
1815 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001816}
1817
Manuel Klimek4da21662012-07-06 05:48:52 +00001818TEST(Matcher, HasNameSupportsNamespaces) {
1819 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001820 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001821 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001822 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001823 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001824 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001825 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001826 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001827 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001828 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001829 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001830 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001831 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001832 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001833 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001834 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001835 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001836 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001837 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001838 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001839 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001840 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001841 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001842 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001843}
1844
1845TEST(Matcher, HasNameSupportsOuterClasses) {
1846 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001847 matches("class A { class B { class C; }; };",
1848 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001849 EXPECT_TRUE(
1850 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001851 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001852 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001853 matches("class A { class B { class C; }; };",
1854 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001855 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001856 matches("class A { class B { class C; }; };",
1857 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001858 EXPECT_TRUE(
1859 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001860 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001861 EXPECT_TRUE(
1862 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001863 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001864 EXPECT_TRUE(
1865 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001866 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001867 EXPECT_TRUE(
1868 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001869 recordDecl(hasName("::C"))));
1870 EXPECT_TRUE(
1871 notMatches("class A { class B { class C; }; };",
1872 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001873 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001874 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001875 EXPECT_TRUE(
1876 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001877 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001878}
1879
1880TEST(Matcher, IsDefinition) {
1881 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001882 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001883 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1884 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1885
1886 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001887 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001888 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1889 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1890
1891 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001892 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001893 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1894 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1895}
1896
1897TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001898 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00001899 ofClass(hasName("X")))));
1900
1901 EXPECT_TRUE(
1902 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
1903 EXPECT_TRUE(
1904 matches("class X { public: X(); }; void x(int) { X x = X(); }",
1905 Constructor));
1906 EXPECT_TRUE(
1907 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
1908 Constructor));
1909}
1910
1911TEST(Matcher, VisitsTemplateInstantiations) {
1912 EXPECT_TRUE(matches(
1913 "class A { public: void x(); };"
1914 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001915 "void f() { B<A> b; b.y(); }",
1916 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001917
1918 EXPECT_TRUE(matches(
1919 "class A { public: void x(); };"
1920 "class C {"
1921 " public:"
1922 " template <typename T> class B { public: void y() { T t; t.x(); } };"
1923 "};"
1924 "void f() {"
1925 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001926 "}",
1927 recordDecl(hasName("C"),
1928 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001929}
1930
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001931TEST(Matcher, HandlesNullQualTypes) {
1932 // FIXME: Add a Type matcher so we can replace uses of this
1933 // variable with Type(True())
1934 const TypeMatcher AnyType = anything();
1935
1936 // We don't really care whether this matcher succeeds; we're testing that
1937 // it completes without crashing.
1938 EXPECT_TRUE(matches(
1939 "struct A { };"
1940 "template <typename T>"
1941 "void f(T t) {"
1942 " T local_t(t /* this becomes a null QualType in the AST */);"
1943 "}"
1944 "void g() {"
1945 " f(0);"
1946 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001947 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001948 anyOf(
1949 TypeMatcher(hasDeclaration(anything())),
1950 pointsTo(AnyType),
1951 references(AnyType)
1952 // Other QualType matchers should go here.
1953 ))))));
1954}
1955
Manuel Klimek4da21662012-07-06 05:48:52 +00001956// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001957AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00001958 // Make sure all special variables are used: node, match_finder,
1959 // bound_nodes_builder, and the parameter named 'AMatcher'.
1960 return AMatcher.matches(Node, Finder, Builder);
1961}
1962
1963TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001964 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001965
1966 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001967 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001968
1969 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001970 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001971
1972 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001973 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001974}
1975
1976AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001977 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
1978 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
1979 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00001980 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00001981 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00001982 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00001983 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
1984 ASTMatchFinder::BK_First);
1985}
1986
1987TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001988 DeclarationMatcher HasClassB =
1989 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001990
1991 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001992 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001993
1994 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001995 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001996
1997 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00001998 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00001999
2000 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002001 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002002
2003 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2004}
2005
2006TEST(For, FindsForLoops) {
2007 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2008 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002009 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2010 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002011 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002012}
2013
Daniel Jasper6a124492012-07-12 08:50:38 +00002014TEST(For, ForLoopInternals) {
2015 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2016 forStmt(hasCondition(anything()))));
2017 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2018 forStmt(hasLoopInit(anything()))));
2019}
2020
2021TEST(For, NegativeForLoopInternals) {
2022 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002023 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002024 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2025 forStmt(hasLoopInit(anything()))));
2026}
2027
Manuel Klimek4da21662012-07-06 05:48:52 +00002028TEST(For, ReportsNoFalsePositives) {
2029 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2030 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2031}
2032
2033TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002034 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2035 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2036 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002037}
2038
2039TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2040 // It's not a compound statement just because there's "{}" in the source
2041 // text. This is an AST search, not grep.
2042 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002043 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002044 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002045 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002046}
2047
Daniel Jasper6a124492012-07-12 08:50:38 +00002048TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002049 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002050 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002051 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002052 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002053 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002054 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002055 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002056 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002057}
2058
2059TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2060 // The simplest case: every compound statement is in a function
2061 // definition, and the function body itself must be a compound
2062 // statement.
2063 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002064 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002065}
2066
2067TEST(HasAnySubstatement, IsNotRecursive) {
2068 // It's really "has any immediate substatement".
2069 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002070 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002071}
2072
2073TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2074 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002075 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002076}
2077
2078TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2079 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002080 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002081}
2082
2083TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2084 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002085 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002086 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002087 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002088}
2089
2090TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2091 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002092 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002093 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002094 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002095 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002096 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002097}
2098
2099TEST(StatementCountIs, WorksWithMultipleStatements) {
2100 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002101 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002102}
2103
2104TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2105 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002106 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002107 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002108 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002109 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002110 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002111 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002112 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002113}
2114
2115TEST(Member, WorksInSimplestCase) {
2116 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002117 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002118}
2119
2120TEST(Member, DoesNotMatchTheBaseExpression) {
2121 // Don't pick out the wrong part of the member expression, this should
2122 // be checking the member (name) only.
2123 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002124 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002125}
2126
2127TEST(Member, MatchesInMemberFunctionCall) {
2128 EXPECT_TRUE(matches("void f() {"
2129 " struct { void first() {}; } s;"
2130 " s.first();"
2131 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002132 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002133}
2134
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002135TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002136 // Fails in C++11 mode
2137 EXPECT_TRUE(matchesConditionally(
2138 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2139 "class X { void *operator new(std::size_t); };",
2140 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002141
2142 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002143 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002144
Daniel Jasper31f7c082012-10-01 13:40:41 +00002145 // Fails in C++11 mode
2146 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002147 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2148 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002149 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002150}
2151
Manuel Klimek4da21662012-07-06 05:48:52 +00002152TEST(HasObjectExpression, DoesNotMatchMember) {
2153 EXPECT_TRUE(notMatches(
2154 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002155 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002156}
2157
2158TEST(HasObjectExpression, MatchesBaseOfVariable) {
2159 EXPECT_TRUE(matches(
2160 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002161 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002162 EXPECT_TRUE(matches(
2163 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002164 memberExpr(hasObjectExpression(
2165 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002166}
2167
2168TEST(HasObjectExpression,
2169 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2170 EXPECT_TRUE(matches(
2171 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002172 memberExpr(hasObjectExpression(
2173 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002174 EXPECT_TRUE(matches(
2175 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002176 memberExpr(hasObjectExpression(
2177 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002178}
2179
2180TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002181 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2182 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2183 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2184 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002185}
2186
2187TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002188 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002189}
2190
2191TEST(IsConstQualified, MatchesConstInt) {
2192 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002193 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002194}
2195
2196TEST(IsConstQualified, MatchesConstPointer) {
2197 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002198 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002199}
2200
2201TEST(IsConstQualified, MatchesThroughTypedef) {
2202 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002203 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002204 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002205 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002206}
2207
2208TEST(IsConstQualified, DoesNotMatchInappropriately) {
2209 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002210 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002211 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002212 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002213}
2214
Sam Panzer089e5b32012-08-16 16:58:10 +00002215TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002216 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2217 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2218 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2219 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002220}
2221TEST(CastExpression, MatchesImplicitCasts) {
2222 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002223 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002224 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002225 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002226}
2227
2228TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002229 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2230 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2231 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2232 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002233}
2234
Manuel Klimek4da21662012-07-06 05:48:52 +00002235TEST(ReinterpretCast, MatchesSimpleCase) {
2236 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002237 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002238}
2239
2240TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002241 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002242 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002243 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002244 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002245 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002246 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2247 "B b;"
2248 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002249 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002250}
2251
2252TEST(FunctionalCast, MatchesSimpleCase) {
2253 std::string foo_class = "class Foo { public: Foo(char*); };";
2254 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002255 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002256}
2257
2258TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2259 std::string FooClass = "class Foo { public: Foo(char*); };";
2260 EXPECT_TRUE(
2261 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002262 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002263 EXPECT_TRUE(
2264 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002265 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002266}
2267
2268TEST(DynamicCast, MatchesSimpleCase) {
2269 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2270 "B b;"
2271 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002272 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002273}
2274
2275TEST(StaticCast, MatchesSimpleCase) {
2276 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002277 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002278}
2279
2280TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002281 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002282 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002283 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002284 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002285 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002286 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2287 "B b;"
2288 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002289 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002290}
2291
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002292TEST(CStyleCast, MatchesSimpleCase) {
2293 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2294}
2295
2296TEST(CStyleCast, DoesNotMatchOtherCasts) {
2297 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2298 "char q, *r = const_cast<char*>(&q);"
2299 "void* s = reinterpret_cast<char*>(&s);"
2300 "struct B { virtual ~B() {} }; struct D : B {};"
2301 "B b;"
2302 "D* t = dynamic_cast<D*>(&b);",
2303 cStyleCastExpr()));
2304}
2305
Manuel Klimek4da21662012-07-06 05:48:52 +00002306TEST(HasDestinationType, MatchesSimpleCase) {
2307 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002308 staticCastExpr(hasDestinationType(
2309 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002310}
2311
Sam Panzer089e5b32012-08-16 16:58:10 +00002312TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2313 // This test creates an implicit const cast.
2314 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002315 implicitCastExpr(
2316 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002317 // This test creates an implicit array-to-pointer cast.
2318 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002319 implicitCastExpr(hasImplicitDestinationType(
2320 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002321}
2322
2323TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2324 // This test creates an implicit cast from int to char.
2325 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002326 implicitCastExpr(hasImplicitDestinationType(
2327 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002328 // This test creates an implicit array-to-pointer cast.
2329 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002330 implicitCastExpr(hasImplicitDestinationType(
2331 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002332}
2333
2334TEST(ImplicitCast, MatchesSimpleCase) {
2335 // This test creates an implicit const cast.
2336 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002337 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002338 // This test creates an implicit cast from int to char.
2339 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002340 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002341 // This test creates an implicit array-to-pointer cast.
2342 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002343 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002344}
2345
2346TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002347 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002348 // are present, and that it ignores explicit and paren casts.
2349
2350 // These two test cases have no casts.
2351 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002352 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002353 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002354 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002355
2356 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002357 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002358 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002359 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002360
2361 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002362 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002363}
2364
2365TEST(IgnoringImpCasts, MatchesImpCasts) {
2366 // This test checks that ignoringImpCasts matches when implicit casts are
2367 // present and its inner matcher alone does not match.
2368 // Note that this test creates an implicit const cast.
2369 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002370 varDecl(hasInitializer(ignoringImpCasts(
2371 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002372 // This test creates an implict cast from int to char.
2373 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002374 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002375 integerLiteral(equals(0)))))));
2376}
2377
2378TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2379 // These tests verify that ignoringImpCasts does not match if the inner
2380 // matcher does not match.
2381 // Note that the first test creates an implicit const cast.
2382 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002383 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002384 unless(anything()))))));
2385 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002386 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002387 unless(anything()))))));
2388
2389 // These tests verify that ignoringImplictCasts does not look through explicit
2390 // casts or parentheses.
2391 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002392 varDecl(hasInitializer(ignoringImpCasts(
2393 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002394 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002395 varDecl(hasInitializer(ignoringImpCasts(
2396 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002397 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002398 varDecl(hasInitializer(ignoringImpCasts(
2399 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002400 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002401 varDecl(hasInitializer(ignoringImpCasts(
2402 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002403}
2404
2405TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2406 // This test verifies that expressions that do not have implicit casts
2407 // still match the inner matcher.
2408 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002409 varDecl(hasInitializer(ignoringImpCasts(
2410 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002411}
2412
2413TEST(IgnoringParenCasts, MatchesParenCasts) {
2414 // This test checks that ignoringParenCasts matches when parentheses and/or
2415 // casts are present and its inner matcher alone does not match.
2416 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002417 varDecl(hasInitializer(ignoringParenCasts(
2418 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002419 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002420 varDecl(hasInitializer(ignoringParenCasts(
2421 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002422
2423 // This test creates an implict cast from int to char in addition to the
2424 // parentheses.
2425 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002426 varDecl(hasInitializer(ignoringParenCasts(
2427 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002428
2429 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002430 varDecl(hasInitializer(ignoringParenCasts(
2431 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002432 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002433 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002434 integerLiteral(equals(0)))))));
2435}
2436
2437TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2438 // This test verifies that expressions that do not have any casts still match.
2439 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002440 varDecl(hasInitializer(ignoringParenCasts(
2441 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002442}
2443
2444TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2445 // These tests verify that ignoringImpCasts does not match if the inner
2446 // matcher does not match.
2447 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002448 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002449 unless(anything()))))));
2450
2451 // This test creates an implicit cast from int to char in addition to the
2452 // parentheses.
2453 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002454 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002455 unless(anything()))))));
2456
2457 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002458 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002459 unless(anything()))))));
2460}
2461
2462TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2463 // This test checks that ignoringParenAndImpCasts matches when
2464 // parentheses and/or implicit casts are present and its inner matcher alone
2465 // does not match.
2466 // Note that this test creates an implicit const cast.
2467 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002468 varDecl(hasInitializer(ignoringParenImpCasts(
2469 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002470 // This test creates an implicit cast from int to char.
2471 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002472 varDecl(hasInitializer(ignoringParenImpCasts(
2473 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002474}
2475
2476TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2477 // This test verifies that expressions that do not have parentheses or
2478 // implicit casts still match.
2479 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002480 varDecl(hasInitializer(ignoringParenImpCasts(
2481 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002482 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002483 varDecl(hasInitializer(ignoringParenImpCasts(
2484 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002485}
2486
2487TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2488 // These tests verify that ignoringParenImpCasts does not match if
2489 // the inner matcher does not match.
2490 // This test creates an implicit cast.
2491 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002492 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002493 unless(anything()))))));
2494 // These tests verify that ignoringParenAndImplictCasts does not look
2495 // through explicit casts.
2496 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002497 varDecl(hasInitializer(ignoringParenImpCasts(
2498 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002499 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002500 varDecl(hasInitializer(ignoringParenImpCasts(
2501 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002502 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002503 varDecl(hasInitializer(ignoringParenImpCasts(
2504 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002505}
2506
Manuel Klimek715c9562012-07-25 10:02:02 +00002507TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002508 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2509 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002510 implicitCastExpr(
2511 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002512}
2513
Manuel Klimek715c9562012-07-25 10:02:02 +00002514TEST(HasSourceExpression, MatchesExplicitCasts) {
2515 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002516 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002517 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002518 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002519}
2520
Manuel Klimek4da21662012-07-06 05:48:52 +00002521TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002522 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002523}
2524
2525TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002526 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002527}
2528
2529TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002530 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002531}
2532
2533TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002534 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002535}
2536
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002537TEST(InitListExpression, MatchesInitListExpression) {
2538 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2539 initListExpr(hasType(asString("int [2]")))));
2540 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002541 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002542}
2543
2544TEST(UsingDeclaration, MatchesUsingDeclarations) {
2545 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2546 usingDecl()));
2547}
2548
2549TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2550 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2551 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2552}
2553
2554TEST(UsingDeclaration, MatchesSpecificTarget) {
2555 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2556 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002557 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002558 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2559 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002560 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002561}
2562
2563TEST(UsingDeclaration, ThroughUsingDeclaration) {
2564 EXPECT_TRUE(matches(
2565 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002566 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002567 EXPECT_TRUE(notMatches(
2568 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002569 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002570}
2571
Sam Panzer425f41b2012-08-16 17:20:59 +00002572TEST(SingleDecl, IsSingleDecl) {
2573 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002574 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002575 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2576 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2577 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2578 SingleDeclStmt));
2579}
2580
2581TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002582 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002583
2584 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002585 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002586 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002587 declStmt(containsDeclaration(0, MatchesInit),
2588 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002589 unsigned WrongIndex = 42;
2590 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002591 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002592 MatchesInit))));
2593}
2594
2595TEST(DeclCount, DeclCountIsCorrect) {
2596 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002597 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002598 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002599 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002600 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002601 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002602}
2603
Manuel Klimek4da21662012-07-06 05:48:52 +00002604TEST(While, MatchesWhileLoops) {
2605 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2606 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2607 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2608}
2609
2610TEST(Do, MatchesDoLoops) {
2611 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2612 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2613}
2614
2615TEST(Do, DoesNotMatchWhileLoops) {
2616 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2617}
2618
2619TEST(SwitchCase, MatchesCase) {
2620 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2621 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2622 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2623 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2624}
2625
Daniel Jasperb54b7642012-09-20 14:12:57 +00002626TEST(SwitchCase, MatchesSwitch) {
2627 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2628 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2629 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2630 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2631}
2632
2633TEST(ExceptionHandling, SimpleCases) {
2634 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2635 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2636 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2637 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2638 throwExpr()));
2639 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2640 throwExpr()));
2641}
2642
Manuel Klimek4da21662012-07-06 05:48:52 +00002643TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2644 EXPECT_TRUE(notMatches(
2645 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002646 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002647 EXPECT_TRUE(notMatches(
2648 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002649 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002650}
2651
2652TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2653 EXPECT_TRUE(matches(
2654 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002655 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002656}
2657
2658TEST(ForEach, BindsOneNode) {
2659 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002660 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002661 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002662}
2663
2664TEST(ForEach, BindsMultipleNodes) {
2665 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002666 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002667 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002668}
2669
2670TEST(ForEach, BindsRecursiveCombinations) {
2671 EXPECT_TRUE(matchAndVerifyResultTrue(
2672 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002673 recordDecl(hasName("C"),
2674 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002675 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002676}
2677
2678TEST(ForEachDescendant, BindsOneNode) {
2679 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002680 recordDecl(hasName("C"),
2681 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002682 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002683}
2684
2685TEST(ForEachDescendant, BindsMultipleNodes) {
2686 EXPECT_TRUE(matchAndVerifyResultTrue(
2687 "class C { class D { int x; int y; }; "
2688 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002689 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002690 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002691}
2692
2693TEST(ForEachDescendant, BindsRecursiveCombinations) {
2694 EXPECT_TRUE(matchAndVerifyResultTrue(
2695 "class C { class D { "
2696 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002697 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2698 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002699 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002700}
2701
2702
2703TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2704 // Make sure that we can both match the class by name (::X) and by the type
2705 // the template was instantiated with (via a field).
2706
2707 EXPECT_TRUE(matches(
2708 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002709 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002710
2711 EXPECT_TRUE(matches(
2712 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002713 recordDecl(isTemplateInstantiation(), hasDescendant(
2714 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002715}
2716
2717TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2718 EXPECT_TRUE(matches(
2719 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002720 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002721 isTemplateInstantiation())));
2722}
2723
2724TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2725 EXPECT_TRUE(matches(
2726 "template <typename T> class X { T t; }; class A {};"
2727 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002728 recordDecl(isTemplateInstantiation(), hasDescendant(
2729 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002730}
2731
2732TEST(IsTemplateInstantiation,
2733 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2734 EXPECT_TRUE(matches(
2735 "template <typename T> class X {};"
2736 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002737 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002738}
2739
2740TEST(IsTemplateInstantiation,
2741 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2742 EXPECT_TRUE(matches(
2743 "class A {};"
2744 "class X {"
2745 " template <typename U> class Y { U u; };"
2746 " Y<A> y;"
2747 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002748 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002749}
2750
2751TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2752 // FIXME: Figure out whether this makes sense. It doesn't affect the
2753 // normal use case as long as the uppermost instantiation always is marked
2754 // as template instantiation, but it might be confusing as a predicate.
2755 EXPECT_TRUE(matches(
2756 "class A {};"
2757 "template <typename T> class X {"
2758 " template <typename U> class Y { U u; };"
2759 " Y<T> y;"
2760 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002761 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002762}
2763
2764TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2765 EXPECT_TRUE(notMatches(
2766 "template <typename T> class X {}; class A {};"
2767 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002768 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002769}
2770
2771TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2772 EXPECT_TRUE(notMatches(
2773 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002774 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002775}
2776
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002777TEST(IsExplicitTemplateSpecialization,
2778 DoesNotMatchPrimaryTemplate) {
2779 EXPECT_TRUE(notMatches(
2780 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002781 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002782 EXPECT_TRUE(notMatches(
2783 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002784 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002785}
2786
2787TEST(IsExplicitTemplateSpecialization,
2788 DoesNotMatchExplicitTemplateInstantiations) {
2789 EXPECT_TRUE(notMatches(
2790 "template <typename T> class X {};"
2791 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002792 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002793 EXPECT_TRUE(notMatches(
2794 "template <typename T> void f(T t) {}"
2795 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002796 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002797}
2798
2799TEST(IsExplicitTemplateSpecialization,
2800 DoesNotMatchImplicitTemplateInstantiations) {
2801 EXPECT_TRUE(notMatches(
2802 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002803 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002804 EXPECT_TRUE(notMatches(
2805 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002806 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002807}
2808
2809TEST(IsExplicitTemplateSpecialization,
2810 MatchesExplicitTemplateSpecializations) {
2811 EXPECT_TRUE(matches(
2812 "template <typename T> class X {};"
2813 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002814 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002815 EXPECT_TRUE(matches(
2816 "template <typename T> void f(T t) {}"
2817 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002818 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002819}
2820
Manuel Klimek579b1202012-09-07 09:26:10 +00002821TEST(HasAncenstor, MatchesDeclarationAncestors) {
2822 EXPECT_TRUE(matches(
2823 "class A { class B { class C {}; }; };",
2824 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2825}
2826
2827TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2828 EXPECT_TRUE(notMatches(
2829 "class A { class B { class C {}; }; };",
2830 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2831}
2832
2833TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2834 EXPECT_TRUE(matches(
2835 "class A { class B { void f() { C c; } class C {}; }; };",
2836 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2837 hasAncestor(recordDecl(hasName("A"))))))));
2838}
2839
2840TEST(HasAncenstor, MatchesStatementAncestors) {
2841 EXPECT_TRUE(matches(
2842 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002843 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002844}
2845
2846TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2847 EXPECT_TRUE(matches(
2848 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002849 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002850}
2851
2852TEST(HasAncestor, BindsRecursiveCombinations) {
2853 EXPECT_TRUE(matchAndVerifyResultTrue(
2854 "class C { class D { class E { class F { int y; }; }; }; };",
2855 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002856 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00002857}
2858
2859TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
2860 EXPECT_TRUE(matchAndVerifyResultTrue(
2861 "class C { class D { class E { class F { int y; }; }; }; };",
2862 fieldDecl(hasAncestor(
2863 decl(
2864 hasDescendant(recordDecl(isDefinition(),
2865 hasAncestor(recordDecl())))
2866 ).bind("d")
2867 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00002868 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00002869}
2870
2871TEST(HasAncestor, MatchesInTemplateInstantiations) {
2872 EXPECT_TRUE(matches(
2873 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
2874 "A<int>::B::C a;",
2875 fieldDecl(hasType(asString("int")),
2876 hasAncestor(recordDecl(hasName("A"))))));
2877}
2878
2879TEST(HasAncestor, MatchesInImplicitCode) {
2880 EXPECT_TRUE(matches(
2881 "struct X {}; struct A { A() {} X x; };",
2882 constructorDecl(
2883 hasAnyConstructorInitializer(withInitializer(expr(
2884 hasAncestor(recordDecl(hasName("A")))))))));
2885}
2886
Daniel Jaspera7564432012-09-13 13:11:25 +00002887TEST(NNS, MatchesNestedNameSpecifiers) {
2888 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
2889 nestedNameSpecifier()));
2890 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
2891 nestedNameSpecifier()));
2892 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
2893 nestedNameSpecifier()));
2894
2895 EXPECT_TRUE(matches(
2896 "struct A { static void f() {} }; void g() { A::f(); }",
2897 nestedNameSpecifier()));
2898 EXPECT_TRUE(notMatches(
2899 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
2900 nestedNameSpecifier()));
2901}
2902
Daniel Jasperb54b7642012-09-20 14:12:57 +00002903TEST(NullStatement, SimpleCases) {
2904 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
2905 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
2906}
2907
Daniel Jaspera7564432012-09-13 13:11:25 +00002908TEST(NNS, MatchesTypes) {
2909 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
2910 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
2911 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
2912 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
2913 Matcher));
2914 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
2915}
2916
2917TEST(NNS, MatchesNamespaceDecls) {
2918 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
2919 specifiesNamespace(hasName("ns")));
2920 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
2921 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
2922 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
2923}
2924
2925TEST(NNS, BindsNestedNameSpecifiers) {
2926 EXPECT_TRUE(matchAndVerifyResultTrue(
2927 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
2928 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
2929 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
2930}
2931
2932TEST(NNS, BindsNestedNameSpecifierLocs) {
2933 EXPECT_TRUE(matchAndVerifyResultTrue(
2934 "namespace ns { struct B {}; } ns::B b;",
2935 loc(nestedNameSpecifier()).bind("loc"),
2936 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
2937}
2938
2939TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
2940 EXPECT_TRUE(matches(
2941 "struct A { struct B { struct C {}; }; }; A::B::C c;",
2942 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
2943 EXPECT_TRUE(matches(
2944 "struct A { struct B { struct C {}; }; }; A::B::C c;",
2945 nestedNameSpecifierLoc(hasPrefix(loc(
2946 specifiesType(asString("struct A")))))));
2947}
2948
Manuel Klimek4da21662012-07-06 05:48:52 +00002949} // end namespace ast_matchers
2950} // end namespace clang