blob: f3d377b771ffcc41c31f06b10b745edcc4bb0499 [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"
Benjamin Kramer8b9ed712012-12-01 17:22:05 +000011#include "clang/AST/PrettyPrinter.h"
Manuel Klimek4da21662012-07-06 05:48:52 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
Chandler Carruth1050e8b2012-12-04 09:45:34 +000013#include "clang/ASTMatchers/ASTMatchers.h"
Manuel Klimek4da21662012-07-06 05:48:52 +000014#include "clang/Tooling/Tooling.h"
15#include "gtest/gtest.h"
16
17namespace clang {
18namespace ast_matchers {
19
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000020#if GTEST_HAS_DEATH_TEST
Manuel Klimek4da21662012-07-06 05:48:52 +000021TEST(HasNameDeathTest, DiesOnEmptyName) {
22 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000023 DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000024 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
25 }, "");
26}
27
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000028TEST(HasNameDeathTest, DiesOnEmptyPattern) {
29 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000030 DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000031 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
32 }, "");
33}
34
Manuel Klimek4da21662012-07-06 05:48:52 +000035TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
36 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000037 DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000038 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
39 }, "");
40}
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000041#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000042
Manuel Klimek715c9562012-07-25 10:02:02 +000043TEST(Decl, MatchesDeclarations) {
44 EXPECT_TRUE(notMatches("", decl(usingDecl())));
45 EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
46 decl(usingDecl())));
47}
48
Manuel Klimek4da21662012-07-06 05:48:52 +000049TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000050 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +000051 EXPECT_TRUE(matches("typedef int X;", NamedX));
52 EXPECT_TRUE(matches("int X;", NamedX));
53 EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
54 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
55 EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
56 EXPECT_TRUE(matches("namespace X { }", NamedX));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000057 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek4da21662012-07-06 05:48:52 +000058
59 EXPECT_TRUE(notMatches("#define X 1", NamedX));
60}
61
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000062TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000063 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000064 EXPECT_TRUE(matches("typedef int Xa;", NamedX));
65 EXPECT_TRUE(matches("int Xb;", NamedX));
66 EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
67 EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
68 EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
69 EXPECT_TRUE(matches("namespace Xij { }", NamedX));
70 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
71
72 EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
73
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000074 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000075 EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
76 EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
77
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000078 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000079 EXPECT_TRUE(matches("int abc;", Abc));
80 EXPECT_TRUE(matches("int aFOObBARc;", Abc));
81 EXPECT_TRUE(notMatches("int cab;", Abc));
82 EXPECT_TRUE(matches("int cabc;", Abc));
83}
84
Manuel Klimek4da21662012-07-06 05:48:52 +000085TEST(DeclarationMatcher, MatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000086 DeclarationMatcher ClassMatcher(recordDecl());
Manuel Klimeke265c872012-07-10 14:21:30 +000087#if !defined(_MSC_VER)
Manuel Klimek4da21662012-07-06 05:48:52 +000088 EXPECT_FALSE(matches("", ClassMatcher));
Manuel Klimeke265c872012-07-10 14:21:30 +000089#else
90 // Matches class type_info.
91 EXPECT_TRUE(matches("", ClassMatcher));
92#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000093
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000094 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +000095 EXPECT_TRUE(matches("class X;", ClassX));
96 EXPECT_TRUE(matches("class X {};", ClassX));
97 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
98 EXPECT_TRUE(notMatches("", ClassX));
99}
100
101TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000102 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000103
104 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000105 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
106 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek4da21662012-07-06 05:48:52 +0000107 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
108 EXPECT_TRUE(notMatches("", IsDerivedFromX));
109
Daniel Jasper63d88722012-09-12 21:14:15 +0000110 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000111
112 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
113 EXPECT_TRUE(matches("class X {};", IsAX));
114 EXPECT_TRUE(matches("class X;", IsAX));
115 EXPECT_TRUE(notMatches("class Y;", IsAX));
116 EXPECT_TRUE(notMatches("", IsAX));
117
Manuel Klimek4da21662012-07-06 05:48:52 +0000118 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000119 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000120 EXPECT_TRUE(
121 matches("class X {}; class Y : public X {}; class Z : public Y {};",
122 ZIsDerivedFromX));
123 EXPECT_TRUE(
124 matches("class X {};"
125 "template<class T> class Y : public X {};"
126 "class Z : public Y<int> {};", ZIsDerivedFromX));
127 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
128 ZIsDerivedFromX));
129 EXPECT_TRUE(
130 matches("template<class T> class X {}; "
131 "template<class T> class Z : public X<T> {};",
132 ZIsDerivedFromX));
133 EXPECT_TRUE(
134 matches("template<class T, class U=T> class X {}; "
135 "template<class T> class Z : public X<T> {};",
136 ZIsDerivedFromX));
137 EXPECT_TRUE(
138 notMatches("template<class X> class A { class Z : public X {}; };",
139 ZIsDerivedFromX));
140 EXPECT_TRUE(
141 matches("template<class X> class A { public: class Z : public X {}; }; "
142 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
143 EXPECT_TRUE(
144 matches("template <class T> class X {}; "
145 "template<class Y> class A { class Z : public X<Y> {}; };",
146 ZIsDerivedFromX));
147 EXPECT_TRUE(
148 notMatches("template<template<class T> class X> class A { "
149 " class Z : public X<int> {}; };", ZIsDerivedFromX));
150 EXPECT_TRUE(
151 matches("template<template<class T> class X> class A { "
152 " public: class Z : public X<int> {}; }; "
153 "template<class T> class X {}; void y() { A<X>::Z z; }",
154 ZIsDerivedFromX));
155 EXPECT_TRUE(
156 notMatches("template<class X> class A { class Z : public X::D {}; };",
157 ZIsDerivedFromX));
158 EXPECT_TRUE(
159 matches("template<class X> class A { public: "
160 " class Z : public X::D {}; }; "
161 "class Y { public: class X {}; typedef X D; }; "
162 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
163 EXPECT_TRUE(
164 matches("class X {}; typedef X Y; class Z : public Y {};",
165 ZIsDerivedFromX));
166 EXPECT_TRUE(
167 matches("template<class T> class Y { typedef typename T::U X; "
168 " class Z : public X {}; };", ZIsDerivedFromX));
169 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
170 ZIsDerivedFromX));
171 EXPECT_TRUE(
172 notMatches("template<class T> class X {}; "
173 "template<class T> class A { class Z : public X<T>::D {}; };",
174 ZIsDerivedFromX));
175 EXPECT_TRUE(
176 matches("template<class T> class X { public: typedef X<T> D; }; "
177 "template<class T> class A { public: "
178 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
179 ZIsDerivedFromX));
180 EXPECT_TRUE(
181 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
182 ZIsDerivedFromX));
183 EXPECT_TRUE(
184 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
185 ZIsDerivedFromX));
186 EXPECT_TRUE(
187 matches("class X {}; class Y : public X {}; "
188 "typedef Y V; typedef V W; class Z : public W {};",
189 ZIsDerivedFromX));
190 EXPECT_TRUE(
191 matches("template<class T, class U> class X {}; "
192 "template<class T> class A { class Z : public X<T, int> {}; };",
193 ZIsDerivedFromX));
194 EXPECT_TRUE(
195 notMatches("template<class X> class D { typedef X A; typedef A B; "
196 " typedef B C; class Z : public C {}; };",
197 ZIsDerivedFromX));
198 EXPECT_TRUE(
199 matches("class X {}; typedef X A; typedef A B; "
200 "class Z : public B {};", ZIsDerivedFromX));
201 EXPECT_TRUE(
202 matches("class X {}; typedef X A; typedef A B; typedef B C; "
203 "class Z : public C {};", ZIsDerivedFromX));
204 EXPECT_TRUE(
205 matches("class U {}; typedef U X; typedef X V; "
206 "class Z : public V {};", ZIsDerivedFromX));
207 EXPECT_TRUE(
208 matches("class Base {}; typedef Base X; "
209 "class Z : public Base {};", ZIsDerivedFromX));
210 EXPECT_TRUE(
211 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
212 "class Z : public Base {};", ZIsDerivedFromX));
213 EXPECT_TRUE(
214 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
215 "class Z : public Base {};", ZIsDerivedFromX));
216 EXPECT_TRUE(
217 matches("class A {}; typedef A X; typedef A Y; "
218 "class Z : public Y {};", ZIsDerivedFromX));
219 EXPECT_TRUE(
220 notMatches("template <typename T> class Z;"
221 "template <> class Z<void> {};"
222 "template <typename T> class Z : public Z<void> {};",
223 IsDerivedFromX));
224 EXPECT_TRUE(
225 matches("template <typename T> class X;"
226 "template <> class X<void> {};"
227 "template <typename T> class X : public X<void> {};",
228 IsDerivedFromX));
229 EXPECT_TRUE(matches(
230 "class X {};"
231 "template <typename T> class Z;"
232 "template <> class Z<void> {};"
233 "template <typename T> class Z : public Z<void>, public X {};",
234 ZIsDerivedFromX));
235
236 // FIXME: Once we have better matchers for template type matching,
237 // get rid of the Variable(...) matching and match the right template
238 // declarations directly.
239 const char *RecursiveTemplateOneParameter =
240 "class Base1 {}; class Base2 {};"
241 "template <typename T> class Z;"
242 "template <> class Z<void> : public Base1 {};"
243 "template <> class Z<int> : public Base2 {};"
244 "template <> class Z<float> : public Z<void> {};"
245 "template <> class Z<double> : public Z<int> {};"
246 "template <typename T> class Z : public Z<float>, public Z<double> {};"
247 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
248 EXPECT_TRUE(matches(
249 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000250 varDecl(hasName("z_float"),
251 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000252 EXPECT_TRUE(notMatches(
253 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000254 varDecl(hasName("z_float"),
255 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000256 EXPECT_TRUE(matches(
257 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000258 varDecl(hasName("z_char"),
259 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
260 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000261
262 const char *RecursiveTemplateTwoParameters =
263 "class Base1 {}; class Base2 {};"
264 "template <typename T1, typename T2> class Z;"
265 "template <typename T> class Z<void, T> : public Base1 {};"
266 "template <typename T> class Z<int, T> : public Base2 {};"
267 "template <typename T> class Z<float, T> : public Z<void, T> {};"
268 "template <typename T> class Z<double, T> : public Z<int, T> {};"
269 "template <typename T1, typename T2> class Z : "
270 " public Z<float, T2>, public Z<double, T2> {};"
271 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
272 " Z<char, void> z_char; }";
273 EXPECT_TRUE(matches(
274 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000275 varDecl(hasName("z_float"),
276 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000277 EXPECT_TRUE(notMatches(
278 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000279 varDecl(hasName("z_float"),
280 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000281 EXPECT_TRUE(matches(
282 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000283 varDecl(hasName("z_char"),
284 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
285 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000286 EXPECT_TRUE(matches(
287 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000288 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000289 EXPECT_TRUE(notMatches(
290 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000291 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000292
293 EXPECT_TRUE(matches(
294 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000295 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000296}
297
Daniel Jasper08f0c532012-09-18 14:17:42 +0000298TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
299 EXPECT_TRUE(matches(
300 "template <typename T> struct A {"
301 " template <typename T2> struct F {};"
302 "};"
303 "template <typename T> struct B : A<T>::template F<T> {};"
304 "B<int> b;",
305 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
306}
307
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000308TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000309 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000310 EXPECT_TRUE(notMatches("class X;", ClassX));
311 EXPECT_TRUE(notMatches("class X {};", ClassX));
312}
313
314TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000315 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000316 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
317 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
318}
319
320TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
321 EXPECT_TRUE(notMatches("template<typename T> class X { };"
322 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000323 classTemplateDecl(hasName("X"),
324 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000325}
326
327TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
328 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
329 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000330 classTemplateDecl(hasName("X"),
331 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000332}
333
Daniel Jasper6a124492012-07-12 08:50:38 +0000334TEST(AllOf, AllOverloadsWork) {
335 const char Program[] =
336 "struct T { }; int f(int, T*); void g(int x) { T t; f(x, &t); }";
337 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000338 callExpr(allOf(callee(functionDecl(hasName("f"))),
339 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000340 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000341 callExpr(allOf(callee(functionDecl(hasName("f"))),
342 hasArgument(0, declRefExpr(to(varDecl()))),
343 hasArgument(1, hasType(pointsTo(
344 recordDecl(hasName("T")))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000345}
346
Manuel Klimek4da21662012-07-06 05:48:52 +0000347TEST(DeclarationMatcher, MatchAnyOf) {
348 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000349 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000350 EXPECT_TRUE(
351 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
352 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
353 EXPECT_TRUE(
354 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
355 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
356
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000357 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000358 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000359 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
360 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
361
Manuel Klimek4da21662012-07-06 05:48:52 +0000362 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000363 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
364 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000365 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
366 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
367 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
368 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
369 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
370 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
371}
372
373TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000374 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000375 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
376 EXPECT_TRUE(matches("class X {};", HasClassX));
377
378 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000379 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000380 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
381 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
382 EXPECT_TRUE(
383 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
384}
385
386TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
387 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000388 recordDecl(
389 has(recordDecl(
390 has(recordDecl(hasName("X"))),
391 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000392 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000393 has(recordDecl(
394 has(recordDecl(hasName("A"))),
395 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000396 hasName("C"))),
397 hasName("F"));
398
399 EXPECT_TRUE(matches(
400 "class F {"
401 " class Z {"
402 " class X {};"
403 " class Y {};"
404 " };"
405 " class C {"
406 " class A {};"
407 " class B {};"
408 " };"
409 "};", Recursive));
410
411 EXPECT_TRUE(matches(
412 "class F {"
413 " class Z {"
414 " class A {};"
415 " class X {};"
416 " class Y {};"
417 " };"
418 " class C {"
419 " class X {};"
420 " class A {};"
421 " class B {};"
422 " };"
423 "};", Recursive));
424
425 EXPECT_TRUE(matches(
426 "class O1 {"
427 " class O2 {"
428 " class F {"
429 " class Z {"
430 " class A {};"
431 " class X {};"
432 " class Y {};"
433 " };"
434 " class C {"
435 " class X {};"
436 " class A {};"
437 " class B {};"
438 " };"
439 " };"
440 " };"
441 "};", Recursive));
442}
443
444TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
445 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000446 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000447 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000448 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000449 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000450 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000451 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000452 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000453 hasName("Y"))),
454 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000455 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000456 anyOf(
457 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000458 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000459 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000460 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000461 hasName("B")))))),
462 hasName("F")));
463
464 EXPECT_TRUE(matches("class F {};", Recursive));
465 EXPECT_TRUE(matches("class Z {};", Recursive));
466 EXPECT_TRUE(matches("class C {};", Recursive));
467 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
468 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
469 EXPECT_TRUE(
470 matches("class O1 { class O2 {"
471 " class M { class N { class B {}; }; }; "
472 "}; };", Recursive));
473}
474
475TEST(DeclarationMatcher, MatchNot) {
476 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000477 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000478 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000479 unless(hasName("X")));
480 EXPECT_TRUE(notMatches("", NotClassX));
481 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
482 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
483 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
484 EXPECT_TRUE(
485 notMatches("class Y {}; class Z {}; class X : public Y {};",
486 NotClassX));
487
488 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000489 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000490 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000491 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000492 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000493 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000494 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
495 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
496 ClassXHasNotClassY));
497}
498
499TEST(DeclarationMatcher, HasDescendant) {
500 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000501 recordDecl(
502 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000503 hasName("Z"));
504 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
505 EXPECT_TRUE(
506 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
507 EXPECT_TRUE(
508 matches("class Z { class A { class Y { class X {}; }; }; };",
509 ZDescendantClassX));
510 EXPECT_TRUE(
511 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
512 ZDescendantClassX));
513 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
514
515 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000516 recordDecl(
517 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000518 hasName("X"))),
519 hasName("Z"));
520 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
521 ZDescendantClassXHasClassY));
522 EXPECT_TRUE(
523 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
524 ZDescendantClassXHasClassY));
525 EXPECT_TRUE(notMatches(
526 "class Z {"
527 " class A {"
528 " class B {"
529 " class X {"
530 " class C {"
531 " class Y {};"
532 " };"
533 " };"
534 " }; "
535 " };"
536 "};", ZDescendantClassXHasClassY));
537
538 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000539 recordDecl(
540 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
541 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000542 hasName("Z"));
543 EXPECT_TRUE(
544 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
545 ZDescendantClassXDescendantClassY));
546 EXPECT_TRUE(matches(
547 "class Z {"
548 " class A {"
549 " class X {"
550 " class B {"
551 " class Y {};"
552 " };"
553 " class Y {};"
554 " };"
555 " };"
556 "};", ZDescendantClassXDescendantClassY));
557}
558
Daniel Jaspera267cf62012-10-29 10:14:44 +0000559// Implements a run method that returns whether BoundNodes contains a
560// Decl bound to Id that can be dynamically cast to T.
561// Optionally checks that the check succeeded a specific number of times.
562template <typename T>
563class VerifyIdIsBoundTo : public BoundNodesCallback {
564public:
565 // Create an object that checks that a node of type \c T was bound to \c Id.
566 // Does not check for a certain number of matches.
567 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
568 : Id(Id), ExpectedCount(-1), Count(0) {}
569
570 // Create an object that checks that a node of type \c T was bound to \c Id.
571 // Checks that there were exactly \c ExpectedCount matches.
572 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
573 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
574
575 // Create an object that checks that a node of type \c T was bound to \c Id.
576 // Checks that there was exactly one match with the name \c ExpectedName.
577 // Note that \c T must be a NamedDecl for this to work.
578 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName)
579 : Id(Id), ExpectedCount(1), Count(0), ExpectedName(ExpectedName) {}
580
581 ~VerifyIdIsBoundTo() {
582 if (ExpectedCount != -1)
583 EXPECT_EQ(ExpectedCount, Count);
584 if (!ExpectedName.empty())
585 EXPECT_EQ(ExpectedName, Name);
586 }
587
588 virtual bool run(const BoundNodes *Nodes) {
589 if (Nodes->getNodeAs<T>(Id)) {
590 ++Count;
591 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
592 Name = Named->getNameAsString();
593 } else if (const NestedNameSpecifier *NNS =
594 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
595 llvm::raw_string_ostream OS(Name);
596 NNS->print(OS, PrintingPolicy(LangOptions()));
597 }
598 return true;
599 }
600 return false;
601 }
602
Daniel Jasper452abbc2012-10-29 10:48:25 +0000603 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
604 return run(Nodes);
605 }
606
Daniel Jaspera267cf62012-10-29 10:14:44 +0000607private:
608 const std::string Id;
609 const int ExpectedCount;
610 int Count;
611 const std::string ExpectedName;
612 std::string Name;
613};
614
615TEST(HasDescendant, MatchesDescendantTypes) {
616 EXPECT_TRUE(matches("void f() { int i = 3; }",
617 decl(hasDescendant(loc(builtinType())))));
618 EXPECT_TRUE(matches("void f() { int i = 3; }",
619 stmt(hasDescendant(builtinType()))));
620
621 EXPECT_TRUE(matches("void f() { int i = 3; }",
622 stmt(hasDescendant(loc(builtinType())))));
623 EXPECT_TRUE(matches("void f() { int i = 3; }",
624 stmt(hasDescendant(qualType(builtinType())))));
625
626 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
627 stmt(hasDescendant(isInteger()))));
628
629 EXPECT_TRUE(matchAndVerifyResultTrue(
630 "void f() { int a; float c; int d; int e; }",
631 functionDecl(forEachDescendant(
632 varDecl(hasDescendant(isInteger())).bind("x"))),
633 new VerifyIdIsBoundTo<Decl>("x", 3)));
634}
635
636TEST(HasDescendant, MatchesDescendantsOfTypes) {
637 EXPECT_TRUE(matches("void f() { int*** i; }",
638 qualType(hasDescendant(builtinType()))));
639 EXPECT_TRUE(matches("void f() { int*** i; }",
640 qualType(hasDescendant(
641 pointerType(pointee(builtinType()))))));
642 EXPECT_TRUE(matches("void f() { int*** i; }",
643 typeLoc(hasDescendant(builtinTypeLoc()))));
644
645 EXPECT_TRUE(matchAndVerifyResultTrue(
646 "void f() { int*** i; }",
647 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
648 new VerifyIdIsBoundTo<Type>("x", 2)));
649}
650
651TEST(Has, MatchesChildrenOfTypes) {
652 EXPECT_TRUE(matches("int i;",
653 varDecl(hasName("i"), has(isInteger()))));
654 EXPECT_TRUE(notMatches("int** i;",
655 varDecl(hasName("i"), has(isInteger()))));
656 EXPECT_TRUE(matchAndVerifyResultTrue(
657 "int (*f)(float, int);",
658 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
659 new VerifyIdIsBoundTo<QualType>("x", 2)));
660}
661
662TEST(Has, MatchesChildTypes) {
663 EXPECT_TRUE(matches(
664 "int* i;",
665 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
666 EXPECT_TRUE(notMatches(
667 "int* i;",
668 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
669}
670
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000671TEST(Enum, DoesNotMatchClasses) {
672 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
673}
674
675TEST(Enum, MatchesEnums) {
676 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
677}
678
679TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000680 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000681 EXPECT_TRUE(matches("enum X{ A };", Matcher));
682 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
683 EXPECT_TRUE(notMatches("enum X {};", Matcher));
684}
685
Manuel Klimek4da21662012-07-06 05:48:52 +0000686TEST(StatementMatcher, Has) {
687 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000688 expr(hasType(pointsTo(recordDecl(hasName("X")))),
689 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000690
691 EXPECT_TRUE(matches(
692 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
693 EXPECT_TRUE(notMatches(
694 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
695}
696
697TEST(StatementMatcher, HasDescendant) {
698 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000699 expr(hasType(pointsTo(recordDecl(hasName("X")))),
700 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000701
702 EXPECT_TRUE(matches(
703 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
704 HasDescendantVariableI));
705 EXPECT_TRUE(notMatches(
706 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
707 HasDescendantVariableI));
708}
709
710TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000711 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000712
713 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
714 EXPECT_TRUE(notMatches("class A {};", TypeA));
715
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000716 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000717
718 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
719 TypeDerivedFromA));
720 EXPECT_TRUE(notMatches("class A {};", TypeA));
721
722 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000723 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000724
725 EXPECT_TRUE(
726 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
727}
728
Manuel Klimek4da21662012-07-06 05:48:52 +0000729TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000730 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000731
732 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000733 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000734
735 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000736 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000737
738 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000739 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000740
741 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
742 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000743 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000744
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000745 StatementMatcher MethodX =
746 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000747
748 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
749 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000750 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000751}
752
753TEST(Matcher, BindTheSameNameInAlternatives) {
754 StatementMatcher matcher = anyOf(
755 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000756 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000757 hasRHS(integerLiteral(equals(0)))),
758 binaryOperator(hasOperatorName("+"),
759 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000760 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000761
762 EXPECT_TRUE(matchAndVerifyResultTrue(
763 // The first branch of the matcher binds x to 0 but then fails.
764 // The second branch binds x to f() and succeeds.
765 "int f() { return 0 + f(); }",
766 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000767 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000768}
769
Manuel Klimek66341c52012-08-30 19:41:06 +0000770TEST(Matcher, BindsIDForMemoizedResults) {
771 // Using the same matcher in two match expressions will make memoization
772 // kick in.
773 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
774 EXPECT_TRUE(matchAndVerifyResultTrue(
775 "class A { class B { class X {}; }; };",
776 DeclarationMatcher(anyOf(
777 recordDecl(hasName("A"), hasDescendant(ClassX)),
778 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000779 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000780}
781
Daniel Jasper189f2e42012-12-03 15:43:25 +0000782TEST(HasDeclaration, HasDeclarationOfEnumType) {
783 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
784 expr(hasType(pointsTo(
785 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
786}
787
Manuel Klimek4da21662012-07-06 05:48:52 +0000788TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000789 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000790 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000791 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000792 EXPECT_TRUE(
793 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000794 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000795 EXPECT_TRUE(
796 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000797 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000798}
799
800TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000801 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000802 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000803 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000804 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000805 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000806 EXPECT_TRUE(
807 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000808 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000809}
810
811TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000812 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000813 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000814 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000815 EXPECT_TRUE(
816 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000817 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000818}
819
820TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000821 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000822 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000823 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000824 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000825 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000826}
827
828TEST(Matcher, Call) {
829 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000830 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000831 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000832
833 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
834 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
835
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000836 StatementMatcher MethodOnY =
837 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000838
839 EXPECT_TRUE(
840 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
841 MethodOnY));
842 EXPECT_TRUE(
843 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
844 MethodOnY));
845 EXPECT_TRUE(
846 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
847 MethodOnY));
848 EXPECT_TRUE(
849 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
850 MethodOnY));
851 EXPECT_TRUE(
852 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
853 MethodOnY));
854
855 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000856 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000857
858 EXPECT_TRUE(
859 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
860 MethodOnYPointer));
861 EXPECT_TRUE(
862 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
863 MethodOnYPointer));
864 EXPECT_TRUE(
865 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
866 MethodOnYPointer));
867 EXPECT_TRUE(
868 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
869 MethodOnYPointer));
870 EXPECT_TRUE(
871 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
872 MethodOnYPointer));
873}
874
Daniel Jasper31f7c082012-10-01 13:40:41 +0000875TEST(Matcher, Lambda) {
876 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
877 lambdaExpr()));
878}
879
880TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000881 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
882 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000883 forRangeStmt()));
884 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
885 forRangeStmt()));
886}
887
888TEST(Matcher, UserDefinedLiteral) {
889 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
890 " return i + 1;"
891 "}"
892 "char c = 'a'_inc;",
893 userDefinedLiteral()));
894}
895
Daniel Jasperb54b7642012-09-20 14:12:57 +0000896TEST(Matcher, FlowControl) {
897 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
898 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
899 continueStmt()));
900 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
901 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
902 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
903}
904
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000905TEST(HasType, MatchesAsString) {
906 EXPECT_TRUE(
907 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000908 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000909 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000910 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000911 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000912 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000913 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000914 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000915}
916
Manuel Klimek4da21662012-07-06 05:48:52 +0000917TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000918 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000919 // Unary operator
920 EXPECT_TRUE(matches("class Y { }; "
921 "bool operator!(Y x) { return false; }; "
922 "Y y; bool c = !y;", OpCall));
923 // No match -- special operators like "new", "delete"
924 // FIXME: operator new takes size_t, for which we need stddef.h, for which
925 // we need to figure out include paths in the test.
926 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
927 // "class Y { }; "
928 // "void *operator new(size_t size) { return 0; } "
929 // "Y *y = new Y;", OpCall));
930 EXPECT_TRUE(notMatches("class Y { }; "
931 "void operator delete(void *p) { } "
932 "void a() {Y *y = new Y; delete y;}", OpCall));
933 // Binary operator
934 EXPECT_TRUE(matches("class Y { }; "
935 "bool operator&&(Y x, Y y) { return true; }; "
936 "Y a; Y b; bool c = a && b;",
937 OpCall));
938 // No match -- normal operator, not an overloaded one.
939 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
940 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
941}
942
943TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
944 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000945 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000946 EXPECT_TRUE(matches("class Y { }; "
947 "bool operator&&(Y x, Y y) { return true; }; "
948 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
949 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000950 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000951 EXPECT_TRUE(notMatches("class Y { }; "
952 "bool operator&&(Y x, Y y) { return true; }; "
953 "Y a; Y b; bool c = a && b;",
954 OpCallLessLess));
955}
956
Daniel Jasper278057f2012-11-15 03:29:05 +0000957TEST(Matcher, NestedOverloadedOperatorCalls) {
958 EXPECT_TRUE(matchAndVerifyResultTrue(
959 "class Y { }; "
960 "Y& operator&&(Y& x, Y& y) { return x; }; "
961 "Y a; Y b; Y c; Y d = a && b && c;",
962 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
963 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
964 EXPECT_TRUE(matches(
965 "class Y { }; "
966 "Y& operator&&(Y& x, Y& y) { return x; }; "
967 "Y a; Y b; Y c; Y d = a && b && c;",
968 operatorCallExpr(hasParent(operatorCallExpr()))));
969 EXPECT_TRUE(matches(
970 "class Y { }; "
971 "Y& operator&&(Y& x, Y& y) { return x; }; "
972 "Y a; Y b; Y c; Y d = a && b && c;",
973 operatorCallExpr(hasDescendant(operatorCallExpr()))));
974}
975
Manuel Klimek4da21662012-07-06 05:48:52 +0000976TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +0000977 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000978 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000979
980 EXPECT_TRUE(
981 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
982 MethodOnY));
983 EXPECT_TRUE(
984 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
985 MethodOnY));
986 EXPECT_TRUE(
987 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
988 MethodOnY));
989 EXPECT_TRUE(
990 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
991 MethodOnY));
992 EXPECT_TRUE(
993 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
994 MethodOnY));
995
996 EXPECT_TRUE(matches(
997 "class Y {"
998 " public: virtual void x();"
999 "};"
1000 "class X : public Y {"
1001 " public: virtual void x();"
1002 "};"
1003 "void z() { X *x; x->Y::x(); }", MethodOnY));
1004}
1005
1006TEST(Matcher, VariableUsage) {
1007 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001008 declRefExpr(to(
1009 varDecl(hasInitializer(
1010 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001011
1012 EXPECT_TRUE(matches(
1013 "class Y {"
1014 " public:"
1015 " bool x() const;"
1016 "};"
1017 "void z(const Y &y) {"
1018 " bool b = y.x();"
1019 " if (b) {}"
1020 "}", Reference));
1021
1022 EXPECT_TRUE(notMatches(
1023 "class Y {"
1024 " public:"
1025 " bool x() const;"
1026 "};"
1027 "void z(const Y &y) {"
1028 " bool b = y.x();"
1029 "}", Reference));
1030}
1031
Daniel Jasper9bd28092012-07-30 05:03:25 +00001032TEST(Matcher, FindsVarDeclInFuncitonParameter) {
1033 EXPECT_TRUE(matches(
1034 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001035 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001036}
1037
Manuel Klimek4da21662012-07-06 05:48:52 +00001038TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001039 StatementMatcher CallOnVariableY =
1040 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001041
1042 EXPECT_TRUE(matches(
1043 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1044 EXPECT_TRUE(matches(
1045 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1046 EXPECT_TRUE(matches(
1047 "class Y { public: void x(); };"
1048 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1049 EXPECT_TRUE(matches(
1050 "class Y { public: void x(); };"
1051 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1052 EXPECT_TRUE(notMatches(
1053 "class Y { public: void x(); };"
1054 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1055 CallOnVariableY));
1056}
1057
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001058TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1059 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1060 unaryExprOrTypeTraitExpr()));
1061 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1062 alignOfExpr(anything())));
1063 // FIXME: Uncomment once alignof is enabled.
1064 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1065 // unaryExprOrTypeTraitExpr()));
1066 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1067 // sizeOfExpr()));
1068}
1069
1070TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1071 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1072 hasArgumentOfType(asString("int")))));
1073 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1074 hasArgumentOfType(asString("float")))));
1075 EXPECT_TRUE(matches(
1076 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001077 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001078 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001079 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001080}
1081
Manuel Klimek4da21662012-07-06 05:48:52 +00001082TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001083 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001084}
1085
1086TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001087 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001088}
1089
1090TEST(MemberExpression, MatchesVariable) {
1091 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001092 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001093 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001094 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001095 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001096 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001097}
1098
1099TEST(MemberExpression, MatchesStaticVariable) {
1100 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001101 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001102 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001103 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001104 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001105 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001106}
1107
Daniel Jasper6a124492012-07-12 08:50:38 +00001108TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001109 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1110 EXPECT_TRUE(matches(
1111 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1112 callExpr(hasArgument(0, declRefExpr(
1113 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001114}
1115
1116TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001117 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001118 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001119 callExpr(hasArgument(0, declRefExpr(
1120 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001121}
1122
Manuel Klimek4da21662012-07-06 05:48:52 +00001123TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1124 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001125 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001126 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001127 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001128 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001129 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001130}
1131
1132TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1133 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001134 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001135 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001136 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001137 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001138 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001139}
1140
1141TEST(IsArrow, MatchesMemberCallsViaArrow) {
1142 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001143 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001144 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001145 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001146 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001147 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001148}
1149
1150TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001151 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001152
1153 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1154 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1155}
1156
1157TEST(Callee, MatchesMemberExpressions) {
1158 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001159 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001160 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001161 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001162}
1163
1164TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001165 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001166
1167 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1168 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1169
Manuel Klimeke265c872012-07-10 14:21:30 +00001170#if !defined(_MSC_VER)
1171 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001172 // Dependent contexts, but a non-dependent call.
1173 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1174 CallFunctionF));
1175 EXPECT_TRUE(
1176 matches("void f(); template <int N> struct S { void g() { f(); } };",
1177 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001178#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001179
1180 // Depedent calls don't match.
1181 EXPECT_TRUE(
1182 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1183 CallFunctionF));
1184 EXPECT_TRUE(
1185 notMatches("void f(int);"
1186 "template <typename T> struct S { void g(T t) { f(t); } };",
1187 CallFunctionF));
1188}
1189
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001190TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1191 EXPECT_TRUE(
1192 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001193 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001194}
1195
1196TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1197 EXPECT_TRUE(
1198 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001199 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001200}
1201
1202TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1203 EXPECT_TRUE(
1204 notMatches("void g(); template <typename T> void f(T t) {}"
1205 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001206 functionTemplateDecl(hasName("f"),
1207 hasDescendant(declRefExpr(to(
1208 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001209}
1210
Manuel Klimek4da21662012-07-06 05:48:52 +00001211TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001212 StatementMatcher CallArgumentY = callExpr(
1213 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001214
1215 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1216 EXPECT_TRUE(
1217 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1218 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1219
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001220 StatementMatcher WrongIndex = callExpr(
1221 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001222 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1223}
1224
1225TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001226 StatementMatcher CallArgumentY = callExpr(
1227 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001228 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1229 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1230 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1231}
1232
1233TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001234 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001235
1236 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1237 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1238 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1239}
1240
Daniel Jasper36e29d62012-12-04 11:54:27 +00001241TEST(Matcher, ParameterCount) {
1242 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1243 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1244 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1245 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1246 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1247}
1248
Manuel Klimek4da21662012-07-06 05:48:52 +00001249TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001250 DeclarationMatcher ReferenceClassX = varDecl(
1251 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001252 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1253 ReferenceClassX));
1254 EXPECT_TRUE(
1255 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1256 EXPECT_TRUE(
1257 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1258 EXPECT_TRUE(
1259 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1260}
1261
1262TEST(HasParameter, CallsInnerMatcher) {
1263 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001264 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001265 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001266 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001267}
1268
1269TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1270 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001271 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001272}
1273
1274TEST(HasType, MatchesParameterVariableTypesStrictly) {
1275 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001276 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001277 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001278 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001279 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001280 methodDecl(hasParameter(0,
1281 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001282 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001283 methodDecl(hasParameter(0,
1284 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001285}
1286
1287TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1288 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001289 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001290 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001291 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001292}
1293
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001294TEST(Returns, MatchesReturnTypes) {
1295 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001296 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001297 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001298 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001299 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001300 functionDecl(returns(hasDeclaration(
1301 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001302}
1303
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001304TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001305 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1306 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1307 functionDecl(isExternC())));
1308 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001309}
1310
Manuel Klimek4da21662012-07-06 05:48:52 +00001311TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1312 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001313 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001314}
1315
1316TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1317 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001318 methodDecl(hasAnyParameter(hasType(pointsTo(
1319 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001320}
1321
1322TEST(HasName, MatchesParameterVariableDeclartions) {
1323 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001324 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001325 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001326 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001327}
1328
Daniel Jasper371f9392012-08-01 08:40:24 +00001329TEST(Matcher, MatchesClassTemplateSpecialization) {
1330 EXPECT_TRUE(matches("template<typename T> struct A {};"
1331 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001332 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001333 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001334 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001335 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001336 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001337}
1338
1339TEST(Matcher, MatchesTypeTemplateArgument) {
1340 EXPECT_TRUE(matches(
1341 "template<typename T> struct B {};"
1342 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001343 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001344 asString("int"))))));
1345}
1346
1347TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1348 EXPECT_TRUE(matches(
1349 "struct B { int next; };"
1350 "template<int(B::*next_ptr)> struct A {};"
1351 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001352 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1353 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001354
1355 EXPECT_TRUE(notMatches(
1356 "template <typename T> struct A {};"
1357 "A<int> a;",
1358 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1359 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001360}
1361
1362TEST(Matcher, MatchesSpecificArgument) {
1363 EXPECT_TRUE(matches(
1364 "template<typename T, typename U> class A {};"
1365 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001366 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001367 1, refersToType(asString("int"))))));
1368 EXPECT_TRUE(notMatches(
1369 "template<typename T, typename U> class A {};"
1370 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001371 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001372 1, refersToType(asString("int"))))));
1373}
1374
Manuel Klimek4da21662012-07-06 05:48:52 +00001375TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001376 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001377
1378 EXPECT_TRUE(
1379 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1380 EXPECT_TRUE(
1381 matches("class X { public: X(); }; void x() { X x = X(); }",
1382 Constructor));
1383 EXPECT_TRUE(
1384 matches("class X { public: X(int); }; void x() { X x = 0; }",
1385 Constructor));
1386 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1387}
1388
1389TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001390 StatementMatcher Constructor = constructExpr(
1391 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001392
1393 EXPECT_TRUE(
1394 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1395 Constructor));
1396 EXPECT_TRUE(
1397 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1398 Constructor));
1399 EXPECT_TRUE(
1400 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1401 Constructor));
1402 EXPECT_TRUE(
1403 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1404 Constructor));
1405
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001406 StatementMatcher WrongIndex = constructExpr(
1407 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001408 EXPECT_TRUE(
1409 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1410 WrongIndex));
1411}
1412
1413TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001414 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001415
1416 EXPECT_TRUE(
1417 matches("class X { public: X(int); }; void x() { X x(0); }",
1418 Constructor1Arg));
1419 EXPECT_TRUE(
1420 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1421 Constructor1Arg));
1422 EXPECT_TRUE(
1423 matches("class X { public: X(int); }; void x() { X x = 0; }",
1424 Constructor1Arg));
1425 EXPECT_TRUE(
1426 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1427 Constructor1Arg));
1428}
1429
Manuel Klimek70b9db92012-10-23 10:40:50 +00001430TEST(Matcher,ThisExpr) {
1431 EXPECT_TRUE(
1432 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1433 EXPECT_TRUE(
1434 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1435}
1436
Manuel Klimek4da21662012-07-06 05:48:52 +00001437TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001438 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001439
1440 std::string ClassString = "class string { public: string(); ~string(); }; ";
1441
1442 EXPECT_TRUE(
1443 matches(ClassString +
1444 "string GetStringByValue();"
1445 "void FunctionTakesString(string s);"
1446 "void run() { FunctionTakesString(GetStringByValue()); }",
1447 TempExpression));
1448
1449 EXPECT_TRUE(
1450 notMatches(ClassString +
1451 "string* GetStringPointer(); "
1452 "void FunctionTakesStringPtr(string* s);"
1453 "void run() {"
1454 " string* s = GetStringPointer();"
1455 " FunctionTakesStringPtr(GetStringPointer());"
1456 " FunctionTakesStringPtr(s);"
1457 "}",
1458 TempExpression));
1459
1460 EXPECT_TRUE(
1461 notMatches("class no_dtor {};"
1462 "no_dtor GetObjByValue();"
1463 "void ConsumeObj(no_dtor param);"
1464 "void run() { ConsumeObj(GetObjByValue()); }",
1465 TempExpression));
1466}
1467
Sam Panzere16acd32012-08-24 22:04:44 +00001468TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1469 std::string ClassString =
1470 "class string { public: string(); int length(); }; ";
1471
1472 EXPECT_TRUE(
1473 matches(ClassString +
1474 "string GetStringByValue();"
1475 "void FunctionTakesString(string s);"
1476 "void run() { FunctionTakesString(GetStringByValue()); }",
1477 materializeTemporaryExpr()));
1478
1479 EXPECT_TRUE(
1480 notMatches(ClassString +
1481 "string* GetStringPointer(); "
1482 "void FunctionTakesStringPtr(string* s);"
1483 "void run() {"
1484 " string* s = GetStringPointer();"
1485 " FunctionTakesStringPtr(GetStringPointer());"
1486 " FunctionTakesStringPtr(s);"
1487 "}",
1488 materializeTemporaryExpr()));
1489
1490 EXPECT_TRUE(
1491 notMatches(ClassString +
1492 "string GetStringByValue();"
1493 "void run() { int k = GetStringByValue().length(); }",
1494 materializeTemporaryExpr()));
1495
1496 EXPECT_TRUE(
1497 notMatches(ClassString +
1498 "string GetStringByValue();"
1499 "void run() { GetStringByValue(); }",
1500 materializeTemporaryExpr()));
1501}
1502
Manuel Klimek4da21662012-07-06 05:48:52 +00001503TEST(ConstructorDeclaration, SimpleCase) {
1504 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001505 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001506 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001507 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001508}
1509
1510TEST(ConstructorDeclaration, IsImplicit) {
1511 // This one doesn't match because the constructor is not added by the
1512 // compiler (it is not needed).
1513 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001514 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001515 // The compiler added the implicit default constructor.
1516 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001517 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001518 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001519 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001520}
1521
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001522TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1523 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001524 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001525}
1526
1527TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001528 EXPECT_TRUE(notMatches("class Foo {};",
1529 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001530}
1531
Manuel Klimek4da21662012-07-06 05:48:52 +00001532TEST(HasAnyConstructorInitializer, SimpleCase) {
1533 EXPECT_TRUE(notMatches(
1534 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001535 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001536 EXPECT_TRUE(matches(
1537 "class Foo {"
1538 " Foo() : foo_() { }"
1539 " int foo_;"
1540 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001541 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001542}
1543
1544TEST(HasAnyConstructorInitializer, ForField) {
1545 static const char Code[] =
1546 "class Baz { };"
1547 "class Foo {"
1548 " Foo() : foo_() { }"
1549 " Baz foo_;"
1550 " Baz bar_;"
1551 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001552 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1553 forField(hasType(recordDecl(hasName("Baz"))))))));
1554 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001555 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001556 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1557 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001558}
1559
1560TEST(HasAnyConstructorInitializer, WithInitializer) {
1561 static const char Code[] =
1562 "class Foo {"
1563 " Foo() : foo_(0) { }"
1564 " int foo_;"
1565 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001566 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001567 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001568 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001569 withInitializer(integerLiteral(equals(1)))))));
1570}
1571
1572TEST(HasAnyConstructorInitializer, IsWritten) {
1573 static const char Code[] =
1574 "struct Bar { Bar(){} };"
1575 "class Foo {"
1576 " Foo() : foo_() { }"
1577 " Bar foo_;"
1578 " Bar bar_;"
1579 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001580 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001581 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001582 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001583 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001584 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001585 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1586}
1587
1588TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001589 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001590
1591 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1592 EXPECT_TRUE(
1593 matches("class X { public: X(); }; void x() { new X(); }", New));
1594 EXPECT_TRUE(
1595 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1596 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1597}
1598
1599TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001600 StatementMatcher New = constructExpr(
1601 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001602
1603 EXPECT_TRUE(
1604 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1605 New));
1606 EXPECT_TRUE(
1607 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1608 New));
1609 EXPECT_TRUE(
1610 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1611 New));
1612
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001613 StatementMatcher WrongIndex = constructExpr(
1614 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001615 EXPECT_TRUE(
1616 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1617 WrongIndex));
1618}
1619
1620TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001621 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001622
1623 EXPECT_TRUE(
1624 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1625 EXPECT_TRUE(
1626 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1627 New));
1628}
1629
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001630TEST(Matcher, DeleteExpression) {
1631 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001632 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001633}
1634
Manuel Klimek4da21662012-07-06 05:48:52 +00001635TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001636 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001637
1638 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1639 EXPECT_TRUE(
1640 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1641 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1642}
1643
1644TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001645 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001646 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1647 // wide string
1648 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1649 // with escaped characters
1650 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1651 // no matching -- though the data type is the same, there is no string literal
1652 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1653}
1654
1655TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001656 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001657 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1658 // wide character
1659 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1660 // wide character, Hex encoded, NOT MATCHED!
1661 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1662 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1663}
1664
1665TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001666 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001667 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1668 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1669 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1670 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1671
1672 // Non-matching cases (character literals, float and double)
1673 EXPECT_TRUE(notMatches("int i = L'a';",
1674 HasIntLiteral)); // this is actually a character
1675 // literal cast to int
1676 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1677 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1678 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1679}
1680
Daniel Jasper31f7c082012-10-01 13:40:41 +00001681TEST(Matcher, NullPtrLiteral) {
1682 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1683}
1684
Daniel Jasperb54b7642012-09-20 14:12:57 +00001685TEST(Matcher, AsmStatement) {
1686 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1687}
1688
Manuel Klimek4da21662012-07-06 05:48:52 +00001689TEST(Matcher, Conditions) {
1690 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1691
1692 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1693 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1694 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1695 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1696 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1697}
1698
1699TEST(MatchBinaryOperator, HasOperatorName) {
1700 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1701
1702 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1703 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1704}
1705
1706TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1707 StatementMatcher OperatorTrueFalse =
1708 binaryOperator(hasLHS(boolLiteral(equals(true))),
1709 hasRHS(boolLiteral(equals(false))));
1710
1711 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1712 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1713 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1714}
1715
1716TEST(MatchBinaryOperator, HasEitherOperand) {
1717 StatementMatcher HasOperand =
1718 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1719
1720 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1721 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1722 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1723}
1724
1725TEST(Matcher, BinaryOperatorTypes) {
1726 // Integration test that verifies the AST provides all binary operators in
1727 // a way we expect.
1728 // FIXME: Operator ','
1729 EXPECT_TRUE(
1730 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1731 EXPECT_TRUE(
1732 matches("bool b; bool c = (b = true);",
1733 binaryOperator(hasOperatorName("="))));
1734 EXPECT_TRUE(
1735 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1736 EXPECT_TRUE(
1737 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1738 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1739 EXPECT_TRUE(
1740 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1741 EXPECT_TRUE(
1742 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1743 EXPECT_TRUE(
1744 matches("int i = 1; int j = (i <<= 2);",
1745 binaryOperator(hasOperatorName("<<="))));
1746 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1747 EXPECT_TRUE(
1748 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1749 EXPECT_TRUE(
1750 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1751 EXPECT_TRUE(
1752 matches("int i = 1; int j = (i >>= 2);",
1753 binaryOperator(hasOperatorName(">>="))));
1754 EXPECT_TRUE(
1755 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1756 EXPECT_TRUE(
1757 matches("int i = 42; int j = (i ^= 42);",
1758 binaryOperator(hasOperatorName("^="))));
1759 EXPECT_TRUE(
1760 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1761 EXPECT_TRUE(
1762 matches("int i = 42; int j = (i %= 42);",
1763 binaryOperator(hasOperatorName("%="))));
1764 EXPECT_TRUE(
1765 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1766 EXPECT_TRUE(
1767 matches("bool b = true && false;",
1768 binaryOperator(hasOperatorName("&&"))));
1769 EXPECT_TRUE(
1770 matches("bool b = true; bool c = (b &= false);",
1771 binaryOperator(hasOperatorName("&="))));
1772 EXPECT_TRUE(
1773 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1774 EXPECT_TRUE(
1775 matches("bool b = true || false;",
1776 binaryOperator(hasOperatorName("||"))));
1777 EXPECT_TRUE(
1778 matches("bool b = true; bool c = (b |= false);",
1779 binaryOperator(hasOperatorName("|="))));
1780 EXPECT_TRUE(
1781 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1782 EXPECT_TRUE(
1783 matches("int i = 42; int j = (i *= 23);",
1784 binaryOperator(hasOperatorName("*="))));
1785 EXPECT_TRUE(
1786 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1787 EXPECT_TRUE(
1788 matches("int i = 42; int j = (i /= 23);",
1789 binaryOperator(hasOperatorName("/="))));
1790 EXPECT_TRUE(
1791 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1792 EXPECT_TRUE(
1793 matches("int i = 42; int j = (i += 23);",
1794 binaryOperator(hasOperatorName("+="))));
1795 EXPECT_TRUE(
1796 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1797 EXPECT_TRUE(
1798 matches("int i = 42; int j = (i -= 23);",
1799 binaryOperator(hasOperatorName("-="))));
1800 EXPECT_TRUE(
1801 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1802 binaryOperator(hasOperatorName("->*"))));
1803 EXPECT_TRUE(
1804 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1805 binaryOperator(hasOperatorName(".*"))));
1806
1807 // Member expressions as operators are not supported in matches.
1808 EXPECT_TRUE(
1809 notMatches("struct A { void x(A *a) { a->x(this); } };",
1810 binaryOperator(hasOperatorName("->"))));
1811
1812 // Initializer assignments are not represented as operator equals.
1813 EXPECT_TRUE(
1814 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1815
1816 // Array indexing is not represented as operator.
1817 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1818
1819 // Overloaded operators do not match at all.
1820 EXPECT_TRUE(notMatches(
1821 "struct A { bool operator&&(const A &a) const { return false; } };"
1822 "void x() { A a, b; a && b; }",
1823 binaryOperator()));
1824}
1825
1826TEST(MatchUnaryOperator, HasOperatorName) {
1827 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1828
1829 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1830 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1831}
1832
1833TEST(MatchUnaryOperator, HasUnaryOperand) {
1834 StatementMatcher OperatorOnFalse =
1835 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1836
1837 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1838 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1839}
1840
1841TEST(Matcher, UnaryOperatorTypes) {
1842 // Integration test that verifies the AST provides all unary operators in
1843 // a way we expect.
1844 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1845 EXPECT_TRUE(
1846 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1847 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1848 EXPECT_TRUE(
1849 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1850 EXPECT_TRUE(
1851 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1852 EXPECT_TRUE(
1853 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1854 EXPECT_TRUE(
1855 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1856 EXPECT_TRUE(
1857 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1858 EXPECT_TRUE(
1859 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1860 EXPECT_TRUE(
1861 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1862
1863 // We don't match conversion operators.
1864 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1865
1866 // Function calls are not represented as operator.
1867 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1868
1869 // Overloaded operators do not match at all.
1870 // FIXME: We probably want to add that.
1871 EXPECT_TRUE(notMatches(
1872 "struct A { bool operator!() const { return false; } };"
1873 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1874}
1875
1876TEST(Matcher, ConditionalOperator) {
1877 StatementMatcher Conditional = conditionalOperator(
1878 hasCondition(boolLiteral(equals(true))),
1879 hasTrueExpression(boolLiteral(equals(false))));
1880
1881 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1882 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1883 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1884
1885 StatementMatcher ConditionalFalse = conditionalOperator(
1886 hasFalseExpression(boolLiteral(equals(false))));
1887
1888 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1889 EXPECT_TRUE(
1890 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1891}
1892
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001893TEST(ArraySubscriptMatchers, ArraySubscripts) {
1894 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1895 arraySubscriptExpr()));
1896 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1897 arraySubscriptExpr()));
1898}
1899
1900TEST(ArraySubscriptMatchers, ArrayIndex) {
1901 EXPECT_TRUE(matches(
1902 "int i[2]; void f() { i[1] = 1; }",
1903 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1904 EXPECT_TRUE(matches(
1905 "int i[2]; void f() { 1[i] = 1; }",
1906 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1907 EXPECT_TRUE(notMatches(
1908 "int i[2]; void f() { i[1] = 1; }",
1909 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1910}
1911
1912TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1913 EXPECT_TRUE(matches(
1914 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001915 arraySubscriptExpr(hasBase(implicitCastExpr(
1916 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001917}
1918
Manuel Klimek4da21662012-07-06 05:48:52 +00001919TEST(Matcher, HasNameSupportsNamespaces) {
1920 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001921 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001922 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001923 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001924 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001925 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001926 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001927 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001928 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001929 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001930 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001931 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001932 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001933 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001934 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001935 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001936 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001937 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001938 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001939 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001940 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001941 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001942 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001943 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001944}
1945
1946TEST(Matcher, HasNameSupportsOuterClasses) {
1947 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001948 matches("class A { class B { class C; }; };",
1949 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001950 EXPECT_TRUE(
1951 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001952 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001953 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001954 matches("class A { class B { class C; }; };",
1955 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001956 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001957 matches("class A { class B { class C; }; };",
1958 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001959 EXPECT_TRUE(
1960 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001961 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001962 EXPECT_TRUE(
1963 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001964 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001965 EXPECT_TRUE(
1966 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001967 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001968 EXPECT_TRUE(
1969 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001970 recordDecl(hasName("::C"))));
1971 EXPECT_TRUE(
1972 notMatches("class A { class B { class C; }; };",
1973 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001974 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001975 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001976 EXPECT_TRUE(
1977 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001978 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001979}
1980
1981TEST(Matcher, IsDefinition) {
1982 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001983 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001984 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1985 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1986
1987 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001988 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001989 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1990 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1991
1992 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001993 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001994 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1995 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1996}
1997
1998TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001999 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00002000 ofClass(hasName("X")))));
2001
2002 EXPECT_TRUE(
2003 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2004 EXPECT_TRUE(
2005 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2006 Constructor));
2007 EXPECT_TRUE(
2008 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2009 Constructor));
2010}
2011
2012TEST(Matcher, VisitsTemplateInstantiations) {
2013 EXPECT_TRUE(matches(
2014 "class A { public: void x(); };"
2015 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002016 "void f() { B<A> b; b.y(); }",
2017 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002018
2019 EXPECT_TRUE(matches(
2020 "class A { public: void x(); };"
2021 "class C {"
2022 " public:"
2023 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2024 "};"
2025 "void f() {"
2026 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002027 "}",
2028 recordDecl(hasName("C"),
2029 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002030}
2031
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002032TEST(Matcher, HandlesNullQualTypes) {
2033 // FIXME: Add a Type matcher so we can replace uses of this
2034 // variable with Type(True())
2035 const TypeMatcher AnyType = anything();
2036
2037 // We don't really care whether this matcher succeeds; we're testing that
2038 // it completes without crashing.
2039 EXPECT_TRUE(matches(
2040 "struct A { };"
2041 "template <typename T>"
2042 "void f(T t) {"
2043 " T local_t(t /* this becomes a null QualType in the AST */);"
2044 "}"
2045 "void g() {"
2046 " f(0);"
2047 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002048 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002049 anyOf(
2050 TypeMatcher(hasDeclaration(anything())),
2051 pointsTo(AnyType),
2052 references(AnyType)
2053 // Other QualType matchers should go here.
2054 ))))));
2055}
2056
Manuel Klimek4da21662012-07-06 05:48:52 +00002057// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002058AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002059 // Make sure all special variables are used: node, match_finder,
2060 // bound_nodes_builder, and the parameter named 'AMatcher'.
2061 return AMatcher.matches(Node, Finder, Builder);
2062}
2063
2064TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002065 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002066
2067 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002068 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002069
2070 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002071 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002072
2073 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002074 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002075}
2076
2077AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002078 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
2079 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
2080 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00002081 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00002082 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002083 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002084 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2085 ASTMatchFinder::BK_First);
2086}
2087
2088TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002089 DeclarationMatcher HasClassB =
2090 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002091
2092 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002093 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002094
2095 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002096 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002097
2098 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002099 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002100
2101 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002102 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002103
2104 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2105}
2106
2107TEST(For, FindsForLoops) {
2108 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2109 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002110 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2111 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002112 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002113}
2114
Daniel Jasper6a124492012-07-12 08:50:38 +00002115TEST(For, ForLoopInternals) {
2116 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2117 forStmt(hasCondition(anything()))));
2118 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2119 forStmt(hasLoopInit(anything()))));
2120}
2121
2122TEST(For, NegativeForLoopInternals) {
2123 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002124 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002125 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2126 forStmt(hasLoopInit(anything()))));
2127}
2128
Manuel Klimek4da21662012-07-06 05:48:52 +00002129TEST(For, ReportsNoFalsePositives) {
2130 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2131 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2132}
2133
2134TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002135 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2136 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2137 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002138}
2139
2140TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2141 // It's not a compound statement just because there's "{}" in the source
2142 // text. This is an AST search, not grep.
2143 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002144 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002145 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002146 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002147}
2148
Daniel Jasper6a124492012-07-12 08:50:38 +00002149TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002150 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002151 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002152 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002153 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002154 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002155 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002156 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002157 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002158}
2159
2160TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2161 // The simplest case: every compound statement is in a function
2162 // definition, and the function body itself must be a compound
2163 // statement.
2164 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002165 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002166}
2167
2168TEST(HasAnySubstatement, IsNotRecursive) {
2169 // It's really "has any immediate substatement".
2170 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002171 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002172}
2173
2174TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2175 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002176 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002177}
2178
2179TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2180 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002181 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002182}
2183
2184TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2185 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002186 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002187 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002188 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002189}
2190
2191TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2192 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002193 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002194 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002195 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002196 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002197 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002198}
2199
2200TEST(StatementCountIs, WorksWithMultipleStatements) {
2201 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002202 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002203}
2204
2205TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2206 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002207 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002208 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002209 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002210 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002211 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002212 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002213 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002214}
2215
2216TEST(Member, WorksInSimplestCase) {
2217 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002218 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002219}
2220
2221TEST(Member, DoesNotMatchTheBaseExpression) {
2222 // Don't pick out the wrong part of the member expression, this should
2223 // be checking the member (name) only.
2224 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002225 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002226}
2227
2228TEST(Member, MatchesInMemberFunctionCall) {
2229 EXPECT_TRUE(matches("void f() {"
2230 " struct { void first() {}; } s;"
2231 " s.first();"
2232 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002233 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002234}
2235
Daniel Jasperc711af22012-10-23 15:46:39 +00002236TEST(Member, MatchesMember) {
2237 EXPECT_TRUE(matches(
2238 "struct A { int i; }; void f() { A a; a.i = 2; }",
2239 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2240 EXPECT_TRUE(notMatches(
2241 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2242 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2243}
2244
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002245TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002246 // Fails in C++11 mode
2247 EXPECT_TRUE(matchesConditionally(
2248 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2249 "class X { void *operator new(std::size_t); };",
2250 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002251
2252 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002253 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002254
Daniel Jasper31f7c082012-10-01 13:40:41 +00002255 // Fails in C++11 mode
2256 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002257 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2258 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002259 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002260}
2261
Manuel Klimek4da21662012-07-06 05:48:52 +00002262TEST(HasObjectExpression, DoesNotMatchMember) {
2263 EXPECT_TRUE(notMatches(
2264 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002265 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002266}
2267
2268TEST(HasObjectExpression, MatchesBaseOfVariable) {
2269 EXPECT_TRUE(matches(
2270 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002271 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002272 EXPECT_TRUE(matches(
2273 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002274 memberExpr(hasObjectExpression(
2275 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002276}
2277
2278TEST(HasObjectExpression,
2279 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2280 EXPECT_TRUE(matches(
2281 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002282 memberExpr(hasObjectExpression(
2283 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002284 EXPECT_TRUE(matches(
2285 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002286 memberExpr(hasObjectExpression(
2287 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002288}
2289
2290TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002291 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2292 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2293 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2294 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002295}
2296
2297TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002298 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002299}
2300
2301TEST(IsConstQualified, MatchesConstInt) {
2302 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002303 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002304}
2305
2306TEST(IsConstQualified, MatchesConstPointer) {
2307 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002308 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002309}
2310
2311TEST(IsConstQualified, MatchesThroughTypedef) {
2312 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002313 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002314 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002315 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002316}
2317
2318TEST(IsConstQualified, DoesNotMatchInappropriately) {
2319 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002320 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002321 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002322 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002323}
2324
Sam Panzer089e5b32012-08-16 16:58:10 +00002325TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002326 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2327 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2328 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2329 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002330}
2331TEST(CastExpression, MatchesImplicitCasts) {
2332 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002333 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002334 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002335 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002336}
2337
2338TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002339 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2340 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2341 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2342 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002343}
2344
Manuel Klimek4da21662012-07-06 05:48:52 +00002345TEST(ReinterpretCast, MatchesSimpleCase) {
2346 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002347 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002348}
2349
2350TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002351 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002352 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002353 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002354 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002355 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002356 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2357 "B b;"
2358 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002359 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002360}
2361
2362TEST(FunctionalCast, MatchesSimpleCase) {
2363 std::string foo_class = "class Foo { public: Foo(char*); };";
2364 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002365 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002366}
2367
2368TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2369 std::string FooClass = "class Foo { public: Foo(char*); };";
2370 EXPECT_TRUE(
2371 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002372 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002373 EXPECT_TRUE(
2374 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002375 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002376}
2377
2378TEST(DynamicCast, MatchesSimpleCase) {
2379 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2380 "B b;"
2381 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002382 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002383}
2384
2385TEST(StaticCast, MatchesSimpleCase) {
2386 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002387 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002388}
2389
2390TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002391 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002392 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002393 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002394 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002395 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002396 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2397 "B b;"
2398 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002399 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002400}
2401
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002402TEST(CStyleCast, MatchesSimpleCase) {
2403 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2404}
2405
2406TEST(CStyleCast, DoesNotMatchOtherCasts) {
2407 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2408 "char q, *r = const_cast<char*>(&q);"
2409 "void* s = reinterpret_cast<char*>(&s);"
2410 "struct B { virtual ~B() {} }; struct D : B {};"
2411 "B b;"
2412 "D* t = dynamic_cast<D*>(&b);",
2413 cStyleCastExpr()));
2414}
2415
Manuel Klimek4da21662012-07-06 05:48:52 +00002416TEST(HasDestinationType, MatchesSimpleCase) {
2417 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002418 staticCastExpr(hasDestinationType(
2419 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002420}
2421
Sam Panzer089e5b32012-08-16 16:58:10 +00002422TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2423 // This test creates an implicit const cast.
2424 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002425 implicitCastExpr(
2426 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002427 // This test creates an implicit array-to-pointer cast.
2428 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002429 implicitCastExpr(hasImplicitDestinationType(
2430 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002431}
2432
2433TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2434 // This test creates an implicit cast from int to char.
2435 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002436 implicitCastExpr(hasImplicitDestinationType(
2437 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002438 // This test creates an implicit array-to-pointer cast.
2439 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002440 implicitCastExpr(hasImplicitDestinationType(
2441 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002442}
2443
2444TEST(ImplicitCast, MatchesSimpleCase) {
2445 // This test creates an implicit const cast.
2446 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002447 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002448 // This test creates an implicit cast from int to char.
2449 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002450 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002451 // This test creates an implicit array-to-pointer cast.
2452 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002453 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002454}
2455
2456TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002457 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002458 // are present, and that it ignores explicit and paren casts.
2459
2460 // These two test cases have no casts.
2461 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002462 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002463 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002464 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002465
2466 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002467 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002468 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002469 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002470
2471 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002472 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002473}
2474
2475TEST(IgnoringImpCasts, MatchesImpCasts) {
2476 // This test checks that ignoringImpCasts matches when implicit casts are
2477 // present and its inner matcher alone does not match.
2478 // Note that this test creates an implicit const cast.
2479 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002480 varDecl(hasInitializer(ignoringImpCasts(
2481 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002482 // This test creates an implict cast from int to char.
2483 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002484 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002485 integerLiteral(equals(0)))))));
2486}
2487
2488TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2489 // These tests verify that ignoringImpCasts does not match if the inner
2490 // matcher does not match.
2491 // Note that the first test creates an implicit const cast.
2492 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002493 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002494 unless(anything()))))));
2495 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002496 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002497 unless(anything()))))));
2498
2499 // These tests verify that ignoringImplictCasts does not look through explicit
2500 // casts or parentheses.
2501 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002502 varDecl(hasInitializer(ignoringImpCasts(
2503 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002504 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002505 varDecl(hasInitializer(ignoringImpCasts(
2506 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002507 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002508 varDecl(hasInitializer(ignoringImpCasts(
2509 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002510 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002511 varDecl(hasInitializer(ignoringImpCasts(
2512 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002513}
2514
2515TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2516 // This test verifies that expressions that do not have implicit casts
2517 // still match the inner matcher.
2518 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002519 varDecl(hasInitializer(ignoringImpCasts(
2520 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002521}
2522
2523TEST(IgnoringParenCasts, MatchesParenCasts) {
2524 // This test checks that ignoringParenCasts matches when parentheses and/or
2525 // casts are present and its inner matcher alone does not match.
2526 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002527 varDecl(hasInitializer(ignoringParenCasts(
2528 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002529 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002530 varDecl(hasInitializer(ignoringParenCasts(
2531 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002532
2533 // This test creates an implict cast from int to char in addition to the
2534 // parentheses.
2535 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002536 varDecl(hasInitializer(ignoringParenCasts(
2537 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002538
2539 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002540 varDecl(hasInitializer(ignoringParenCasts(
2541 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002542 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002543 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002544 integerLiteral(equals(0)))))));
2545}
2546
2547TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2548 // This test verifies that expressions that do not have any casts still match.
2549 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002550 varDecl(hasInitializer(ignoringParenCasts(
2551 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002552}
2553
2554TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2555 // These tests verify that ignoringImpCasts does not match if the inner
2556 // matcher does not match.
2557 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002558 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002559 unless(anything()))))));
2560
2561 // This test creates an implicit cast from int to char in addition to the
2562 // parentheses.
2563 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002564 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002565 unless(anything()))))));
2566
2567 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002568 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002569 unless(anything()))))));
2570}
2571
2572TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2573 // This test checks that ignoringParenAndImpCasts matches when
2574 // parentheses and/or implicit casts are present and its inner matcher alone
2575 // does not match.
2576 // Note that this test creates an implicit const cast.
2577 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002578 varDecl(hasInitializer(ignoringParenImpCasts(
2579 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002580 // This test creates an implicit cast from int to char.
2581 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002582 varDecl(hasInitializer(ignoringParenImpCasts(
2583 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002584}
2585
2586TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2587 // This test verifies that expressions that do not have parentheses or
2588 // implicit casts still match.
2589 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002590 varDecl(hasInitializer(ignoringParenImpCasts(
2591 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002592 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002593 varDecl(hasInitializer(ignoringParenImpCasts(
2594 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002595}
2596
2597TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2598 // These tests verify that ignoringParenImpCasts does not match if
2599 // the inner matcher does not match.
2600 // This test creates an implicit cast.
2601 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002602 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002603 unless(anything()))))));
2604 // These tests verify that ignoringParenAndImplictCasts does not look
2605 // through explicit casts.
2606 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002607 varDecl(hasInitializer(ignoringParenImpCasts(
2608 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002609 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002610 varDecl(hasInitializer(ignoringParenImpCasts(
2611 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002612 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002613 varDecl(hasInitializer(ignoringParenImpCasts(
2614 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002615}
2616
Manuel Klimek715c9562012-07-25 10:02:02 +00002617TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002618 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2619 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002620 implicitCastExpr(
2621 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002622}
2623
Manuel Klimek715c9562012-07-25 10:02:02 +00002624TEST(HasSourceExpression, MatchesExplicitCasts) {
2625 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002626 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002627 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002628 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002629}
2630
Manuel Klimek4da21662012-07-06 05:48:52 +00002631TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002632 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002633}
2634
2635TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002636 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002637}
2638
2639TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002640 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002641}
2642
2643TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002644 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002645}
2646
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002647TEST(InitListExpression, MatchesInitListExpression) {
2648 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2649 initListExpr(hasType(asString("int [2]")))));
2650 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002651 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002652}
2653
2654TEST(UsingDeclaration, MatchesUsingDeclarations) {
2655 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2656 usingDecl()));
2657}
2658
2659TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2660 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2661 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2662}
2663
2664TEST(UsingDeclaration, MatchesSpecificTarget) {
2665 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2666 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002667 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002668 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2669 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002670 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002671}
2672
2673TEST(UsingDeclaration, ThroughUsingDeclaration) {
2674 EXPECT_TRUE(matches(
2675 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002676 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002677 EXPECT_TRUE(notMatches(
2678 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002679 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002680}
2681
Sam Panzer425f41b2012-08-16 17:20:59 +00002682TEST(SingleDecl, IsSingleDecl) {
2683 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002684 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002685 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2686 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2687 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2688 SingleDeclStmt));
2689}
2690
2691TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002692 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002693
2694 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002695 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002696 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002697 declStmt(containsDeclaration(0, MatchesInit),
2698 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002699 unsigned WrongIndex = 42;
2700 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002701 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002702 MatchesInit))));
2703}
2704
2705TEST(DeclCount, DeclCountIsCorrect) {
2706 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002707 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002708 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002709 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002710 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002711 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002712}
2713
Manuel Klimek4da21662012-07-06 05:48:52 +00002714TEST(While, MatchesWhileLoops) {
2715 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2716 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2717 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2718}
2719
2720TEST(Do, MatchesDoLoops) {
2721 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2722 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2723}
2724
2725TEST(Do, DoesNotMatchWhileLoops) {
2726 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2727}
2728
2729TEST(SwitchCase, MatchesCase) {
2730 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2731 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2732 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2733 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2734}
2735
Daniel Jasperb54b7642012-09-20 14:12:57 +00002736TEST(SwitchCase, MatchesSwitch) {
2737 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2738 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2739 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2740 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2741}
2742
2743TEST(ExceptionHandling, SimpleCases) {
2744 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2745 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2746 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2747 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2748 throwExpr()));
2749 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2750 throwExpr()));
2751}
2752
Manuel Klimek4da21662012-07-06 05:48:52 +00002753TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2754 EXPECT_TRUE(notMatches(
2755 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002756 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002757 EXPECT_TRUE(notMatches(
2758 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002759 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002760}
2761
2762TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2763 EXPECT_TRUE(matches(
2764 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002765 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002766}
2767
2768TEST(ForEach, BindsOneNode) {
2769 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002770 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002771 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002772}
2773
2774TEST(ForEach, BindsMultipleNodes) {
2775 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002776 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002777 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002778}
2779
2780TEST(ForEach, BindsRecursiveCombinations) {
2781 EXPECT_TRUE(matchAndVerifyResultTrue(
2782 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002783 recordDecl(hasName("C"),
2784 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002785 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002786}
2787
2788TEST(ForEachDescendant, BindsOneNode) {
2789 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002790 recordDecl(hasName("C"),
2791 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002792 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002793}
2794
Daniel Jasper5f684e92012-11-16 18:39:22 +00002795TEST(ForEachDescendant, NestedForEachDescendant) {
2796 DeclarationMatcher m = recordDecl(
2797 isDefinition(), decl().bind("x"), hasName("C"));
2798 EXPECT_TRUE(matchAndVerifyResultTrue(
2799 "class A { class B { class C {}; }; };",
2800 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
2801 new VerifyIdIsBoundTo<Decl>("x", "C")));
2802
2803 // FIXME: This is not really a useful matcher, but the result is still
2804 // surprising (currently binds "A").
2805 //EXPECT_TRUE(matchAndVerifyResultTrue(
2806 // "class A { class B { class C {}; }; };",
2807 // recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
2808 // new VerifyIdIsBoundTo<Decl>("x", "C")));
2809}
2810
Manuel Klimek4da21662012-07-06 05:48:52 +00002811TEST(ForEachDescendant, BindsMultipleNodes) {
2812 EXPECT_TRUE(matchAndVerifyResultTrue(
2813 "class C { class D { int x; int y; }; "
2814 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002815 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002816 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002817}
2818
2819TEST(ForEachDescendant, BindsRecursiveCombinations) {
2820 EXPECT_TRUE(matchAndVerifyResultTrue(
2821 "class C { class D { "
2822 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002823 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2824 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002825 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002826}
2827
Daniel Jasper11c98772012-11-11 22:14:55 +00002828TEST(ForEachDescendant, BindsCorrectNodes) {
2829 EXPECT_TRUE(matchAndVerifyResultTrue(
2830 "class C { void f(); int i; };",
2831 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2832 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
2833 EXPECT_TRUE(matchAndVerifyResultTrue(
2834 "class C { void f() {} int i; };",
2835 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2836 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
2837}
2838
Manuel Klimek4da21662012-07-06 05:48:52 +00002839
2840TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2841 // Make sure that we can both match the class by name (::X) and by the type
2842 // the template was instantiated with (via a field).
2843
2844 EXPECT_TRUE(matches(
2845 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002846 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002847
2848 EXPECT_TRUE(matches(
2849 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002850 recordDecl(isTemplateInstantiation(), hasDescendant(
2851 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002852}
2853
2854TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2855 EXPECT_TRUE(matches(
2856 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002857 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002858 isTemplateInstantiation())));
2859}
2860
2861TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2862 EXPECT_TRUE(matches(
2863 "template <typename T> class X { T t; }; class A {};"
2864 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002865 recordDecl(isTemplateInstantiation(), hasDescendant(
2866 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002867}
2868
2869TEST(IsTemplateInstantiation,
2870 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2871 EXPECT_TRUE(matches(
2872 "template <typename T> class X {};"
2873 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002874 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002875}
2876
2877TEST(IsTemplateInstantiation,
2878 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2879 EXPECT_TRUE(matches(
2880 "class A {};"
2881 "class X {"
2882 " template <typename U> class Y { U u; };"
2883 " Y<A> y;"
2884 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002885 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002886}
2887
2888TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2889 // FIXME: Figure out whether this makes sense. It doesn't affect the
2890 // normal use case as long as the uppermost instantiation always is marked
2891 // as template instantiation, but it might be confusing as a predicate.
2892 EXPECT_TRUE(matches(
2893 "class A {};"
2894 "template <typename T> class X {"
2895 " template <typename U> class Y { U u; };"
2896 " Y<T> y;"
2897 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002898 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002899}
2900
2901TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2902 EXPECT_TRUE(notMatches(
2903 "template <typename T> class X {}; class A {};"
2904 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002905 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002906}
2907
2908TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2909 EXPECT_TRUE(notMatches(
2910 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002911 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002912}
2913
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002914TEST(IsExplicitTemplateSpecialization,
2915 DoesNotMatchPrimaryTemplate) {
2916 EXPECT_TRUE(notMatches(
2917 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002918 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002919 EXPECT_TRUE(notMatches(
2920 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002921 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002922}
2923
2924TEST(IsExplicitTemplateSpecialization,
2925 DoesNotMatchExplicitTemplateInstantiations) {
2926 EXPECT_TRUE(notMatches(
2927 "template <typename T> class X {};"
2928 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002929 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002930 EXPECT_TRUE(notMatches(
2931 "template <typename T> void f(T t) {}"
2932 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002933 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002934}
2935
2936TEST(IsExplicitTemplateSpecialization,
2937 DoesNotMatchImplicitTemplateInstantiations) {
2938 EXPECT_TRUE(notMatches(
2939 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002940 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002941 EXPECT_TRUE(notMatches(
2942 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002943 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002944}
2945
2946TEST(IsExplicitTemplateSpecialization,
2947 MatchesExplicitTemplateSpecializations) {
2948 EXPECT_TRUE(matches(
2949 "template <typename T> class X {};"
2950 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002951 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002952 EXPECT_TRUE(matches(
2953 "template <typename T> void f(T t) {}"
2954 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002955 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002956}
2957
Manuel Klimek579b1202012-09-07 09:26:10 +00002958TEST(HasAncenstor, MatchesDeclarationAncestors) {
2959 EXPECT_TRUE(matches(
2960 "class A { class B { class C {}; }; };",
2961 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2962}
2963
2964TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2965 EXPECT_TRUE(notMatches(
2966 "class A { class B { class C {}; }; };",
2967 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2968}
2969
2970TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2971 EXPECT_TRUE(matches(
2972 "class A { class B { void f() { C c; } class C {}; }; };",
2973 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2974 hasAncestor(recordDecl(hasName("A"))))))));
2975}
2976
2977TEST(HasAncenstor, MatchesStatementAncestors) {
2978 EXPECT_TRUE(matches(
2979 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002980 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002981}
2982
2983TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2984 EXPECT_TRUE(matches(
2985 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002986 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002987}
2988
2989TEST(HasAncestor, BindsRecursiveCombinations) {
2990 EXPECT_TRUE(matchAndVerifyResultTrue(
2991 "class C { class D { class E { class F { int y; }; }; }; };",
2992 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002993 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00002994}
2995
2996TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
2997 EXPECT_TRUE(matchAndVerifyResultTrue(
2998 "class C { class D { class E { class F { int y; }; }; }; };",
2999 fieldDecl(hasAncestor(
3000 decl(
3001 hasDescendant(recordDecl(isDefinition(),
3002 hasAncestor(recordDecl())))
3003 ).bind("d")
3004 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00003005 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00003006}
3007
3008TEST(HasAncestor, MatchesInTemplateInstantiations) {
3009 EXPECT_TRUE(matches(
3010 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3011 "A<int>::B::C a;",
3012 fieldDecl(hasType(asString("int")),
3013 hasAncestor(recordDecl(hasName("A"))))));
3014}
3015
3016TEST(HasAncestor, MatchesInImplicitCode) {
3017 EXPECT_TRUE(matches(
3018 "struct X {}; struct A { A() {} X x; };",
3019 constructorDecl(
3020 hasAnyConstructorInitializer(withInitializer(expr(
3021 hasAncestor(recordDecl(hasName("A")))))))));
3022}
3023
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003024TEST(HasParent, MatchesOnlyParent) {
3025 EXPECT_TRUE(matches(
3026 "void f() { if (true) { int x = 42; } }",
3027 compoundStmt(hasParent(ifStmt()))));
3028 EXPECT_TRUE(notMatches(
3029 "void f() { for (;;) { int x = 42; } }",
3030 compoundStmt(hasParent(ifStmt()))));
3031 EXPECT_TRUE(notMatches(
3032 "void f() { if (true) for (;;) { int x = 42; } }",
3033 compoundStmt(hasParent(ifStmt()))));
3034}
3035
Daniel Jasperce620072012-10-17 08:52:59 +00003036TEST(TypeMatching, MatchesTypes) {
3037 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3038}
3039
3040TEST(TypeMatching, MatchesArrayTypes) {
3041 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3042 EXPECT_TRUE(matches("int a[42];", arrayType()));
3043 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3044
3045 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3046 arrayType(hasElementType(builtinType()))));
3047
3048 EXPECT_TRUE(matches(
3049 "int const a[] = { 2, 3 };",
3050 qualType(arrayType(hasElementType(builtinType())))));
3051 EXPECT_TRUE(matches(
3052 "int const a[] = { 2, 3 };",
3053 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3054 EXPECT_TRUE(matches(
3055 "typedef const int T; T x[] = { 1, 2 };",
3056 qualType(isConstQualified(), arrayType())));
3057
3058 EXPECT_TRUE(notMatches(
3059 "int a[] = { 2, 3 };",
3060 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3061 EXPECT_TRUE(notMatches(
3062 "int a[] = { 2, 3 };",
3063 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3064 EXPECT_TRUE(notMatches(
3065 "int const a[] = { 2, 3 };",
3066 qualType(arrayType(hasElementType(builtinType())),
3067 unless(isConstQualified()))));
3068
3069 EXPECT_TRUE(matches("int a[2];",
3070 constantArrayType(hasElementType(builtinType()))));
3071 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3072}
3073
3074TEST(TypeMatching, MatchesComplexTypes) {
3075 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3076 EXPECT_TRUE(matches(
3077 "_Complex float f;",
3078 complexType(hasElementType(builtinType()))));
3079 EXPECT_TRUE(notMatches(
3080 "_Complex float f;",
3081 complexType(hasElementType(isInteger()))));
3082}
3083
3084TEST(TypeMatching, MatchesConstantArrayTypes) {
3085 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3086 EXPECT_TRUE(notMatches(
3087 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3088 constantArrayType(hasElementType(builtinType()))));
3089
3090 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3091 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3092 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3093}
3094
3095TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3096 EXPECT_TRUE(matches(
3097 "template <typename T, int Size> class array { T data[Size]; };",
3098 dependentSizedArrayType()));
3099 EXPECT_TRUE(notMatches(
3100 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3101 dependentSizedArrayType()));
3102}
3103
3104TEST(TypeMatching, MatchesIncompleteArrayType) {
3105 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3106 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3107
3108 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3109 incompleteArrayType()));
3110}
3111
3112TEST(TypeMatching, MatchesVariableArrayType) {
3113 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3114 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3115
3116 EXPECT_TRUE(matches(
3117 "void f(int b) { int a[b]; }",
3118 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3119 varDecl(hasName("b")))))))));
3120}
3121
3122TEST(TypeMatching, MatchesAtomicTypes) {
3123 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3124
3125 EXPECT_TRUE(matches("_Atomic(int) i;",
3126 atomicType(hasValueType(isInteger()))));
3127 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3128 atomicType(hasValueType(isInteger()))));
3129}
3130
3131TEST(TypeMatching, MatchesAutoTypes) {
3132 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3133 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3134 autoType()));
3135
3136 EXPECT_TRUE(matches("auto a = 1;",
3137 autoType(hasDeducedType(isInteger()))));
3138 EXPECT_TRUE(notMatches("auto b = 2.0;",
3139 autoType(hasDeducedType(isInteger()))));
3140}
3141
Daniel Jaspera267cf62012-10-29 10:14:44 +00003142TEST(TypeMatching, MatchesFunctionTypes) {
3143 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3144 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3145}
3146
Daniel Jasperce620072012-10-17 08:52:59 +00003147TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003148 // FIXME: Reactive when these tests can be more specific (not matching
3149 // implicit code on certain platforms), likely when we have hasDescendant for
3150 // Types/TypeLocs.
3151 //EXPECT_TRUE(matchAndVerifyResultTrue(
3152 // "int* a;",
3153 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3154 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3155 //EXPECT_TRUE(matchAndVerifyResultTrue(
3156 // "int* a;",
3157 // pointerTypeLoc().bind("loc"),
3158 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003159 EXPECT_TRUE(matches(
3160 "int** a;",
3161 pointerTypeLoc(pointeeLoc(loc(qualType())))));
3162 EXPECT_TRUE(matches(
3163 "int** a;",
3164 loc(pointerType(pointee(pointerType())))));
3165 EXPECT_TRUE(matches(
3166 "int* b; int* * const a = &b;",
3167 loc(qualType(isConstQualified(), pointerType()))));
3168
3169 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003170 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3171 hasType(blockPointerType()))));
3172 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3173 hasType(memberPointerType()))));
3174 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3175 hasType(pointerType()))));
3176 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3177 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003178
Daniel Jasper1802daf2012-10-17 13:35:36 +00003179 Fragment = "int *ptr;";
3180 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3181 hasType(blockPointerType()))));
3182 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3183 hasType(memberPointerType()))));
3184 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3185 hasType(pointerType()))));
3186 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3187 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003188
Daniel Jasper1802daf2012-10-17 13:35:36 +00003189 Fragment = "int a; int &ref = a;";
3190 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3191 hasType(blockPointerType()))));
3192 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3193 hasType(memberPointerType()))));
3194 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3195 hasType(pointerType()))));
3196 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3197 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003198}
3199
3200TEST(TypeMatching, PointeeTypes) {
3201 EXPECT_TRUE(matches("int b; int &a = b;",
3202 referenceType(pointee(builtinType()))));
3203 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3204
3205 EXPECT_TRUE(matches("int *a;",
3206 pointerTypeLoc(pointeeLoc(loc(builtinType())))));
3207
3208 EXPECT_TRUE(matches(
3209 "int const *A;",
3210 pointerType(pointee(isConstQualified(), builtinType()))));
3211 EXPECT_TRUE(notMatches(
3212 "int *A;",
3213 pointerType(pointee(isConstQualified(), builtinType()))));
3214}
3215
3216TEST(TypeMatching, MatchesPointersToConstTypes) {
3217 EXPECT_TRUE(matches("int b; int * const a = &b;",
3218 loc(pointerType())));
3219 EXPECT_TRUE(matches("int b; int * const a = &b;",
3220 pointerTypeLoc()));
3221 EXPECT_TRUE(matches(
3222 "int b; const int * a = &b;",
3223 pointerTypeLoc(pointeeLoc(builtinTypeLoc()))));
3224 EXPECT_TRUE(matches(
3225 "int b; const int * a = &b;",
3226 pointerType(pointee(builtinType()))));
3227}
3228
3229TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003230 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3231 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003232
Daniel Jasper1802daf2012-10-17 13:35:36 +00003233 EXPECT_TRUE(matches("typedef int X; X a;",
3234 varDecl(hasName("a"),
3235 hasType(typedefType(hasDecl(decl()))))));
Daniel Jasperce620072012-10-17 08:52:59 +00003236}
3237
Daniel Jaspera7564432012-09-13 13:11:25 +00003238TEST(NNS, MatchesNestedNameSpecifiers) {
3239 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3240 nestedNameSpecifier()));
3241 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3242 nestedNameSpecifier()));
3243 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3244 nestedNameSpecifier()));
3245
3246 EXPECT_TRUE(matches(
3247 "struct A { static void f() {} }; void g() { A::f(); }",
3248 nestedNameSpecifier()));
3249 EXPECT_TRUE(notMatches(
3250 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3251 nestedNameSpecifier()));
3252}
3253
Daniel Jasperb54b7642012-09-20 14:12:57 +00003254TEST(NullStatement, SimpleCases) {
3255 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3256 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3257}
3258
Daniel Jaspera7564432012-09-13 13:11:25 +00003259TEST(NNS, MatchesTypes) {
3260 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3261 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3262 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3263 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3264 Matcher));
3265 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3266}
3267
3268TEST(NNS, MatchesNamespaceDecls) {
3269 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3270 specifiesNamespace(hasName("ns")));
3271 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3272 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3273 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3274}
3275
3276TEST(NNS, BindsNestedNameSpecifiers) {
3277 EXPECT_TRUE(matchAndVerifyResultTrue(
3278 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3279 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3280 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3281}
3282
3283TEST(NNS, BindsNestedNameSpecifierLocs) {
3284 EXPECT_TRUE(matchAndVerifyResultTrue(
3285 "namespace ns { struct B {}; } ns::B b;",
3286 loc(nestedNameSpecifier()).bind("loc"),
3287 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3288}
3289
3290TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3291 EXPECT_TRUE(matches(
3292 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3293 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3294 EXPECT_TRUE(matches(
3295 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003296 nestedNameSpecifierLoc(hasPrefix(
3297 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003298}
3299
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003300TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3301 std::string Fragment =
3302 "namespace a { struct A { struct B { struct C {}; }; }; };"
3303 "void f() { a::A::B::C c; }";
3304 EXPECT_TRUE(matches(
3305 Fragment,
3306 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3307 hasDescendant(nestedNameSpecifier(
3308 specifiesNamespace(hasName("a")))))));
3309 EXPECT_TRUE(notMatches(
3310 Fragment,
3311 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3312 has(nestedNameSpecifier(
3313 specifiesNamespace(hasName("a")))))));
3314 EXPECT_TRUE(matches(
3315 Fragment,
3316 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3317 has(nestedNameSpecifier(
3318 specifiesNamespace(hasName("a")))))));
3319
3320 // Not really useful because a NestedNameSpecifier can af at most one child,
3321 // but to complete the interface.
3322 EXPECT_TRUE(matchAndVerifyResultTrue(
3323 Fragment,
3324 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3325 forEach(nestedNameSpecifier().bind("x"))),
3326 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3327}
3328
3329TEST(NNS, NestedNameSpecifiersAsDescendants) {
3330 std::string Fragment =
3331 "namespace a { struct A { struct B { struct C {}; }; }; };"
3332 "void f() { a::A::B::C c; }";
3333 EXPECT_TRUE(matches(
3334 Fragment,
3335 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3336 asString("struct a::A")))))));
3337 EXPECT_TRUE(matchAndVerifyResultTrue(
3338 Fragment,
3339 functionDecl(hasName("f"),
3340 forEachDescendant(nestedNameSpecifier().bind("x"))),
3341 // Nested names: a, a::A and a::A::B.
3342 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3343}
3344
3345TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3346 std::string Fragment =
3347 "namespace a { struct A { struct B { struct C {}; }; }; };"
3348 "void f() { a::A::B::C c; }";
3349 EXPECT_TRUE(matches(
3350 Fragment,
3351 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3352 hasDescendant(loc(nestedNameSpecifier(
3353 specifiesNamespace(hasName("a"))))))));
3354 EXPECT_TRUE(notMatches(
3355 Fragment,
3356 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3357 has(loc(nestedNameSpecifier(
3358 specifiesNamespace(hasName("a"))))))));
3359 EXPECT_TRUE(matches(
3360 Fragment,
3361 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3362 has(loc(nestedNameSpecifier(
3363 specifiesNamespace(hasName("a"))))))));
3364
3365 EXPECT_TRUE(matchAndVerifyResultTrue(
3366 Fragment,
3367 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3368 forEach(nestedNameSpecifierLoc().bind("x"))),
3369 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3370}
3371
3372TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3373 std::string Fragment =
3374 "namespace a { struct A { struct B { struct C {}; }; }; };"
3375 "void f() { a::A::B::C c; }";
3376 EXPECT_TRUE(matches(
3377 Fragment,
3378 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3379 asString("struct a::A"))))))));
3380 EXPECT_TRUE(matchAndVerifyResultTrue(
3381 Fragment,
3382 functionDecl(hasName("f"),
3383 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3384 // Nested names: a, a::A and a::A::B.
3385 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3386}
3387
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003388template <typename T>
3389class VerifyRecursiveMatch : public BoundNodesCallback {
3390public:
3391 explicit VerifyRecursiveMatch(StringRef Id,
3392 const internal::Matcher<T> &InnerMatcher)
3393 : Id(Id), InnerMatcher(InnerMatcher) {}
Daniel Jasper452abbc2012-10-29 10:48:25 +00003394
3395 virtual bool run(const BoundNodes *Nodes) {
3396 return false;
3397 }
3398
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003399 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3400 const T *Node = Nodes->getNodeAs<T>(Id);
3401 bool Found = false;
3402 MatchFinder Finder;
3403 Finder.addMatcher(InnerMatcher, new VerifyMatch(0, &Found));
3404 Finder.findAll(*Node, *Context);
3405 return Found;
3406 }
3407private:
3408 std::string Id;
3409 internal::Matcher<T> InnerMatcher;
3410};
3411
3412TEST(MatchFinder, CanMatchDeclarationsRecursively) {
3413 EXPECT_TRUE(matchAndVerifyResultTrue("class X { class Y {}; };",
3414 recordDecl(hasName("::X")).bind("X"),
3415 new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Y")))));
3416 EXPECT_TRUE(matchAndVerifyResultFalse("class X { class Y {}; };",
3417 recordDecl(hasName("::X")).bind("X"),
3418 new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Z")))));
3419}
3420
3421TEST(MatchFinder, CanMatchStatementsRecursively) {
3422 EXPECT_TRUE(matchAndVerifyResultTrue("void f() { if (1) { for (;;) { } } }",
3423 ifStmt().bind("if"),
3424 new VerifyRecursiveMatch<clang::Stmt>("if", forStmt())));
3425 EXPECT_TRUE(matchAndVerifyResultFalse("void f() { if (1) { for (;;) { } } }",
3426 ifStmt().bind("if"),
3427 new VerifyRecursiveMatch<clang::Stmt>("if", declStmt())));
3428}
3429
Manuel Klimeke5793282012-11-02 01:31:03 +00003430class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
3431public:
3432 VerifyStartOfTranslationUnit() : Called(false) {}
3433 virtual void run(const MatchFinder::MatchResult &Result) {
3434 EXPECT_TRUE(Called);
3435 }
3436 virtual void onStartOfTranslationUnit() {
3437 Called = true;
3438 }
3439 bool Called;
3440};
3441
3442TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
3443 MatchFinder Finder;
3444 VerifyStartOfTranslationUnit VerifyCallback;
3445 Finder.addMatcher(decl(), &VerifyCallback);
3446 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
3447 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
3448 EXPECT_TRUE(VerifyCallback.Called);
3449}
3450
Manuel Klimek4da21662012-07-06 05:48:52 +00003451} // end namespace ast_matchers
3452} // end namespace clang