blob: 32b149625216c4c776d5489ebdf8730c14d95da8 [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));
Manuel Klimek987c2f52012-12-04 13:40:29 +0000235 EXPECT_TRUE(
236 notMatches("template<int> struct X;"
237 "template<int i> struct X : public X<i-1> {};",
238 recordDecl(isDerivedFrom(recordDecl(hasName("Some"))))));
239 EXPECT_TRUE(matches(
240 "struct A {};"
241 "template<int> struct X;"
242 "template<int i> struct X : public X<i-1> {};"
243 "template<> struct X<0> : public A {};"
244 "struct B : public X<42> {};",
245 recordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000246
247 // FIXME: Once we have better matchers for template type matching,
248 // get rid of the Variable(...) matching and match the right template
249 // declarations directly.
250 const char *RecursiveTemplateOneParameter =
251 "class Base1 {}; class Base2 {};"
252 "template <typename T> class Z;"
253 "template <> class Z<void> : public Base1 {};"
254 "template <> class Z<int> : public Base2 {};"
255 "template <> class Z<float> : public Z<void> {};"
256 "template <> class Z<double> : public Z<int> {};"
257 "template <typename T> class Z : public Z<float>, public Z<double> {};"
258 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
259 EXPECT_TRUE(matches(
260 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000261 varDecl(hasName("z_float"),
262 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000263 EXPECT_TRUE(notMatches(
264 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000265 varDecl(hasName("z_float"),
266 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000267 EXPECT_TRUE(matches(
268 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000269 varDecl(hasName("z_char"),
270 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
271 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000272
273 const char *RecursiveTemplateTwoParameters =
274 "class Base1 {}; class Base2 {};"
275 "template <typename T1, typename T2> class Z;"
276 "template <typename T> class Z<void, T> : public Base1 {};"
277 "template <typename T> class Z<int, T> : public Base2 {};"
278 "template <typename T> class Z<float, T> : public Z<void, T> {};"
279 "template <typename T> class Z<double, T> : public Z<int, T> {};"
280 "template <typename T1, typename T2> class Z : "
281 " public Z<float, T2>, public Z<double, T2> {};"
282 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
283 " Z<char, void> z_char; }";
284 EXPECT_TRUE(matches(
285 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000286 varDecl(hasName("z_float"),
287 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000288 EXPECT_TRUE(notMatches(
289 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000290 varDecl(hasName("z_float"),
291 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000292 EXPECT_TRUE(matches(
293 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000294 varDecl(hasName("z_char"),
295 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
296 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000297 EXPECT_TRUE(matches(
298 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000299 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000300 EXPECT_TRUE(notMatches(
301 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000302 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000303
304 EXPECT_TRUE(matches(
305 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000306 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000307}
308
Daniel Jasper08f0c532012-09-18 14:17:42 +0000309TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
310 EXPECT_TRUE(matches(
311 "template <typename T> struct A {"
312 " template <typename T2> struct F {};"
313 "};"
314 "template <typename T> struct B : A<T>::template F<T> {};"
315 "B<int> b;",
316 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
317}
318
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000319TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000320 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000321 EXPECT_TRUE(notMatches("class X;", ClassX));
322 EXPECT_TRUE(notMatches("class X {};", ClassX));
323}
324
325TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000326 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000327 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
328 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
329}
330
331TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
332 EXPECT_TRUE(notMatches("template<typename T> class X { };"
333 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000334 classTemplateDecl(hasName("X"),
335 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000336}
337
338TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
339 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
340 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000341 classTemplateDecl(hasName("X"),
342 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000343}
344
Daniel Jasper6a124492012-07-12 08:50:38 +0000345TEST(AllOf, AllOverloadsWork) {
346 const char Program[] =
347 "struct T { }; int f(int, T*); void g(int x) { T t; f(x, &t); }";
348 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000349 callExpr(allOf(callee(functionDecl(hasName("f"))),
350 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000351 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000352 callExpr(allOf(callee(functionDecl(hasName("f"))),
353 hasArgument(0, declRefExpr(to(varDecl()))),
354 hasArgument(1, hasType(pointsTo(
355 recordDecl(hasName("T")))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000356}
357
Manuel Klimek4da21662012-07-06 05:48:52 +0000358TEST(DeclarationMatcher, MatchAnyOf) {
359 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000360 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000361 EXPECT_TRUE(
362 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
363 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
364 EXPECT_TRUE(
365 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
366 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
367
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000368 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000369 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000370 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
371 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
372
Manuel Klimek4da21662012-07-06 05:48:52 +0000373 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000374 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
375 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000376 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
377 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
378 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
379 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
380 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
381 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
382}
383
384TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000385 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000386 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
387 EXPECT_TRUE(matches("class X {};", HasClassX));
388
389 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000390 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000391 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
392 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
393 EXPECT_TRUE(
394 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
395}
396
397TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
398 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000399 recordDecl(
400 has(recordDecl(
401 has(recordDecl(hasName("X"))),
402 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000403 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000404 has(recordDecl(
405 has(recordDecl(hasName("A"))),
406 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000407 hasName("C"))),
408 hasName("F"));
409
410 EXPECT_TRUE(matches(
411 "class F {"
412 " class Z {"
413 " class X {};"
414 " class Y {};"
415 " };"
416 " class C {"
417 " class A {};"
418 " class B {};"
419 " };"
420 "};", Recursive));
421
422 EXPECT_TRUE(matches(
423 "class F {"
424 " class Z {"
425 " class A {};"
426 " class X {};"
427 " class Y {};"
428 " };"
429 " class C {"
430 " class X {};"
431 " class A {};"
432 " class B {};"
433 " };"
434 "};", Recursive));
435
436 EXPECT_TRUE(matches(
437 "class O1 {"
438 " class O2 {"
439 " class F {"
440 " class Z {"
441 " class A {};"
442 " class X {};"
443 " class Y {};"
444 " };"
445 " class C {"
446 " class X {};"
447 " class A {};"
448 " class B {};"
449 " };"
450 " };"
451 " };"
452 "};", Recursive));
453}
454
455TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
456 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000457 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000458 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000459 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000460 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000461 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000462 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000463 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000464 hasName("Y"))),
465 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000466 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000467 anyOf(
468 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000469 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000470 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000471 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000472 hasName("B")))))),
473 hasName("F")));
474
475 EXPECT_TRUE(matches("class F {};", Recursive));
476 EXPECT_TRUE(matches("class Z {};", Recursive));
477 EXPECT_TRUE(matches("class C {};", Recursive));
478 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
479 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
480 EXPECT_TRUE(
481 matches("class O1 { class O2 {"
482 " class M { class N { class B {}; }; }; "
483 "}; };", Recursive));
484}
485
486TEST(DeclarationMatcher, MatchNot) {
487 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000488 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000489 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000490 unless(hasName("X")));
491 EXPECT_TRUE(notMatches("", NotClassX));
492 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
493 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
494 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
495 EXPECT_TRUE(
496 notMatches("class Y {}; class Z {}; class X : public Y {};",
497 NotClassX));
498
499 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000500 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000501 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000502 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000503 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000504 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000505 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
506 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
507 ClassXHasNotClassY));
508}
509
510TEST(DeclarationMatcher, HasDescendant) {
511 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000512 recordDecl(
513 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000514 hasName("Z"));
515 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
516 EXPECT_TRUE(
517 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
518 EXPECT_TRUE(
519 matches("class Z { class A { class Y { class X {}; }; }; };",
520 ZDescendantClassX));
521 EXPECT_TRUE(
522 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
523 ZDescendantClassX));
524 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
525
526 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000527 recordDecl(
528 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000529 hasName("X"))),
530 hasName("Z"));
531 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
532 ZDescendantClassXHasClassY));
533 EXPECT_TRUE(
534 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
535 ZDescendantClassXHasClassY));
536 EXPECT_TRUE(notMatches(
537 "class Z {"
538 " class A {"
539 " class B {"
540 " class X {"
541 " class C {"
542 " class Y {};"
543 " };"
544 " };"
545 " }; "
546 " };"
547 "};", ZDescendantClassXHasClassY));
548
549 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000550 recordDecl(
551 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
552 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000553 hasName("Z"));
554 EXPECT_TRUE(
555 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
556 ZDescendantClassXDescendantClassY));
557 EXPECT_TRUE(matches(
558 "class Z {"
559 " class A {"
560 " class X {"
561 " class B {"
562 " class Y {};"
563 " };"
564 " class Y {};"
565 " };"
566 " };"
567 "};", ZDescendantClassXDescendantClassY));
568}
569
Daniel Jaspera267cf62012-10-29 10:14:44 +0000570// Implements a run method that returns whether BoundNodes contains a
571// Decl bound to Id that can be dynamically cast to T.
572// Optionally checks that the check succeeded a specific number of times.
573template <typename T>
574class VerifyIdIsBoundTo : public BoundNodesCallback {
575public:
576 // Create an object that checks that a node of type \c T was bound to \c Id.
577 // Does not check for a certain number of matches.
578 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
579 : Id(Id), ExpectedCount(-1), Count(0) {}
580
581 // Create an object that checks that a node of type \c T was bound to \c Id.
582 // Checks that there were exactly \c ExpectedCount matches.
583 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
584 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
585
586 // Create an object that checks that a node of type \c T was bound to \c Id.
587 // Checks that there was exactly one match with the name \c ExpectedName.
588 // Note that \c T must be a NamedDecl for this to work.
589 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName)
590 : Id(Id), ExpectedCount(1), Count(0), ExpectedName(ExpectedName) {}
591
592 ~VerifyIdIsBoundTo() {
593 if (ExpectedCount != -1)
594 EXPECT_EQ(ExpectedCount, Count);
595 if (!ExpectedName.empty())
596 EXPECT_EQ(ExpectedName, Name);
597 }
598
599 virtual bool run(const BoundNodes *Nodes) {
600 if (Nodes->getNodeAs<T>(Id)) {
601 ++Count;
602 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
603 Name = Named->getNameAsString();
604 } else if (const NestedNameSpecifier *NNS =
605 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
606 llvm::raw_string_ostream OS(Name);
607 NNS->print(OS, PrintingPolicy(LangOptions()));
608 }
609 return true;
610 }
611 return false;
612 }
613
Daniel Jasper452abbc2012-10-29 10:48:25 +0000614 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
615 return run(Nodes);
616 }
617
Daniel Jaspera267cf62012-10-29 10:14:44 +0000618private:
619 const std::string Id;
620 const int ExpectedCount;
621 int Count;
622 const std::string ExpectedName;
623 std::string Name;
624};
625
626TEST(HasDescendant, MatchesDescendantTypes) {
627 EXPECT_TRUE(matches("void f() { int i = 3; }",
628 decl(hasDescendant(loc(builtinType())))));
629 EXPECT_TRUE(matches("void f() { int i = 3; }",
630 stmt(hasDescendant(builtinType()))));
631
632 EXPECT_TRUE(matches("void f() { int i = 3; }",
633 stmt(hasDescendant(loc(builtinType())))));
634 EXPECT_TRUE(matches("void f() { int i = 3; }",
635 stmt(hasDescendant(qualType(builtinType())))));
636
637 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
638 stmt(hasDescendant(isInteger()))));
639
640 EXPECT_TRUE(matchAndVerifyResultTrue(
641 "void f() { int a; float c; int d; int e; }",
642 functionDecl(forEachDescendant(
643 varDecl(hasDescendant(isInteger())).bind("x"))),
644 new VerifyIdIsBoundTo<Decl>("x", 3)));
645}
646
647TEST(HasDescendant, MatchesDescendantsOfTypes) {
648 EXPECT_TRUE(matches("void f() { int*** i; }",
649 qualType(hasDescendant(builtinType()))));
650 EXPECT_TRUE(matches("void f() { int*** i; }",
651 qualType(hasDescendant(
652 pointerType(pointee(builtinType()))))));
653 EXPECT_TRUE(matches("void f() { int*** i; }",
654 typeLoc(hasDescendant(builtinTypeLoc()))));
655
656 EXPECT_TRUE(matchAndVerifyResultTrue(
657 "void f() { int*** i; }",
658 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
659 new VerifyIdIsBoundTo<Type>("x", 2)));
660}
661
662TEST(Has, MatchesChildrenOfTypes) {
663 EXPECT_TRUE(matches("int i;",
664 varDecl(hasName("i"), has(isInteger()))));
665 EXPECT_TRUE(notMatches("int** i;",
666 varDecl(hasName("i"), has(isInteger()))));
667 EXPECT_TRUE(matchAndVerifyResultTrue(
668 "int (*f)(float, int);",
669 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
670 new VerifyIdIsBoundTo<QualType>("x", 2)));
671}
672
673TEST(Has, MatchesChildTypes) {
674 EXPECT_TRUE(matches(
675 "int* i;",
676 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
677 EXPECT_TRUE(notMatches(
678 "int* i;",
679 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
680}
681
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000682TEST(Enum, DoesNotMatchClasses) {
683 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
684}
685
686TEST(Enum, MatchesEnums) {
687 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
688}
689
690TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000691 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000692 EXPECT_TRUE(matches("enum X{ A };", Matcher));
693 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
694 EXPECT_TRUE(notMatches("enum X {};", Matcher));
695}
696
Manuel Klimek4da21662012-07-06 05:48:52 +0000697TEST(StatementMatcher, Has) {
698 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000699 expr(hasType(pointsTo(recordDecl(hasName("X")))),
700 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000701
702 EXPECT_TRUE(matches(
703 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
704 EXPECT_TRUE(notMatches(
705 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
706}
707
708TEST(StatementMatcher, HasDescendant) {
709 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000710 expr(hasType(pointsTo(recordDecl(hasName("X")))),
711 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000712
713 EXPECT_TRUE(matches(
714 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
715 HasDescendantVariableI));
716 EXPECT_TRUE(notMatches(
717 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
718 HasDescendantVariableI));
719}
720
721TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000722 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000723
724 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
725 EXPECT_TRUE(notMatches("class A {};", TypeA));
726
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000727 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000728
729 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
730 TypeDerivedFromA));
731 EXPECT_TRUE(notMatches("class A {};", TypeA));
732
733 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000734 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000735
736 EXPECT_TRUE(
737 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
738}
739
Manuel Klimek4da21662012-07-06 05:48:52 +0000740TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000741 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000742
743 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000744 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000745
746 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000747 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000748
749 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000750 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000751
752 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
753 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000754 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000755
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000756 StatementMatcher MethodX =
757 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000758
759 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
760 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000761 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000762}
763
764TEST(Matcher, BindTheSameNameInAlternatives) {
765 StatementMatcher matcher = anyOf(
766 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000767 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000768 hasRHS(integerLiteral(equals(0)))),
769 binaryOperator(hasOperatorName("+"),
770 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000771 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000772
773 EXPECT_TRUE(matchAndVerifyResultTrue(
774 // The first branch of the matcher binds x to 0 but then fails.
775 // The second branch binds x to f() and succeeds.
776 "int f() { return 0 + f(); }",
777 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000778 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000779}
780
Manuel Klimek66341c52012-08-30 19:41:06 +0000781TEST(Matcher, BindsIDForMemoizedResults) {
782 // Using the same matcher in two match expressions will make memoization
783 // kick in.
784 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
785 EXPECT_TRUE(matchAndVerifyResultTrue(
786 "class A { class B { class X {}; }; };",
787 DeclarationMatcher(anyOf(
788 recordDecl(hasName("A"), hasDescendant(ClassX)),
789 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000790 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000791}
792
Daniel Jasper189f2e42012-12-03 15:43:25 +0000793TEST(HasDeclaration, HasDeclarationOfEnumType) {
794 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
795 expr(hasType(pointsTo(
796 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
797}
798
Manuel Klimek4da21662012-07-06 05:48:52 +0000799TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000800 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000801 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000802 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000803 EXPECT_TRUE(
804 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000805 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000806 EXPECT_TRUE(
807 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000808 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000809}
810
811TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000812 TypeMatcher ClassX = hasDeclaration(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; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000815 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000816 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000817 EXPECT_TRUE(
818 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000819 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000820}
821
822TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000823 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000824 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000825 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000826 EXPECT_TRUE(
827 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000828 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000829}
830
831TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000832 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000833 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000834 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000835 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000836 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000837}
838
839TEST(Matcher, Call) {
840 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000841 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000842 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000843
844 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
845 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
846
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000847 StatementMatcher MethodOnY =
848 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000849
850 EXPECT_TRUE(
851 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
852 MethodOnY));
853 EXPECT_TRUE(
854 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
855 MethodOnY));
856 EXPECT_TRUE(
857 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
858 MethodOnY));
859 EXPECT_TRUE(
860 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
861 MethodOnY));
862 EXPECT_TRUE(
863 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
864 MethodOnY));
865
866 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000867 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000868
869 EXPECT_TRUE(
870 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
871 MethodOnYPointer));
872 EXPECT_TRUE(
873 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
874 MethodOnYPointer));
875 EXPECT_TRUE(
876 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
877 MethodOnYPointer));
878 EXPECT_TRUE(
879 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
880 MethodOnYPointer));
881 EXPECT_TRUE(
882 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
883 MethodOnYPointer));
884}
885
Daniel Jasper31f7c082012-10-01 13:40:41 +0000886TEST(Matcher, Lambda) {
887 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
888 lambdaExpr()));
889}
890
891TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000892 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
893 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000894 forRangeStmt()));
895 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
896 forRangeStmt()));
897}
898
899TEST(Matcher, UserDefinedLiteral) {
900 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
901 " return i + 1;"
902 "}"
903 "char c = 'a'_inc;",
904 userDefinedLiteral()));
905}
906
Daniel Jasperb54b7642012-09-20 14:12:57 +0000907TEST(Matcher, FlowControl) {
908 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
909 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
910 continueStmt()));
911 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
912 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
913 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
914}
915
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000916TEST(HasType, MatchesAsString) {
917 EXPECT_TRUE(
918 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000919 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000920 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000921 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000922 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000923 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000924 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000925 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000926}
927
Manuel Klimek4da21662012-07-06 05:48:52 +0000928TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000929 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000930 // Unary operator
931 EXPECT_TRUE(matches("class Y { }; "
932 "bool operator!(Y x) { return false; }; "
933 "Y y; bool c = !y;", OpCall));
934 // No match -- special operators like "new", "delete"
935 // FIXME: operator new takes size_t, for which we need stddef.h, for which
936 // we need to figure out include paths in the test.
937 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
938 // "class Y { }; "
939 // "void *operator new(size_t size) { return 0; } "
940 // "Y *y = new Y;", OpCall));
941 EXPECT_TRUE(notMatches("class Y { }; "
942 "void operator delete(void *p) { } "
943 "void a() {Y *y = new Y; delete y;}", OpCall));
944 // Binary operator
945 EXPECT_TRUE(matches("class Y { }; "
946 "bool operator&&(Y x, Y y) { return true; }; "
947 "Y a; Y b; bool c = a && b;",
948 OpCall));
949 // No match -- normal operator, not an overloaded one.
950 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
951 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
952}
953
954TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
955 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000956 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000957 EXPECT_TRUE(matches("class Y { }; "
958 "bool operator&&(Y x, Y y) { return true; }; "
959 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
960 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000961 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000962 EXPECT_TRUE(notMatches("class Y { }; "
963 "bool operator&&(Y x, Y y) { return true; }; "
964 "Y a; Y b; bool c = a && b;",
965 OpCallLessLess));
966}
967
Daniel Jasper278057f2012-11-15 03:29:05 +0000968TEST(Matcher, NestedOverloadedOperatorCalls) {
969 EXPECT_TRUE(matchAndVerifyResultTrue(
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(hasOverloadedOperatorName("&&")).bind("x"),
974 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
975 EXPECT_TRUE(matches(
976 "class Y { }; "
977 "Y& operator&&(Y& x, Y& y) { return x; }; "
978 "Y a; Y b; Y c; Y d = a && b && c;",
979 operatorCallExpr(hasParent(operatorCallExpr()))));
980 EXPECT_TRUE(matches(
981 "class Y { }; "
982 "Y& operator&&(Y& x, Y& y) { return x; }; "
983 "Y a; Y b; Y c; Y d = a && b && c;",
984 operatorCallExpr(hasDescendant(operatorCallExpr()))));
985}
986
Manuel Klimek4da21662012-07-06 05:48:52 +0000987TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +0000988 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000989 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000990
991 EXPECT_TRUE(
992 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
993 MethodOnY));
994 EXPECT_TRUE(
995 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
996 MethodOnY));
997 EXPECT_TRUE(
998 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
999 MethodOnY));
1000 EXPECT_TRUE(
1001 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1002 MethodOnY));
1003 EXPECT_TRUE(
1004 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1005 MethodOnY));
1006
1007 EXPECT_TRUE(matches(
1008 "class Y {"
1009 " public: virtual void x();"
1010 "};"
1011 "class X : public Y {"
1012 " public: virtual void x();"
1013 "};"
1014 "void z() { X *x; x->Y::x(); }", MethodOnY));
1015}
1016
1017TEST(Matcher, VariableUsage) {
1018 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001019 declRefExpr(to(
1020 varDecl(hasInitializer(
1021 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001022
1023 EXPECT_TRUE(matches(
1024 "class Y {"
1025 " public:"
1026 " bool x() const;"
1027 "};"
1028 "void z(const Y &y) {"
1029 " bool b = y.x();"
1030 " if (b) {}"
1031 "}", Reference));
1032
1033 EXPECT_TRUE(notMatches(
1034 "class Y {"
1035 " public:"
1036 " bool x() const;"
1037 "};"
1038 "void z(const Y &y) {"
1039 " bool b = y.x();"
1040 "}", Reference));
1041}
1042
Manuel Klimek8cb9bf52012-12-04 14:42:08 +00001043TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper9bd28092012-07-30 05:03:25 +00001044 EXPECT_TRUE(matches(
1045 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001046 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001047}
1048
Manuel Klimek4da21662012-07-06 05:48:52 +00001049TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001050 StatementMatcher CallOnVariableY =
1051 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001052
1053 EXPECT_TRUE(matches(
1054 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1055 EXPECT_TRUE(matches(
1056 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1057 EXPECT_TRUE(matches(
1058 "class Y { public: void x(); };"
1059 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1060 EXPECT_TRUE(matches(
1061 "class Y { public: void x(); };"
1062 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1063 EXPECT_TRUE(notMatches(
1064 "class Y { public: void x(); };"
1065 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1066 CallOnVariableY));
1067}
1068
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001069TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1070 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1071 unaryExprOrTypeTraitExpr()));
1072 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1073 alignOfExpr(anything())));
1074 // FIXME: Uncomment once alignof is enabled.
1075 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1076 // unaryExprOrTypeTraitExpr()));
1077 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1078 // sizeOfExpr()));
1079}
1080
1081TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1082 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1083 hasArgumentOfType(asString("int")))));
1084 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1085 hasArgumentOfType(asString("float")))));
1086 EXPECT_TRUE(matches(
1087 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001088 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001089 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001090 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001091}
1092
Manuel Klimek4da21662012-07-06 05:48:52 +00001093TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001094 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001095}
1096
1097TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001098 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001099}
1100
1101TEST(MemberExpression, MatchesVariable) {
1102 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001103 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001104 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001105 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001106 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001107 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001108}
1109
1110TEST(MemberExpression, MatchesStaticVariable) {
1111 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001112 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001113 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001114 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001115 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001116 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001117}
1118
Daniel Jasper6a124492012-07-12 08:50:38 +00001119TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001120 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1121 EXPECT_TRUE(matches(
1122 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1123 callExpr(hasArgument(0, declRefExpr(
1124 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001125}
1126
1127TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001128 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001129 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001130 callExpr(hasArgument(0, declRefExpr(
1131 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001132}
1133
Manuel Klimek4da21662012-07-06 05:48:52 +00001134TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1135 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001136 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001137 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001138 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001139 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001140 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001141}
1142
1143TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1144 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
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; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001147 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001148 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001149 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001150}
1151
1152TEST(IsArrow, MatchesMemberCallsViaArrow) {
1153 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001154 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001155 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001156 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001157 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001158 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001159}
1160
1161TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001162 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001163
1164 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1165 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1166}
1167
1168TEST(Callee, MatchesMemberExpressions) {
1169 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001170 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001171 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001172 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001173}
1174
1175TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001176 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001177
1178 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1179 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1180
Manuel Klimeke265c872012-07-10 14:21:30 +00001181#if !defined(_MSC_VER)
1182 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001183 // Dependent contexts, but a non-dependent call.
1184 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1185 CallFunctionF));
1186 EXPECT_TRUE(
1187 matches("void f(); template <int N> struct S { void g() { f(); } };",
1188 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001189#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001190
1191 // Depedent calls don't match.
1192 EXPECT_TRUE(
1193 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1194 CallFunctionF));
1195 EXPECT_TRUE(
1196 notMatches("void f(int);"
1197 "template <typename T> struct S { void g(T t) { f(t); } };",
1198 CallFunctionF));
1199}
1200
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001201TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1202 EXPECT_TRUE(
1203 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001204 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001205}
1206
1207TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1208 EXPECT_TRUE(
1209 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001210 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001211}
1212
1213TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1214 EXPECT_TRUE(
1215 notMatches("void g(); template <typename T> void f(T t) {}"
1216 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001217 functionTemplateDecl(hasName("f"),
1218 hasDescendant(declRefExpr(to(
1219 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001220}
1221
Manuel Klimek4da21662012-07-06 05:48:52 +00001222TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001223 StatementMatcher CallArgumentY = callExpr(
1224 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001225
1226 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1227 EXPECT_TRUE(
1228 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1229 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1230
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001231 StatementMatcher WrongIndex = callExpr(
1232 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001233 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1234}
1235
1236TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001237 StatementMatcher CallArgumentY = callExpr(
1238 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001239 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1240 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1241 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1242}
1243
1244TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001245 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001246
1247 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1248 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1249 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1250}
1251
Daniel Jasper36e29d62012-12-04 11:54:27 +00001252TEST(Matcher, ParameterCount) {
1253 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1254 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1255 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1256 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1257 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1258}
1259
Manuel Klimek4da21662012-07-06 05:48:52 +00001260TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001261 DeclarationMatcher ReferenceClassX = varDecl(
1262 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001263 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1264 ReferenceClassX));
1265 EXPECT_TRUE(
1266 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1267 EXPECT_TRUE(
1268 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1269 EXPECT_TRUE(
1270 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1271}
1272
1273TEST(HasParameter, CallsInnerMatcher) {
1274 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001275 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001276 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001277 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001278}
1279
1280TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1281 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001282 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001283}
1284
1285TEST(HasType, MatchesParameterVariableTypesStrictly) {
1286 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001287 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001288 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001289 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001290 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001291 methodDecl(hasParameter(0,
1292 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001293 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001294 methodDecl(hasParameter(0,
1295 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001296}
1297
1298TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1299 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001300 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001301 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001302 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001303}
1304
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001305TEST(Returns, MatchesReturnTypes) {
1306 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001307 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001308 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001309 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001310 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001311 functionDecl(returns(hasDeclaration(
1312 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001313}
1314
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001315TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001316 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1317 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1318 functionDecl(isExternC())));
1319 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001320}
1321
Manuel Klimek4da21662012-07-06 05:48:52 +00001322TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1323 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001324 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001325}
1326
1327TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1328 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001329 methodDecl(hasAnyParameter(hasType(pointsTo(
1330 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001331}
1332
1333TEST(HasName, MatchesParameterVariableDeclartions) {
1334 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001335 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001336 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001337 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001338}
1339
Daniel Jasper371f9392012-08-01 08:40:24 +00001340TEST(Matcher, MatchesClassTemplateSpecialization) {
1341 EXPECT_TRUE(matches("template<typename T> struct A {};"
1342 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001343 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001344 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001345 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001346 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001347 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001348}
1349
1350TEST(Matcher, MatchesTypeTemplateArgument) {
1351 EXPECT_TRUE(matches(
1352 "template<typename T> struct B {};"
1353 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001354 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001355 asString("int"))))));
1356}
1357
1358TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1359 EXPECT_TRUE(matches(
1360 "struct B { int next; };"
1361 "template<int(B::*next_ptr)> struct A {};"
1362 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001363 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1364 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001365
1366 EXPECT_TRUE(notMatches(
1367 "template <typename T> struct A {};"
1368 "A<int> a;",
1369 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1370 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001371}
1372
1373TEST(Matcher, MatchesSpecificArgument) {
1374 EXPECT_TRUE(matches(
1375 "template<typename T, typename U> class A {};"
1376 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001377 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001378 1, refersToType(asString("int"))))));
1379 EXPECT_TRUE(notMatches(
1380 "template<typename T, typename U> class A {};"
1381 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001382 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001383 1, refersToType(asString("int"))))));
1384}
1385
Manuel Klimek4da21662012-07-06 05:48:52 +00001386TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001387 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001388
1389 EXPECT_TRUE(
1390 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1391 EXPECT_TRUE(
1392 matches("class X { public: X(); }; void x() { X x = X(); }",
1393 Constructor));
1394 EXPECT_TRUE(
1395 matches("class X { public: X(int); }; void x() { X x = 0; }",
1396 Constructor));
1397 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1398}
1399
1400TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001401 StatementMatcher Constructor = constructExpr(
1402 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001403
1404 EXPECT_TRUE(
1405 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1406 Constructor));
1407 EXPECT_TRUE(
1408 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1409 Constructor));
1410 EXPECT_TRUE(
1411 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1412 Constructor));
1413 EXPECT_TRUE(
1414 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1415 Constructor));
1416
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001417 StatementMatcher WrongIndex = constructExpr(
1418 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001419 EXPECT_TRUE(
1420 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1421 WrongIndex));
1422}
1423
1424TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001425 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001426
1427 EXPECT_TRUE(
1428 matches("class X { public: X(int); }; void x() { X x(0); }",
1429 Constructor1Arg));
1430 EXPECT_TRUE(
1431 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1432 Constructor1Arg));
1433 EXPECT_TRUE(
1434 matches("class X { public: X(int); }; void x() { X x = 0; }",
1435 Constructor1Arg));
1436 EXPECT_TRUE(
1437 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1438 Constructor1Arg));
1439}
1440
Manuel Klimek70b9db92012-10-23 10:40:50 +00001441TEST(Matcher,ThisExpr) {
1442 EXPECT_TRUE(
1443 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1444 EXPECT_TRUE(
1445 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1446}
1447
Manuel Klimek4da21662012-07-06 05:48:52 +00001448TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001449 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001450
1451 std::string ClassString = "class string { public: string(); ~string(); }; ";
1452
1453 EXPECT_TRUE(
1454 matches(ClassString +
1455 "string GetStringByValue();"
1456 "void FunctionTakesString(string s);"
1457 "void run() { FunctionTakesString(GetStringByValue()); }",
1458 TempExpression));
1459
1460 EXPECT_TRUE(
1461 notMatches(ClassString +
1462 "string* GetStringPointer(); "
1463 "void FunctionTakesStringPtr(string* s);"
1464 "void run() {"
1465 " string* s = GetStringPointer();"
1466 " FunctionTakesStringPtr(GetStringPointer());"
1467 " FunctionTakesStringPtr(s);"
1468 "}",
1469 TempExpression));
1470
1471 EXPECT_TRUE(
1472 notMatches("class no_dtor {};"
1473 "no_dtor GetObjByValue();"
1474 "void ConsumeObj(no_dtor param);"
1475 "void run() { ConsumeObj(GetObjByValue()); }",
1476 TempExpression));
1477}
1478
Sam Panzere16acd32012-08-24 22:04:44 +00001479TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1480 std::string ClassString =
1481 "class string { public: string(); int length(); }; ";
1482
1483 EXPECT_TRUE(
1484 matches(ClassString +
1485 "string GetStringByValue();"
1486 "void FunctionTakesString(string s);"
1487 "void run() { FunctionTakesString(GetStringByValue()); }",
1488 materializeTemporaryExpr()));
1489
1490 EXPECT_TRUE(
1491 notMatches(ClassString +
1492 "string* GetStringPointer(); "
1493 "void FunctionTakesStringPtr(string* s);"
1494 "void run() {"
1495 " string* s = GetStringPointer();"
1496 " FunctionTakesStringPtr(GetStringPointer());"
1497 " FunctionTakesStringPtr(s);"
1498 "}",
1499 materializeTemporaryExpr()));
1500
1501 EXPECT_TRUE(
1502 notMatches(ClassString +
1503 "string GetStringByValue();"
1504 "void run() { int k = GetStringByValue().length(); }",
1505 materializeTemporaryExpr()));
1506
1507 EXPECT_TRUE(
1508 notMatches(ClassString +
1509 "string GetStringByValue();"
1510 "void run() { GetStringByValue(); }",
1511 materializeTemporaryExpr()));
1512}
1513
Manuel Klimek4da21662012-07-06 05:48:52 +00001514TEST(ConstructorDeclaration, SimpleCase) {
1515 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001516 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001517 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001518 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001519}
1520
1521TEST(ConstructorDeclaration, IsImplicit) {
1522 // This one doesn't match because the constructor is not added by the
1523 // compiler (it is not needed).
1524 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001525 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001526 // The compiler added the implicit default constructor.
1527 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001528 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001529 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001530 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001531}
1532
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001533TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1534 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001535 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001536}
1537
1538TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001539 EXPECT_TRUE(notMatches("class Foo {};",
1540 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001541}
1542
Manuel Klimek4da21662012-07-06 05:48:52 +00001543TEST(HasAnyConstructorInitializer, SimpleCase) {
1544 EXPECT_TRUE(notMatches(
1545 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001546 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001547 EXPECT_TRUE(matches(
1548 "class Foo {"
1549 " Foo() : foo_() { }"
1550 " int foo_;"
1551 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001552 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001553}
1554
1555TEST(HasAnyConstructorInitializer, ForField) {
1556 static const char Code[] =
1557 "class Baz { };"
1558 "class Foo {"
1559 " Foo() : foo_() { }"
1560 " Baz foo_;"
1561 " Baz bar_;"
1562 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001563 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1564 forField(hasType(recordDecl(hasName("Baz"))))))));
1565 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001566 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001567 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1568 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001569}
1570
1571TEST(HasAnyConstructorInitializer, WithInitializer) {
1572 static const char Code[] =
1573 "class Foo {"
1574 " Foo() : foo_(0) { }"
1575 " int foo_;"
1576 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001577 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001578 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001579 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001580 withInitializer(integerLiteral(equals(1)))))));
1581}
1582
1583TEST(HasAnyConstructorInitializer, IsWritten) {
1584 static const char Code[] =
1585 "struct Bar { Bar(){} };"
1586 "class Foo {"
1587 " Foo() : foo_() { }"
1588 " Bar foo_;"
1589 " Bar bar_;"
1590 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001591 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001592 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001593 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001594 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001595 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001596 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1597}
1598
1599TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001600 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001601
1602 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1603 EXPECT_TRUE(
1604 matches("class X { public: X(); }; void x() { new X(); }", New));
1605 EXPECT_TRUE(
1606 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1607 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1608}
1609
1610TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001611 StatementMatcher New = constructExpr(
1612 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001613
1614 EXPECT_TRUE(
1615 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1616 New));
1617 EXPECT_TRUE(
1618 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1619 New));
1620 EXPECT_TRUE(
1621 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1622 New));
1623
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001624 StatementMatcher WrongIndex = constructExpr(
1625 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001626 EXPECT_TRUE(
1627 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1628 WrongIndex));
1629}
1630
1631TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001632 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001633
1634 EXPECT_TRUE(
1635 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1636 EXPECT_TRUE(
1637 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1638 New));
1639}
1640
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001641TEST(Matcher, DeleteExpression) {
1642 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001643 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001644}
1645
Manuel Klimek4da21662012-07-06 05:48:52 +00001646TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001647 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001648
1649 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1650 EXPECT_TRUE(
1651 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1652 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1653}
1654
1655TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001656 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001657 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1658 // wide string
1659 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1660 // with escaped characters
1661 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1662 // no matching -- though the data type is the same, there is no string literal
1663 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1664}
1665
1666TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001667 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001668 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1669 // wide character
1670 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1671 // wide character, Hex encoded, NOT MATCHED!
1672 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1673 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1674}
1675
1676TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001677 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001678 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1679 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1680 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1681 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1682
1683 // Non-matching cases (character literals, float and double)
1684 EXPECT_TRUE(notMatches("int i = L'a';",
1685 HasIntLiteral)); // this is actually a character
1686 // literal cast to int
1687 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1688 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1689 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1690}
1691
Daniel Jasper31f7c082012-10-01 13:40:41 +00001692TEST(Matcher, NullPtrLiteral) {
1693 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1694}
1695
Daniel Jasperb54b7642012-09-20 14:12:57 +00001696TEST(Matcher, AsmStatement) {
1697 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1698}
1699
Manuel Klimek4da21662012-07-06 05:48:52 +00001700TEST(Matcher, Conditions) {
1701 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1702
1703 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1704 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1705 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1706 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1707 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1708}
1709
1710TEST(MatchBinaryOperator, HasOperatorName) {
1711 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1712
1713 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1714 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1715}
1716
1717TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1718 StatementMatcher OperatorTrueFalse =
1719 binaryOperator(hasLHS(boolLiteral(equals(true))),
1720 hasRHS(boolLiteral(equals(false))));
1721
1722 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1723 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1724 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1725}
1726
1727TEST(MatchBinaryOperator, HasEitherOperand) {
1728 StatementMatcher HasOperand =
1729 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1730
1731 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1732 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1733 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1734}
1735
1736TEST(Matcher, BinaryOperatorTypes) {
1737 // Integration test that verifies the AST provides all binary operators in
1738 // a way we expect.
1739 // FIXME: Operator ','
1740 EXPECT_TRUE(
1741 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1742 EXPECT_TRUE(
1743 matches("bool b; bool c = (b = true);",
1744 binaryOperator(hasOperatorName("="))));
1745 EXPECT_TRUE(
1746 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1747 EXPECT_TRUE(
1748 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1749 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1750 EXPECT_TRUE(
1751 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1752 EXPECT_TRUE(
1753 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1754 EXPECT_TRUE(
1755 matches("int i = 1; int j = (i <<= 2);",
1756 binaryOperator(hasOperatorName("<<="))));
1757 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1758 EXPECT_TRUE(
1759 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1760 EXPECT_TRUE(
1761 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1762 EXPECT_TRUE(
1763 matches("int i = 1; int j = (i >>= 2);",
1764 binaryOperator(hasOperatorName(">>="))));
1765 EXPECT_TRUE(
1766 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1767 EXPECT_TRUE(
1768 matches("int i = 42; int j = (i ^= 42);",
1769 binaryOperator(hasOperatorName("^="))));
1770 EXPECT_TRUE(
1771 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1772 EXPECT_TRUE(
1773 matches("int i = 42; int j = (i %= 42);",
1774 binaryOperator(hasOperatorName("%="))));
1775 EXPECT_TRUE(
1776 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1777 EXPECT_TRUE(
1778 matches("bool b = true && false;",
1779 binaryOperator(hasOperatorName("&&"))));
1780 EXPECT_TRUE(
1781 matches("bool b = true; bool c = (b &= false);",
1782 binaryOperator(hasOperatorName("&="))));
1783 EXPECT_TRUE(
1784 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1785 EXPECT_TRUE(
1786 matches("bool b = true || false;",
1787 binaryOperator(hasOperatorName("||"))));
1788 EXPECT_TRUE(
1789 matches("bool b = true; bool c = (b |= false);",
1790 binaryOperator(hasOperatorName("|="))));
1791 EXPECT_TRUE(
1792 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1793 EXPECT_TRUE(
1794 matches("int i = 42; int j = (i *= 23);",
1795 binaryOperator(hasOperatorName("*="))));
1796 EXPECT_TRUE(
1797 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1798 EXPECT_TRUE(
1799 matches("int i = 42; int j = (i /= 23);",
1800 binaryOperator(hasOperatorName("/="))));
1801 EXPECT_TRUE(
1802 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1803 EXPECT_TRUE(
1804 matches("int i = 42; int j = (i += 23);",
1805 binaryOperator(hasOperatorName("+="))));
1806 EXPECT_TRUE(
1807 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1808 EXPECT_TRUE(
1809 matches("int i = 42; int j = (i -= 23);",
1810 binaryOperator(hasOperatorName("-="))));
1811 EXPECT_TRUE(
1812 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1813 binaryOperator(hasOperatorName("->*"))));
1814 EXPECT_TRUE(
1815 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1816 binaryOperator(hasOperatorName(".*"))));
1817
1818 // Member expressions as operators are not supported in matches.
1819 EXPECT_TRUE(
1820 notMatches("struct A { void x(A *a) { a->x(this); } };",
1821 binaryOperator(hasOperatorName("->"))));
1822
1823 // Initializer assignments are not represented as operator equals.
1824 EXPECT_TRUE(
1825 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1826
1827 // Array indexing is not represented as operator.
1828 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1829
1830 // Overloaded operators do not match at all.
1831 EXPECT_TRUE(notMatches(
1832 "struct A { bool operator&&(const A &a) const { return false; } };"
1833 "void x() { A a, b; a && b; }",
1834 binaryOperator()));
1835}
1836
1837TEST(MatchUnaryOperator, HasOperatorName) {
1838 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1839
1840 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1841 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1842}
1843
1844TEST(MatchUnaryOperator, HasUnaryOperand) {
1845 StatementMatcher OperatorOnFalse =
1846 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1847
1848 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1849 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1850}
1851
1852TEST(Matcher, UnaryOperatorTypes) {
1853 // Integration test that verifies the AST provides all unary operators in
1854 // a way we expect.
1855 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1856 EXPECT_TRUE(
1857 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1858 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1859 EXPECT_TRUE(
1860 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1861 EXPECT_TRUE(
1862 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1863 EXPECT_TRUE(
1864 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1865 EXPECT_TRUE(
1866 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1867 EXPECT_TRUE(
1868 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1869 EXPECT_TRUE(
1870 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1871 EXPECT_TRUE(
1872 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1873
1874 // We don't match conversion operators.
1875 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1876
1877 // Function calls are not represented as operator.
1878 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1879
1880 // Overloaded operators do not match at all.
1881 // FIXME: We probably want to add that.
1882 EXPECT_TRUE(notMatches(
1883 "struct A { bool operator!() const { return false; } };"
1884 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1885}
1886
1887TEST(Matcher, ConditionalOperator) {
1888 StatementMatcher Conditional = conditionalOperator(
1889 hasCondition(boolLiteral(equals(true))),
1890 hasTrueExpression(boolLiteral(equals(false))));
1891
1892 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1893 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1894 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1895
1896 StatementMatcher ConditionalFalse = conditionalOperator(
1897 hasFalseExpression(boolLiteral(equals(false))));
1898
1899 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1900 EXPECT_TRUE(
1901 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1902}
1903
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001904TEST(ArraySubscriptMatchers, ArraySubscripts) {
1905 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1906 arraySubscriptExpr()));
1907 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1908 arraySubscriptExpr()));
1909}
1910
1911TEST(ArraySubscriptMatchers, ArrayIndex) {
1912 EXPECT_TRUE(matches(
1913 "int i[2]; void f() { i[1] = 1; }",
1914 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1915 EXPECT_TRUE(matches(
1916 "int i[2]; void f() { 1[i] = 1; }",
1917 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1918 EXPECT_TRUE(notMatches(
1919 "int i[2]; void f() { i[1] = 1; }",
1920 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1921}
1922
1923TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1924 EXPECT_TRUE(matches(
1925 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001926 arraySubscriptExpr(hasBase(implicitCastExpr(
1927 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001928}
1929
Manuel Klimek4da21662012-07-06 05:48:52 +00001930TEST(Matcher, HasNameSupportsNamespaces) {
1931 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001932 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001933 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001934 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001935 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001936 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001937 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001938 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001939 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001940 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001941 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001942 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001943 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001944 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001945 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001946 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001947 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001948 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001949 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001950 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001951 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001952 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001953 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001954 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001955}
1956
1957TEST(Matcher, HasNameSupportsOuterClasses) {
1958 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001959 matches("class A { class B { class C; }; };",
1960 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001961 EXPECT_TRUE(
1962 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001963 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001964 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001965 matches("class A { class B { class C; }; };",
1966 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001967 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001968 matches("class A { class B { class C; }; };",
1969 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001970 EXPECT_TRUE(
1971 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001972 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001973 EXPECT_TRUE(
1974 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001975 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001976 EXPECT_TRUE(
1977 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001978 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001979 EXPECT_TRUE(
1980 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001981 recordDecl(hasName("::C"))));
1982 EXPECT_TRUE(
1983 notMatches("class A { class B { class C; }; };",
1984 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001985 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001986 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001987 EXPECT_TRUE(
1988 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001989 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001990}
1991
1992TEST(Matcher, IsDefinition) {
1993 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001994 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00001995 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
1996 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
1997
1998 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001999 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002000 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2001 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2002
2003 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002004 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002005 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2006 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2007}
2008
2009TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002010 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00002011 ofClass(hasName("X")))));
2012
2013 EXPECT_TRUE(
2014 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2015 EXPECT_TRUE(
2016 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2017 Constructor));
2018 EXPECT_TRUE(
2019 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2020 Constructor));
2021}
2022
2023TEST(Matcher, VisitsTemplateInstantiations) {
2024 EXPECT_TRUE(matches(
2025 "class A { public: void x(); };"
2026 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002027 "void f() { B<A> b; b.y(); }",
2028 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002029
2030 EXPECT_TRUE(matches(
2031 "class A { public: void x(); };"
2032 "class C {"
2033 " public:"
2034 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2035 "};"
2036 "void f() {"
2037 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002038 "}",
2039 recordDecl(hasName("C"),
2040 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002041}
2042
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002043TEST(Matcher, HandlesNullQualTypes) {
2044 // FIXME: Add a Type matcher so we can replace uses of this
2045 // variable with Type(True())
2046 const TypeMatcher AnyType = anything();
2047
2048 // We don't really care whether this matcher succeeds; we're testing that
2049 // it completes without crashing.
2050 EXPECT_TRUE(matches(
2051 "struct A { };"
2052 "template <typename T>"
2053 "void f(T t) {"
2054 " T local_t(t /* this becomes a null QualType in the AST */);"
2055 "}"
2056 "void g() {"
2057 " f(0);"
2058 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002059 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002060 anyOf(
2061 TypeMatcher(hasDeclaration(anything())),
2062 pointsTo(AnyType),
2063 references(AnyType)
2064 // Other QualType matchers should go here.
2065 ))))));
2066}
2067
Manuel Klimek4da21662012-07-06 05:48:52 +00002068// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002069AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002070 // Make sure all special variables are used: node, match_finder,
2071 // bound_nodes_builder, and the parameter named 'AMatcher'.
2072 return AMatcher.matches(Node, Finder, Builder);
2073}
2074
2075TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002076 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002077
2078 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002079 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002080
2081 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002082 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002083
2084 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002085 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002086}
2087
2088AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002089 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
2090 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
2091 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00002092 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00002093 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002094 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002095 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2096 ASTMatchFinder::BK_First);
2097}
2098
2099TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002100 DeclarationMatcher HasClassB =
2101 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002102
2103 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002104 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002105
2106 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002107 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002108
2109 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002110 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002111
2112 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002113 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002114
2115 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2116}
2117
2118TEST(For, FindsForLoops) {
2119 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2120 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002121 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2122 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002123 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002124}
2125
Daniel Jasper6a124492012-07-12 08:50:38 +00002126TEST(For, ForLoopInternals) {
2127 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2128 forStmt(hasCondition(anything()))));
2129 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2130 forStmt(hasLoopInit(anything()))));
2131}
2132
2133TEST(For, NegativeForLoopInternals) {
2134 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002135 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002136 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2137 forStmt(hasLoopInit(anything()))));
2138}
2139
Manuel Klimek4da21662012-07-06 05:48:52 +00002140TEST(For, ReportsNoFalsePositives) {
2141 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2142 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2143}
2144
2145TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002146 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2147 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2148 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002149}
2150
2151TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2152 // It's not a compound statement just because there's "{}" in the source
2153 // text. This is an AST search, not grep.
2154 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002155 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002156 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002157 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002158}
2159
Daniel Jasper6a124492012-07-12 08:50:38 +00002160TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002161 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002162 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002163 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002164 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002165 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002166 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002167 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002168 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002169}
2170
2171TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2172 // The simplest case: every compound statement is in a function
2173 // definition, and the function body itself must be a compound
2174 // statement.
2175 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002176 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002177}
2178
2179TEST(HasAnySubstatement, IsNotRecursive) {
2180 // It's really "has any immediate substatement".
2181 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002182 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002183}
2184
2185TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2186 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002187 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002188}
2189
2190TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2191 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002192 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002193}
2194
2195TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2196 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002197 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002198 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002199 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002200}
2201
2202TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2203 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002204 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002205 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002206 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002207 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002208 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002209}
2210
2211TEST(StatementCountIs, WorksWithMultipleStatements) {
2212 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002213 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002214}
2215
2216TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2217 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002218 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002219 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002220 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002221 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002222 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002223 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002224 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002225}
2226
2227TEST(Member, WorksInSimplestCase) {
2228 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002229 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002230}
2231
2232TEST(Member, DoesNotMatchTheBaseExpression) {
2233 // Don't pick out the wrong part of the member expression, this should
2234 // be checking the member (name) only.
2235 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002236 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002237}
2238
2239TEST(Member, MatchesInMemberFunctionCall) {
2240 EXPECT_TRUE(matches("void f() {"
2241 " struct { void first() {}; } s;"
2242 " s.first();"
2243 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002244 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002245}
2246
Daniel Jasperc711af22012-10-23 15:46:39 +00002247TEST(Member, MatchesMember) {
2248 EXPECT_TRUE(matches(
2249 "struct A { int i; }; void f() { A a; a.i = 2; }",
2250 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2251 EXPECT_TRUE(notMatches(
2252 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2253 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2254}
2255
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002256TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002257 // Fails in C++11 mode
2258 EXPECT_TRUE(matchesConditionally(
2259 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2260 "class X { void *operator new(std::size_t); };",
2261 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002262
2263 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002264 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002265
Daniel Jasper31f7c082012-10-01 13:40:41 +00002266 // Fails in C++11 mode
2267 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002268 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2269 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002270 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002271}
2272
Manuel Klimek4da21662012-07-06 05:48:52 +00002273TEST(HasObjectExpression, DoesNotMatchMember) {
2274 EXPECT_TRUE(notMatches(
2275 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002276 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002277}
2278
2279TEST(HasObjectExpression, MatchesBaseOfVariable) {
2280 EXPECT_TRUE(matches(
2281 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002282 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002283 EXPECT_TRUE(matches(
2284 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002285 memberExpr(hasObjectExpression(
2286 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002287}
2288
2289TEST(HasObjectExpression,
2290 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2291 EXPECT_TRUE(matches(
2292 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002293 memberExpr(hasObjectExpression(
2294 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002295 EXPECT_TRUE(matches(
2296 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002297 memberExpr(hasObjectExpression(
2298 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002299}
2300
2301TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002302 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2303 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2304 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2305 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002306}
2307
2308TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002309 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002310}
2311
2312TEST(IsConstQualified, MatchesConstInt) {
2313 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002314 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002315}
2316
2317TEST(IsConstQualified, MatchesConstPointer) {
2318 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002319 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002320}
2321
2322TEST(IsConstQualified, MatchesThroughTypedef) {
2323 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002324 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002325 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002326 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002327}
2328
2329TEST(IsConstQualified, DoesNotMatchInappropriately) {
2330 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002331 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002332 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002333 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002334}
2335
Sam Panzer089e5b32012-08-16 16:58:10 +00002336TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002337 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2338 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2339 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2340 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002341}
2342TEST(CastExpression, MatchesImplicitCasts) {
2343 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002344 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002345 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002346 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002347}
2348
2349TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002350 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2351 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2352 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2353 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002354}
2355
Manuel Klimek4da21662012-07-06 05:48:52 +00002356TEST(ReinterpretCast, MatchesSimpleCase) {
2357 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002358 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002359}
2360
2361TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002362 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002363 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002364 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002365 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002366 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002367 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2368 "B b;"
2369 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002370 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002371}
2372
2373TEST(FunctionalCast, MatchesSimpleCase) {
2374 std::string foo_class = "class Foo { public: Foo(char*); };";
2375 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002376 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002377}
2378
2379TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2380 std::string FooClass = "class Foo { public: Foo(char*); };";
2381 EXPECT_TRUE(
2382 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002383 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002384 EXPECT_TRUE(
2385 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002386 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002387}
2388
2389TEST(DynamicCast, MatchesSimpleCase) {
2390 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2391 "B b;"
2392 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002393 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002394}
2395
2396TEST(StaticCast, MatchesSimpleCase) {
2397 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002398 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002399}
2400
2401TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002402 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002403 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002404 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002405 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002406 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002407 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2408 "B b;"
2409 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002410 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002411}
2412
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002413TEST(CStyleCast, MatchesSimpleCase) {
2414 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2415}
2416
2417TEST(CStyleCast, DoesNotMatchOtherCasts) {
2418 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2419 "char q, *r = const_cast<char*>(&q);"
2420 "void* s = reinterpret_cast<char*>(&s);"
2421 "struct B { virtual ~B() {} }; struct D : B {};"
2422 "B b;"
2423 "D* t = dynamic_cast<D*>(&b);",
2424 cStyleCastExpr()));
2425}
2426
Manuel Klimek4da21662012-07-06 05:48:52 +00002427TEST(HasDestinationType, MatchesSimpleCase) {
2428 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002429 staticCastExpr(hasDestinationType(
2430 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002431}
2432
Sam Panzer089e5b32012-08-16 16:58:10 +00002433TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2434 // This test creates an implicit const cast.
2435 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002436 implicitCastExpr(
2437 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002438 // This test creates an implicit array-to-pointer cast.
2439 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002440 implicitCastExpr(hasImplicitDestinationType(
2441 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002442}
2443
2444TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2445 // This test creates an implicit cast from int to char.
2446 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002447 implicitCastExpr(hasImplicitDestinationType(
2448 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002449 // This test creates an implicit array-to-pointer cast.
2450 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002451 implicitCastExpr(hasImplicitDestinationType(
2452 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002453}
2454
2455TEST(ImplicitCast, MatchesSimpleCase) {
2456 // This test creates an implicit const cast.
2457 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002458 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002459 // This test creates an implicit cast from int to char.
2460 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002461 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002462 // This test creates an implicit array-to-pointer cast.
2463 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002464 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002465}
2466
2467TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002468 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002469 // are present, and that it ignores explicit and paren casts.
2470
2471 // These two test cases have no casts.
2472 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002473 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002474 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002475 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002476
2477 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002478 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002479 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002480 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002481
2482 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002483 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002484}
2485
2486TEST(IgnoringImpCasts, MatchesImpCasts) {
2487 // This test checks that ignoringImpCasts matches when implicit casts are
2488 // present and its inner matcher alone does not match.
2489 // Note that this test creates an implicit const cast.
2490 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002491 varDecl(hasInitializer(ignoringImpCasts(
2492 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002493 // This test creates an implict cast from int to char.
2494 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002495 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002496 integerLiteral(equals(0)))))));
2497}
2498
2499TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2500 // These tests verify that ignoringImpCasts does not match if the inner
2501 // matcher does not match.
2502 // Note that the first test creates an implicit const cast.
2503 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002504 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002505 unless(anything()))))));
2506 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002507 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002508 unless(anything()))))));
2509
2510 // These tests verify that ignoringImplictCasts does not look through explicit
2511 // casts or parentheses.
2512 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002513 varDecl(hasInitializer(ignoringImpCasts(
2514 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002515 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002516 varDecl(hasInitializer(ignoringImpCasts(
2517 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002518 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002519 varDecl(hasInitializer(ignoringImpCasts(
2520 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002521 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002522 varDecl(hasInitializer(ignoringImpCasts(
2523 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002524}
2525
2526TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2527 // This test verifies that expressions that do not have implicit casts
2528 // still match the inner matcher.
2529 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002530 varDecl(hasInitializer(ignoringImpCasts(
2531 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002532}
2533
2534TEST(IgnoringParenCasts, MatchesParenCasts) {
2535 // This test checks that ignoringParenCasts matches when parentheses and/or
2536 // casts are present and its inner matcher alone does not match.
2537 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002538 varDecl(hasInitializer(ignoringParenCasts(
2539 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002540 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002541 varDecl(hasInitializer(ignoringParenCasts(
2542 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002543
2544 // This test creates an implict cast from int to char in addition to the
2545 // parentheses.
2546 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002547 varDecl(hasInitializer(ignoringParenCasts(
2548 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002549
2550 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002551 varDecl(hasInitializer(ignoringParenCasts(
2552 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002553 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002554 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002555 integerLiteral(equals(0)))))));
2556}
2557
2558TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2559 // This test verifies that expressions that do not have any casts still match.
2560 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002561 varDecl(hasInitializer(ignoringParenCasts(
2562 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002563}
2564
2565TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2566 // These tests verify that ignoringImpCasts does not match if the inner
2567 // matcher does not match.
2568 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002569 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002570 unless(anything()))))));
2571
2572 // This test creates an implicit cast from int to char in addition to the
2573 // parentheses.
2574 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002575 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002576 unless(anything()))))));
2577
2578 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002579 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002580 unless(anything()))))));
2581}
2582
2583TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2584 // This test checks that ignoringParenAndImpCasts matches when
2585 // parentheses and/or implicit casts are present and its inner matcher alone
2586 // does not match.
2587 // Note that this test creates an implicit const cast.
2588 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002589 varDecl(hasInitializer(ignoringParenImpCasts(
2590 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002591 // This test creates an implicit cast from int to char.
2592 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002593 varDecl(hasInitializer(ignoringParenImpCasts(
2594 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002595}
2596
2597TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2598 // This test verifies that expressions that do not have parentheses or
2599 // implicit casts still match.
2600 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002601 varDecl(hasInitializer(ignoringParenImpCasts(
2602 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002603 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002604 varDecl(hasInitializer(ignoringParenImpCasts(
2605 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002606}
2607
2608TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2609 // These tests verify that ignoringParenImpCasts does not match if
2610 // the inner matcher does not match.
2611 // This test creates an implicit cast.
2612 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002613 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002614 unless(anything()))))));
2615 // These tests verify that ignoringParenAndImplictCasts does not look
2616 // through explicit casts.
2617 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002618 varDecl(hasInitializer(ignoringParenImpCasts(
2619 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002620 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002621 varDecl(hasInitializer(ignoringParenImpCasts(
2622 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002623 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002624 varDecl(hasInitializer(ignoringParenImpCasts(
2625 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002626}
2627
Manuel Klimek715c9562012-07-25 10:02:02 +00002628TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002629 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2630 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002631 implicitCastExpr(
2632 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002633}
2634
Manuel Klimek715c9562012-07-25 10:02:02 +00002635TEST(HasSourceExpression, MatchesExplicitCasts) {
2636 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002637 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002638 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002639 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002640}
2641
Manuel Klimek4da21662012-07-06 05:48:52 +00002642TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002643 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002644}
2645
2646TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002647 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002648}
2649
2650TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002651 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002652}
2653
2654TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002655 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002656}
2657
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002658TEST(InitListExpression, MatchesInitListExpression) {
2659 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2660 initListExpr(hasType(asString("int [2]")))));
2661 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002662 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002663}
2664
2665TEST(UsingDeclaration, MatchesUsingDeclarations) {
2666 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2667 usingDecl()));
2668}
2669
2670TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2671 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2672 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2673}
2674
2675TEST(UsingDeclaration, MatchesSpecificTarget) {
2676 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2677 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002678 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002679 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2680 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002681 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002682}
2683
2684TEST(UsingDeclaration, ThroughUsingDeclaration) {
2685 EXPECT_TRUE(matches(
2686 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002687 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002688 EXPECT_TRUE(notMatches(
2689 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002690 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002691}
2692
Sam Panzer425f41b2012-08-16 17:20:59 +00002693TEST(SingleDecl, IsSingleDecl) {
2694 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002695 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002696 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2697 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2698 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2699 SingleDeclStmt));
2700}
2701
2702TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002703 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002704
2705 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002706 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002707 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002708 declStmt(containsDeclaration(0, MatchesInit),
2709 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002710 unsigned WrongIndex = 42;
2711 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002712 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002713 MatchesInit))));
2714}
2715
2716TEST(DeclCount, DeclCountIsCorrect) {
2717 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002718 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002719 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002720 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002721 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002722 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002723}
2724
Manuel Klimek4da21662012-07-06 05:48:52 +00002725TEST(While, MatchesWhileLoops) {
2726 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2727 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2728 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2729}
2730
2731TEST(Do, MatchesDoLoops) {
2732 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2733 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2734}
2735
2736TEST(Do, DoesNotMatchWhileLoops) {
2737 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2738}
2739
2740TEST(SwitchCase, MatchesCase) {
2741 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2742 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2743 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2744 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2745}
2746
Daniel Jasperb54b7642012-09-20 14:12:57 +00002747TEST(SwitchCase, MatchesSwitch) {
2748 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2749 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2750 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2751 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2752}
2753
2754TEST(ExceptionHandling, SimpleCases) {
2755 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2756 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2757 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2758 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2759 throwExpr()));
2760 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2761 throwExpr()));
2762}
2763
Manuel Klimek4da21662012-07-06 05:48:52 +00002764TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2765 EXPECT_TRUE(notMatches(
2766 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002767 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002768 EXPECT_TRUE(notMatches(
2769 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002770 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002771}
2772
2773TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2774 EXPECT_TRUE(matches(
2775 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002776 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002777}
2778
2779TEST(ForEach, BindsOneNode) {
2780 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002781 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002782 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002783}
2784
2785TEST(ForEach, BindsMultipleNodes) {
2786 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002787 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002788 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002789}
2790
2791TEST(ForEach, BindsRecursiveCombinations) {
2792 EXPECT_TRUE(matchAndVerifyResultTrue(
2793 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002794 recordDecl(hasName("C"),
2795 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002796 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002797}
2798
2799TEST(ForEachDescendant, BindsOneNode) {
2800 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002801 recordDecl(hasName("C"),
2802 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002803 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002804}
2805
Daniel Jasper5f684e92012-11-16 18:39:22 +00002806TEST(ForEachDescendant, NestedForEachDescendant) {
2807 DeclarationMatcher m = recordDecl(
2808 isDefinition(), decl().bind("x"), hasName("C"));
2809 EXPECT_TRUE(matchAndVerifyResultTrue(
2810 "class A { class B { class C {}; }; };",
2811 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
2812 new VerifyIdIsBoundTo<Decl>("x", "C")));
2813
2814 // FIXME: This is not really a useful matcher, but the result is still
2815 // surprising (currently binds "A").
2816 //EXPECT_TRUE(matchAndVerifyResultTrue(
2817 // "class A { class B { class C {}; }; };",
2818 // recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
2819 // new VerifyIdIsBoundTo<Decl>("x", "C")));
2820}
2821
Manuel Klimek4da21662012-07-06 05:48:52 +00002822TEST(ForEachDescendant, BindsMultipleNodes) {
2823 EXPECT_TRUE(matchAndVerifyResultTrue(
2824 "class C { class D { int x; int y; }; "
2825 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002826 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002827 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002828}
2829
2830TEST(ForEachDescendant, BindsRecursiveCombinations) {
2831 EXPECT_TRUE(matchAndVerifyResultTrue(
2832 "class C { class D { "
2833 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002834 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2835 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002836 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002837}
2838
Daniel Jasper11c98772012-11-11 22:14:55 +00002839TEST(ForEachDescendant, BindsCorrectNodes) {
2840 EXPECT_TRUE(matchAndVerifyResultTrue(
2841 "class C { void f(); int i; };",
2842 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2843 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
2844 EXPECT_TRUE(matchAndVerifyResultTrue(
2845 "class C { void f() {} int i; };",
2846 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2847 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
2848}
2849
Manuel Klimek4da21662012-07-06 05:48:52 +00002850
2851TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2852 // Make sure that we can both match the class by name (::X) and by the type
2853 // the template was instantiated with (via a field).
2854
2855 EXPECT_TRUE(matches(
2856 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002857 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002858
2859 EXPECT_TRUE(matches(
2860 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002861 recordDecl(isTemplateInstantiation(), hasDescendant(
2862 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002863}
2864
2865TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2866 EXPECT_TRUE(matches(
2867 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002868 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002869 isTemplateInstantiation())));
2870}
2871
2872TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2873 EXPECT_TRUE(matches(
2874 "template <typename T> class X { T t; }; class A {};"
2875 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002876 recordDecl(isTemplateInstantiation(), hasDescendant(
2877 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002878}
2879
2880TEST(IsTemplateInstantiation,
2881 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2882 EXPECT_TRUE(matches(
2883 "template <typename T> class X {};"
2884 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002885 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002886}
2887
2888TEST(IsTemplateInstantiation,
2889 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2890 EXPECT_TRUE(matches(
2891 "class A {};"
2892 "class X {"
2893 " template <typename U> class Y { U u; };"
2894 " Y<A> y;"
2895 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002896 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002897}
2898
2899TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2900 // FIXME: Figure out whether this makes sense. It doesn't affect the
2901 // normal use case as long as the uppermost instantiation always is marked
2902 // as template instantiation, but it might be confusing as a predicate.
2903 EXPECT_TRUE(matches(
2904 "class A {};"
2905 "template <typename T> class X {"
2906 " template <typename U> class Y { U u; };"
2907 " Y<T> y;"
2908 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002909 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002910}
2911
2912TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2913 EXPECT_TRUE(notMatches(
2914 "template <typename T> class X {}; class A {};"
2915 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002916 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002917}
2918
2919TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2920 EXPECT_TRUE(notMatches(
2921 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002922 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002923}
2924
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002925TEST(IsExplicitTemplateSpecialization,
2926 DoesNotMatchPrimaryTemplate) {
2927 EXPECT_TRUE(notMatches(
2928 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002929 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002930 EXPECT_TRUE(notMatches(
2931 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002932 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002933}
2934
2935TEST(IsExplicitTemplateSpecialization,
2936 DoesNotMatchExplicitTemplateInstantiations) {
2937 EXPECT_TRUE(notMatches(
2938 "template <typename T> class X {};"
2939 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002940 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002941 EXPECT_TRUE(notMatches(
2942 "template <typename T> void f(T t) {}"
2943 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002944 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002945}
2946
2947TEST(IsExplicitTemplateSpecialization,
2948 DoesNotMatchImplicitTemplateInstantiations) {
2949 EXPECT_TRUE(notMatches(
2950 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002951 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002952 EXPECT_TRUE(notMatches(
2953 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002954 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002955}
2956
2957TEST(IsExplicitTemplateSpecialization,
2958 MatchesExplicitTemplateSpecializations) {
2959 EXPECT_TRUE(matches(
2960 "template <typename T> class X {};"
2961 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002962 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002963 EXPECT_TRUE(matches(
2964 "template <typename T> void f(T t) {}"
2965 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002966 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002967}
2968
Manuel Klimek579b1202012-09-07 09:26:10 +00002969TEST(HasAncenstor, MatchesDeclarationAncestors) {
2970 EXPECT_TRUE(matches(
2971 "class A { class B { class C {}; }; };",
2972 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
2973}
2974
2975TEST(HasAncenstor, FailsIfNoAncestorMatches) {
2976 EXPECT_TRUE(notMatches(
2977 "class A { class B { class C {}; }; };",
2978 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
2979}
2980
2981TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
2982 EXPECT_TRUE(matches(
2983 "class A { class B { void f() { C c; } class C {}; }; };",
2984 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
2985 hasAncestor(recordDecl(hasName("A"))))))));
2986}
2987
2988TEST(HasAncenstor, MatchesStatementAncestors) {
2989 EXPECT_TRUE(matches(
2990 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002991 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002992}
2993
2994TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
2995 EXPECT_TRUE(matches(
2996 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002997 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00002998}
2999
3000TEST(HasAncestor, BindsRecursiveCombinations) {
3001 EXPECT_TRUE(matchAndVerifyResultTrue(
3002 "class C { class D { class E { class F { int y; }; }; }; };",
3003 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003004 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00003005}
3006
3007TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3008 EXPECT_TRUE(matchAndVerifyResultTrue(
3009 "class C { class D { class E { class F { int y; }; }; }; };",
3010 fieldDecl(hasAncestor(
3011 decl(
3012 hasDescendant(recordDecl(isDefinition(),
3013 hasAncestor(recordDecl())))
3014 ).bind("d")
3015 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00003016 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00003017}
3018
3019TEST(HasAncestor, MatchesInTemplateInstantiations) {
3020 EXPECT_TRUE(matches(
3021 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3022 "A<int>::B::C a;",
3023 fieldDecl(hasType(asString("int")),
3024 hasAncestor(recordDecl(hasName("A"))))));
3025}
3026
3027TEST(HasAncestor, MatchesInImplicitCode) {
3028 EXPECT_TRUE(matches(
3029 "struct X {}; struct A { A() {} X x; };",
3030 constructorDecl(
3031 hasAnyConstructorInitializer(withInitializer(expr(
3032 hasAncestor(recordDecl(hasName("A")))))))));
3033}
3034
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003035TEST(HasParent, MatchesOnlyParent) {
3036 EXPECT_TRUE(matches(
3037 "void f() { if (true) { int x = 42; } }",
3038 compoundStmt(hasParent(ifStmt()))));
3039 EXPECT_TRUE(notMatches(
3040 "void f() { for (;;) { int x = 42; } }",
3041 compoundStmt(hasParent(ifStmt()))));
3042 EXPECT_TRUE(notMatches(
3043 "void f() { if (true) for (;;) { int x = 42; } }",
3044 compoundStmt(hasParent(ifStmt()))));
3045}
3046
Daniel Jasperce620072012-10-17 08:52:59 +00003047TEST(TypeMatching, MatchesTypes) {
3048 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3049}
3050
3051TEST(TypeMatching, MatchesArrayTypes) {
3052 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3053 EXPECT_TRUE(matches("int a[42];", arrayType()));
3054 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3055
3056 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3057 arrayType(hasElementType(builtinType()))));
3058
3059 EXPECT_TRUE(matches(
3060 "int const a[] = { 2, 3 };",
3061 qualType(arrayType(hasElementType(builtinType())))));
3062 EXPECT_TRUE(matches(
3063 "int const a[] = { 2, 3 };",
3064 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3065 EXPECT_TRUE(matches(
3066 "typedef const int T; T x[] = { 1, 2 };",
3067 qualType(isConstQualified(), arrayType())));
3068
3069 EXPECT_TRUE(notMatches(
3070 "int a[] = { 2, 3 };",
3071 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3072 EXPECT_TRUE(notMatches(
3073 "int a[] = { 2, 3 };",
3074 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3075 EXPECT_TRUE(notMatches(
3076 "int const a[] = { 2, 3 };",
3077 qualType(arrayType(hasElementType(builtinType())),
3078 unless(isConstQualified()))));
3079
3080 EXPECT_TRUE(matches("int a[2];",
3081 constantArrayType(hasElementType(builtinType()))));
3082 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3083}
3084
3085TEST(TypeMatching, MatchesComplexTypes) {
3086 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3087 EXPECT_TRUE(matches(
3088 "_Complex float f;",
3089 complexType(hasElementType(builtinType()))));
3090 EXPECT_TRUE(notMatches(
3091 "_Complex float f;",
3092 complexType(hasElementType(isInteger()))));
3093}
3094
3095TEST(TypeMatching, MatchesConstantArrayTypes) {
3096 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3097 EXPECT_TRUE(notMatches(
3098 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3099 constantArrayType(hasElementType(builtinType()))));
3100
3101 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3102 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3103 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3104}
3105
3106TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3107 EXPECT_TRUE(matches(
3108 "template <typename T, int Size> class array { T data[Size]; };",
3109 dependentSizedArrayType()));
3110 EXPECT_TRUE(notMatches(
3111 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3112 dependentSizedArrayType()));
3113}
3114
3115TEST(TypeMatching, MatchesIncompleteArrayType) {
3116 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3117 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3118
3119 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3120 incompleteArrayType()));
3121}
3122
3123TEST(TypeMatching, MatchesVariableArrayType) {
3124 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3125 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3126
3127 EXPECT_TRUE(matches(
3128 "void f(int b) { int a[b]; }",
3129 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3130 varDecl(hasName("b")))))))));
3131}
3132
3133TEST(TypeMatching, MatchesAtomicTypes) {
3134 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3135
3136 EXPECT_TRUE(matches("_Atomic(int) i;",
3137 atomicType(hasValueType(isInteger()))));
3138 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3139 atomicType(hasValueType(isInteger()))));
3140}
3141
3142TEST(TypeMatching, MatchesAutoTypes) {
3143 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3144 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3145 autoType()));
3146
3147 EXPECT_TRUE(matches("auto a = 1;",
3148 autoType(hasDeducedType(isInteger()))));
3149 EXPECT_TRUE(notMatches("auto b = 2.0;",
3150 autoType(hasDeducedType(isInteger()))));
3151}
3152
Daniel Jaspera267cf62012-10-29 10:14:44 +00003153TEST(TypeMatching, MatchesFunctionTypes) {
3154 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3155 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3156}
3157
Daniel Jasperce620072012-10-17 08:52:59 +00003158TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003159 // FIXME: Reactive when these tests can be more specific (not matching
3160 // implicit code on certain platforms), likely when we have hasDescendant for
3161 // Types/TypeLocs.
3162 //EXPECT_TRUE(matchAndVerifyResultTrue(
3163 // "int* a;",
3164 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3165 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3166 //EXPECT_TRUE(matchAndVerifyResultTrue(
3167 // "int* a;",
3168 // pointerTypeLoc().bind("loc"),
3169 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003170 EXPECT_TRUE(matches(
3171 "int** a;",
3172 pointerTypeLoc(pointeeLoc(loc(qualType())))));
3173 EXPECT_TRUE(matches(
3174 "int** a;",
3175 loc(pointerType(pointee(pointerType())))));
3176 EXPECT_TRUE(matches(
3177 "int* b; int* * const a = &b;",
3178 loc(qualType(isConstQualified(), pointerType()))));
3179
3180 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003181 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3182 hasType(blockPointerType()))));
3183 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3184 hasType(memberPointerType()))));
3185 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3186 hasType(pointerType()))));
3187 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3188 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003189
Daniel Jasper1802daf2012-10-17 13:35:36 +00003190 Fragment = "int *ptr;";
3191 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3192 hasType(blockPointerType()))));
3193 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3194 hasType(memberPointerType()))));
3195 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3196 hasType(pointerType()))));
3197 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3198 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003199
Daniel Jasper1802daf2012-10-17 13:35:36 +00003200 Fragment = "int a; int &ref = a;";
3201 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3202 hasType(blockPointerType()))));
3203 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3204 hasType(memberPointerType()))));
3205 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3206 hasType(pointerType()))));
3207 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3208 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003209}
3210
3211TEST(TypeMatching, PointeeTypes) {
3212 EXPECT_TRUE(matches("int b; int &a = b;",
3213 referenceType(pointee(builtinType()))));
3214 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3215
3216 EXPECT_TRUE(matches("int *a;",
3217 pointerTypeLoc(pointeeLoc(loc(builtinType())))));
3218
3219 EXPECT_TRUE(matches(
3220 "int const *A;",
3221 pointerType(pointee(isConstQualified(), builtinType()))));
3222 EXPECT_TRUE(notMatches(
3223 "int *A;",
3224 pointerType(pointee(isConstQualified(), builtinType()))));
3225}
3226
3227TEST(TypeMatching, MatchesPointersToConstTypes) {
3228 EXPECT_TRUE(matches("int b; int * const a = &b;",
3229 loc(pointerType())));
3230 EXPECT_TRUE(matches("int b; int * const a = &b;",
3231 pointerTypeLoc()));
3232 EXPECT_TRUE(matches(
3233 "int b; const int * a = &b;",
3234 pointerTypeLoc(pointeeLoc(builtinTypeLoc()))));
3235 EXPECT_TRUE(matches(
3236 "int b; const int * a = &b;",
3237 pointerType(pointee(builtinType()))));
3238}
3239
3240TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003241 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3242 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003243
Daniel Jasper1802daf2012-10-17 13:35:36 +00003244 EXPECT_TRUE(matches("typedef int X; X a;",
3245 varDecl(hasName("a"),
3246 hasType(typedefType(hasDecl(decl()))))));
Daniel Jasperce620072012-10-17 08:52:59 +00003247}
3248
Daniel Jaspera7564432012-09-13 13:11:25 +00003249TEST(NNS, MatchesNestedNameSpecifiers) {
3250 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3251 nestedNameSpecifier()));
3252 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3253 nestedNameSpecifier()));
3254 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3255 nestedNameSpecifier()));
3256
3257 EXPECT_TRUE(matches(
3258 "struct A { static void f() {} }; void g() { A::f(); }",
3259 nestedNameSpecifier()));
3260 EXPECT_TRUE(notMatches(
3261 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3262 nestedNameSpecifier()));
3263}
3264
Daniel Jasperb54b7642012-09-20 14:12:57 +00003265TEST(NullStatement, SimpleCases) {
3266 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3267 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3268}
3269
Daniel Jaspera7564432012-09-13 13:11:25 +00003270TEST(NNS, MatchesTypes) {
3271 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3272 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3273 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3274 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3275 Matcher));
3276 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3277}
3278
3279TEST(NNS, MatchesNamespaceDecls) {
3280 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3281 specifiesNamespace(hasName("ns")));
3282 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3283 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3284 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3285}
3286
3287TEST(NNS, BindsNestedNameSpecifiers) {
3288 EXPECT_TRUE(matchAndVerifyResultTrue(
3289 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3290 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3291 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3292}
3293
3294TEST(NNS, BindsNestedNameSpecifierLocs) {
3295 EXPECT_TRUE(matchAndVerifyResultTrue(
3296 "namespace ns { struct B {}; } ns::B b;",
3297 loc(nestedNameSpecifier()).bind("loc"),
3298 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3299}
3300
3301TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3302 EXPECT_TRUE(matches(
3303 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3304 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3305 EXPECT_TRUE(matches(
3306 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003307 nestedNameSpecifierLoc(hasPrefix(
3308 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003309}
3310
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003311TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3312 std::string Fragment =
3313 "namespace a { struct A { struct B { struct C {}; }; }; };"
3314 "void f() { a::A::B::C c; }";
3315 EXPECT_TRUE(matches(
3316 Fragment,
3317 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3318 hasDescendant(nestedNameSpecifier(
3319 specifiesNamespace(hasName("a")))))));
3320 EXPECT_TRUE(notMatches(
3321 Fragment,
3322 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3323 has(nestedNameSpecifier(
3324 specifiesNamespace(hasName("a")))))));
3325 EXPECT_TRUE(matches(
3326 Fragment,
3327 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3328 has(nestedNameSpecifier(
3329 specifiesNamespace(hasName("a")))))));
3330
3331 // Not really useful because a NestedNameSpecifier can af at most one child,
3332 // but to complete the interface.
3333 EXPECT_TRUE(matchAndVerifyResultTrue(
3334 Fragment,
3335 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3336 forEach(nestedNameSpecifier().bind("x"))),
3337 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3338}
3339
3340TEST(NNS, NestedNameSpecifiersAsDescendants) {
3341 std::string Fragment =
3342 "namespace a { struct A { struct B { struct C {}; }; }; };"
3343 "void f() { a::A::B::C c; }";
3344 EXPECT_TRUE(matches(
3345 Fragment,
3346 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3347 asString("struct a::A")))))));
3348 EXPECT_TRUE(matchAndVerifyResultTrue(
3349 Fragment,
3350 functionDecl(hasName("f"),
3351 forEachDescendant(nestedNameSpecifier().bind("x"))),
3352 // Nested names: a, a::A and a::A::B.
3353 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3354}
3355
3356TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3357 std::string Fragment =
3358 "namespace a { struct A { struct B { struct C {}; }; }; };"
3359 "void f() { a::A::B::C c; }";
3360 EXPECT_TRUE(matches(
3361 Fragment,
3362 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3363 hasDescendant(loc(nestedNameSpecifier(
3364 specifiesNamespace(hasName("a"))))))));
3365 EXPECT_TRUE(notMatches(
3366 Fragment,
3367 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3368 has(loc(nestedNameSpecifier(
3369 specifiesNamespace(hasName("a"))))))));
3370 EXPECT_TRUE(matches(
3371 Fragment,
3372 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3373 has(loc(nestedNameSpecifier(
3374 specifiesNamespace(hasName("a"))))))));
3375
3376 EXPECT_TRUE(matchAndVerifyResultTrue(
3377 Fragment,
3378 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3379 forEach(nestedNameSpecifierLoc().bind("x"))),
3380 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3381}
3382
3383TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3384 std::string Fragment =
3385 "namespace a { struct A { struct B { struct C {}; }; }; };"
3386 "void f() { a::A::B::C c; }";
3387 EXPECT_TRUE(matches(
3388 Fragment,
3389 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3390 asString("struct a::A"))))))));
3391 EXPECT_TRUE(matchAndVerifyResultTrue(
3392 Fragment,
3393 functionDecl(hasName("f"),
3394 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3395 // Nested names: a, a::A and a::A::B.
3396 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3397}
3398
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003399template <typename T>
3400class VerifyRecursiveMatch : public BoundNodesCallback {
3401public:
3402 explicit VerifyRecursiveMatch(StringRef Id,
3403 const internal::Matcher<T> &InnerMatcher)
3404 : Id(Id), InnerMatcher(InnerMatcher) {}
Daniel Jasper452abbc2012-10-29 10:48:25 +00003405
3406 virtual bool run(const BoundNodes *Nodes) {
3407 return false;
3408 }
3409
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003410 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3411 const T *Node = Nodes->getNodeAs<T>(Id);
3412 bool Found = false;
3413 MatchFinder Finder;
3414 Finder.addMatcher(InnerMatcher, new VerifyMatch(0, &Found));
3415 Finder.findAll(*Node, *Context);
3416 return Found;
3417 }
3418private:
3419 std::string Id;
3420 internal::Matcher<T> InnerMatcher;
3421};
3422
3423TEST(MatchFinder, CanMatchDeclarationsRecursively) {
3424 EXPECT_TRUE(matchAndVerifyResultTrue("class X { class Y {}; };",
3425 recordDecl(hasName("::X")).bind("X"),
3426 new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Y")))));
3427 EXPECT_TRUE(matchAndVerifyResultFalse("class X { class Y {}; };",
3428 recordDecl(hasName("::X")).bind("X"),
3429 new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Z")))));
3430}
3431
3432TEST(MatchFinder, CanMatchStatementsRecursively) {
3433 EXPECT_TRUE(matchAndVerifyResultTrue("void f() { if (1) { for (;;) { } } }",
3434 ifStmt().bind("if"),
3435 new VerifyRecursiveMatch<clang::Stmt>("if", forStmt())));
3436 EXPECT_TRUE(matchAndVerifyResultFalse("void f() { if (1) { for (;;) { } } }",
3437 ifStmt().bind("if"),
3438 new VerifyRecursiveMatch<clang::Stmt>("if", declStmt())));
3439}
3440
Manuel Klimeke5793282012-11-02 01:31:03 +00003441class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
3442public:
3443 VerifyStartOfTranslationUnit() : Called(false) {}
3444 virtual void run(const MatchFinder::MatchResult &Result) {
3445 EXPECT_TRUE(Called);
3446 }
3447 virtual void onStartOfTranslationUnit() {
3448 Called = true;
3449 }
3450 bool Called;
3451};
3452
3453TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
3454 MatchFinder Finder;
3455 VerifyStartOfTranslationUnit VerifyCallback;
3456 Finder.addMatcher(decl(), &VerifyCallback);
3457 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
3458 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
3459 EXPECT_TRUE(VerifyCallback.Called);
3460}
3461
Manuel Klimek4da21662012-07-06 05:48:52 +00003462} // end namespace ast_matchers
3463} // end namespace clang