blob: ad072aa7ce359540ec64711ef24fbb939aa62c88 [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
1241TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001242 DeclarationMatcher ReferenceClassX = varDecl(
1243 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001244 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1245 ReferenceClassX));
1246 EXPECT_TRUE(
1247 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1248 EXPECT_TRUE(
1249 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1250 EXPECT_TRUE(
1251 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1252}
1253
1254TEST(HasParameter, CallsInnerMatcher) {
1255 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001256 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001257 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001258 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001259}
1260
1261TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1262 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001263 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001264}
1265
1266TEST(HasType, MatchesParameterVariableTypesStrictly) {
1267 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001268 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001269 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001270 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001271 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001272 methodDecl(hasParameter(0,
1273 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001274 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001275 methodDecl(hasParameter(0,
1276 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001277}
1278
1279TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1280 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001281 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001282 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001283 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001284}
1285
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001286TEST(Returns, MatchesReturnTypes) {
1287 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001288 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001289 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001290 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001291 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001292 functionDecl(returns(hasDeclaration(
1293 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001294}
1295
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001296TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001297 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1298 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1299 functionDecl(isExternC())));
1300 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001301}
1302
Manuel Klimek4da21662012-07-06 05:48:52 +00001303TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1304 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001305 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001306}
1307
1308TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1309 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001310 methodDecl(hasAnyParameter(hasType(pointsTo(
1311 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001312}
1313
1314TEST(HasName, MatchesParameterVariableDeclartions) {
1315 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001316 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001317 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001318 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001319}
1320
Daniel Jasper371f9392012-08-01 08:40:24 +00001321TEST(Matcher, MatchesClassTemplateSpecialization) {
1322 EXPECT_TRUE(matches("template<typename T> struct A {};"
1323 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001324 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001325 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001326 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001327 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001328 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001329}
1330
1331TEST(Matcher, MatchesTypeTemplateArgument) {
1332 EXPECT_TRUE(matches(
1333 "template<typename T> struct B {};"
1334 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001335 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001336 asString("int"))))));
1337}
1338
1339TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1340 EXPECT_TRUE(matches(
1341 "struct B { int next; };"
1342 "template<int(B::*next_ptr)> struct A {};"
1343 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001344 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1345 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001346
1347 EXPECT_TRUE(notMatches(
1348 "template <typename T> struct A {};"
1349 "A<int> a;",
1350 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1351 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001352}
1353
1354TEST(Matcher, MatchesSpecificArgument) {
1355 EXPECT_TRUE(matches(
1356 "template<typename T, typename U> class A {};"
1357 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001358 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001359 1, refersToType(asString("int"))))));
1360 EXPECT_TRUE(notMatches(
1361 "template<typename T, typename U> class A {};"
1362 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001363 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001364 1, refersToType(asString("int"))))));
1365}
1366
Manuel Klimek4da21662012-07-06 05:48:52 +00001367TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001368 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001369
1370 EXPECT_TRUE(
1371 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1372 EXPECT_TRUE(
1373 matches("class X { public: X(); }; void x() { X x = X(); }",
1374 Constructor));
1375 EXPECT_TRUE(
1376 matches("class X { public: X(int); }; void x() { X x = 0; }",
1377 Constructor));
1378 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1379}
1380
1381TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001382 StatementMatcher Constructor = constructExpr(
1383 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001384
1385 EXPECT_TRUE(
1386 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1387 Constructor));
1388 EXPECT_TRUE(
1389 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1390 Constructor));
1391 EXPECT_TRUE(
1392 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1393 Constructor));
1394 EXPECT_TRUE(
1395 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1396 Constructor));
1397
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001398 StatementMatcher WrongIndex = constructExpr(
1399 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001400 EXPECT_TRUE(
1401 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1402 WrongIndex));
1403}
1404
1405TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001406 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001407
1408 EXPECT_TRUE(
1409 matches("class X { public: X(int); }; void x() { X x(0); }",
1410 Constructor1Arg));
1411 EXPECT_TRUE(
1412 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1413 Constructor1Arg));
1414 EXPECT_TRUE(
1415 matches("class X { public: X(int); }; void x() { X x = 0; }",
1416 Constructor1Arg));
1417 EXPECT_TRUE(
1418 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1419 Constructor1Arg));
1420}
1421
Manuel Klimek70b9db92012-10-23 10:40:50 +00001422TEST(Matcher,ThisExpr) {
1423 EXPECT_TRUE(
1424 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1425 EXPECT_TRUE(
1426 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1427}
1428
Manuel Klimek4da21662012-07-06 05:48:52 +00001429TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001430 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001431
1432 std::string ClassString = "class string { public: string(); ~string(); }; ";
1433
1434 EXPECT_TRUE(
1435 matches(ClassString +
1436 "string GetStringByValue();"
1437 "void FunctionTakesString(string s);"
1438 "void run() { FunctionTakesString(GetStringByValue()); }",
1439 TempExpression));
1440
1441 EXPECT_TRUE(
1442 notMatches(ClassString +
1443 "string* GetStringPointer(); "
1444 "void FunctionTakesStringPtr(string* s);"
1445 "void run() {"
1446 " string* s = GetStringPointer();"
1447 " FunctionTakesStringPtr(GetStringPointer());"
1448 " FunctionTakesStringPtr(s);"
1449 "}",
1450 TempExpression));
1451
1452 EXPECT_TRUE(
1453 notMatches("class no_dtor {};"
1454 "no_dtor GetObjByValue();"
1455 "void ConsumeObj(no_dtor param);"
1456 "void run() { ConsumeObj(GetObjByValue()); }",
1457 TempExpression));
1458}
1459
Sam Panzere16acd32012-08-24 22:04:44 +00001460TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1461 std::string ClassString =
1462 "class string { public: string(); int length(); }; ";
1463
1464 EXPECT_TRUE(
1465 matches(ClassString +
1466 "string GetStringByValue();"
1467 "void FunctionTakesString(string s);"
1468 "void run() { FunctionTakesString(GetStringByValue()); }",
1469 materializeTemporaryExpr()));
1470
1471 EXPECT_TRUE(
1472 notMatches(ClassString +
1473 "string* GetStringPointer(); "
1474 "void FunctionTakesStringPtr(string* s);"
1475 "void run() {"
1476 " string* s = GetStringPointer();"
1477 " FunctionTakesStringPtr(GetStringPointer());"
1478 " FunctionTakesStringPtr(s);"
1479 "}",
1480 materializeTemporaryExpr()));
1481
1482 EXPECT_TRUE(
1483 notMatches(ClassString +
1484 "string GetStringByValue();"
1485 "void run() { int k = GetStringByValue().length(); }",
1486 materializeTemporaryExpr()));
1487
1488 EXPECT_TRUE(
1489 notMatches(ClassString +
1490 "string GetStringByValue();"
1491 "void run() { GetStringByValue(); }",
1492 materializeTemporaryExpr()));
1493}
1494
Manuel Klimek4da21662012-07-06 05:48:52 +00001495TEST(ConstructorDeclaration, SimpleCase) {
1496 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001497 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001498 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001499 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001500}
1501
1502TEST(ConstructorDeclaration, IsImplicit) {
1503 // This one doesn't match because the constructor is not added by the
1504 // compiler (it is not needed).
1505 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001506 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001507 // The compiler added the implicit default constructor.
1508 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001509 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001510 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001511 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001512}
1513
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001514TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1515 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001516 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001517}
1518
1519TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001520 EXPECT_TRUE(notMatches("class Foo {};",
1521 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001522}
1523
Manuel Klimek4da21662012-07-06 05:48:52 +00001524TEST(HasAnyConstructorInitializer, SimpleCase) {
1525 EXPECT_TRUE(notMatches(
1526 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001527 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001528 EXPECT_TRUE(matches(
1529 "class Foo {"
1530 " Foo() : foo_() { }"
1531 " int foo_;"
1532 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001533 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001534}
1535
1536TEST(HasAnyConstructorInitializer, ForField) {
1537 static const char Code[] =
1538 "class Baz { };"
1539 "class Foo {"
1540 " Foo() : foo_() { }"
1541 " Baz foo_;"
1542 " Baz bar_;"
1543 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001544 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1545 forField(hasType(recordDecl(hasName("Baz"))))))));
1546 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001547 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001548 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1549 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001550}
1551
1552TEST(HasAnyConstructorInitializer, WithInitializer) {
1553 static const char Code[] =
1554 "class Foo {"
1555 " Foo() : foo_(0) { }"
1556 " int foo_;"
1557 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001558 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001559 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001560 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001561 withInitializer(integerLiteral(equals(1)))))));
1562}
1563
1564TEST(HasAnyConstructorInitializer, IsWritten) {
1565 static const char Code[] =
1566 "struct Bar { Bar(){} };"
1567 "class Foo {"
1568 " Foo() : foo_() { }"
1569 " Bar foo_;"
1570 " Bar bar_;"
1571 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001572 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001573 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001574 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001575 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001576 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001577 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1578}
1579
1580TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001581 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001582
1583 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1584 EXPECT_TRUE(
1585 matches("class X { public: X(); }; void x() { new X(); }", New));
1586 EXPECT_TRUE(
1587 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1588 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1589}
1590
1591TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001592 StatementMatcher New = constructExpr(
1593 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001594
1595 EXPECT_TRUE(
1596 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1597 New));
1598 EXPECT_TRUE(
1599 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1600 New));
1601 EXPECT_TRUE(
1602 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1603 New));
1604
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001605 StatementMatcher WrongIndex = constructExpr(
1606 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001607 EXPECT_TRUE(
1608 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1609 WrongIndex));
1610}
1611
1612TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001613 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001614
1615 EXPECT_TRUE(
1616 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1617 EXPECT_TRUE(
1618 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1619 New));
1620}
1621
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001622TEST(Matcher, DeleteExpression) {
1623 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001624 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001625}
1626
Manuel Klimek4da21662012-07-06 05:48:52 +00001627TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001628 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001629
1630 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1631 EXPECT_TRUE(
1632 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1633 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1634}
1635
1636TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001637 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001638 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1639 // wide string
1640 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1641 // with escaped characters
1642 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1643 // no matching -- though the data type is the same, there is no string literal
1644 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1645}
1646
1647TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001648 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001649 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1650 // wide character
1651 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1652 // wide character, Hex encoded, NOT MATCHED!
1653 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1654 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1655}
1656
1657TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001658 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001659 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1660 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1661 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1662 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1663
1664 // Non-matching cases (character literals, float and double)
1665 EXPECT_TRUE(notMatches("int i = L'a';",
1666 HasIntLiteral)); // this is actually a character
1667 // literal cast to int
1668 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1669 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1670 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1671}
1672
Daniel Jasper31f7c082012-10-01 13:40:41 +00001673TEST(Matcher, NullPtrLiteral) {
1674 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1675}
1676
Daniel Jasperb54b7642012-09-20 14:12:57 +00001677TEST(Matcher, AsmStatement) {
1678 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1679}
1680
Manuel Klimek4da21662012-07-06 05:48:52 +00001681TEST(Matcher, Conditions) {
1682 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1683
1684 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1685 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1686 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1687 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1688 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1689}
1690
1691TEST(MatchBinaryOperator, HasOperatorName) {
1692 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1693
1694 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1695 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1696}
1697
1698TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1699 StatementMatcher OperatorTrueFalse =
1700 binaryOperator(hasLHS(boolLiteral(equals(true))),
1701 hasRHS(boolLiteral(equals(false))));
1702
1703 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1704 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1705 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1706}
1707
1708TEST(MatchBinaryOperator, HasEitherOperand) {
1709 StatementMatcher HasOperand =
1710 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1711
1712 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1713 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1714 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1715}
1716
1717TEST(Matcher, BinaryOperatorTypes) {
1718 // Integration test that verifies the AST provides all binary operators in
1719 // a way we expect.
1720 // FIXME: Operator ','
1721 EXPECT_TRUE(
1722 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1723 EXPECT_TRUE(
1724 matches("bool b; bool c = (b = true);",
1725 binaryOperator(hasOperatorName("="))));
1726 EXPECT_TRUE(
1727 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1728 EXPECT_TRUE(
1729 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1730 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1731 EXPECT_TRUE(
1732 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1733 EXPECT_TRUE(
1734 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1735 EXPECT_TRUE(
1736 matches("int i = 1; int j = (i <<= 2);",
1737 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(
1747 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1748 EXPECT_TRUE(
1749 matches("int i = 42; int j = (i ^= 42);",
1750 binaryOperator(hasOperatorName("^="))));
1751 EXPECT_TRUE(
1752 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1753 EXPECT_TRUE(
1754 matches("int i = 42; int j = (i %= 42);",
1755 binaryOperator(hasOperatorName("%="))));
1756 EXPECT_TRUE(
1757 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1758 EXPECT_TRUE(
1759 matches("bool b = true && false;",
1760 binaryOperator(hasOperatorName("&&"))));
1761 EXPECT_TRUE(
1762 matches("bool b = true; bool c = (b &= false);",
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("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1774 EXPECT_TRUE(
1775 matches("int i = 42; int j = (i *= 23);",
1776 binaryOperator(hasOperatorName("*="))));
1777 EXPECT_TRUE(
1778 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1779 EXPECT_TRUE(
1780 matches("int i = 42; int j = (i /= 23);",
1781 binaryOperator(hasOperatorName("/="))));
1782 EXPECT_TRUE(
1783 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1784 EXPECT_TRUE(
1785 matches("int i = 42; int j = (i += 23);",
1786 binaryOperator(hasOperatorName("+="))));
1787 EXPECT_TRUE(
1788 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1789 EXPECT_TRUE(
1790 matches("int i = 42; int j = (i -= 23);",
1791 binaryOperator(hasOperatorName("-="))));
1792 EXPECT_TRUE(
1793 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1794 binaryOperator(hasOperatorName("->*"))));
1795 EXPECT_TRUE(
1796 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1797 binaryOperator(hasOperatorName(".*"))));
1798
1799 // Member expressions as operators are not supported in matches.
1800 EXPECT_TRUE(
1801 notMatches("struct A { void x(A *a) { a->x(this); } };",
1802 binaryOperator(hasOperatorName("->"))));
1803
1804 // Initializer assignments are not represented as operator equals.
1805 EXPECT_TRUE(
1806 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1807
1808 // Array indexing is not represented as operator.
1809 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1810
1811 // Overloaded operators do not match at all.
1812 EXPECT_TRUE(notMatches(
1813 "struct A { bool operator&&(const A &a) const { return false; } };"
1814 "void x() { A a, b; a && b; }",
1815 binaryOperator()));
1816}
1817
1818TEST(MatchUnaryOperator, HasOperatorName) {
1819 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1820
1821 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1822 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1823}
1824
1825TEST(MatchUnaryOperator, HasUnaryOperand) {
1826 StatementMatcher OperatorOnFalse =
1827 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1828
1829 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1830 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1831}
1832
1833TEST(Matcher, UnaryOperatorTypes) {
1834 // Integration test that verifies the AST provides all unary operators in
1835 // a way we expect.
1836 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1837 EXPECT_TRUE(
1838 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1839 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1840 EXPECT_TRUE(
1841 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1842 EXPECT_TRUE(
1843 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1844 EXPECT_TRUE(
1845 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1846 EXPECT_TRUE(
1847 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1848 EXPECT_TRUE(
1849 matches("int i; int j = i++;", 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
1855 // We don't match conversion operators.
1856 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1857
1858 // Function calls are not represented as operator.
1859 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1860
1861 // Overloaded operators do not match at all.
1862 // FIXME: We probably want to add that.
1863 EXPECT_TRUE(notMatches(
1864 "struct A { bool operator!() const { return false; } };"
1865 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1866}
1867
1868TEST(Matcher, ConditionalOperator) {
1869 StatementMatcher Conditional = conditionalOperator(
1870 hasCondition(boolLiteral(equals(true))),
1871 hasTrueExpression(boolLiteral(equals(false))));
1872
1873 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1874 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1875 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1876
1877 StatementMatcher ConditionalFalse = conditionalOperator(
1878 hasFalseExpression(boolLiteral(equals(false))));
1879
1880 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1881 EXPECT_TRUE(
1882 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1883}
1884
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001885TEST(ArraySubscriptMatchers, ArraySubscripts) {
1886 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1887 arraySubscriptExpr()));
1888 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1889 arraySubscriptExpr()));
1890}
1891
1892TEST(ArraySubscriptMatchers, ArrayIndex) {
1893 EXPECT_TRUE(matches(
1894 "int i[2]; void f() { i[1] = 1; }",
1895 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1896 EXPECT_TRUE(matches(
1897 "int i[2]; void f() { 1[i] = 1; }",
1898 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1899 EXPECT_TRUE(notMatches(
1900 "int i[2]; void f() { i[1] = 1; }",
1901 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1902}
1903
1904TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1905 EXPECT_TRUE(matches(
1906 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001907 arraySubscriptExpr(hasBase(implicitCastExpr(
1908 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001909}
1910
Manuel Klimek4da21662012-07-06 05:48:52 +00001911TEST(Matcher, HasNameSupportsNamespaces) {
1912 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001913 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001914 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001915 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001916 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001917 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001918 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001919 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001920 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001921 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001922 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001923 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001924 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001925 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001926 EXPECT_TRUE(notMatches("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("::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("z::a::b::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::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001934 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001935 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001936}
1937
1938TEST(Matcher, HasNameSupportsOuterClasses) {
1939 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001940 matches("class A { class B { class C; }; };",
1941 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001942 EXPECT_TRUE(
1943 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001944 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001945 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001946 matches("class A { class B { class C; }; };",
1947 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001948 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001949 matches("class A { class B { class C; }; };",
1950 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001951 EXPECT_TRUE(
1952 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001953 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001954 EXPECT_TRUE(
1955 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001956 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001957 EXPECT_TRUE(
1958 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001959 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001960 EXPECT_TRUE(
1961 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001962 recordDecl(hasName("::C"))));
1963 EXPECT_TRUE(
1964 notMatches("class A { class B { class C; }; };",
1965 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001966 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001967 recordDecl(hasName("z::A::B::C"))));
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("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001971}
1972
1973TEST(Matcher, IsDefinition) {
1974 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001975 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001976 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1977 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1978
1979 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001980 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001981 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
1982 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
1983
1984 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001985 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001986 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
1987 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
1988}
1989
1990TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001991 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00001992 ofClass(hasName("X")))));
1993
1994 EXPECT_TRUE(
1995 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
1996 EXPECT_TRUE(
1997 matches("class X { public: X(); }; void x(int) { X x = X(); }",
1998 Constructor));
1999 EXPECT_TRUE(
2000 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2001 Constructor));
2002}
2003
2004TEST(Matcher, VisitsTemplateInstantiations) {
2005 EXPECT_TRUE(matches(
2006 "class A { public: void x(); };"
2007 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002008 "void f() { B<A> b; b.y(); }",
2009 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002010
2011 EXPECT_TRUE(matches(
2012 "class A { public: void x(); };"
2013 "class C {"
2014 " public:"
2015 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2016 "};"
2017 "void f() {"
2018 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002019 "}",
2020 recordDecl(hasName("C"),
2021 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002022}
2023
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002024TEST(Matcher, HandlesNullQualTypes) {
2025 // FIXME: Add a Type matcher so we can replace uses of this
2026 // variable with Type(True())
2027 const TypeMatcher AnyType = anything();
2028
2029 // We don't really care whether this matcher succeeds; we're testing that
2030 // it completes without crashing.
2031 EXPECT_TRUE(matches(
2032 "struct A { };"
2033 "template <typename T>"
2034 "void f(T t) {"
2035 " T local_t(t /* this becomes a null QualType in the AST */);"
2036 "}"
2037 "void g() {"
2038 " f(0);"
2039 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002040 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002041 anyOf(
2042 TypeMatcher(hasDeclaration(anything())),
2043 pointsTo(AnyType),
2044 references(AnyType)
2045 // Other QualType matchers should go here.
2046 ))))));
2047}
2048
Manuel Klimek4da21662012-07-06 05:48:52 +00002049// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002050AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002051 // Make sure all special variables are used: node, match_finder,
2052 // bound_nodes_builder, and the parameter named 'AMatcher'.
2053 return AMatcher.matches(Node, Finder, Builder);
2054}
2055
2056TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002057 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002058
2059 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002060 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002061
2062 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002063 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002064
2065 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002066 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002067}
2068
2069AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002070 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
2071 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
2072 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00002073 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00002074 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002075 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002076 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2077 ASTMatchFinder::BK_First);
2078}
2079
2080TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002081 DeclarationMatcher HasClassB =
2082 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002083
2084 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002085 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002086
2087 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002088 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002089
2090 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002091 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002092
2093 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002094 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002095
2096 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2097}
2098
2099TEST(For, FindsForLoops) {
2100 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2101 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002102 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2103 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002104 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002105}
2106
Daniel Jasper6a124492012-07-12 08:50:38 +00002107TEST(For, ForLoopInternals) {
2108 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2109 forStmt(hasCondition(anything()))));
2110 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2111 forStmt(hasLoopInit(anything()))));
2112}
2113
2114TEST(For, NegativeForLoopInternals) {
2115 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002116 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002117 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2118 forStmt(hasLoopInit(anything()))));
2119}
2120
Manuel Klimek4da21662012-07-06 05:48:52 +00002121TEST(For, ReportsNoFalsePositives) {
2122 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2123 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2124}
2125
2126TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002127 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2128 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2129 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002130}
2131
2132TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2133 // It's not a compound statement just because there's "{}" in the source
2134 // text. This is an AST search, not grep.
2135 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002136 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002137 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002138 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002139}
2140
Daniel Jasper6a124492012-07-12 08:50:38 +00002141TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002142 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002143 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002144 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002145 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002146 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002147 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002148 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002149 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002150}
2151
2152TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2153 // The simplest case: every compound statement is in a function
2154 // definition, and the function body itself must be a compound
2155 // statement.
2156 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002157 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002158}
2159
2160TEST(HasAnySubstatement, IsNotRecursive) {
2161 // It's really "has any immediate substatement".
2162 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002163 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002164}
2165
2166TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2167 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002168 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002169}
2170
2171TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2172 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002173 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002174}
2175
2176TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2177 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002178 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002179 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002180 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002181}
2182
2183TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2184 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002185 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002186 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002187 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002188 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002189 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002190}
2191
2192TEST(StatementCountIs, WorksWithMultipleStatements) {
2193 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002194 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002195}
2196
2197TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2198 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002199 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002200 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002201 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002202 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002203 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002204 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002205 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002206}
2207
2208TEST(Member, WorksInSimplestCase) {
2209 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002210 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002211}
2212
2213TEST(Member, DoesNotMatchTheBaseExpression) {
2214 // Don't pick out the wrong part of the member expression, this should
2215 // be checking the member (name) only.
2216 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002217 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002218}
2219
2220TEST(Member, MatchesInMemberFunctionCall) {
2221 EXPECT_TRUE(matches("void f() {"
2222 " struct { void first() {}; } s;"
2223 " s.first();"
2224 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002225 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002226}
2227
Daniel Jasperc711af22012-10-23 15:46:39 +00002228TEST(Member, MatchesMember) {
2229 EXPECT_TRUE(matches(
2230 "struct A { int i; }; void f() { A a; a.i = 2; }",
2231 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2232 EXPECT_TRUE(notMatches(
2233 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2234 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2235}
2236
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002237TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002238 // Fails in C++11 mode
2239 EXPECT_TRUE(matchesConditionally(
2240 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2241 "class X { void *operator new(std::size_t); };",
2242 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002243
2244 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002245 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002246
Daniel Jasper31f7c082012-10-01 13:40:41 +00002247 // Fails in C++11 mode
2248 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002249 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2250 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002251 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002252}
2253
Manuel Klimek4da21662012-07-06 05:48:52 +00002254TEST(HasObjectExpression, DoesNotMatchMember) {
2255 EXPECT_TRUE(notMatches(
2256 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002257 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002258}
2259
2260TEST(HasObjectExpression, MatchesBaseOfVariable) {
2261 EXPECT_TRUE(matches(
2262 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002263 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002264 EXPECT_TRUE(matches(
2265 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002266 memberExpr(hasObjectExpression(
2267 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002268}
2269
2270TEST(HasObjectExpression,
2271 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2272 EXPECT_TRUE(matches(
2273 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002274 memberExpr(hasObjectExpression(
2275 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002276 EXPECT_TRUE(matches(
2277 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002278 memberExpr(hasObjectExpression(
2279 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002280}
2281
2282TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002283 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2284 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2285 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2286 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002287}
2288
2289TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002290 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002291}
2292
2293TEST(IsConstQualified, MatchesConstInt) {
2294 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002295 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002296}
2297
2298TEST(IsConstQualified, MatchesConstPointer) {
2299 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002300 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002301}
2302
2303TEST(IsConstQualified, MatchesThroughTypedef) {
2304 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002305 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002306 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002307 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002308}
2309
2310TEST(IsConstQualified, DoesNotMatchInappropriately) {
2311 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002312 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002313 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002314 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002315}
2316
Sam Panzer089e5b32012-08-16 16:58:10 +00002317TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002318 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2319 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2320 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2321 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002322}
2323TEST(CastExpression, MatchesImplicitCasts) {
2324 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002325 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002326 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002327 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002328}
2329
2330TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002331 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2332 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2333 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2334 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002335}
2336
Manuel Klimek4da21662012-07-06 05:48:52 +00002337TEST(ReinterpretCast, MatchesSimpleCase) {
2338 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002339 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002340}
2341
2342TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002343 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002344 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002345 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002346 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002347 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002348 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2349 "B b;"
2350 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002351 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002352}
2353
2354TEST(FunctionalCast, MatchesSimpleCase) {
2355 std::string foo_class = "class Foo { public: Foo(char*); };";
2356 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002357 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002358}
2359
2360TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2361 std::string FooClass = "class Foo { public: Foo(char*); };";
2362 EXPECT_TRUE(
2363 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002364 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002365 EXPECT_TRUE(
2366 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002367 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002368}
2369
2370TEST(DynamicCast, MatchesSimpleCase) {
2371 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2372 "B b;"
2373 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002374 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002375}
2376
2377TEST(StaticCast, MatchesSimpleCase) {
2378 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002379 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002380}
2381
2382TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002383 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002384 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002385 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002386 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002387 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002388 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2389 "B b;"
2390 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002391 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002392}
2393
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002394TEST(CStyleCast, MatchesSimpleCase) {
2395 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2396}
2397
2398TEST(CStyleCast, DoesNotMatchOtherCasts) {
2399 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2400 "char q, *r = const_cast<char*>(&q);"
2401 "void* s = reinterpret_cast<char*>(&s);"
2402 "struct B { virtual ~B() {} }; struct D : B {};"
2403 "B b;"
2404 "D* t = dynamic_cast<D*>(&b);",
2405 cStyleCastExpr()));
2406}
2407
Manuel Klimek4da21662012-07-06 05:48:52 +00002408TEST(HasDestinationType, MatchesSimpleCase) {
2409 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002410 staticCastExpr(hasDestinationType(
2411 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002412}
2413
Sam Panzer089e5b32012-08-16 16:58:10 +00002414TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2415 // This test creates an implicit const cast.
2416 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002417 implicitCastExpr(
2418 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002419 // This test creates an implicit array-to-pointer cast.
2420 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002421 implicitCastExpr(hasImplicitDestinationType(
2422 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002423}
2424
2425TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2426 // This test creates an implicit cast from int to char.
2427 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002428 implicitCastExpr(hasImplicitDestinationType(
2429 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002430 // This test creates an implicit array-to-pointer cast.
2431 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002432 implicitCastExpr(hasImplicitDestinationType(
2433 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002434}
2435
2436TEST(ImplicitCast, MatchesSimpleCase) {
2437 // This test creates an implicit const cast.
2438 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002439 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002440 // This test creates an implicit cast from int to char.
2441 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002442 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002443 // This test creates an implicit array-to-pointer cast.
2444 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002445 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002446}
2447
2448TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002449 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002450 // are present, and that it ignores explicit and paren casts.
2451
2452 // These two test cases have no casts.
2453 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002454 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002455 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002456 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002457
2458 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002459 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002460 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002461 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002462
2463 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002464 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002465}
2466
2467TEST(IgnoringImpCasts, MatchesImpCasts) {
2468 // This test checks that ignoringImpCasts matches when implicit casts are
2469 // present and its inner matcher alone does not match.
2470 // Note that this test creates an implicit const cast.
2471 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002472 varDecl(hasInitializer(ignoringImpCasts(
2473 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002474 // This test creates an implict cast from int to char.
2475 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002476 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002477 integerLiteral(equals(0)))))));
2478}
2479
2480TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2481 // These tests verify that ignoringImpCasts does not match if the inner
2482 // matcher does not match.
2483 // Note that the first test creates an implicit const cast.
2484 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002485 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002486 unless(anything()))))));
2487 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002488 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002489 unless(anything()))))));
2490
2491 // These tests verify that ignoringImplictCasts does not look through explicit
2492 // casts or parentheses.
2493 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002494 varDecl(hasInitializer(ignoringImpCasts(
2495 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002496 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002497 varDecl(hasInitializer(ignoringImpCasts(
2498 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002499 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002500 varDecl(hasInitializer(ignoringImpCasts(
2501 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002502 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002503 varDecl(hasInitializer(ignoringImpCasts(
2504 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002505}
2506
2507TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2508 // This test verifies that expressions that do not have implicit casts
2509 // still match the inner matcher.
2510 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002511 varDecl(hasInitializer(ignoringImpCasts(
2512 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002513}
2514
2515TEST(IgnoringParenCasts, MatchesParenCasts) {
2516 // This test checks that ignoringParenCasts matches when parentheses and/or
2517 // casts are present and its inner matcher alone does not match.
2518 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002519 varDecl(hasInitializer(ignoringParenCasts(
2520 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002521 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002522 varDecl(hasInitializer(ignoringParenCasts(
2523 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002524
2525 // This test creates an implict cast from int to char in addition to the
2526 // parentheses.
2527 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002528 varDecl(hasInitializer(ignoringParenCasts(
2529 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002530
2531 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002532 varDecl(hasInitializer(ignoringParenCasts(
2533 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002534 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002535 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002536 integerLiteral(equals(0)))))));
2537}
2538
2539TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2540 // This test verifies that expressions that do not have any casts still match.
2541 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002542 varDecl(hasInitializer(ignoringParenCasts(
2543 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002544}
2545
2546TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2547 // These tests verify that ignoringImpCasts does not match if the inner
2548 // matcher does not match.
2549 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002550 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002551 unless(anything()))))));
2552
2553 // This test creates an implicit cast from int to char in addition to the
2554 // parentheses.
2555 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002556 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002557 unless(anything()))))));
2558
2559 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002560 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002561 unless(anything()))))));
2562}
2563
2564TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2565 // This test checks that ignoringParenAndImpCasts matches when
2566 // parentheses and/or implicit casts are present and its inner matcher alone
2567 // does not match.
2568 // Note that this test creates an implicit const cast.
2569 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002570 varDecl(hasInitializer(ignoringParenImpCasts(
2571 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002572 // This test creates an implicit cast from int to char.
2573 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002574 varDecl(hasInitializer(ignoringParenImpCasts(
2575 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002576}
2577
2578TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2579 // This test verifies that expressions that do not have parentheses or
2580 // implicit casts still match.
2581 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002582 varDecl(hasInitializer(ignoringParenImpCasts(
2583 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002584 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002585 varDecl(hasInitializer(ignoringParenImpCasts(
2586 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002587}
2588
2589TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2590 // These tests verify that ignoringParenImpCasts does not match if
2591 // the inner matcher does not match.
2592 // This test creates an implicit cast.
2593 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002594 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002595 unless(anything()))))));
2596 // These tests verify that ignoringParenAndImplictCasts does not look
2597 // through explicit casts.
2598 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002599 varDecl(hasInitializer(ignoringParenImpCasts(
2600 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002601 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002602 varDecl(hasInitializer(ignoringParenImpCasts(
2603 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002604 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002605 varDecl(hasInitializer(ignoringParenImpCasts(
2606 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002607}
2608
Manuel Klimek715c9562012-07-25 10:02:02 +00002609TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002610 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2611 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002612 implicitCastExpr(
2613 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002614}
2615
Manuel Klimek715c9562012-07-25 10:02:02 +00002616TEST(HasSourceExpression, MatchesExplicitCasts) {
2617 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002618 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002619 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002620 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002621}
2622
Manuel Klimek4da21662012-07-06 05:48:52 +00002623TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002624 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002625}
2626
2627TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002628 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002629}
2630
2631TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002632 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002633}
2634
2635TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002636 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002637}
2638
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002639TEST(InitListExpression, MatchesInitListExpression) {
2640 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2641 initListExpr(hasType(asString("int [2]")))));
2642 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002643 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002644}
2645
2646TEST(UsingDeclaration, MatchesUsingDeclarations) {
2647 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2648 usingDecl()));
2649}
2650
2651TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2652 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2653 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2654}
2655
2656TEST(UsingDeclaration, MatchesSpecificTarget) {
2657 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2658 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002659 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002660 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2661 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002662 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002663}
2664
2665TEST(UsingDeclaration, ThroughUsingDeclaration) {
2666 EXPECT_TRUE(matches(
2667 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002668 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002669 EXPECT_TRUE(notMatches(
2670 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002671 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002672}
2673
Sam Panzer425f41b2012-08-16 17:20:59 +00002674TEST(SingleDecl, IsSingleDecl) {
2675 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002676 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002677 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2678 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2679 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2680 SingleDeclStmt));
2681}
2682
2683TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002684 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002685
2686 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002687 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002688 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002689 declStmt(containsDeclaration(0, MatchesInit),
2690 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002691 unsigned WrongIndex = 42;
2692 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002693 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002694 MatchesInit))));
2695}
2696
2697TEST(DeclCount, DeclCountIsCorrect) {
2698 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002699 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002700 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002701 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002702 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002703 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002704}
2705
Manuel Klimek4da21662012-07-06 05:48:52 +00002706TEST(While, MatchesWhileLoops) {
2707 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2708 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2709 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2710}
2711
2712TEST(Do, MatchesDoLoops) {
2713 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2714 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2715}
2716
2717TEST(Do, DoesNotMatchWhileLoops) {
2718 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2719}
2720
2721TEST(SwitchCase, MatchesCase) {
2722 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2723 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2724 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2725 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2726}
2727
Daniel Jasperb54b7642012-09-20 14:12:57 +00002728TEST(SwitchCase, MatchesSwitch) {
2729 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2730 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2731 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2732 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2733}
2734
2735TEST(ExceptionHandling, SimpleCases) {
2736 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2737 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2738 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2739 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2740 throwExpr()));
2741 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2742 throwExpr()));
2743}
2744
Manuel Klimek4da21662012-07-06 05:48:52 +00002745TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2746 EXPECT_TRUE(notMatches(
2747 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002748 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002749 EXPECT_TRUE(notMatches(
2750 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002751 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002752}
2753
2754TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2755 EXPECT_TRUE(matches(
2756 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002757 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002758}
2759
2760TEST(ForEach, BindsOneNode) {
2761 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002762 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002763 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002764}
2765
2766TEST(ForEach, BindsMultipleNodes) {
2767 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002768 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002769 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002770}
2771
2772TEST(ForEach, BindsRecursiveCombinations) {
2773 EXPECT_TRUE(matchAndVerifyResultTrue(
2774 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002775 recordDecl(hasName("C"),
2776 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002777 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002778}
2779
2780TEST(ForEachDescendant, BindsOneNode) {
2781 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002782 recordDecl(hasName("C"),
2783 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002784 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002785}
2786
Daniel Jasper5f684e92012-11-16 18:39:22 +00002787TEST(ForEachDescendant, NestedForEachDescendant) {
2788 DeclarationMatcher m = recordDecl(
2789 isDefinition(), decl().bind("x"), hasName("C"));
2790 EXPECT_TRUE(matchAndVerifyResultTrue(
2791 "class A { class B { class C {}; }; };",
2792 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
2793 new VerifyIdIsBoundTo<Decl>("x", "C")));
2794
2795 // FIXME: This is not really a useful matcher, but the result is still
2796 // surprising (currently binds "A").
2797 //EXPECT_TRUE(matchAndVerifyResultTrue(
2798 // "class A { class B { class C {}; }; };",
2799 // recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
2800 // new VerifyIdIsBoundTo<Decl>("x", "C")));
2801}
2802
Manuel Klimek4da21662012-07-06 05:48:52 +00002803TEST(ForEachDescendant, BindsMultipleNodes) {
2804 EXPECT_TRUE(matchAndVerifyResultTrue(
2805 "class C { class D { int x; int y; }; "
2806 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002807 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002808 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002809}
2810
2811TEST(ForEachDescendant, BindsRecursiveCombinations) {
2812 EXPECT_TRUE(matchAndVerifyResultTrue(
2813 "class C { class D { "
2814 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002815 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2816 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002817 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002818}
2819
Daniel Jasper11c98772012-11-11 22:14:55 +00002820TEST(ForEachDescendant, BindsCorrectNodes) {
2821 EXPECT_TRUE(matchAndVerifyResultTrue(
2822 "class C { void f(); int i; };",
2823 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2824 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
2825 EXPECT_TRUE(matchAndVerifyResultTrue(
2826 "class C { void f() {} int i; };",
2827 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2828 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
2829}
2830
Manuel Klimek4da21662012-07-06 05:48:52 +00002831
2832TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2833 // Make sure that we can both match the class by name (::X) and by the type
2834 // the template was instantiated with (via a field).
2835
2836 EXPECT_TRUE(matches(
2837 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002838 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002839
2840 EXPECT_TRUE(matches(
2841 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002842 recordDecl(isTemplateInstantiation(), hasDescendant(
2843 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002844}
2845
2846TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2847 EXPECT_TRUE(matches(
2848 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002849 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002850 isTemplateInstantiation())));
2851}
2852
2853TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2854 EXPECT_TRUE(matches(
2855 "template <typename T> class X { T t; }; class A {};"
2856 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002857 recordDecl(isTemplateInstantiation(), hasDescendant(
2858 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002859}
2860
2861TEST(IsTemplateInstantiation,
2862 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2863 EXPECT_TRUE(matches(
2864 "template <typename T> class X {};"
2865 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002866 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002867}
2868
2869TEST(IsTemplateInstantiation,
2870 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2871 EXPECT_TRUE(matches(
2872 "class A {};"
2873 "class X {"
2874 " template <typename U> class Y { U u; };"
2875 " Y<A> y;"
2876 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002877 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002878}
2879
2880TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2881 // FIXME: Figure out whether this makes sense. It doesn't affect the
2882 // normal use case as long as the uppermost instantiation always is marked
2883 // as template instantiation, but it might be confusing as a predicate.
2884 EXPECT_TRUE(matches(
2885 "class A {};"
2886 "template <typename T> class X {"
2887 " template <typename U> class Y { U u; };"
2888 " Y<T> y;"
2889 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002890 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002891}
2892
2893TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2894 EXPECT_TRUE(notMatches(
2895 "template <typename T> class X {}; class A {};"
2896 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002897 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002898}
2899
2900TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2901 EXPECT_TRUE(notMatches(
2902 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002903 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002904}
2905
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002906TEST(IsExplicitTemplateSpecialization,
2907 DoesNotMatchPrimaryTemplate) {
2908 EXPECT_TRUE(notMatches(
2909 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002910 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002911 EXPECT_TRUE(notMatches(
2912 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002913 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002914}
2915
2916TEST(IsExplicitTemplateSpecialization,
2917 DoesNotMatchExplicitTemplateInstantiations) {
2918 EXPECT_TRUE(notMatches(
2919 "template <typename T> class X {};"
2920 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002921 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002922 EXPECT_TRUE(notMatches(
2923 "template <typename T> void f(T t) {}"
2924 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002925 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002926}
2927
2928TEST(IsExplicitTemplateSpecialization,
2929 DoesNotMatchImplicitTemplateInstantiations) {
2930 EXPECT_TRUE(notMatches(
2931 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002932 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002933 EXPECT_TRUE(notMatches(
2934 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002935 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002936}
2937
2938TEST(IsExplicitTemplateSpecialization,
2939 MatchesExplicitTemplateSpecializations) {
2940 EXPECT_TRUE(matches(
2941 "template <typename T> class X {};"
2942 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002943 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002944 EXPECT_TRUE(matches(
2945 "template <typename T> void f(T t) {}"
2946 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002947 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002948}
2949
Manuel Klimek579b1202012-09-07 09:26:10 +00002950TEST(HasAncenstor, MatchesDeclarationAncestors) {
2951 EXPECT_TRUE(matches(
2952 "class A { class B { class C {}; }; };",
2953 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2954}
2955
2956TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2957 EXPECT_TRUE(notMatches(
2958 "class A { class B { class C {}; }; };",
2959 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2960}
2961
2962TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2963 EXPECT_TRUE(matches(
2964 "class A { class B { void f() { C c; } class C {}; }; };",
2965 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2966 hasAncestor(recordDecl(hasName("A"))))))));
2967}
2968
2969TEST(HasAncenstor, MatchesStatementAncestors) {
2970 EXPECT_TRUE(matches(
2971 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002972 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002973}
2974
2975TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2976 EXPECT_TRUE(matches(
2977 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002978 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002979}
2980
2981TEST(HasAncestor, BindsRecursiveCombinations) {
2982 EXPECT_TRUE(matchAndVerifyResultTrue(
2983 "class C { class D { class E { class F { int y; }; }; }; };",
2984 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002985 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00002986}
2987
2988TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
2989 EXPECT_TRUE(matchAndVerifyResultTrue(
2990 "class C { class D { class E { class F { int y; }; }; }; };",
2991 fieldDecl(hasAncestor(
2992 decl(
2993 hasDescendant(recordDecl(isDefinition(),
2994 hasAncestor(recordDecl())))
2995 ).bind("d")
2996 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00002997 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00002998}
2999
3000TEST(HasAncestor, MatchesInTemplateInstantiations) {
3001 EXPECT_TRUE(matches(
3002 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3003 "A<int>::B::C a;",
3004 fieldDecl(hasType(asString("int")),
3005 hasAncestor(recordDecl(hasName("A"))))));
3006}
3007
3008TEST(HasAncestor, MatchesInImplicitCode) {
3009 EXPECT_TRUE(matches(
3010 "struct X {}; struct A { A() {} X x; };",
3011 constructorDecl(
3012 hasAnyConstructorInitializer(withInitializer(expr(
3013 hasAncestor(recordDecl(hasName("A")))))))));
3014}
3015
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003016TEST(HasParent, MatchesOnlyParent) {
3017 EXPECT_TRUE(matches(
3018 "void f() { if (true) { int x = 42; } }",
3019 compoundStmt(hasParent(ifStmt()))));
3020 EXPECT_TRUE(notMatches(
3021 "void f() { for (;;) { int x = 42; } }",
3022 compoundStmt(hasParent(ifStmt()))));
3023 EXPECT_TRUE(notMatches(
3024 "void f() { if (true) for (;;) { int x = 42; } }",
3025 compoundStmt(hasParent(ifStmt()))));
3026}
3027
Daniel Jasperce620072012-10-17 08:52:59 +00003028TEST(TypeMatching, MatchesTypes) {
3029 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3030}
3031
3032TEST(TypeMatching, MatchesArrayTypes) {
3033 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3034 EXPECT_TRUE(matches("int a[42];", arrayType()));
3035 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3036
3037 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3038 arrayType(hasElementType(builtinType()))));
3039
3040 EXPECT_TRUE(matches(
3041 "int const a[] = { 2, 3 };",
3042 qualType(arrayType(hasElementType(builtinType())))));
3043 EXPECT_TRUE(matches(
3044 "int const a[] = { 2, 3 };",
3045 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3046 EXPECT_TRUE(matches(
3047 "typedef const int T; T x[] = { 1, 2 };",
3048 qualType(isConstQualified(), arrayType())));
3049
3050 EXPECT_TRUE(notMatches(
3051 "int a[] = { 2, 3 };",
3052 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3053 EXPECT_TRUE(notMatches(
3054 "int a[] = { 2, 3 };",
3055 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3056 EXPECT_TRUE(notMatches(
3057 "int const a[] = { 2, 3 };",
3058 qualType(arrayType(hasElementType(builtinType())),
3059 unless(isConstQualified()))));
3060
3061 EXPECT_TRUE(matches("int a[2];",
3062 constantArrayType(hasElementType(builtinType()))));
3063 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3064}
3065
3066TEST(TypeMatching, MatchesComplexTypes) {
3067 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3068 EXPECT_TRUE(matches(
3069 "_Complex float f;",
3070 complexType(hasElementType(builtinType()))));
3071 EXPECT_TRUE(notMatches(
3072 "_Complex float f;",
3073 complexType(hasElementType(isInteger()))));
3074}
3075
3076TEST(TypeMatching, MatchesConstantArrayTypes) {
3077 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3078 EXPECT_TRUE(notMatches(
3079 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3080 constantArrayType(hasElementType(builtinType()))));
3081
3082 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3083 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3084 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3085}
3086
3087TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3088 EXPECT_TRUE(matches(
3089 "template <typename T, int Size> class array { T data[Size]; };",
3090 dependentSizedArrayType()));
3091 EXPECT_TRUE(notMatches(
3092 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3093 dependentSizedArrayType()));
3094}
3095
3096TEST(TypeMatching, MatchesIncompleteArrayType) {
3097 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3098 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3099
3100 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3101 incompleteArrayType()));
3102}
3103
3104TEST(TypeMatching, MatchesVariableArrayType) {
3105 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3106 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3107
3108 EXPECT_TRUE(matches(
3109 "void f(int b) { int a[b]; }",
3110 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3111 varDecl(hasName("b")))))))));
3112}
3113
3114TEST(TypeMatching, MatchesAtomicTypes) {
3115 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3116
3117 EXPECT_TRUE(matches("_Atomic(int) i;",
3118 atomicType(hasValueType(isInteger()))));
3119 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3120 atomicType(hasValueType(isInteger()))));
3121}
3122
3123TEST(TypeMatching, MatchesAutoTypes) {
3124 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3125 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3126 autoType()));
3127
3128 EXPECT_TRUE(matches("auto a = 1;",
3129 autoType(hasDeducedType(isInteger()))));
3130 EXPECT_TRUE(notMatches("auto b = 2.0;",
3131 autoType(hasDeducedType(isInteger()))));
3132}
3133
Daniel Jaspera267cf62012-10-29 10:14:44 +00003134TEST(TypeMatching, MatchesFunctionTypes) {
3135 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3136 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3137}
3138
Daniel Jasperce620072012-10-17 08:52:59 +00003139TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003140 // FIXME: Reactive when these tests can be more specific (not matching
3141 // implicit code on certain platforms), likely when we have hasDescendant for
3142 // Types/TypeLocs.
3143 //EXPECT_TRUE(matchAndVerifyResultTrue(
3144 // "int* a;",
3145 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3146 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3147 //EXPECT_TRUE(matchAndVerifyResultTrue(
3148 // "int* a;",
3149 // pointerTypeLoc().bind("loc"),
3150 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003151 EXPECT_TRUE(matches(
3152 "int** a;",
3153 pointerTypeLoc(pointeeLoc(loc(qualType())))));
3154 EXPECT_TRUE(matches(
3155 "int** a;",
3156 loc(pointerType(pointee(pointerType())))));
3157 EXPECT_TRUE(matches(
3158 "int* b; int* * const a = &b;",
3159 loc(qualType(isConstQualified(), pointerType()))));
3160
3161 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003162 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3163 hasType(blockPointerType()))));
3164 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3165 hasType(memberPointerType()))));
3166 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3167 hasType(pointerType()))));
3168 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3169 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003170
Daniel Jasper1802daf2012-10-17 13:35:36 +00003171 Fragment = "int *ptr;";
3172 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3173 hasType(blockPointerType()))));
3174 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3175 hasType(memberPointerType()))));
3176 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3177 hasType(pointerType()))));
3178 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3179 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003180
Daniel Jasper1802daf2012-10-17 13:35:36 +00003181 Fragment = "int a; int &ref = a;";
3182 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3183 hasType(blockPointerType()))));
3184 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3185 hasType(memberPointerType()))));
3186 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3187 hasType(pointerType()))));
3188 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3189 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003190}
3191
3192TEST(TypeMatching, PointeeTypes) {
3193 EXPECT_TRUE(matches("int b; int &a = b;",
3194 referenceType(pointee(builtinType()))));
3195 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3196
3197 EXPECT_TRUE(matches("int *a;",
3198 pointerTypeLoc(pointeeLoc(loc(builtinType())))));
3199
3200 EXPECT_TRUE(matches(
3201 "int const *A;",
3202 pointerType(pointee(isConstQualified(), builtinType()))));
3203 EXPECT_TRUE(notMatches(
3204 "int *A;",
3205 pointerType(pointee(isConstQualified(), builtinType()))));
3206}
3207
3208TEST(TypeMatching, MatchesPointersToConstTypes) {
3209 EXPECT_TRUE(matches("int b; int * const a = &b;",
3210 loc(pointerType())));
3211 EXPECT_TRUE(matches("int b; int * const a = &b;",
3212 pointerTypeLoc()));
3213 EXPECT_TRUE(matches(
3214 "int b; const int * a = &b;",
3215 pointerTypeLoc(pointeeLoc(builtinTypeLoc()))));
3216 EXPECT_TRUE(matches(
3217 "int b; const int * a = &b;",
3218 pointerType(pointee(builtinType()))));
3219}
3220
3221TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003222 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3223 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003224
Daniel Jasper1802daf2012-10-17 13:35:36 +00003225 EXPECT_TRUE(matches("typedef int X; X a;",
3226 varDecl(hasName("a"),
3227 hasType(typedefType(hasDecl(decl()))))));
Daniel Jasperce620072012-10-17 08:52:59 +00003228}
3229
Daniel Jaspera7564432012-09-13 13:11:25 +00003230TEST(NNS, MatchesNestedNameSpecifiers) {
3231 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3232 nestedNameSpecifier()));
3233 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3234 nestedNameSpecifier()));
3235 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3236 nestedNameSpecifier()));
3237
3238 EXPECT_TRUE(matches(
3239 "struct A { static void f() {} }; void g() { A::f(); }",
3240 nestedNameSpecifier()));
3241 EXPECT_TRUE(notMatches(
3242 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3243 nestedNameSpecifier()));
3244}
3245
Daniel Jasperb54b7642012-09-20 14:12:57 +00003246TEST(NullStatement, SimpleCases) {
3247 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3248 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3249}
3250
Daniel Jaspera7564432012-09-13 13:11:25 +00003251TEST(NNS, MatchesTypes) {
3252 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3253 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3254 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3255 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3256 Matcher));
3257 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3258}
3259
3260TEST(NNS, MatchesNamespaceDecls) {
3261 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3262 specifiesNamespace(hasName("ns")));
3263 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3264 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3265 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3266}
3267
3268TEST(NNS, BindsNestedNameSpecifiers) {
3269 EXPECT_TRUE(matchAndVerifyResultTrue(
3270 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3271 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3272 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3273}
3274
3275TEST(NNS, BindsNestedNameSpecifierLocs) {
3276 EXPECT_TRUE(matchAndVerifyResultTrue(
3277 "namespace ns { struct B {}; } ns::B b;",
3278 loc(nestedNameSpecifier()).bind("loc"),
3279 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3280}
3281
3282TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3283 EXPECT_TRUE(matches(
3284 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3285 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3286 EXPECT_TRUE(matches(
3287 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003288 nestedNameSpecifierLoc(hasPrefix(
3289 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003290}
3291
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003292TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3293 std::string Fragment =
3294 "namespace a { struct A { struct B { struct C {}; }; }; };"
3295 "void f() { a::A::B::C c; }";
3296 EXPECT_TRUE(matches(
3297 Fragment,
3298 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3299 hasDescendant(nestedNameSpecifier(
3300 specifiesNamespace(hasName("a")))))));
3301 EXPECT_TRUE(notMatches(
3302 Fragment,
3303 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3304 has(nestedNameSpecifier(
3305 specifiesNamespace(hasName("a")))))));
3306 EXPECT_TRUE(matches(
3307 Fragment,
3308 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3309 has(nestedNameSpecifier(
3310 specifiesNamespace(hasName("a")))))));
3311
3312 // Not really useful because a NestedNameSpecifier can af at most one child,
3313 // but to complete the interface.
3314 EXPECT_TRUE(matchAndVerifyResultTrue(
3315 Fragment,
3316 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3317 forEach(nestedNameSpecifier().bind("x"))),
3318 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3319}
3320
3321TEST(NNS, NestedNameSpecifiersAsDescendants) {
3322 std::string Fragment =
3323 "namespace a { struct A { struct B { struct C {}; }; }; };"
3324 "void f() { a::A::B::C c; }";
3325 EXPECT_TRUE(matches(
3326 Fragment,
3327 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3328 asString("struct a::A")))))));
3329 EXPECT_TRUE(matchAndVerifyResultTrue(
3330 Fragment,
3331 functionDecl(hasName("f"),
3332 forEachDescendant(nestedNameSpecifier().bind("x"))),
3333 // Nested names: a, a::A and a::A::B.
3334 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3335}
3336
3337TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3338 std::string Fragment =
3339 "namespace a { struct A { struct B { struct C {}; }; }; };"
3340 "void f() { a::A::B::C c; }";
3341 EXPECT_TRUE(matches(
3342 Fragment,
3343 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3344 hasDescendant(loc(nestedNameSpecifier(
3345 specifiesNamespace(hasName("a"))))))));
3346 EXPECT_TRUE(notMatches(
3347 Fragment,
3348 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3349 has(loc(nestedNameSpecifier(
3350 specifiesNamespace(hasName("a"))))))));
3351 EXPECT_TRUE(matches(
3352 Fragment,
3353 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3354 has(loc(nestedNameSpecifier(
3355 specifiesNamespace(hasName("a"))))))));
3356
3357 EXPECT_TRUE(matchAndVerifyResultTrue(
3358 Fragment,
3359 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3360 forEach(nestedNameSpecifierLoc().bind("x"))),
3361 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3362}
3363
3364TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3365 std::string Fragment =
3366 "namespace a { struct A { struct B { struct C {}; }; }; };"
3367 "void f() { a::A::B::C c; }";
3368 EXPECT_TRUE(matches(
3369 Fragment,
3370 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3371 asString("struct a::A"))))))));
3372 EXPECT_TRUE(matchAndVerifyResultTrue(
3373 Fragment,
3374 functionDecl(hasName("f"),
3375 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3376 // Nested names: a, a::A and a::A::B.
3377 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3378}
3379
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003380template <typename T>
3381class VerifyRecursiveMatch : public BoundNodesCallback {
3382public:
3383 explicit VerifyRecursiveMatch(StringRef Id,
3384 const internal::Matcher<T> &InnerMatcher)
3385 : Id(Id), InnerMatcher(InnerMatcher) {}
Daniel Jasper452abbc2012-10-29 10:48:25 +00003386
3387 virtual bool run(const BoundNodes *Nodes) {
3388 return false;
3389 }
3390
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003391 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3392 const T *Node = Nodes->getNodeAs<T>(Id);
3393 bool Found = false;
3394 MatchFinder Finder;
3395 Finder.addMatcher(InnerMatcher, new VerifyMatch(0, &Found));
3396 Finder.findAll(*Node, *Context);
3397 return Found;
3398 }
3399private:
3400 std::string Id;
3401 internal::Matcher<T> InnerMatcher;
3402};
3403
3404TEST(MatchFinder, CanMatchDeclarationsRecursively) {
3405 EXPECT_TRUE(matchAndVerifyResultTrue("class X { class Y {}; };",
3406 recordDecl(hasName("::X")).bind("X"),
3407 new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Y")))));
3408 EXPECT_TRUE(matchAndVerifyResultFalse("class X { class Y {}; };",
3409 recordDecl(hasName("::X")).bind("X"),
3410 new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Z")))));
3411}
3412
3413TEST(MatchFinder, CanMatchStatementsRecursively) {
3414 EXPECT_TRUE(matchAndVerifyResultTrue("void f() { if (1) { for (;;) { } } }",
3415 ifStmt().bind("if"),
3416 new VerifyRecursiveMatch<clang::Stmt>("if", forStmt())));
3417 EXPECT_TRUE(matchAndVerifyResultFalse("void f() { if (1) { for (;;) { } } }",
3418 ifStmt().bind("if"),
3419 new VerifyRecursiveMatch<clang::Stmt>("if", declStmt())));
3420}
3421
Manuel Klimeke5793282012-11-02 01:31:03 +00003422class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
3423public:
3424 VerifyStartOfTranslationUnit() : Called(false) {}
3425 virtual void run(const MatchFinder::MatchResult &Result) {
3426 EXPECT_TRUE(Called);
3427 }
3428 virtual void onStartOfTranslationUnit() {
3429 Called = true;
3430 }
3431 bool Called;
3432};
3433
3434TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
3435 MatchFinder Finder;
3436 VerifyStartOfTranslationUnit VerifyCallback;
3437 Finder.addMatcher(decl(), &VerifyCallback);
3438 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
3439 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
3440 EXPECT_TRUE(VerifyCallback.Called);
3441}
3442
Manuel Klimek4da21662012-07-06 05:48:52 +00003443} // end namespace ast_matchers
3444} // end namespace clang