blob: a848d55f989ee9f04c983e608777628d536f39fa [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));
Manuel Klimekec33b6f2012-12-10 07:08:53 +000083
84 DeclarationMatcher StartsWithK = namedDecl(matchesName(":k[^:]*$"));
85 EXPECT_TRUE(matches("int k;", StartsWithK));
86 EXPECT_TRUE(matches("int kAbc;", StartsWithK));
87 EXPECT_TRUE(matches("namespace x { int kTest; }", StartsWithK));
88 EXPECT_TRUE(matches("class C { int k; };", StartsWithK));
89 EXPECT_TRUE(notMatches("class C { int ckc; };", StartsWithK));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000090}
91
Manuel Klimek4da21662012-07-06 05:48:52 +000092TEST(DeclarationMatcher, MatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000093 DeclarationMatcher ClassMatcher(recordDecl());
Manuel Klimeke265c872012-07-10 14:21:30 +000094#if !defined(_MSC_VER)
Manuel Klimek4da21662012-07-06 05:48:52 +000095 EXPECT_FALSE(matches("", ClassMatcher));
Manuel Klimeke265c872012-07-10 14:21:30 +000096#else
97 // Matches class type_info.
98 EXPECT_TRUE(matches("", ClassMatcher));
99#endif
Manuel Klimek4da21662012-07-06 05:48:52 +0000100
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000101 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000102 EXPECT_TRUE(matches("class X;", ClassX));
103 EXPECT_TRUE(matches("class X {};", ClassX));
104 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
105 EXPECT_TRUE(notMatches("", ClassX));
106}
107
108TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000109 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000110
111 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000112 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
113 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek4da21662012-07-06 05:48:52 +0000114 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
115 EXPECT_TRUE(notMatches("", IsDerivedFromX));
116
Daniel Jasper63d88722012-09-12 21:14:15 +0000117 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000118
119 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
120 EXPECT_TRUE(matches("class X {};", IsAX));
121 EXPECT_TRUE(matches("class X;", IsAX));
122 EXPECT_TRUE(notMatches("class Y;", IsAX));
123 EXPECT_TRUE(notMatches("", IsAX));
124
Manuel Klimek4da21662012-07-06 05:48:52 +0000125 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000126 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000127 EXPECT_TRUE(
128 matches("class X {}; class Y : public X {}; class Z : public Y {};",
129 ZIsDerivedFromX));
130 EXPECT_TRUE(
131 matches("class X {};"
132 "template<class T> class Y : public X {};"
133 "class Z : public Y<int> {};", ZIsDerivedFromX));
134 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
135 ZIsDerivedFromX));
136 EXPECT_TRUE(
137 matches("template<class T> class X {}; "
138 "template<class T> class Z : public X<T> {};",
139 ZIsDerivedFromX));
140 EXPECT_TRUE(
141 matches("template<class T, class U=T> class X {}; "
142 "template<class T> class Z : public X<T> {};",
143 ZIsDerivedFromX));
144 EXPECT_TRUE(
145 notMatches("template<class X> class A { class Z : public X {}; };",
146 ZIsDerivedFromX));
147 EXPECT_TRUE(
148 matches("template<class X> class A { public: class Z : public X {}; }; "
149 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
150 EXPECT_TRUE(
151 matches("template <class T> class X {}; "
152 "template<class Y> class A { class Z : public X<Y> {}; };",
153 ZIsDerivedFromX));
154 EXPECT_TRUE(
155 notMatches("template<template<class T> class X> class A { "
156 " class Z : public X<int> {}; };", ZIsDerivedFromX));
157 EXPECT_TRUE(
158 matches("template<template<class T> class X> class A { "
159 " public: class Z : public X<int> {}; }; "
160 "template<class T> class X {}; void y() { A<X>::Z z; }",
161 ZIsDerivedFromX));
162 EXPECT_TRUE(
163 notMatches("template<class X> class A { class Z : public X::D {}; };",
164 ZIsDerivedFromX));
165 EXPECT_TRUE(
166 matches("template<class X> class A { public: "
167 " class Z : public X::D {}; }; "
168 "class Y { public: class X {}; typedef X D; }; "
169 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
170 EXPECT_TRUE(
171 matches("class X {}; typedef X Y; class Z : public Y {};",
172 ZIsDerivedFromX));
173 EXPECT_TRUE(
174 matches("template<class T> class Y { typedef typename T::U X; "
175 " class Z : public X {}; };", ZIsDerivedFromX));
176 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
177 ZIsDerivedFromX));
178 EXPECT_TRUE(
179 notMatches("template<class T> class X {}; "
180 "template<class T> class A { class Z : public X<T>::D {}; };",
181 ZIsDerivedFromX));
182 EXPECT_TRUE(
183 matches("template<class T> class X { public: typedef X<T> D; }; "
184 "template<class T> class A { public: "
185 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
186 ZIsDerivedFromX));
187 EXPECT_TRUE(
188 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
189 ZIsDerivedFromX));
190 EXPECT_TRUE(
191 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
192 ZIsDerivedFromX));
193 EXPECT_TRUE(
194 matches("class X {}; class Y : public X {}; "
195 "typedef Y V; typedef V W; class Z : public W {};",
196 ZIsDerivedFromX));
197 EXPECT_TRUE(
198 matches("template<class T, class U> class X {}; "
199 "template<class T> class A { class Z : public X<T, int> {}; };",
200 ZIsDerivedFromX));
201 EXPECT_TRUE(
202 notMatches("template<class X> class D { typedef X A; typedef A B; "
203 " typedef B C; class Z : public C {}; };",
204 ZIsDerivedFromX));
205 EXPECT_TRUE(
206 matches("class X {}; typedef X A; typedef A B; "
207 "class Z : public B {};", ZIsDerivedFromX));
208 EXPECT_TRUE(
209 matches("class X {}; typedef X A; typedef A B; typedef B C; "
210 "class Z : public C {};", ZIsDerivedFromX));
211 EXPECT_TRUE(
212 matches("class U {}; typedef U X; typedef X V; "
213 "class Z : public V {};", ZIsDerivedFromX));
214 EXPECT_TRUE(
215 matches("class Base {}; typedef Base X; "
216 "class Z : public Base {};", ZIsDerivedFromX));
217 EXPECT_TRUE(
218 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
219 "class Z : public Base {};", ZIsDerivedFromX));
220 EXPECT_TRUE(
221 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
222 "class Z : public Base {};", ZIsDerivedFromX));
223 EXPECT_TRUE(
224 matches("class A {}; typedef A X; typedef A Y; "
225 "class Z : public Y {};", ZIsDerivedFromX));
226 EXPECT_TRUE(
227 notMatches("template <typename T> class Z;"
228 "template <> class Z<void> {};"
229 "template <typename T> class Z : public Z<void> {};",
230 IsDerivedFromX));
231 EXPECT_TRUE(
232 matches("template <typename T> class X;"
233 "template <> class X<void> {};"
234 "template <typename T> class X : public X<void> {};",
235 IsDerivedFromX));
236 EXPECT_TRUE(matches(
237 "class X {};"
238 "template <typename T> class Z;"
239 "template <> class Z<void> {};"
240 "template <typename T> class Z : public Z<void>, public X {};",
241 ZIsDerivedFromX));
Manuel Klimek987c2f52012-12-04 13:40:29 +0000242 EXPECT_TRUE(
243 notMatches("template<int> struct X;"
244 "template<int i> struct X : public X<i-1> {};",
245 recordDecl(isDerivedFrom(recordDecl(hasName("Some"))))));
246 EXPECT_TRUE(matches(
247 "struct A {};"
248 "template<int> struct X;"
249 "template<int i> struct X : public X<i-1> {};"
250 "template<> struct X<0> : public A {};"
251 "struct B : public X<42> {};",
252 recordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000253
254 // FIXME: Once we have better matchers for template type matching,
255 // get rid of the Variable(...) matching and match the right template
256 // declarations directly.
257 const char *RecursiveTemplateOneParameter =
258 "class Base1 {}; class Base2 {};"
259 "template <typename T> class Z;"
260 "template <> class Z<void> : public Base1 {};"
261 "template <> class Z<int> : public Base2 {};"
262 "template <> class Z<float> : public Z<void> {};"
263 "template <> class Z<double> : public Z<int> {};"
264 "template <typename T> class Z : public Z<float>, public Z<double> {};"
265 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
266 EXPECT_TRUE(matches(
267 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000268 varDecl(hasName("z_float"),
269 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000270 EXPECT_TRUE(notMatches(
271 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000272 varDecl(hasName("z_float"),
273 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000274 EXPECT_TRUE(matches(
275 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000276 varDecl(hasName("z_char"),
277 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
278 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000279
280 const char *RecursiveTemplateTwoParameters =
281 "class Base1 {}; class Base2 {};"
282 "template <typename T1, typename T2> class Z;"
283 "template <typename T> class Z<void, T> : public Base1 {};"
284 "template <typename T> class Z<int, T> : public Base2 {};"
285 "template <typename T> class Z<float, T> : public Z<void, T> {};"
286 "template <typename T> class Z<double, T> : public Z<int, T> {};"
287 "template <typename T1, typename T2> class Z : "
288 " public Z<float, T2>, public Z<double, T2> {};"
289 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
290 " Z<char, void> z_char; }";
291 EXPECT_TRUE(matches(
292 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000293 varDecl(hasName("z_float"),
294 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000295 EXPECT_TRUE(notMatches(
296 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000297 varDecl(hasName("z_float"),
298 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000299 EXPECT_TRUE(matches(
300 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000301 varDecl(hasName("z_char"),
302 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
303 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000304 EXPECT_TRUE(matches(
305 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000306 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000307 EXPECT_TRUE(notMatches(
308 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000309 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000310
311 EXPECT_TRUE(matches(
312 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000313 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimekb56da8c2013-08-02 21:24:09 +0000314
315 EXPECT_TRUE(matches(
316 "template<typename T> class X {};"
317 "template<typename T> using Z = X<T>;"
318 "template <typename T> class Y : Z<T> {};",
319 recordDecl(isDerivedFrom(namedDecl(hasName("X"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000320}
321
Edwin Vane6a19a972013-03-06 17:02:57 +0000322TEST(DeclarationMatcher, hasMethod) {
323 EXPECT_TRUE(matches("class A { void func(); };",
324 recordDecl(hasMethod(hasName("func")))));
325 EXPECT_TRUE(notMatches("class A { void func(); };",
326 recordDecl(hasMethod(isPublic()))));
327}
328
Daniel Jasper08f0c532012-09-18 14:17:42 +0000329TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
330 EXPECT_TRUE(matches(
331 "template <typename T> struct A {"
332 " template <typename T2> struct F {};"
333 "};"
334 "template <typename T> struct B : A<T>::template F<T> {};"
335 "B<int> b;",
336 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
337}
338
Edwin Vane742d9e72013-02-25 20:43:32 +0000339TEST(DeclarationMatcher, hasDeclContext) {
340 EXPECT_TRUE(matches(
341 "namespace N {"
342 " namespace M {"
343 " class D {};"
344 " }"
345 "}",
Daniel Jasperabe92232013-04-08 16:44:05 +0000346 recordDecl(hasDeclContext(namespaceDecl(hasName("M"))))));
Edwin Vane742d9e72013-02-25 20:43:32 +0000347 EXPECT_TRUE(notMatches(
348 "namespace N {"
349 " namespace M {"
350 " class D {};"
351 " }"
352 "}",
Daniel Jasperabe92232013-04-08 16:44:05 +0000353 recordDecl(hasDeclContext(namespaceDecl(hasName("N"))))));
354
355 EXPECT_TRUE(matches("namespace {"
356 " namespace M {"
357 " class D {};"
358 " }"
359 "}",
360 recordDecl(hasDeclContext(namespaceDecl(
361 hasName("M"), hasDeclContext(namespaceDecl()))))));
Edwin Vane742d9e72013-02-25 20:43:32 +0000362}
363
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000364TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000365 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000366 EXPECT_TRUE(notMatches("class X;", ClassX));
367 EXPECT_TRUE(notMatches("class X {};", ClassX));
368}
369
370TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000371 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000372 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
373 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
374}
375
376TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
377 EXPECT_TRUE(notMatches("template<typename T> class X { };"
378 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000379 classTemplateDecl(hasName("X"),
380 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000381}
382
383TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
384 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
385 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000386 classTemplateDecl(hasName("X"),
387 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000388}
389
Daniel Jasper6a124492012-07-12 08:50:38 +0000390TEST(AllOf, AllOverloadsWork) {
391 const char Program[] =
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000392 "struct T { };"
393 "int f(int, T*, int, int);"
394 "void g(int x) { T t; f(x, &t, 3, 4); }";
Daniel Jasper6a124492012-07-12 08:50:38 +0000395 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000396 callExpr(allOf(callee(functionDecl(hasName("f"))),
397 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000398 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000399 callExpr(allOf(callee(functionDecl(hasName("f"))),
400 hasArgument(0, declRefExpr(to(varDecl()))),
401 hasArgument(1, hasType(pointsTo(
402 recordDecl(hasName("T")))))))));
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000403 EXPECT_TRUE(matches(Program,
404 callExpr(allOf(callee(functionDecl(hasName("f"))),
405 hasArgument(0, declRefExpr(to(varDecl()))),
406 hasArgument(1, hasType(pointsTo(
407 recordDecl(hasName("T"))))),
408 hasArgument(2, integerLiteral(equals(3)))))));
409 EXPECT_TRUE(matches(Program,
410 callExpr(allOf(callee(functionDecl(hasName("f"))),
411 hasArgument(0, declRefExpr(to(varDecl()))),
412 hasArgument(1, hasType(pointsTo(
413 recordDecl(hasName("T"))))),
414 hasArgument(2, integerLiteral(equals(3))),
415 hasArgument(3, integerLiteral(equals(4)))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000416}
417
Manuel Klimek4da21662012-07-06 05:48:52 +0000418TEST(DeclarationMatcher, MatchAnyOf) {
419 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000420 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000421 EXPECT_TRUE(
422 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
423 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
424 EXPECT_TRUE(
425 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
426 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
427
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000428 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000429 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000430 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
431 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
432
Manuel Klimek4da21662012-07-06 05:48:52 +0000433 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000434 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
435 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000436 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
437 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
438 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
439 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
440 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
441 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
442}
443
444TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000445 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000446 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
447 EXPECT_TRUE(matches("class X {};", HasClassX));
448
449 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000450 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000451 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
452 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
453 EXPECT_TRUE(
454 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
455}
456
457TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
458 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000459 recordDecl(
460 has(recordDecl(
461 has(recordDecl(hasName("X"))),
462 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000463 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000464 has(recordDecl(
465 has(recordDecl(hasName("A"))),
466 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000467 hasName("C"))),
468 hasName("F"));
469
470 EXPECT_TRUE(matches(
471 "class F {"
472 " class Z {"
473 " class X {};"
474 " class Y {};"
475 " };"
476 " class C {"
477 " class A {};"
478 " class B {};"
479 " };"
480 "};", Recursive));
481
482 EXPECT_TRUE(matches(
483 "class F {"
484 " class Z {"
485 " class A {};"
486 " class X {};"
487 " class Y {};"
488 " };"
489 " class C {"
490 " class X {};"
491 " class A {};"
492 " class B {};"
493 " };"
494 "};", Recursive));
495
496 EXPECT_TRUE(matches(
497 "class O1 {"
498 " class O2 {"
499 " class F {"
500 " class Z {"
501 " class A {};"
502 " class X {};"
503 " class Y {};"
504 " };"
505 " class C {"
506 " class X {};"
507 " class A {};"
508 " class B {};"
509 " };"
510 " };"
511 " };"
512 "};", Recursive));
513}
514
515TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
516 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000517 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000518 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000519 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000520 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000521 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000522 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000523 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000524 hasName("Y"))),
525 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000526 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000527 anyOf(
528 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000529 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000530 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000531 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000532 hasName("B")))))),
533 hasName("F")));
534
535 EXPECT_TRUE(matches("class F {};", Recursive));
536 EXPECT_TRUE(matches("class Z {};", Recursive));
537 EXPECT_TRUE(matches("class C {};", Recursive));
538 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
539 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
540 EXPECT_TRUE(
541 matches("class O1 { class O2 {"
542 " class M { class N { class B {}; }; }; "
543 "}; };", Recursive));
544}
545
546TEST(DeclarationMatcher, MatchNot) {
547 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000548 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000549 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000550 unless(hasName("X")));
551 EXPECT_TRUE(notMatches("", NotClassX));
552 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
553 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
554 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
555 EXPECT_TRUE(
556 notMatches("class Y {}; class Z {}; class X : public Y {};",
557 NotClassX));
558
559 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000560 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000561 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000562 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000563 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000564 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000565 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
566 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
567 ClassXHasNotClassY));
568}
569
570TEST(DeclarationMatcher, HasDescendant) {
571 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000572 recordDecl(
573 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000574 hasName("Z"));
575 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
576 EXPECT_TRUE(
577 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
578 EXPECT_TRUE(
579 matches("class Z { class A { class Y { class X {}; }; }; };",
580 ZDescendantClassX));
581 EXPECT_TRUE(
582 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
583 ZDescendantClassX));
584 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
585
586 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000587 recordDecl(
588 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000589 hasName("X"))),
590 hasName("Z"));
591 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
592 ZDescendantClassXHasClassY));
593 EXPECT_TRUE(
594 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
595 ZDescendantClassXHasClassY));
596 EXPECT_TRUE(notMatches(
597 "class Z {"
598 " class A {"
599 " class B {"
600 " class X {"
601 " class C {"
602 " class Y {};"
603 " };"
604 " };"
605 " }; "
606 " };"
607 "};", ZDescendantClassXHasClassY));
608
609 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000610 recordDecl(
611 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
612 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000613 hasName("Z"));
614 EXPECT_TRUE(
615 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
616 ZDescendantClassXDescendantClassY));
617 EXPECT_TRUE(matches(
618 "class Z {"
619 " class A {"
620 " class X {"
621 " class B {"
622 " class Y {};"
623 " };"
624 " class Y {};"
625 " };"
626 " };"
627 "};", ZDescendantClassXDescendantClassY));
628}
629
Daniel Jaspera267cf62012-10-29 10:14:44 +0000630// Implements a run method that returns whether BoundNodes contains a
631// Decl bound to Id that can be dynamically cast to T.
632// Optionally checks that the check succeeded a specific number of times.
633template <typename T>
634class VerifyIdIsBoundTo : public BoundNodesCallback {
635public:
636 // Create an object that checks that a node of type \c T was bound to \c Id.
637 // Does not check for a certain number of matches.
638 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
639 : Id(Id), ExpectedCount(-1), Count(0) {}
640
641 // Create an object that checks that a node of type \c T was bound to \c Id.
642 // Checks that there were exactly \c ExpectedCount matches.
643 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
644 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
645
646 // Create an object that checks that a node of type \c T was bound to \c Id.
647 // Checks that there was exactly one match with the name \c ExpectedName.
648 // Note that \c T must be a NamedDecl for this to work.
Manuel Klimek374516c2013-03-14 16:33:21 +0000649 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName,
650 int ExpectedCount = 1)
651 : Id(Id), ExpectedCount(ExpectedCount), Count(0),
652 ExpectedName(ExpectedName) {}
Daniel Jaspera267cf62012-10-29 10:14:44 +0000653
654 ~VerifyIdIsBoundTo() {
655 if (ExpectedCount != -1)
656 EXPECT_EQ(ExpectedCount, Count);
657 if (!ExpectedName.empty())
658 EXPECT_EQ(ExpectedName, Name);
659 }
660
661 virtual bool run(const BoundNodes *Nodes) {
Peter Collingbourne3f0e0402013-11-06 00:27:07 +0000662 const BoundNodes::IDToNodeMap &M = Nodes->getMap();
Daniel Jaspera267cf62012-10-29 10:14:44 +0000663 if (Nodes->getNodeAs<T>(Id)) {
664 ++Count;
665 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
666 Name = Named->getNameAsString();
667 } else if (const NestedNameSpecifier *NNS =
668 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
669 llvm::raw_string_ostream OS(Name);
670 NNS->print(OS, PrintingPolicy(LangOptions()));
671 }
Peter Collingbourne3f0e0402013-11-06 00:27:07 +0000672 BoundNodes::IDToNodeMap::const_iterator I = M.find(Id);
673 EXPECT_NE(M.end(), I);
674 if (I != M.end())
675 EXPECT_EQ(Nodes->getNodeAs<T>(Id), I->second.get<T>());
Daniel Jaspera267cf62012-10-29 10:14:44 +0000676 return true;
677 }
Peter Collingbourne3f0e0402013-11-06 00:27:07 +0000678 EXPECT_TRUE(M.count(Id) == 0 || M.find(Id)->second.template get<T>() == 0);
Daniel Jaspera267cf62012-10-29 10:14:44 +0000679 return false;
680 }
681
Daniel Jasper452abbc2012-10-29 10:48:25 +0000682 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
683 return run(Nodes);
684 }
685
Daniel Jaspera267cf62012-10-29 10:14:44 +0000686private:
687 const std::string Id;
688 const int ExpectedCount;
689 int Count;
690 const std::string ExpectedName;
691 std::string Name;
692};
693
694TEST(HasDescendant, MatchesDescendantTypes) {
695 EXPECT_TRUE(matches("void f() { int i = 3; }",
696 decl(hasDescendant(loc(builtinType())))));
697 EXPECT_TRUE(matches("void f() { int i = 3; }",
698 stmt(hasDescendant(builtinType()))));
699
700 EXPECT_TRUE(matches("void f() { int i = 3; }",
701 stmt(hasDescendant(loc(builtinType())))));
702 EXPECT_TRUE(matches("void f() { int i = 3; }",
703 stmt(hasDescendant(qualType(builtinType())))));
704
705 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
706 stmt(hasDescendant(isInteger()))));
707
708 EXPECT_TRUE(matchAndVerifyResultTrue(
709 "void f() { int a; float c; int d; int e; }",
710 functionDecl(forEachDescendant(
711 varDecl(hasDescendant(isInteger())).bind("x"))),
712 new VerifyIdIsBoundTo<Decl>("x", 3)));
713}
714
715TEST(HasDescendant, MatchesDescendantsOfTypes) {
716 EXPECT_TRUE(matches("void f() { int*** i; }",
717 qualType(hasDescendant(builtinType()))));
718 EXPECT_TRUE(matches("void f() { int*** i; }",
719 qualType(hasDescendant(
720 pointerType(pointee(builtinType()))))));
721 EXPECT_TRUE(matches("void f() { int*** i; }",
David Blaikie5be093c2013-02-18 19:04:16 +0000722 typeLoc(hasDescendant(loc(builtinType())))));
Daniel Jaspera267cf62012-10-29 10:14:44 +0000723
724 EXPECT_TRUE(matchAndVerifyResultTrue(
725 "void f() { int*** i; }",
726 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
727 new VerifyIdIsBoundTo<Type>("x", 2)));
728}
729
730TEST(Has, MatchesChildrenOfTypes) {
731 EXPECT_TRUE(matches("int i;",
732 varDecl(hasName("i"), has(isInteger()))));
733 EXPECT_TRUE(notMatches("int** i;",
734 varDecl(hasName("i"), has(isInteger()))));
735 EXPECT_TRUE(matchAndVerifyResultTrue(
736 "int (*f)(float, int);",
737 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
738 new VerifyIdIsBoundTo<QualType>("x", 2)));
739}
740
741TEST(Has, MatchesChildTypes) {
742 EXPECT_TRUE(matches(
743 "int* i;",
744 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
745 EXPECT_TRUE(notMatches(
746 "int* i;",
747 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
748}
749
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000750TEST(Enum, DoesNotMatchClasses) {
751 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
752}
753
754TEST(Enum, MatchesEnums) {
755 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
756}
757
758TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000759 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000760 EXPECT_TRUE(matches("enum X{ A };", Matcher));
761 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
762 EXPECT_TRUE(notMatches("enum X {};", Matcher));
763}
764
Manuel Klimek4da21662012-07-06 05:48:52 +0000765TEST(StatementMatcher, Has) {
766 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000767 expr(hasType(pointsTo(recordDecl(hasName("X")))),
768 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000769
770 EXPECT_TRUE(matches(
771 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
772 EXPECT_TRUE(notMatches(
773 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
774}
775
776TEST(StatementMatcher, HasDescendant) {
777 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000778 expr(hasType(pointsTo(recordDecl(hasName("X")))),
779 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000780
781 EXPECT_TRUE(matches(
782 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
783 HasDescendantVariableI));
784 EXPECT_TRUE(notMatches(
785 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
786 HasDescendantVariableI));
787}
788
789TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000790 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000791
792 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
793 EXPECT_TRUE(notMatches("class A {};", TypeA));
794
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000795 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000796
797 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
798 TypeDerivedFromA));
799 EXPECT_TRUE(notMatches("class A {};", TypeA));
800
801 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000802 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000803
804 EXPECT_TRUE(
805 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
806}
807
Manuel Klimek4da21662012-07-06 05:48:52 +0000808TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000809 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000810
811 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000812 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000813
814 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000815 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000816
817 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000818 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000819
820 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
821 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000822 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000823
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000824 StatementMatcher MethodX =
825 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000826
827 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
828 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000829 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000830}
831
832TEST(Matcher, BindTheSameNameInAlternatives) {
833 StatementMatcher matcher = anyOf(
834 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000835 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000836 hasRHS(integerLiteral(equals(0)))),
837 binaryOperator(hasOperatorName("+"),
838 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000839 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000840
841 EXPECT_TRUE(matchAndVerifyResultTrue(
842 // The first branch of the matcher binds x to 0 but then fails.
843 // The second branch binds x to f() and succeeds.
844 "int f() { return 0 + f(); }",
845 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000846 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000847}
848
Manuel Klimek66341c52012-08-30 19:41:06 +0000849TEST(Matcher, BindsIDForMemoizedResults) {
850 // Using the same matcher in two match expressions will make memoization
851 // kick in.
852 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
853 EXPECT_TRUE(matchAndVerifyResultTrue(
854 "class A { class B { class X {}; }; };",
855 DeclarationMatcher(anyOf(
856 recordDecl(hasName("A"), hasDescendant(ClassX)),
857 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000858 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000859}
860
Daniel Jasper189f2e42012-12-03 15:43:25 +0000861TEST(HasDeclaration, HasDeclarationOfEnumType) {
862 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
863 expr(hasType(pointsTo(
864 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
865}
866
Edwin Vaneb45083d2013-02-25 14:32:42 +0000867TEST(HasDeclaration, HasGetDeclTraitTest) {
868 EXPECT_TRUE(internal::has_getDecl<TypedefType>::value);
869 EXPECT_TRUE(internal::has_getDecl<RecordType>::value);
870 EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value);
871}
872
Edwin Vane52380602013-02-19 17:14:34 +0000873TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) {
874 EXPECT_TRUE(matches("typedef int X; X a;",
875 varDecl(hasName("a"),
876 hasType(typedefType(hasDeclaration(decl()))))));
877
878 // FIXME: Add tests for other types with getDecl() (e.g. RecordType)
879}
880
Edwin Vane3abf7782013-02-25 14:49:29 +0000881TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) {
882 EXPECT_TRUE(matches("template <typename T> class A {}; A<int> a;",
883 varDecl(hasType(templateSpecializationType(
884 hasDeclaration(namedDecl(hasName("A"))))))));
885}
886
Manuel Klimek4da21662012-07-06 05:48:52 +0000887TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000888 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000889 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000890 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000891 EXPECT_TRUE(
892 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000893 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000894 EXPECT_TRUE(
895 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000896 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000897}
898
899TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000900 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000901 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000902 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000903 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000904 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000905 EXPECT_TRUE(
906 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000907 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000908}
909
910TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000911 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000912 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000913 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000914 EXPECT_TRUE(
915 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000916 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000917}
918
919TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000920 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000921 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000922 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000923 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000924 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000925}
926
Manuel Klimek1a68afd2013-06-20 13:08:29 +0000927TEST(HasTypeLoc, MatchesDeclaratorDecls) {
928 EXPECT_TRUE(matches("int x;",
929 varDecl(hasName("x"), hasTypeLoc(loc(asString("int"))))));
930
931 // Make sure we don't crash on implicit constructors.
932 EXPECT_TRUE(notMatches("class X {}; X x;",
933 declaratorDecl(hasTypeLoc(loc(asString("int"))))));
934}
935
Manuel Klimek4da21662012-07-06 05:48:52 +0000936TEST(Matcher, Call) {
937 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000938 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000939 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000940
941 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
942 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
943
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000944 StatementMatcher MethodOnY =
945 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000946
947 EXPECT_TRUE(
948 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
949 MethodOnY));
950 EXPECT_TRUE(
951 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
952 MethodOnY));
953 EXPECT_TRUE(
954 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
955 MethodOnY));
956 EXPECT_TRUE(
957 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
958 MethodOnY));
959 EXPECT_TRUE(
960 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
961 MethodOnY));
962
963 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000964 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000965
966 EXPECT_TRUE(
967 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
968 MethodOnYPointer));
969 EXPECT_TRUE(
970 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
971 MethodOnYPointer));
972 EXPECT_TRUE(
973 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
974 MethodOnYPointer));
975 EXPECT_TRUE(
976 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
977 MethodOnYPointer));
978 EXPECT_TRUE(
979 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
980 MethodOnYPointer));
981}
982
Daniel Jasper31f7c082012-10-01 13:40:41 +0000983TEST(Matcher, Lambda) {
984 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
985 lambdaExpr()));
986}
987
988TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000989 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
990 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000991 forRangeStmt()));
992 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
993 forRangeStmt()));
994}
995
996TEST(Matcher, UserDefinedLiteral) {
997 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
998 " return i + 1;"
999 "}"
1000 "char c = 'a'_inc;",
1001 userDefinedLiteral()));
1002}
1003
Daniel Jasperb54b7642012-09-20 14:12:57 +00001004TEST(Matcher, FlowControl) {
1005 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
1006 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
1007 continueStmt()));
1008 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
1009 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
1010 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
1011}
1012
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001013TEST(HasType, MatchesAsString) {
1014 EXPECT_TRUE(
1015 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001016 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001017 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001018 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001019 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001020 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001021 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001022 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001023}
1024
Manuel Klimek4da21662012-07-06 05:48:52 +00001025TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001026 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001027 // Unary operator
1028 EXPECT_TRUE(matches("class Y { }; "
1029 "bool operator!(Y x) { return false; }; "
1030 "Y y; bool c = !y;", OpCall));
1031 // No match -- special operators like "new", "delete"
1032 // FIXME: operator new takes size_t, for which we need stddef.h, for which
1033 // we need to figure out include paths in the test.
1034 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
1035 // "class Y { }; "
1036 // "void *operator new(size_t size) { return 0; } "
1037 // "Y *y = new Y;", OpCall));
1038 EXPECT_TRUE(notMatches("class Y { }; "
1039 "void operator delete(void *p) { } "
1040 "void a() {Y *y = new Y; delete y;}", OpCall));
1041 // Binary operator
1042 EXPECT_TRUE(matches("class Y { }; "
1043 "bool operator&&(Y x, Y y) { return true; }; "
1044 "Y a; Y b; bool c = a && b;",
1045 OpCall));
1046 // No match -- normal operator, not an overloaded one.
1047 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
1048 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
1049}
1050
1051TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
1052 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001053 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001054 EXPECT_TRUE(matches("class Y { }; "
1055 "bool operator&&(Y x, Y y) { return true; }; "
1056 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
1057 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001058 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001059 EXPECT_TRUE(notMatches("class Y { }; "
1060 "bool operator&&(Y x, Y y) { return true; }; "
1061 "Y a; Y b; bool c = a && b;",
1062 OpCallLessLess));
Edwin Vane6a19a972013-03-06 17:02:57 +00001063 DeclarationMatcher ClassWithOpStar =
1064 recordDecl(hasMethod(hasOverloadedOperatorName("*")));
1065 EXPECT_TRUE(matches("class Y { int operator*(); };",
1066 ClassWithOpStar));
1067 EXPECT_TRUE(notMatches("class Y { void myOperator(); };",
1068 ClassWithOpStar)) ;
Manuel Klimek4da21662012-07-06 05:48:52 +00001069}
1070
Daniel Jasper278057f2012-11-15 03:29:05 +00001071TEST(Matcher, NestedOverloadedOperatorCalls) {
1072 EXPECT_TRUE(matchAndVerifyResultTrue(
1073 "class Y { }; "
1074 "Y& operator&&(Y& x, Y& y) { return x; }; "
1075 "Y a; Y b; Y c; Y d = a && b && c;",
1076 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
1077 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
1078 EXPECT_TRUE(matches(
1079 "class Y { }; "
1080 "Y& operator&&(Y& x, Y& y) { return x; }; "
1081 "Y a; Y b; Y c; Y d = a && b && c;",
1082 operatorCallExpr(hasParent(operatorCallExpr()))));
1083 EXPECT_TRUE(matches(
1084 "class Y { }; "
1085 "Y& operator&&(Y& x, Y& y) { return x; }; "
1086 "Y a; Y b; Y c; Y d = a && b && c;",
1087 operatorCallExpr(hasDescendant(operatorCallExpr()))));
1088}
1089
Manuel Klimek4da21662012-07-06 05:48:52 +00001090TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +00001091 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001092 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001093
1094 EXPECT_TRUE(
1095 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1096 MethodOnY));
1097 EXPECT_TRUE(
1098 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1099 MethodOnY));
1100 EXPECT_TRUE(
1101 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1102 MethodOnY));
1103 EXPECT_TRUE(
1104 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1105 MethodOnY));
1106 EXPECT_TRUE(
1107 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1108 MethodOnY));
1109
1110 EXPECT_TRUE(matches(
1111 "class Y {"
1112 " public: virtual void x();"
1113 "};"
1114 "class X : public Y {"
1115 " public: virtual void x();"
1116 "};"
1117 "void z() { X *x; x->Y::x(); }", MethodOnY));
1118}
1119
1120TEST(Matcher, VariableUsage) {
1121 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001122 declRefExpr(to(
1123 varDecl(hasInitializer(
1124 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001125
1126 EXPECT_TRUE(matches(
1127 "class Y {"
1128 " public:"
1129 " bool x() const;"
1130 "};"
1131 "void z(const Y &y) {"
1132 " bool b = y.x();"
1133 " if (b) {}"
1134 "}", Reference));
1135
1136 EXPECT_TRUE(notMatches(
1137 "class Y {"
1138 " public:"
1139 " bool x() const;"
1140 "};"
1141 "void z(const Y &y) {"
1142 " bool b = y.x();"
1143 "}", Reference));
1144}
1145
Manuel Klimek8cb9bf52012-12-04 14:42:08 +00001146TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper9bd28092012-07-30 05:03:25 +00001147 EXPECT_TRUE(matches(
1148 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001149 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001150}
1151
Manuel Klimek4da21662012-07-06 05:48:52 +00001152TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001153 StatementMatcher CallOnVariableY =
1154 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001155
1156 EXPECT_TRUE(matches(
1157 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1158 EXPECT_TRUE(matches(
1159 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1160 EXPECT_TRUE(matches(
1161 "class Y { public: void x(); };"
1162 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1163 EXPECT_TRUE(matches(
1164 "class Y { public: void x(); };"
1165 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1166 EXPECT_TRUE(notMatches(
1167 "class Y { public: void x(); };"
1168 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1169 CallOnVariableY));
1170}
1171
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001172TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1173 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1174 unaryExprOrTypeTraitExpr()));
1175 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1176 alignOfExpr(anything())));
1177 // FIXME: Uncomment once alignof is enabled.
1178 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1179 // unaryExprOrTypeTraitExpr()));
1180 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1181 // sizeOfExpr()));
1182}
1183
1184TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1185 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1186 hasArgumentOfType(asString("int")))));
1187 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1188 hasArgumentOfType(asString("float")))));
1189 EXPECT_TRUE(matches(
1190 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001191 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001192 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001193 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001194}
1195
Manuel Klimek4da21662012-07-06 05:48:52 +00001196TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001197 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001198}
1199
1200TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001201 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001202}
1203
1204TEST(MemberExpression, MatchesVariable) {
1205 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001206 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001207 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001208 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001209 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001210 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001211}
1212
1213TEST(MemberExpression, MatchesStaticVariable) {
1214 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001215 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001216 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001217 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001218 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001219 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001220}
1221
Daniel Jasper6a124492012-07-12 08:50:38 +00001222TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001223 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1224 EXPECT_TRUE(matches(
1225 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1226 callExpr(hasArgument(0, declRefExpr(
1227 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001228}
1229
1230TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001231 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001232 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001233 callExpr(hasArgument(0, declRefExpr(
1234 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001235}
1236
Manuel Klimek4da21662012-07-06 05:48:52 +00001237TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1238 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001239 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001240 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001241 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001242 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001243 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001244}
1245
1246TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1247 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001248 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001249 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001250 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001251 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001252 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001253}
1254
1255TEST(IsArrow, MatchesMemberCallsViaArrow) {
1256 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001257 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001258 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001259 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001260 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001261 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001262}
1263
1264TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001265 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001266
1267 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1268 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1269}
1270
1271TEST(Callee, MatchesMemberExpressions) {
1272 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001273 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001274 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001275 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001276}
1277
1278TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001279 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001280
1281 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1282 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1283
Manuel Klimeke265c872012-07-10 14:21:30 +00001284#if !defined(_MSC_VER)
1285 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001286 // Dependent contexts, but a non-dependent call.
1287 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1288 CallFunctionF));
1289 EXPECT_TRUE(
1290 matches("void f(); template <int N> struct S { void g() { f(); } };",
1291 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001292#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001293
1294 // Depedent calls don't match.
1295 EXPECT_TRUE(
1296 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1297 CallFunctionF));
1298 EXPECT_TRUE(
1299 notMatches("void f(int);"
1300 "template <typename T> struct S { void g(T t) { f(t); } };",
1301 CallFunctionF));
1302}
1303
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001304TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1305 EXPECT_TRUE(
1306 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001307 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001308}
1309
1310TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1311 EXPECT_TRUE(
1312 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001313 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001314}
1315
1316TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1317 EXPECT_TRUE(
1318 notMatches("void g(); template <typename T> void f(T t) {}"
1319 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001320 functionTemplateDecl(hasName("f"),
1321 hasDescendant(declRefExpr(to(
1322 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001323}
1324
Manuel Klimek4da21662012-07-06 05:48:52 +00001325TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001326 StatementMatcher CallArgumentY = callExpr(
1327 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001328
1329 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1330 EXPECT_TRUE(
1331 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1332 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1333
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001334 StatementMatcher WrongIndex = callExpr(
1335 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001336 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1337}
1338
1339TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001340 StatementMatcher CallArgumentY = callExpr(
1341 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001342 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1343 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1344 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1345}
1346
1347TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001348 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001349
1350 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1351 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1352 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1353}
1354
Daniel Jasper36e29d62012-12-04 11:54:27 +00001355TEST(Matcher, ParameterCount) {
1356 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1357 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1358 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1359 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1360 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1361}
1362
Manuel Klimek4da21662012-07-06 05:48:52 +00001363TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001364 DeclarationMatcher ReferenceClassX = varDecl(
1365 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001366 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1367 ReferenceClassX));
1368 EXPECT_TRUE(
1369 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
Michael Han4b6730d2013-09-11 15:53:29 +00001370 // The match here is on the implicit copy constructor code for
1371 // class X, not on code 'X x = y'.
Manuel Klimek4da21662012-07-06 05:48:52 +00001372 EXPECT_TRUE(
Michael Han4b6730d2013-09-11 15:53:29 +00001373 matches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1374 EXPECT_TRUE(
1375 notMatches("class X {}; extern X x;", ReferenceClassX));
Manuel Klimek4da21662012-07-06 05:48:52 +00001376 EXPECT_TRUE(
1377 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1378}
1379
Edwin Vane6a19a972013-03-06 17:02:57 +00001380TEST(QualType, hasCanonicalType) {
1381 EXPECT_TRUE(notMatches("typedef int &int_ref;"
1382 "int a;"
1383 "int_ref b = a;",
1384 varDecl(hasType(qualType(referenceType())))));
1385 EXPECT_TRUE(
1386 matches("typedef int &int_ref;"
1387 "int a;"
1388 "int_ref b = a;",
1389 varDecl(hasType(qualType(hasCanonicalType(referenceType()))))));
1390}
1391
Edwin Vane7b69cd02013-04-02 18:15:55 +00001392TEST(QualType, hasLocalQualifiers) {
1393 EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;",
1394 varDecl(hasType(hasLocalQualifiers()))));
1395 EXPECT_TRUE(matches("int *const j = nullptr;",
1396 varDecl(hasType(hasLocalQualifiers()))));
1397 EXPECT_TRUE(matches("int *volatile k;",
1398 varDecl(hasType(hasLocalQualifiers()))));
1399 EXPECT_TRUE(notMatches("int m;",
1400 varDecl(hasType(hasLocalQualifiers()))));
1401}
1402
Manuel Klimek4da21662012-07-06 05:48:52 +00001403TEST(HasParameter, CallsInnerMatcher) {
1404 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001405 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001406 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001407 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001408}
1409
1410TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1411 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001412 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001413}
1414
1415TEST(HasType, MatchesParameterVariableTypesStrictly) {
1416 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001417 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001418 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001419 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001420 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001421 methodDecl(hasParameter(0,
1422 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001423 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001424 methodDecl(hasParameter(0,
1425 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001426}
1427
1428TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1429 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001430 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001431 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001432 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001433}
1434
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001435TEST(Returns, MatchesReturnTypes) {
1436 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001437 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001438 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001439 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001440 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001441 functionDecl(returns(hasDeclaration(
1442 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001443}
1444
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001445TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001446 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1447 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1448 functionDecl(isExternC())));
1449 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001450}
1451
Manuel Klimek4da21662012-07-06 05:48:52 +00001452TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1453 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001454 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001455}
1456
1457TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1458 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001459 methodDecl(hasAnyParameter(hasType(pointsTo(
1460 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001461}
1462
1463TEST(HasName, MatchesParameterVariableDeclartions) {
1464 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001465 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001466 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001467 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001468}
1469
Daniel Jasper371f9392012-08-01 08:40:24 +00001470TEST(Matcher, MatchesClassTemplateSpecialization) {
1471 EXPECT_TRUE(matches("template<typename T> struct A {};"
1472 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001473 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001474 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001475 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001476 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001477 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001478}
1479
Manuel Klimek1a68afd2013-06-20 13:08:29 +00001480TEST(DeclaratorDecl, MatchesDeclaratorDecls) {
1481 EXPECT_TRUE(matches("int x;", declaratorDecl()));
1482 EXPECT_TRUE(notMatches("class A {};", declaratorDecl()));
1483}
1484
1485TEST(ParmVarDecl, MatchesParmVars) {
1486 EXPECT_TRUE(matches("void f(int x);", parmVarDecl()));
1487 EXPECT_TRUE(notMatches("void f();", parmVarDecl()));
1488}
1489
Daniel Jasper371f9392012-08-01 08:40:24 +00001490TEST(Matcher, MatchesTypeTemplateArgument) {
1491 EXPECT_TRUE(matches(
1492 "template<typename T> struct B {};"
1493 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001494 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001495 asString("int"))))));
1496}
1497
1498TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1499 EXPECT_TRUE(matches(
1500 "struct B { int next; };"
1501 "template<int(B::*next_ptr)> struct A {};"
1502 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001503 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1504 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001505
1506 EXPECT_TRUE(notMatches(
1507 "template <typename T> struct A {};"
1508 "A<int> a;",
1509 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1510 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001511}
1512
1513TEST(Matcher, MatchesSpecificArgument) {
1514 EXPECT_TRUE(matches(
1515 "template<typename T, typename U> class A {};"
1516 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001517 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001518 1, refersToType(asString("int"))))));
1519 EXPECT_TRUE(notMatches(
1520 "template<typename T, typename U> class A {};"
1521 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001522 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001523 1, refersToType(asString("int"))))));
1524}
1525
Daniel Jasperf3197e92013-02-25 12:02:08 +00001526TEST(Matcher, MatchesAccessSpecDecls) {
1527 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1528 EXPECT_TRUE(
1529 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1530 EXPECT_TRUE(
1531 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1532 EXPECT_TRUE(
1533 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1534
1535 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1536}
1537
Edwin Vane5771a2f2013-04-09 20:46:36 +00001538TEST(Matcher, MatchesVirtualMethod) {
1539 EXPECT_TRUE(matches("class X { virtual int f(); };",
1540 methodDecl(isVirtual(), hasName("::X::f"))));
1541 EXPECT_TRUE(notMatches("class X { int f(); };",
1542 methodDecl(isVirtual())));
1543}
1544
Edwin Vane32a6ebc2013-05-09 17:00:17 +00001545TEST(Matcher, MatchesConstMethod) {
1546 EXPECT_TRUE(matches("struct A { void foo() const; };",
1547 methodDecl(isConst())));
1548 EXPECT_TRUE(notMatches("struct A { void foo(); };",
1549 methodDecl(isConst())));
1550}
1551
Edwin Vane5771a2f2013-04-09 20:46:36 +00001552TEST(Matcher, MatchesOverridingMethod) {
1553 EXPECT_TRUE(matches("class X { virtual int f(); }; "
1554 "class Y : public X { int f(); };",
1555 methodDecl(isOverride(), hasName("::Y::f"))));
1556 EXPECT_TRUE(notMatches("class X { virtual int f(); }; "
1557 "class Y : public X { int f(); };",
1558 methodDecl(isOverride(), hasName("::X::f"))));
1559 EXPECT_TRUE(notMatches("class X { int f(); }; "
1560 "class Y : public X { int f(); };",
1561 methodDecl(isOverride())));
1562 EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ",
1563 methodDecl(isOverride())));
1564}
1565
Manuel Klimek4da21662012-07-06 05:48:52 +00001566TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001567 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001568
1569 EXPECT_TRUE(
1570 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1571 EXPECT_TRUE(
1572 matches("class X { public: X(); }; void x() { X x = X(); }",
1573 Constructor));
1574 EXPECT_TRUE(
1575 matches("class X { public: X(int); }; void x() { X x = 0; }",
1576 Constructor));
1577 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1578}
1579
1580TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001581 StatementMatcher Constructor = constructExpr(
1582 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001583
1584 EXPECT_TRUE(
1585 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1586 Constructor));
1587 EXPECT_TRUE(
1588 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1589 Constructor));
1590 EXPECT_TRUE(
1591 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1592 Constructor));
1593 EXPECT_TRUE(
1594 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1595 Constructor));
1596
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001597 StatementMatcher WrongIndex = constructExpr(
1598 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001599 EXPECT_TRUE(
1600 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1601 WrongIndex));
1602}
1603
1604TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001605 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001606
1607 EXPECT_TRUE(
1608 matches("class X { public: X(int); }; void x() { X x(0); }",
1609 Constructor1Arg));
1610 EXPECT_TRUE(
1611 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1612 Constructor1Arg));
1613 EXPECT_TRUE(
1614 matches("class X { public: X(int); }; void x() { X x = 0; }",
1615 Constructor1Arg));
1616 EXPECT_TRUE(
1617 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1618 Constructor1Arg));
1619}
1620
Manuel Klimek70b9db92012-10-23 10:40:50 +00001621TEST(Matcher,ThisExpr) {
1622 EXPECT_TRUE(
1623 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1624 EXPECT_TRUE(
1625 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1626}
1627
Manuel Klimek4da21662012-07-06 05:48:52 +00001628TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001629 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001630
1631 std::string ClassString = "class string { public: string(); ~string(); }; ";
1632
1633 EXPECT_TRUE(
1634 matches(ClassString +
1635 "string GetStringByValue();"
1636 "void FunctionTakesString(string s);"
1637 "void run() { FunctionTakesString(GetStringByValue()); }",
1638 TempExpression));
1639
1640 EXPECT_TRUE(
1641 notMatches(ClassString +
1642 "string* GetStringPointer(); "
1643 "void FunctionTakesStringPtr(string* s);"
1644 "void run() {"
1645 " string* s = GetStringPointer();"
1646 " FunctionTakesStringPtr(GetStringPointer());"
1647 " FunctionTakesStringPtr(s);"
1648 "}",
1649 TempExpression));
1650
1651 EXPECT_TRUE(
1652 notMatches("class no_dtor {};"
1653 "no_dtor GetObjByValue();"
1654 "void ConsumeObj(no_dtor param);"
1655 "void run() { ConsumeObj(GetObjByValue()); }",
1656 TempExpression));
1657}
1658
Sam Panzere16acd32012-08-24 22:04:44 +00001659TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1660 std::string ClassString =
1661 "class string { public: string(); int length(); }; ";
1662
1663 EXPECT_TRUE(
1664 matches(ClassString +
1665 "string GetStringByValue();"
1666 "void FunctionTakesString(string s);"
1667 "void run() { FunctionTakesString(GetStringByValue()); }",
1668 materializeTemporaryExpr()));
1669
1670 EXPECT_TRUE(
1671 notMatches(ClassString +
1672 "string* GetStringPointer(); "
1673 "void FunctionTakesStringPtr(string* s);"
1674 "void run() {"
1675 " string* s = GetStringPointer();"
1676 " FunctionTakesStringPtr(GetStringPointer());"
1677 " FunctionTakesStringPtr(s);"
1678 "}",
1679 materializeTemporaryExpr()));
1680
1681 EXPECT_TRUE(
1682 notMatches(ClassString +
1683 "string GetStringByValue();"
1684 "void run() { int k = GetStringByValue().length(); }",
1685 materializeTemporaryExpr()));
1686
1687 EXPECT_TRUE(
1688 notMatches(ClassString +
1689 "string GetStringByValue();"
1690 "void run() { GetStringByValue(); }",
1691 materializeTemporaryExpr()));
1692}
1693
Manuel Klimek4da21662012-07-06 05:48:52 +00001694TEST(ConstructorDeclaration, SimpleCase) {
1695 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001696 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001697 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001698 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001699}
1700
1701TEST(ConstructorDeclaration, IsImplicit) {
1702 // This one doesn't match because the constructor is not added by the
1703 // compiler (it is not needed).
1704 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001705 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001706 // The compiler added the implicit default constructor.
1707 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001708 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001709 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001710 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001711}
1712
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001713TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1714 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001715 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001716}
1717
1718TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001719 EXPECT_TRUE(notMatches("class Foo {};",
1720 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001721}
1722
Manuel Klimek4da21662012-07-06 05:48:52 +00001723TEST(HasAnyConstructorInitializer, SimpleCase) {
1724 EXPECT_TRUE(notMatches(
1725 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001726 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001727 EXPECT_TRUE(matches(
1728 "class Foo {"
1729 " Foo() : foo_() { }"
1730 " int foo_;"
1731 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001732 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001733}
1734
1735TEST(HasAnyConstructorInitializer, ForField) {
1736 static const char Code[] =
1737 "class Baz { };"
1738 "class Foo {"
1739 " Foo() : foo_() { }"
1740 " Baz foo_;"
1741 " Baz bar_;"
1742 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001743 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1744 forField(hasType(recordDecl(hasName("Baz"))))))));
1745 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001746 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001747 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1748 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001749}
1750
1751TEST(HasAnyConstructorInitializer, WithInitializer) {
1752 static const char Code[] =
1753 "class Foo {"
1754 " Foo() : foo_(0) { }"
1755 " int foo_;"
1756 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001757 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001758 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001759 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001760 withInitializer(integerLiteral(equals(1)))))));
1761}
1762
1763TEST(HasAnyConstructorInitializer, IsWritten) {
1764 static const char Code[] =
1765 "struct Bar { Bar(){} };"
1766 "class Foo {"
1767 " Foo() : foo_() { }"
1768 " Bar foo_;"
1769 " Bar bar_;"
1770 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001771 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001772 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001773 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001774 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001775 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001776 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1777}
1778
1779TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001780 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001781
1782 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1783 EXPECT_TRUE(
1784 matches("class X { public: X(); }; void x() { new X(); }", New));
1785 EXPECT_TRUE(
1786 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1787 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1788}
1789
1790TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001791 StatementMatcher New = constructExpr(
1792 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001793
1794 EXPECT_TRUE(
1795 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1796 New));
1797 EXPECT_TRUE(
1798 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1799 New));
1800 EXPECT_TRUE(
1801 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1802 New));
1803
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001804 StatementMatcher WrongIndex = constructExpr(
1805 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001806 EXPECT_TRUE(
1807 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1808 WrongIndex));
1809}
1810
1811TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001812 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001813
1814 EXPECT_TRUE(
1815 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1816 EXPECT_TRUE(
1817 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1818 New));
1819}
1820
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001821TEST(Matcher, DeleteExpression) {
1822 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001823 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001824}
1825
Manuel Klimek4da21662012-07-06 05:48:52 +00001826TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001827 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001828
1829 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1830 EXPECT_TRUE(
1831 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1832 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1833}
1834
1835TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001836 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001837 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1838 // wide string
1839 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1840 // with escaped characters
1841 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1842 // no matching -- though the data type is the same, there is no string literal
1843 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1844}
1845
1846TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001847 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001848 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1849 // wide character
1850 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1851 // wide character, Hex encoded, NOT MATCHED!
1852 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1853 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1854}
1855
1856TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001857 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001858 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1859 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1860 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1861 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1862
1863 // Non-matching cases (character literals, float and double)
1864 EXPECT_TRUE(notMatches("int i = L'a';",
1865 HasIntLiteral)); // this is actually a character
1866 // literal cast to int
1867 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1868 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1869 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1870}
1871
Daniel Jasperc5ae7172013-07-26 18:52:58 +00001872TEST(Matcher, FloatLiterals) {
1873 StatementMatcher HasFloatLiteral = floatLiteral();
1874 EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral));
1875 EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral));
1876 EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral));
1877 EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral));
1878 EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral));
1879
1880 EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral));
1881}
1882
Daniel Jasper31f7c082012-10-01 13:40:41 +00001883TEST(Matcher, NullPtrLiteral) {
1884 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1885}
1886
Daniel Jasperb54b7642012-09-20 14:12:57 +00001887TEST(Matcher, AsmStatement) {
1888 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1889}
1890
Manuel Klimek4da21662012-07-06 05:48:52 +00001891TEST(Matcher, Conditions) {
1892 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1893
1894 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1895 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1896 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1897 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1898 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1899}
1900
1901TEST(MatchBinaryOperator, HasOperatorName) {
1902 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1903
1904 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1905 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1906}
1907
1908TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1909 StatementMatcher OperatorTrueFalse =
1910 binaryOperator(hasLHS(boolLiteral(equals(true))),
1911 hasRHS(boolLiteral(equals(false))));
1912
1913 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1914 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1915 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1916}
1917
1918TEST(MatchBinaryOperator, HasEitherOperand) {
1919 StatementMatcher HasOperand =
1920 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1921
1922 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1923 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1924 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1925}
1926
1927TEST(Matcher, BinaryOperatorTypes) {
1928 // Integration test that verifies the AST provides all binary operators in
1929 // a way we expect.
1930 // FIXME: Operator ','
1931 EXPECT_TRUE(
1932 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1933 EXPECT_TRUE(
1934 matches("bool b; bool c = (b = true);",
1935 binaryOperator(hasOperatorName("="))));
1936 EXPECT_TRUE(
1937 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1938 EXPECT_TRUE(
1939 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1940 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1941 EXPECT_TRUE(
1942 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1943 EXPECT_TRUE(
1944 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1945 EXPECT_TRUE(
1946 matches("int i = 1; int j = (i <<= 2);",
1947 binaryOperator(hasOperatorName("<<="))));
1948 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1949 EXPECT_TRUE(
1950 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1951 EXPECT_TRUE(
1952 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1953 EXPECT_TRUE(
1954 matches("int i = 1; int j = (i >>= 2);",
1955 binaryOperator(hasOperatorName(">>="))));
1956 EXPECT_TRUE(
1957 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1958 EXPECT_TRUE(
1959 matches("int i = 42; int j = (i ^= 42);",
1960 binaryOperator(hasOperatorName("^="))));
1961 EXPECT_TRUE(
1962 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1963 EXPECT_TRUE(
1964 matches("int i = 42; int j = (i %= 42);",
1965 binaryOperator(hasOperatorName("%="))));
1966 EXPECT_TRUE(
1967 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1968 EXPECT_TRUE(
1969 matches("bool b = true && false;",
1970 binaryOperator(hasOperatorName("&&"))));
1971 EXPECT_TRUE(
1972 matches("bool b = true; bool c = (b &= false);",
1973 binaryOperator(hasOperatorName("&="))));
1974 EXPECT_TRUE(
1975 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1976 EXPECT_TRUE(
1977 matches("bool b = true || false;",
1978 binaryOperator(hasOperatorName("||"))));
1979 EXPECT_TRUE(
1980 matches("bool b = true; bool c = (b |= false);",
1981 binaryOperator(hasOperatorName("|="))));
1982 EXPECT_TRUE(
1983 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1984 EXPECT_TRUE(
1985 matches("int i = 42; int j = (i *= 23);",
1986 binaryOperator(hasOperatorName("*="))));
1987 EXPECT_TRUE(
1988 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1989 EXPECT_TRUE(
1990 matches("int i = 42; int j = (i /= 23);",
1991 binaryOperator(hasOperatorName("/="))));
1992 EXPECT_TRUE(
1993 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1994 EXPECT_TRUE(
1995 matches("int i = 42; int j = (i += 23);",
1996 binaryOperator(hasOperatorName("+="))));
1997 EXPECT_TRUE(
1998 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1999 EXPECT_TRUE(
2000 matches("int i = 42; int j = (i -= 23);",
2001 binaryOperator(hasOperatorName("-="))));
2002 EXPECT_TRUE(
2003 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
2004 binaryOperator(hasOperatorName("->*"))));
2005 EXPECT_TRUE(
2006 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
2007 binaryOperator(hasOperatorName(".*"))));
2008
2009 // Member expressions as operators are not supported in matches.
2010 EXPECT_TRUE(
2011 notMatches("struct A { void x(A *a) { a->x(this); } };",
2012 binaryOperator(hasOperatorName("->"))));
2013
2014 // Initializer assignments are not represented as operator equals.
2015 EXPECT_TRUE(
2016 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
2017
2018 // Array indexing is not represented as operator.
2019 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
2020
2021 // Overloaded operators do not match at all.
2022 EXPECT_TRUE(notMatches(
2023 "struct A { bool operator&&(const A &a) const { return false; } };"
2024 "void x() { A a, b; a && b; }",
2025 binaryOperator()));
2026}
2027
2028TEST(MatchUnaryOperator, HasOperatorName) {
2029 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
2030
2031 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
2032 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
2033}
2034
2035TEST(MatchUnaryOperator, HasUnaryOperand) {
2036 StatementMatcher OperatorOnFalse =
2037 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
2038
2039 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
2040 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
2041}
2042
2043TEST(Matcher, UnaryOperatorTypes) {
2044 // Integration test that verifies the AST provides all unary operators in
2045 // a way we expect.
2046 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
2047 EXPECT_TRUE(
2048 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
2049 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
2050 EXPECT_TRUE(
2051 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
2052 EXPECT_TRUE(
2053 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
2054 EXPECT_TRUE(
2055 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
2056 EXPECT_TRUE(
2057 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
2058 EXPECT_TRUE(
2059 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
2060 EXPECT_TRUE(
2061 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
2062 EXPECT_TRUE(
2063 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
2064
2065 // We don't match conversion operators.
2066 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
2067
2068 // Function calls are not represented as operator.
2069 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
2070
2071 // Overloaded operators do not match at all.
2072 // FIXME: We probably want to add that.
2073 EXPECT_TRUE(notMatches(
2074 "struct A { bool operator!() const { return false; } };"
2075 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
2076}
2077
2078TEST(Matcher, ConditionalOperator) {
2079 StatementMatcher Conditional = conditionalOperator(
2080 hasCondition(boolLiteral(equals(true))),
2081 hasTrueExpression(boolLiteral(equals(false))));
2082
2083 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
2084 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
2085 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
2086
2087 StatementMatcher ConditionalFalse = conditionalOperator(
2088 hasFalseExpression(boolLiteral(equals(false))));
2089
2090 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
2091 EXPECT_TRUE(
2092 notMatches("void x() { true ? false : true; }", ConditionalFalse));
2093}
2094
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002095TEST(ArraySubscriptMatchers, ArraySubscripts) {
2096 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2097 arraySubscriptExpr()));
2098 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2099 arraySubscriptExpr()));
2100}
2101
2102TEST(ArraySubscriptMatchers, ArrayIndex) {
2103 EXPECT_TRUE(matches(
2104 "int i[2]; void f() { i[1] = 1; }",
2105 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2106 EXPECT_TRUE(matches(
2107 "int i[2]; void f() { 1[i] = 1; }",
2108 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2109 EXPECT_TRUE(notMatches(
2110 "int i[2]; void f() { i[1] = 1; }",
2111 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2112}
2113
2114TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2115 EXPECT_TRUE(matches(
2116 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002117 arraySubscriptExpr(hasBase(implicitCastExpr(
2118 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002119}
2120
Manuel Klimek4da21662012-07-06 05:48:52 +00002121TEST(Matcher, HasNameSupportsNamespaces) {
2122 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002123 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002124 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002125 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002126 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002127 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002128 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002129 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002130 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002131 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002132 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002133 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002134 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002135 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002136 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002137 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002138 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002139 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002140 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002141 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002142 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002143 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002144 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002145 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002146}
2147
2148TEST(Matcher, HasNameSupportsOuterClasses) {
2149 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002150 matches("class A { class B { class C; }; };",
2151 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002152 EXPECT_TRUE(
2153 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002154 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002155 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002156 matches("class A { class B { class C; }; };",
2157 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002158 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002159 matches("class A { class B { class C; }; };",
2160 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002161 EXPECT_TRUE(
2162 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002163 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002164 EXPECT_TRUE(
2165 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002166 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002167 EXPECT_TRUE(
2168 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002169 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002170 EXPECT_TRUE(
2171 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002172 recordDecl(hasName("::C"))));
2173 EXPECT_TRUE(
2174 notMatches("class A { class B { class C; }; };",
2175 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002176 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002177 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002178 EXPECT_TRUE(
2179 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002180 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002181}
2182
2183TEST(Matcher, IsDefinition) {
2184 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002185 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002186 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2187 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2188
2189 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002190 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002191 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2192 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2193
2194 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002195 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002196 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2197 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2198}
2199
2200TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002201 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00002202 ofClass(hasName("X")))));
2203
2204 EXPECT_TRUE(
2205 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2206 EXPECT_TRUE(
2207 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2208 Constructor));
2209 EXPECT_TRUE(
2210 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2211 Constructor));
2212}
2213
2214TEST(Matcher, VisitsTemplateInstantiations) {
2215 EXPECT_TRUE(matches(
2216 "class A { public: void x(); };"
2217 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002218 "void f() { B<A> b; b.y(); }",
2219 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002220
2221 EXPECT_TRUE(matches(
2222 "class A { public: void x(); };"
2223 "class C {"
2224 " public:"
2225 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2226 "};"
2227 "void f() {"
2228 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002229 "}",
2230 recordDecl(hasName("C"),
2231 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002232}
2233
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002234TEST(Matcher, HandlesNullQualTypes) {
2235 // FIXME: Add a Type matcher so we can replace uses of this
2236 // variable with Type(True())
2237 const TypeMatcher AnyType = anything();
2238
2239 // We don't really care whether this matcher succeeds; we're testing that
2240 // it completes without crashing.
2241 EXPECT_TRUE(matches(
2242 "struct A { };"
2243 "template <typename T>"
2244 "void f(T t) {"
2245 " T local_t(t /* this becomes a null QualType in the AST */);"
2246 "}"
2247 "void g() {"
2248 " f(0);"
2249 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002250 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002251 anyOf(
2252 TypeMatcher(hasDeclaration(anything())),
2253 pointsTo(AnyType),
2254 references(AnyType)
2255 // Other QualType matchers should go here.
2256 ))))));
2257}
2258
Manuel Klimek4da21662012-07-06 05:48:52 +00002259// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002260AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002261 // Make sure all special variables are used: node, match_finder,
2262 // bound_nodes_builder, and the parameter named 'AMatcher'.
2263 return AMatcher.matches(Node, Finder, Builder);
2264}
2265
2266TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002267 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002268
2269 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002270 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002271
2272 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002273 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002274
2275 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002276 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002277}
2278
2279AST_POLYMORPHIC_MATCHER_P(
Samuel Benzaquenef7eb022013-06-21 15:51:31 +00002280 polymorphicHas,
2281 AST_POLYMORPHIC_SUPPORTED_TYPES_2(Decl, Stmt),
2282 internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002283 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002284 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002285 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2286 ASTMatchFinder::BK_First);
2287}
2288
2289TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002290 DeclarationMatcher HasClassB =
2291 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002292
2293 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002294 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002295
2296 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002297 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002298
2299 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002300 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002301
2302 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002303 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002304
2305 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2306}
2307
2308TEST(For, FindsForLoops) {
2309 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2310 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002311 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2312 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002313 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002314}
2315
Daniel Jasper6a124492012-07-12 08:50:38 +00002316TEST(For, ForLoopInternals) {
2317 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2318 forStmt(hasCondition(anything()))));
2319 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2320 forStmt(hasLoopInit(anything()))));
2321}
2322
2323TEST(For, NegativeForLoopInternals) {
2324 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002325 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002326 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2327 forStmt(hasLoopInit(anything()))));
2328}
2329
Manuel Klimek4da21662012-07-06 05:48:52 +00002330TEST(For, ReportsNoFalsePositives) {
2331 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2332 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2333}
2334
2335TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002336 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2337 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2338 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002339}
2340
2341TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2342 // It's not a compound statement just because there's "{}" in the source
2343 // text. This is an AST search, not grep.
2344 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002345 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002346 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002347 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002348}
2349
Daniel Jasper6a124492012-07-12 08:50:38 +00002350TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002351 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002352 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002353 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002354 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002355 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002356 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002357 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002358 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002359}
2360
2361TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2362 // The simplest case: every compound statement is in a function
2363 // definition, and the function body itself must be a compound
2364 // statement.
2365 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002366 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002367}
2368
2369TEST(HasAnySubstatement, IsNotRecursive) {
2370 // It's really "has any immediate substatement".
2371 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002372 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002373}
2374
2375TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2376 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002377 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002378}
2379
2380TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2381 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002382 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002383}
2384
2385TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2386 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002387 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002388 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002389 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002390}
2391
2392TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2393 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002394 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002395 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002396 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002397 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002398 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002399}
2400
2401TEST(StatementCountIs, WorksWithMultipleStatements) {
2402 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002403 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002404}
2405
2406TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2407 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002408 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002409 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002410 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002411 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002412 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002413 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002414 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002415}
2416
2417TEST(Member, WorksInSimplestCase) {
2418 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002419 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002420}
2421
2422TEST(Member, DoesNotMatchTheBaseExpression) {
2423 // Don't pick out the wrong part of the member expression, this should
2424 // be checking the member (name) only.
2425 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002426 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002427}
2428
2429TEST(Member, MatchesInMemberFunctionCall) {
2430 EXPECT_TRUE(matches("void f() {"
2431 " struct { void first() {}; } s;"
2432 " s.first();"
2433 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002434 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002435}
2436
Daniel Jasperc711af22012-10-23 15:46:39 +00002437TEST(Member, MatchesMember) {
2438 EXPECT_TRUE(matches(
2439 "struct A { int i; }; void f() { A a; a.i = 2; }",
2440 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2441 EXPECT_TRUE(notMatches(
2442 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2443 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2444}
2445
Daniel Jasperf3197e92013-02-25 12:02:08 +00002446TEST(Member, UnderstandsAccess) {
2447 EXPECT_TRUE(matches(
2448 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2449 EXPECT_TRUE(notMatches(
2450 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2451 EXPECT_TRUE(notMatches(
2452 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2453
2454 EXPECT_TRUE(notMatches(
2455 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2456 EXPECT_TRUE(notMatches(
2457 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2458 EXPECT_TRUE(matches(
2459 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2460
2461 EXPECT_TRUE(notMatches(
2462 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2463 EXPECT_TRUE(matches("class A { protected: int i; };",
2464 fieldDecl(isProtected(), hasName("i"))));
2465 EXPECT_TRUE(notMatches(
2466 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2467
2468 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2469 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2470 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2471 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2472}
2473
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002474TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002475 // Fails in C++11 mode
2476 EXPECT_TRUE(matchesConditionally(
2477 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2478 "class X { void *operator new(std::size_t); };",
2479 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002480
2481 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002482 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002483
Daniel Jasper31f7c082012-10-01 13:40:41 +00002484 // Fails in C++11 mode
2485 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002486 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2487 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002488 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002489}
2490
Manuel Klimek4da21662012-07-06 05:48:52 +00002491TEST(HasObjectExpression, DoesNotMatchMember) {
2492 EXPECT_TRUE(notMatches(
2493 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002494 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002495}
2496
2497TEST(HasObjectExpression, MatchesBaseOfVariable) {
2498 EXPECT_TRUE(matches(
2499 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002500 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002501 EXPECT_TRUE(matches(
2502 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002503 memberExpr(hasObjectExpression(
2504 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002505}
2506
2507TEST(HasObjectExpression,
2508 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2509 EXPECT_TRUE(matches(
2510 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002511 memberExpr(hasObjectExpression(
2512 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002513 EXPECT_TRUE(matches(
2514 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002515 memberExpr(hasObjectExpression(
2516 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002517}
2518
2519TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002520 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2521 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2522 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2523 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002524}
2525
2526TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002527 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002528}
2529
2530TEST(IsConstQualified, MatchesConstInt) {
2531 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002532 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002533}
2534
2535TEST(IsConstQualified, MatchesConstPointer) {
2536 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002537 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002538}
2539
2540TEST(IsConstQualified, MatchesThroughTypedef) {
2541 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002542 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002543 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002544 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002545}
2546
2547TEST(IsConstQualified, DoesNotMatchInappropriately) {
2548 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002549 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002550 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002551 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002552}
2553
Sam Panzer089e5b32012-08-16 16:58:10 +00002554TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002555 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2556 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2557 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2558 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002559}
2560TEST(CastExpression, MatchesImplicitCasts) {
2561 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002562 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002563 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002564 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002565}
2566
2567TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002568 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2569 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2570 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2571 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002572}
2573
Manuel Klimek4da21662012-07-06 05:48:52 +00002574TEST(ReinterpretCast, MatchesSimpleCase) {
2575 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002576 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002577}
2578
2579TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002580 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002581 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002582 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002583 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002584 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002585 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2586 "B b;"
2587 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002588 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002589}
2590
2591TEST(FunctionalCast, MatchesSimpleCase) {
2592 std::string foo_class = "class Foo { public: Foo(char*); };";
2593 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002594 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002595}
2596
2597TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2598 std::string FooClass = "class Foo { public: Foo(char*); };";
2599 EXPECT_TRUE(
2600 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002601 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002602 EXPECT_TRUE(
2603 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002604 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002605}
2606
2607TEST(DynamicCast, MatchesSimpleCase) {
2608 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2609 "B b;"
2610 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002611 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002612}
2613
2614TEST(StaticCast, MatchesSimpleCase) {
2615 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002616 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002617}
2618
2619TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002620 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002621 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002622 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002623 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002624 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002625 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2626 "B b;"
2627 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002628 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002629}
2630
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002631TEST(CStyleCast, MatchesSimpleCase) {
2632 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2633}
2634
2635TEST(CStyleCast, DoesNotMatchOtherCasts) {
2636 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2637 "char q, *r = const_cast<char*>(&q);"
2638 "void* s = reinterpret_cast<char*>(&s);"
2639 "struct B { virtual ~B() {} }; struct D : B {};"
2640 "B b;"
2641 "D* t = dynamic_cast<D*>(&b);",
2642 cStyleCastExpr()));
2643}
2644
Manuel Klimek4da21662012-07-06 05:48:52 +00002645TEST(HasDestinationType, MatchesSimpleCase) {
2646 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002647 staticCastExpr(hasDestinationType(
2648 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002649}
2650
Sam Panzer089e5b32012-08-16 16:58:10 +00002651TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2652 // This test creates an implicit const cast.
2653 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002654 implicitCastExpr(
2655 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002656 // This test creates an implicit array-to-pointer cast.
2657 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002658 implicitCastExpr(hasImplicitDestinationType(
2659 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002660}
2661
2662TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2663 // This test creates an implicit cast from int to char.
2664 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002665 implicitCastExpr(hasImplicitDestinationType(
2666 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002667 // This test creates an implicit array-to-pointer cast.
2668 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002669 implicitCastExpr(hasImplicitDestinationType(
2670 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002671}
2672
2673TEST(ImplicitCast, MatchesSimpleCase) {
2674 // This test creates an implicit const cast.
2675 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002676 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002677 // This test creates an implicit cast from int to char.
2678 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002679 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002680 // This test creates an implicit array-to-pointer cast.
2681 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002682 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002683}
2684
2685TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002686 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002687 // are present, and that it ignores explicit and paren casts.
2688
2689 // These two test cases have no casts.
2690 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002691 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002692 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002693 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002694
2695 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002696 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002697 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002698 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002699
2700 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002701 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002702}
2703
2704TEST(IgnoringImpCasts, MatchesImpCasts) {
2705 // This test checks that ignoringImpCasts matches when implicit casts are
2706 // present and its inner matcher alone does not match.
2707 // Note that this test creates an implicit const cast.
2708 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002709 varDecl(hasInitializer(ignoringImpCasts(
2710 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002711 // This test creates an implict cast from int to char.
2712 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002713 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002714 integerLiteral(equals(0)))))));
2715}
2716
2717TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2718 // These tests verify that ignoringImpCasts does not match if the inner
2719 // matcher does not match.
2720 // Note that the first test creates an implicit const cast.
2721 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002722 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002723 unless(anything()))))));
2724 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002725 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002726 unless(anything()))))));
2727
2728 // These tests verify that ignoringImplictCasts does not look through explicit
2729 // casts or parentheses.
2730 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002731 varDecl(hasInitializer(ignoringImpCasts(
2732 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002733 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002734 varDecl(hasInitializer(ignoringImpCasts(
2735 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002736 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002737 varDecl(hasInitializer(ignoringImpCasts(
2738 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002739 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002740 varDecl(hasInitializer(ignoringImpCasts(
2741 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002742}
2743
2744TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2745 // This test verifies that expressions that do not have implicit casts
2746 // still match the inner matcher.
2747 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002748 varDecl(hasInitializer(ignoringImpCasts(
2749 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002750}
2751
2752TEST(IgnoringParenCasts, MatchesParenCasts) {
2753 // This test checks that ignoringParenCasts matches when parentheses and/or
2754 // casts are present and its inner matcher alone does not match.
2755 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002756 varDecl(hasInitializer(ignoringParenCasts(
2757 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002758 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002759 varDecl(hasInitializer(ignoringParenCasts(
2760 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002761
2762 // This test creates an implict cast from int to char in addition to the
2763 // parentheses.
2764 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002765 varDecl(hasInitializer(ignoringParenCasts(
2766 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002767
2768 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002769 varDecl(hasInitializer(ignoringParenCasts(
2770 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002771 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002772 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002773 integerLiteral(equals(0)))))));
2774}
2775
2776TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2777 // This test verifies that expressions that do not have any casts still match.
2778 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002779 varDecl(hasInitializer(ignoringParenCasts(
2780 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002781}
2782
2783TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2784 // These tests verify that ignoringImpCasts does not match if the inner
2785 // matcher does not match.
2786 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002787 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002788 unless(anything()))))));
2789
2790 // This test creates an implicit cast from int to char in addition to the
2791 // parentheses.
2792 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002793 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002794 unless(anything()))))));
2795
2796 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002797 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002798 unless(anything()))))));
2799}
2800
2801TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2802 // This test checks that ignoringParenAndImpCasts matches when
2803 // parentheses and/or implicit casts are present and its inner matcher alone
2804 // does not match.
2805 // Note that this test creates an implicit const cast.
2806 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002807 varDecl(hasInitializer(ignoringParenImpCasts(
2808 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002809 // This test creates an implicit cast from int to char.
2810 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002811 varDecl(hasInitializer(ignoringParenImpCasts(
2812 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002813}
2814
2815TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2816 // This test verifies that expressions that do not have parentheses or
2817 // implicit casts still match.
2818 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002819 varDecl(hasInitializer(ignoringParenImpCasts(
2820 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002821 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002822 varDecl(hasInitializer(ignoringParenImpCasts(
2823 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002824}
2825
2826TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2827 // These tests verify that ignoringParenImpCasts does not match if
2828 // the inner matcher does not match.
2829 // This test creates an implicit cast.
2830 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002831 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002832 unless(anything()))))));
2833 // These tests verify that ignoringParenAndImplictCasts does not look
2834 // through explicit casts.
2835 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002836 varDecl(hasInitializer(ignoringParenImpCasts(
2837 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002838 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002839 varDecl(hasInitializer(ignoringParenImpCasts(
2840 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002841 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002842 varDecl(hasInitializer(ignoringParenImpCasts(
2843 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002844}
2845
Manuel Klimek715c9562012-07-25 10:02:02 +00002846TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002847 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2848 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002849 implicitCastExpr(
2850 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002851}
2852
Manuel Klimek715c9562012-07-25 10:02:02 +00002853TEST(HasSourceExpression, MatchesExplicitCasts) {
2854 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002855 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002856 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002857 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002858}
2859
Manuel Klimek4da21662012-07-06 05:48:52 +00002860TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002861 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002862}
2863
2864TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002865 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002866}
2867
2868TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002869 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002870}
2871
2872TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002873 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002874}
2875
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002876TEST(InitListExpression, MatchesInitListExpression) {
2877 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2878 initListExpr(hasType(asString("int [2]")))));
2879 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002880 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002881}
2882
2883TEST(UsingDeclaration, MatchesUsingDeclarations) {
2884 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2885 usingDecl()));
2886}
2887
2888TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2889 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2890 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2891}
2892
2893TEST(UsingDeclaration, MatchesSpecificTarget) {
2894 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2895 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002896 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002897 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2898 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002899 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002900}
2901
2902TEST(UsingDeclaration, ThroughUsingDeclaration) {
2903 EXPECT_TRUE(matches(
2904 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002905 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002906 EXPECT_TRUE(notMatches(
2907 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002908 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002909}
2910
Sam Panzer425f41b2012-08-16 17:20:59 +00002911TEST(SingleDecl, IsSingleDecl) {
2912 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002913 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002914 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2915 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2916 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2917 SingleDeclStmt));
2918}
2919
2920TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002921 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002922
2923 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002924 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002925 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002926 declStmt(containsDeclaration(0, MatchesInit),
2927 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002928 unsigned WrongIndex = 42;
2929 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002930 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002931 MatchesInit))));
2932}
2933
2934TEST(DeclCount, DeclCountIsCorrect) {
2935 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002936 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002937 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002938 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002939 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002940 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002941}
2942
Manuel Klimek4da21662012-07-06 05:48:52 +00002943TEST(While, MatchesWhileLoops) {
2944 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2945 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2946 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2947}
2948
2949TEST(Do, MatchesDoLoops) {
2950 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2951 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2952}
2953
2954TEST(Do, DoesNotMatchWhileLoops) {
2955 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2956}
2957
2958TEST(SwitchCase, MatchesCase) {
2959 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2960 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2961 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2962 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2963}
2964
Daniel Jasperb54b7642012-09-20 14:12:57 +00002965TEST(SwitchCase, MatchesSwitch) {
2966 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2967 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2968 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2969 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2970}
2971
Peter Collingbourneacf02712013-05-10 11:52:02 +00002972TEST(SwitchCase, MatchesEachCase) {
2973 EXPECT_TRUE(notMatches("void x() { switch(42); }",
2974 switchStmt(forEachSwitchCase(caseStmt()))));
2975 EXPECT_TRUE(matches("void x() { switch(42) case 42:; }",
2976 switchStmt(forEachSwitchCase(caseStmt()))));
2977 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }",
2978 switchStmt(forEachSwitchCase(caseStmt()))));
2979 EXPECT_TRUE(notMatches(
2980 "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }",
2981 ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt()))))));
2982 EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }",
2983 switchStmt(forEachSwitchCase(
2984 caseStmt(hasCaseConstant(integerLiteral()))))));
2985 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }",
2986 switchStmt(forEachSwitchCase(
2987 caseStmt(hasCaseConstant(integerLiteral()))))));
2988 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }",
2989 switchStmt(forEachSwitchCase(
2990 caseStmt(hasCaseConstant(integerLiteral()))))));
2991 EXPECT_TRUE(matchAndVerifyResultTrue(
2992 "void x() { switch (42) { case 1: case 2: case 3: default:; } }",
2993 switchStmt(forEachSwitchCase(caseStmt().bind("x"))),
2994 new VerifyIdIsBoundTo<CaseStmt>("x", 3)));
2995}
2996
Manuel Klimek06963012013-07-19 11:50:54 +00002997TEST(ForEachConstructorInitializer, MatchesInitializers) {
2998 EXPECT_TRUE(matches(
2999 "struct X { X() : i(42), j(42) {} int i, j; };",
3000 constructorDecl(forEachConstructorInitializer(ctorInitializer()))));
3001}
3002
Daniel Jasperb54b7642012-09-20 14:12:57 +00003003TEST(ExceptionHandling, SimpleCases) {
3004 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
3005 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
3006 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
3007 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
3008 throwExpr()));
3009 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
3010 throwExpr()));
3011}
3012
Manuel Klimek4da21662012-07-06 05:48:52 +00003013TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
3014 EXPECT_TRUE(notMatches(
3015 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003016 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003017 EXPECT_TRUE(notMatches(
3018 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003019 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003020}
3021
3022TEST(HasConditionVariableStatement, MatchesConditionVariables) {
3023 EXPECT_TRUE(matches(
3024 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003025 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003026}
3027
3028TEST(ForEach, BindsOneNode) {
3029 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003030 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003031 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003032}
3033
3034TEST(ForEach, BindsMultipleNodes) {
3035 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003036 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003037 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003038}
3039
3040TEST(ForEach, BindsRecursiveCombinations) {
3041 EXPECT_TRUE(matchAndVerifyResultTrue(
3042 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003043 recordDecl(hasName("C"),
3044 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003045 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003046}
3047
3048TEST(ForEachDescendant, BindsOneNode) {
3049 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003050 recordDecl(hasName("C"),
3051 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003052 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003053}
3054
Daniel Jasper5f684e92012-11-16 18:39:22 +00003055TEST(ForEachDescendant, NestedForEachDescendant) {
3056 DeclarationMatcher m = recordDecl(
3057 isDefinition(), decl().bind("x"), hasName("C"));
3058 EXPECT_TRUE(matchAndVerifyResultTrue(
3059 "class A { class B { class C {}; }; };",
3060 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
3061 new VerifyIdIsBoundTo<Decl>("x", "C")));
3062
Manuel Klimek054d0492013-06-19 15:42:45 +00003063 // Check that a partial match of 'm' that binds 'x' in the
3064 // first part of anyOf(m, anything()) will not overwrite the
3065 // binding created by the earlier binding in the hasDescendant.
3066 EXPECT_TRUE(matchAndVerifyResultTrue(
3067 "class A { class B { class C {}; }; };",
3068 recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
3069 new VerifyIdIsBoundTo<Decl>("x", "C")));
Daniel Jasper5f684e92012-11-16 18:39:22 +00003070}
3071
Manuel Klimek4da21662012-07-06 05:48:52 +00003072TEST(ForEachDescendant, BindsMultipleNodes) {
3073 EXPECT_TRUE(matchAndVerifyResultTrue(
3074 "class C { class D { int x; int y; }; "
3075 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003076 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003077 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003078}
3079
3080TEST(ForEachDescendant, BindsRecursiveCombinations) {
3081 EXPECT_TRUE(matchAndVerifyResultTrue(
3082 "class C { class D { "
3083 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003084 recordDecl(hasName("C"), forEachDescendant(recordDecl(
3085 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003086 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003087}
3088
Manuel Klimek054d0492013-06-19 15:42:45 +00003089TEST(ForEachDescendant, BindsCombinations) {
3090 EXPECT_TRUE(matchAndVerifyResultTrue(
3091 "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while "
3092 "(true) {} }",
3093 compoundStmt(forEachDescendant(ifStmt().bind("if")),
3094 forEachDescendant(whileStmt().bind("while"))),
3095 new VerifyIdIsBoundTo<IfStmt>("if", 6)));
3096}
3097
3098TEST(Has, DoesNotDeleteBindings) {
3099 EXPECT_TRUE(matchAndVerifyResultTrue(
3100 "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())),
3101 new VerifyIdIsBoundTo<Decl>("x", 1)));
3102}
3103
3104TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) {
3105 // Those matchers cover all the cases where an inner matcher is called
3106 // and there is not a 1:1 relationship between the match of the outer
3107 // matcher and the match of the inner matcher.
3108 // The pattern to look for is:
3109 // ... return InnerMatcher.matches(...); ...
3110 // In which case no special handling is needed.
3111 //
3112 // On the other hand, if there are multiple alternative matches
3113 // (for example forEach*) or matches might be discarded (for example has*)
3114 // the implementation must make sure that the discarded matches do not
3115 // affect the bindings.
3116 // When new such matchers are added, add a test here that:
3117 // - matches a simple node, and binds it as the first thing in the matcher:
3118 // recordDecl(decl().bind("x"), hasName("X")))
3119 // - uses the matcher under test afterwards in a way that not the first
3120 // alternative is matched; for anyOf, that means the first branch
3121 // would need to return false; for hasAncestor, it means that not
3122 // the direct parent matches the inner matcher.
3123
3124 EXPECT_TRUE(matchAndVerifyResultTrue(
3125 "class X { int y; };",
3126 recordDecl(
3127 recordDecl().bind("x"), hasName("::X"),
3128 anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())),
3129 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3130 EXPECT_TRUE(matchAndVerifyResultTrue(
3131 "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"),
3132 anyOf(unless(anything()), anything())),
3133 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3134 EXPECT_TRUE(matchAndVerifyResultTrue(
3135 "template<typename T1, typename T2> class X {}; X<float, int> x;",
3136 classTemplateSpecializationDecl(
3137 decl().bind("x"),
3138 hasAnyTemplateArgument(refersToType(asString("int")))),
3139 new VerifyIdIsBoundTo<Decl>("x", 1)));
3140 EXPECT_TRUE(matchAndVerifyResultTrue(
3141 "class X { void f(); void g(); };",
3142 recordDecl(decl().bind("x"), hasMethod(hasName("g"))),
3143 new VerifyIdIsBoundTo<Decl>("x", 1)));
3144 EXPECT_TRUE(matchAndVerifyResultTrue(
3145 "class X { X() : a(1), b(2) {} double a; int b; };",
3146 recordDecl(decl().bind("x"),
3147 has(constructorDecl(
3148 hasAnyConstructorInitializer(forField(hasName("b")))))),
3149 new VerifyIdIsBoundTo<Decl>("x", 1)));
3150 EXPECT_TRUE(matchAndVerifyResultTrue(
3151 "void x(int, int) { x(0, 42); }",
3152 callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))),
3153 new VerifyIdIsBoundTo<Expr>("x", 1)));
3154 EXPECT_TRUE(matchAndVerifyResultTrue(
3155 "void x(int, int y) {}",
3156 functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))),
3157 new VerifyIdIsBoundTo<Decl>("x", 1)));
3158 EXPECT_TRUE(matchAndVerifyResultTrue(
3159 "void x() { return; if (true) {} }",
3160 functionDecl(decl().bind("x"),
3161 has(compoundStmt(hasAnySubstatement(ifStmt())))),
3162 new VerifyIdIsBoundTo<Decl>("x", 1)));
3163 EXPECT_TRUE(matchAndVerifyResultTrue(
3164 "namespace X { void b(int); void b(); }"
3165 "using X::b;",
3166 usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl(
3167 functionDecl(parameterCountIs(1))))),
3168 new VerifyIdIsBoundTo<Decl>("x", 1)));
3169 EXPECT_TRUE(matchAndVerifyResultTrue(
3170 "class A{}; class B{}; class C : B, A {};",
3171 recordDecl(decl().bind("x"), isDerivedFrom("::A")),
3172 new VerifyIdIsBoundTo<Decl>("x", 1)));
3173 EXPECT_TRUE(matchAndVerifyResultTrue(
3174 "class A{}; typedef A B; typedef A C; typedef A D;"
3175 "class E : A {};",
3176 recordDecl(decl().bind("x"), isDerivedFrom("C")),
3177 new VerifyIdIsBoundTo<Decl>("x", 1)));
3178 EXPECT_TRUE(matchAndVerifyResultTrue(
3179 "class A { class B { void f() {} }; };",
3180 functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3181 new VerifyIdIsBoundTo<Decl>("x", 1)));
3182 EXPECT_TRUE(matchAndVerifyResultTrue(
3183 "template <typename T> struct A { struct B {"
3184 " void f() { if(true) {} }"
3185 "}; };"
3186 "void t() { A<int>::B b; b.f(); }",
3187 ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3188 new VerifyIdIsBoundTo<Stmt>("x", 2)));
3189 EXPECT_TRUE(matchAndVerifyResultTrue(
3190 "class A {};",
3191 recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))),
3192 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimek06963012013-07-19 11:50:54 +00003193 EXPECT_TRUE(matchAndVerifyResultTrue(
3194 "class A { A() : s(), i(42) {} const char *s; int i; };",
3195 constructorDecl(hasName("::A::A"), decl().bind("x"),
3196 forEachConstructorInitializer(forField(hasName("i")))),
3197 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimek054d0492013-06-19 15:42:45 +00003198}
3199
Daniel Jasper11c98772012-11-11 22:14:55 +00003200TEST(ForEachDescendant, BindsCorrectNodes) {
3201 EXPECT_TRUE(matchAndVerifyResultTrue(
3202 "class C { void f(); int i; };",
3203 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3204 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
3205 EXPECT_TRUE(matchAndVerifyResultTrue(
3206 "class C { void f() {} int i; };",
3207 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3208 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
3209}
3210
Manuel Klimek152ea0e2013-02-04 10:59:20 +00003211TEST(FindAll, BindsNodeOnMatch) {
3212 EXPECT_TRUE(matchAndVerifyResultTrue(
3213 "class A {};",
3214 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
3215 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
3216}
3217
3218TEST(FindAll, BindsDescendantNodeOnMatch) {
3219 EXPECT_TRUE(matchAndVerifyResultTrue(
3220 "class A { int a; int b; };",
3221 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
3222 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3223}
3224
3225TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
3226 EXPECT_TRUE(matchAndVerifyResultTrue(
3227 "class A { int a; int b; };",
3228 recordDecl(hasName("::A"),
3229 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
3230 fieldDecl().bind("v"))))),
3231 new VerifyIdIsBoundTo<Decl>("v", 3)));
3232
3233 EXPECT_TRUE(matchAndVerifyResultTrue(
3234 "class A { class B {}; class C {}; };",
3235 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
3236 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3237}
3238
Manuel Klimek73876732013-02-04 09:42:38 +00003239TEST(EachOf, TriggersForEachMatch) {
3240 EXPECT_TRUE(matchAndVerifyResultTrue(
3241 "class A { int a; int b; };",
3242 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3243 has(fieldDecl(hasName("b")).bind("v")))),
3244 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3245}
3246
3247TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3248 EXPECT_TRUE(matchAndVerifyResultTrue(
3249 "class A { int a; int c; };",
3250 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3251 has(fieldDecl(hasName("b")).bind("v")))),
3252 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3253 EXPECT_TRUE(matchAndVerifyResultTrue(
3254 "class A { int c; int b; };",
3255 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3256 has(fieldDecl(hasName("b")).bind("v")))),
3257 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3258 EXPECT_TRUE(notMatches(
3259 "class A { int c; int d; };",
3260 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3261 has(fieldDecl(hasName("b")).bind("v"))))));
3262}
Manuel Klimek4da21662012-07-06 05:48:52 +00003263
3264TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3265 // Make sure that we can both match the class by name (::X) and by the type
3266 // the template was instantiated with (via a field).
3267
3268 EXPECT_TRUE(matches(
3269 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003270 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003271
3272 EXPECT_TRUE(matches(
3273 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003274 recordDecl(isTemplateInstantiation(), hasDescendant(
3275 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003276}
3277
3278TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3279 EXPECT_TRUE(matches(
3280 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003281 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00003282 isTemplateInstantiation())));
3283}
3284
3285TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3286 EXPECT_TRUE(matches(
3287 "template <typename T> class X { T t; }; class A {};"
3288 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003289 recordDecl(isTemplateInstantiation(), hasDescendant(
3290 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003291}
3292
3293TEST(IsTemplateInstantiation,
3294 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3295 EXPECT_TRUE(matches(
3296 "template <typename T> class X {};"
3297 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003298 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003299}
3300
3301TEST(IsTemplateInstantiation,
3302 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3303 EXPECT_TRUE(matches(
3304 "class A {};"
3305 "class X {"
3306 " template <typename U> class Y { U u; };"
3307 " Y<A> y;"
3308 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003309 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003310}
3311
3312TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3313 // FIXME: Figure out whether this makes sense. It doesn't affect the
3314 // normal use case as long as the uppermost instantiation always is marked
3315 // as template instantiation, but it might be confusing as a predicate.
3316 EXPECT_TRUE(matches(
3317 "class A {};"
3318 "template <typename T> class X {"
3319 " template <typename U> class Y { U u; };"
3320 " Y<T> y;"
3321 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003322 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003323}
3324
3325TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3326 EXPECT_TRUE(notMatches(
3327 "template <typename T> class X {}; class A {};"
3328 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003329 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003330}
3331
3332TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3333 EXPECT_TRUE(notMatches(
3334 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003335 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003336}
3337
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003338TEST(IsExplicitTemplateSpecialization,
3339 DoesNotMatchPrimaryTemplate) {
3340 EXPECT_TRUE(notMatches(
3341 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003342 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003343 EXPECT_TRUE(notMatches(
3344 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003345 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003346}
3347
3348TEST(IsExplicitTemplateSpecialization,
3349 DoesNotMatchExplicitTemplateInstantiations) {
3350 EXPECT_TRUE(notMatches(
3351 "template <typename T> class X {};"
3352 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003353 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003354 EXPECT_TRUE(notMatches(
3355 "template <typename T> void f(T t) {}"
3356 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003357 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003358}
3359
3360TEST(IsExplicitTemplateSpecialization,
3361 DoesNotMatchImplicitTemplateInstantiations) {
3362 EXPECT_TRUE(notMatches(
3363 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003364 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003365 EXPECT_TRUE(notMatches(
3366 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003367 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003368}
3369
3370TEST(IsExplicitTemplateSpecialization,
3371 MatchesExplicitTemplateSpecializations) {
3372 EXPECT_TRUE(matches(
3373 "template <typename T> class X {};"
3374 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003375 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003376 EXPECT_TRUE(matches(
3377 "template <typename T> void f(T t) {}"
3378 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003379 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003380}
3381
Manuel Klimek579b1202012-09-07 09:26:10 +00003382TEST(HasAncenstor, MatchesDeclarationAncestors) {
3383 EXPECT_TRUE(matches(
3384 "class A { class B { class C {}; }; };",
3385 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3386}
3387
3388TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3389 EXPECT_TRUE(notMatches(
3390 "class A { class B { class C {}; }; };",
3391 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3392}
3393
3394TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3395 EXPECT_TRUE(matches(
3396 "class A { class B { void f() { C c; } class C {}; }; };",
3397 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3398 hasAncestor(recordDecl(hasName("A"))))))));
3399}
3400
3401TEST(HasAncenstor, MatchesStatementAncestors) {
3402 EXPECT_TRUE(matches(
3403 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003404 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003405}
3406
3407TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3408 EXPECT_TRUE(matches(
3409 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003410 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003411}
3412
3413TEST(HasAncestor, BindsRecursiveCombinations) {
3414 EXPECT_TRUE(matchAndVerifyResultTrue(
3415 "class C { class D { class E { class F { int y; }; }; }; };",
3416 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003417 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00003418}
3419
3420TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3421 EXPECT_TRUE(matchAndVerifyResultTrue(
3422 "class C { class D { class E { class F { int y; }; }; }; };",
3423 fieldDecl(hasAncestor(
3424 decl(
3425 hasDescendant(recordDecl(isDefinition(),
3426 hasAncestor(recordDecl())))
3427 ).bind("d")
3428 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00003429 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00003430}
3431
Manuel Klimek374516c2013-03-14 16:33:21 +00003432TEST(HasAncestor, MatchesClosestAncestor) {
3433 EXPECT_TRUE(matchAndVerifyResultTrue(
3434 "template <typename T> struct C {"
3435 " void f(int) {"
3436 " struct I { void g(T) { int x; } } i; i.g(42);"
3437 " }"
3438 "};"
3439 "template struct C<int>;",
3440 varDecl(hasName("x"),
3441 hasAncestor(functionDecl(hasParameter(
3442 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"),
3443 new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2)));
3444}
3445
Manuel Klimek579b1202012-09-07 09:26:10 +00003446TEST(HasAncestor, MatchesInTemplateInstantiations) {
3447 EXPECT_TRUE(matches(
3448 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3449 "A<int>::B::C a;",
3450 fieldDecl(hasType(asString("int")),
3451 hasAncestor(recordDecl(hasName("A"))))));
3452}
3453
3454TEST(HasAncestor, MatchesInImplicitCode) {
3455 EXPECT_TRUE(matches(
3456 "struct X {}; struct A { A() {} X x; };",
3457 constructorDecl(
3458 hasAnyConstructorInitializer(withInitializer(expr(
3459 hasAncestor(recordDecl(hasName("A")))))))));
3460}
3461
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003462TEST(HasParent, MatchesOnlyParent) {
3463 EXPECT_TRUE(matches(
3464 "void f() { if (true) { int x = 42; } }",
3465 compoundStmt(hasParent(ifStmt()))));
3466 EXPECT_TRUE(notMatches(
3467 "void f() { for (;;) { int x = 42; } }",
3468 compoundStmt(hasParent(ifStmt()))));
3469 EXPECT_TRUE(notMatches(
3470 "void f() { if (true) for (;;) { int x = 42; } }",
3471 compoundStmt(hasParent(ifStmt()))));
3472}
3473
Manuel Klimek30ace372012-12-06 14:42:48 +00003474TEST(HasAncestor, MatchesAllAncestors) {
3475 EXPECT_TRUE(matches(
3476 "template <typename T> struct C { static void f() { 42; } };"
3477 "void t() { C<int>::f(); }",
3478 integerLiteral(
3479 equals(42),
3480 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3481 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3482}
3483
3484TEST(HasParent, MatchesAllParents) {
3485 EXPECT_TRUE(matches(
3486 "template <typename T> struct C { static void f() { 42; } };"
3487 "void t() { C<int>::f(); }",
3488 integerLiteral(
3489 equals(42),
3490 hasParent(compoundStmt(hasParent(functionDecl(
3491 hasParent(recordDecl(isTemplateInstantiation())))))))));
3492 EXPECT_TRUE(matches(
3493 "template <typename T> struct C { static void f() { 42; } };"
3494 "void t() { C<int>::f(); }",
3495 integerLiteral(
3496 equals(42),
3497 hasParent(compoundStmt(hasParent(functionDecl(
3498 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3499 EXPECT_TRUE(matches(
3500 "template <typename T> struct C { static void f() { 42; } };"
3501 "void t() { C<int>::f(); }",
3502 integerLiteral(equals(42),
3503 hasParent(compoundStmt(allOf(
3504 hasParent(functionDecl(
3505 hasParent(recordDecl(isTemplateInstantiation())))),
3506 hasParent(functionDecl(hasParent(recordDecl(
3507 unless(isTemplateInstantiation())))))))))));
Manuel Klimek374516c2013-03-14 16:33:21 +00003508 EXPECT_TRUE(
3509 notMatches("template <typename T> struct C { static void f() {} };"
3510 "void t() { C<int>::f(); }",
3511 compoundStmt(hasParent(recordDecl()))));
Manuel Klimek30ace372012-12-06 14:42:48 +00003512}
3513
Daniel Jasperce620072012-10-17 08:52:59 +00003514TEST(TypeMatching, MatchesTypes) {
3515 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3516}
3517
3518TEST(TypeMatching, MatchesArrayTypes) {
3519 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3520 EXPECT_TRUE(matches("int a[42];", arrayType()));
3521 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3522
3523 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3524 arrayType(hasElementType(builtinType()))));
3525
3526 EXPECT_TRUE(matches(
3527 "int const a[] = { 2, 3 };",
3528 qualType(arrayType(hasElementType(builtinType())))));
3529 EXPECT_TRUE(matches(
3530 "int const a[] = { 2, 3 };",
3531 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3532 EXPECT_TRUE(matches(
3533 "typedef const int T; T x[] = { 1, 2 };",
3534 qualType(isConstQualified(), arrayType())));
3535
3536 EXPECT_TRUE(notMatches(
3537 "int a[] = { 2, 3 };",
3538 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3539 EXPECT_TRUE(notMatches(
3540 "int a[] = { 2, 3 };",
3541 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3542 EXPECT_TRUE(notMatches(
3543 "int const a[] = { 2, 3 };",
3544 qualType(arrayType(hasElementType(builtinType())),
3545 unless(isConstQualified()))));
3546
3547 EXPECT_TRUE(matches("int a[2];",
3548 constantArrayType(hasElementType(builtinType()))));
3549 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3550}
3551
3552TEST(TypeMatching, MatchesComplexTypes) {
3553 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3554 EXPECT_TRUE(matches(
3555 "_Complex float f;",
3556 complexType(hasElementType(builtinType()))));
3557 EXPECT_TRUE(notMatches(
3558 "_Complex float f;",
3559 complexType(hasElementType(isInteger()))));
3560}
3561
3562TEST(TypeMatching, MatchesConstantArrayTypes) {
3563 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3564 EXPECT_TRUE(notMatches(
3565 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3566 constantArrayType(hasElementType(builtinType()))));
3567
3568 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3569 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3570 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3571}
3572
3573TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3574 EXPECT_TRUE(matches(
3575 "template <typename T, int Size> class array { T data[Size]; };",
3576 dependentSizedArrayType()));
3577 EXPECT_TRUE(notMatches(
3578 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3579 dependentSizedArrayType()));
3580}
3581
3582TEST(TypeMatching, MatchesIncompleteArrayType) {
3583 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3584 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3585
3586 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3587 incompleteArrayType()));
3588}
3589
3590TEST(TypeMatching, MatchesVariableArrayType) {
3591 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3592 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3593
3594 EXPECT_TRUE(matches(
3595 "void f(int b) { int a[b]; }",
3596 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3597 varDecl(hasName("b")))))))));
3598}
3599
3600TEST(TypeMatching, MatchesAtomicTypes) {
3601 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3602
3603 EXPECT_TRUE(matches("_Atomic(int) i;",
3604 atomicType(hasValueType(isInteger()))));
3605 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3606 atomicType(hasValueType(isInteger()))));
3607}
3608
3609TEST(TypeMatching, MatchesAutoTypes) {
3610 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3611 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3612 autoType()));
3613
Richard Smith9b131752013-04-30 21:23:01 +00003614 // FIXME: Matching against the type-as-written can't work here, because the
3615 // type as written was not deduced.
3616 //EXPECT_TRUE(matches("auto a = 1;",
3617 // autoType(hasDeducedType(isInteger()))));
3618 //EXPECT_TRUE(notMatches("auto b = 2.0;",
3619 // autoType(hasDeducedType(isInteger()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003620}
3621
Daniel Jaspera267cf62012-10-29 10:14:44 +00003622TEST(TypeMatching, MatchesFunctionTypes) {
3623 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3624 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3625}
3626
Edwin Vane88be2fd2013-04-01 18:33:34 +00003627TEST(TypeMatching, MatchesParenType) {
3628 EXPECT_TRUE(
3629 matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
3630 EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
3631
3632 EXPECT_TRUE(matches(
3633 "int (*ptr_to_func)(int);",
3634 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3635 EXPECT_TRUE(notMatches(
3636 "int (*ptr_to_array)[4];",
3637 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3638}
3639
Daniel Jasperce620072012-10-17 08:52:59 +00003640TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003641 // FIXME: Reactive when these tests can be more specific (not matching
3642 // implicit code on certain platforms), likely when we have hasDescendant for
3643 // Types/TypeLocs.
3644 //EXPECT_TRUE(matchAndVerifyResultTrue(
3645 // "int* a;",
3646 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3647 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3648 //EXPECT_TRUE(matchAndVerifyResultTrue(
3649 // "int* a;",
3650 // pointerTypeLoc().bind("loc"),
3651 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003652 EXPECT_TRUE(matches(
3653 "int** a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003654 loc(pointerType(pointee(qualType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003655 EXPECT_TRUE(matches(
3656 "int** a;",
3657 loc(pointerType(pointee(pointerType())))));
3658 EXPECT_TRUE(matches(
3659 "int* b; int* * const a = &b;",
3660 loc(qualType(isConstQualified(), pointerType()))));
3661
3662 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003663 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3664 hasType(blockPointerType()))));
3665 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3666 hasType(memberPointerType()))));
3667 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3668 hasType(pointerType()))));
3669 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3670 hasType(referenceType()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003671 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3672 hasType(lValueReferenceType()))));
3673 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3674 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003675
Daniel Jasper1802daf2012-10-17 13:35:36 +00003676 Fragment = "int *ptr;";
3677 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3678 hasType(blockPointerType()))));
3679 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3680 hasType(memberPointerType()))));
3681 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3682 hasType(pointerType()))));
3683 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3684 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003685
Daniel Jasper1802daf2012-10-17 13:35:36 +00003686 Fragment = "int a; int &ref = a;";
3687 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3688 hasType(blockPointerType()))));
3689 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3690 hasType(memberPointerType()))));
3691 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3692 hasType(pointerType()))));
3693 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3694 hasType(referenceType()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003695 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3696 hasType(lValueReferenceType()))));
3697 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3698 hasType(rValueReferenceType()))));
3699
3700 Fragment = "int &&ref = 2;";
3701 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3702 hasType(blockPointerType()))));
3703 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3704 hasType(memberPointerType()))));
3705 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3706 hasType(pointerType()))));
3707 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3708 hasType(referenceType()))));
3709 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3710 hasType(lValueReferenceType()))));
3711 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3712 hasType(rValueReferenceType()))));
3713}
3714
3715TEST(TypeMatching, AutoRefTypes) {
3716 std::string Fragment = "auto a = 1;"
3717 "auto b = a;"
3718 "auto &c = a;"
3719 "auto &&d = c;"
3720 "auto &&e = 2;";
3721 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
3722 hasType(referenceType()))));
3723 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
3724 hasType(referenceType()))));
3725 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3726 hasType(referenceType()))));
3727 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3728 hasType(lValueReferenceType()))));
3729 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
3730 hasType(rValueReferenceType()))));
3731 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3732 hasType(referenceType()))));
3733 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3734 hasType(lValueReferenceType()))));
3735 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
3736 hasType(rValueReferenceType()))));
3737 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3738 hasType(referenceType()))));
3739 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
3740 hasType(lValueReferenceType()))));
3741 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3742 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003743}
3744
3745TEST(TypeMatching, PointeeTypes) {
3746 EXPECT_TRUE(matches("int b; int &a = b;",
3747 referenceType(pointee(builtinType()))));
3748 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3749
3750 EXPECT_TRUE(matches("int *a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003751 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003752
3753 EXPECT_TRUE(matches(
3754 "int const *A;",
3755 pointerType(pointee(isConstQualified(), builtinType()))));
3756 EXPECT_TRUE(notMatches(
3757 "int *A;",
3758 pointerType(pointee(isConstQualified(), builtinType()))));
3759}
3760
3761TEST(TypeMatching, MatchesPointersToConstTypes) {
3762 EXPECT_TRUE(matches("int b; int * const a = &b;",
3763 loc(pointerType())));
3764 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003765 loc(pointerType())));
Daniel Jasperce620072012-10-17 08:52:59 +00003766 EXPECT_TRUE(matches(
3767 "int b; const int * a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003768 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003769 EXPECT_TRUE(matches(
3770 "int b; const int * a = &b;",
3771 pointerType(pointee(builtinType()))));
3772}
3773
3774TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003775 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3776 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003777}
3778
Edwin Vane3abf7782013-02-25 14:49:29 +00003779TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vane742d9e72013-02-25 20:43:32 +00003780 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vane3abf7782013-02-25 14:49:29 +00003781 templateSpecializationType()));
3782}
3783
Edwin Vane742d9e72013-02-25 20:43:32 +00003784TEST(TypeMatching, MatchesRecordType) {
3785 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek0cc798f2013-02-27 11:56:58 +00003786 EXPECT_TRUE(matches("struct S{}; S s;",
3787 recordType(hasDeclaration(recordDecl(hasName("S"))))));
3788 EXPECT_TRUE(notMatches("int i;",
3789 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003790}
3791
3792TEST(TypeMatching, MatchesElaboratedType) {
3793 EXPECT_TRUE(matches(
3794 "namespace N {"
3795 " namespace M {"
3796 " class D {};"
3797 " }"
3798 "}"
3799 "N::M::D d;", elaboratedType()));
3800 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
3801 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
3802}
3803
3804TEST(ElaboratedTypeNarrowing, hasQualifier) {
3805 EXPECT_TRUE(matches(
3806 "namespace N {"
3807 " namespace M {"
3808 " class D {};"
3809 " }"
3810 "}"
3811 "N::M::D d;",
3812 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3813 EXPECT_TRUE(notMatches(
3814 "namespace M {"
3815 " class D {};"
3816 "}"
3817 "M::D d;",
3818 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vaneaec89ac2013-03-04 17:51:00 +00003819 EXPECT_TRUE(notMatches(
3820 "struct D {"
3821 "} d;",
3822 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003823}
3824
3825TEST(ElaboratedTypeNarrowing, namesType) {
3826 EXPECT_TRUE(matches(
3827 "namespace N {"
3828 " namespace M {"
3829 " class D {};"
3830 " }"
3831 "}"
3832 "N::M::D d;",
3833 elaboratedType(elaboratedType(namesType(recordType(
3834 hasDeclaration(namedDecl(hasName("D")))))))));
3835 EXPECT_TRUE(notMatches(
3836 "namespace M {"
3837 " class D {};"
3838 "}"
3839 "M::D d;",
3840 elaboratedType(elaboratedType(namesType(typedefType())))));
3841}
3842
Daniel Jaspera7564432012-09-13 13:11:25 +00003843TEST(NNS, MatchesNestedNameSpecifiers) {
3844 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3845 nestedNameSpecifier()));
3846 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3847 nestedNameSpecifier()));
3848 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3849 nestedNameSpecifier()));
3850
3851 EXPECT_TRUE(matches(
3852 "struct A { static void f() {} }; void g() { A::f(); }",
3853 nestedNameSpecifier()));
3854 EXPECT_TRUE(notMatches(
3855 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3856 nestedNameSpecifier()));
3857}
3858
Daniel Jasperb54b7642012-09-20 14:12:57 +00003859TEST(NullStatement, SimpleCases) {
3860 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3861 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3862}
3863
Daniel Jaspera7564432012-09-13 13:11:25 +00003864TEST(NNS, MatchesTypes) {
3865 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3866 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3867 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3868 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3869 Matcher));
3870 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3871}
3872
3873TEST(NNS, MatchesNamespaceDecls) {
3874 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3875 specifiesNamespace(hasName("ns")));
3876 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3877 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3878 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3879}
3880
3881TEST(NNS, BindsNestedNameSpecifiers) {
3882 EXPECT_TRUE(matchAndVerifyResultTrue(
3883 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3884 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3885 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3886}
3887
3888TEST(NNS, BindsNestedNameSpecifierLocs) {
3889 EXPECT_TRUE(matchAndVerifyResultTrue(
3890 "namespace ns { struct B {}; } ns::B b;",
3891 loc(nestedNameSpecifier()).bind("loc"),
3892 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3893}
3894
3895TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3896 EXPECT_TRUE(matches(
3897 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3898 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3899 EXPECT_TRUE(matches(
3900 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003901 nestedNameSpecifierLoc(hasPrefix(
3902 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003903}
3904
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003905TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3906 std::string Fragment =
3907 "namespace a { struct A { struct B { struct C {}; }; }; };"
3908 "void f() { a::A::B::C c; }";
3909 EXPECT_TRUE(matches(
3910 Fragment,
3911 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3912 hasDescendant(nestedNameSpecifier(
3913 specifiesNamespace(hasName("a")))))));
3914 EXPECT_TRUE(notMatches(
3915 Fragment,
3916 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3917 has(nestedNameSpecifier(
3918 specifiesNamespace(hasName("a")))))));
3919 EXPECT_TRUE(matches(
3920 Fragment,
3921 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3922 has(nestedNameSpecifier(
3923 specifiesNamespace(hasName("a")))))));
3924
3925 // Not really useful because a NestedNameSpecifier can af at most one child,
3926 // but to complete the interface.
3927 EXPECT_TRUE(matchAndVerifyResultTrue(
3928 Fragment,
3929 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3930 forEach(nestedNameSpecifier().bind("x"))),
3931 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3932}
3933
3934TEST(NNS, NestedNameSpecifiersAsDescendants) {
3935 std::string Fragment =
3936 "namespace a { struct A { struct B { struct C {}; }; }; };"
3937 "void f() { a::A::B::C c; }";
3938 EXPECT_TRUE(matches(
3939 Fragment,
3940 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3941 asString("struct a::A")))))));
3942 EXPECT_TRUE(matchAndVerifyResultTrue(
3943 Fragment,
3944 functionDecl(hasName("f"),
3945 forEachDescendant(nestedNameSpecifier().bind("x"))),
3946 // Nested names: a, a::A and a::A::B.
3947 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3948}
3949
3950TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3951 std::string Fragment =
3952 "namespace a { struct A { struct B { struct C {}; }; }; };"
3953 "void f() { a::A::B::C c; }";
3954 EXPECT_TRUE(matches(
3955 Fragment,
3956 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3957 hasDescendant(loc(nestedNameSpecifier(
3958 specifiesNamespace(hasName("a"))))))));
3959 EXPECT_TRUE(notMatches(
3960 Fragment,
3961 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3962 has(loc(nestedNameSpecifier(
3963 specifiesNamespace(hasName("a"))))))));
3964 EXPECT_TRUE(matches(
3965 Fragment,
3966 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3967 has(loc(nestedNameSpecifier(
3968 specifiesNamespace(hasName("a"))))))));
3969
3970 EXPECT_TRUE(matchAndVerifyResultTrue(
3971 Fragment,
3972 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3973 forEach(nestedNameSpecifierLoc().bind("x"))),
3974 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3975}
3976
3977TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3978 std::string Fragment =
3979 "namespace a { struct A { struct B { struct C {}; }; }; };"
3980 "void f() { a::A::B::C c; }";
3981 EXPECT_TRUE(matches(
3982 Fragment,
3983 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3984 asString("struct a::A"))))))));
3985 EXPECT_TRUE(matchAndVerifyResultTrue(
3986 Fragment,
3987 functionDecl(hasName("f"),
3988 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3989 // Nested names: a, a::A and a::A::B.
3990 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3991}
3992
Manuel Klimek60969f52013-02-01 13:41:35 +00003993template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003994public:
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003995 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
3996 StringRef InnerId)
3997 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jasper452abbc2012-10-29 10:48:25 +00003998 }
3999
Manuel Klimek60969f52013-02-01 13:41:35 +00004000 virtual bool run(const BoundNodes *Nodes) { return false; }
4001
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004002 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4003 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004004 return selectFirst<const T>(InnerId,
4005 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004006 }
4007private:
4008 std::string Id;
4009 internal::Matcher<T> InnerMatcher;
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004010 std::string InnerId;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004011};
4012
4013TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00004014 EXPECT_TRUE(matchAndVerifyResultTrue(
4015 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4016 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004017 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
4018 "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004019 EXPECT_TRUE(matchAndVerifyResultFalse(
4020 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4021 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004022 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
4023 "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004024}
4025
4026TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00004027 EXPECT_TRUE(matchAndVerifyResultTrue(
4028 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004029 new VerifyMatchOnNode<clang::Stmt>(
4030 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004031 EXPECT_TRUE(matchAndVerifyResultFalse(
4032 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004033 new VerifyMatchOnNode<clang::Stmt>(
4034 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004035}
4036
4037TEST(MatchFinder, CanMatchSingleNodesRecursively) {
4038 EXPECT_TRUE(matchAndVerifyResultTrue(
4039 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4040 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004041 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004042 EXPECT_TRUE(matchAndVerifyResultFalse(
4043 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4044 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004045 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004046}
4047
Manuel Klimekfa37c5c2013-02-07 12:42:10 +00004048template <typename T>
4049class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
4050public:
4051 virtual bool run(const BoundNodes *Nodes) { return false; }
4052
4053 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4054 const T *Node = Nodes->getNodeAs<T>("");
4055 return verify(*Nodes, *Context, Node);
4056 }
4057
4058 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
4059 return selectFirst<const T>(
4060 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
4061 *Node, Context)) != NULL;
4062 }
4063 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
4064 return selectFirst<const T>(
4065 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
4066 *Node, Context)) != NULL;
4067 }
4068};
4069
4070TEST(IsEqualTo, MatchesNodesByIdentity) {
4071 EXPECT_TRUE(matchAndVerifyResultTrue(
4072 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
4073 new VerifyAncestorHasChildIsEqual<Decl>()));
4074 EXPECT_TRUE(
4075 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
4076 new VerifyAncestorHasChildIsEqual<Stmt>()));
4077}
4078
Manuel Klimeke5793282012-11-02 01:31:03 +00004079class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
4080public:
4081 VerifyStartOfTranslationUnit() : Called(false) {}
4082 virtual void run(const MatchFinder::MatchResult &Result) {
4083 EXPECT_TRUE(Called);
4084 }
4085 virtual void onStartOfTranslationUnit() {
4086 Called = true;
4087 }
4088 bool Called;
4089};
4090
4091TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
4092 MatchFinder Finder;
4093 VerifyStartOfTranslationUnit VerifyCallback;
4094 Finder.addMatcher(decl(), &VerifyCallback);
4095 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4096 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4097 EXPECT_TRUE(VerifyCallback.Called);
4098}
4099
Peter Collingbourne8f9e5902013-05-28 19:21:51 +00004100class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback {
4101public:
4102 VerifyEndOfTranslationUnit() : Called(false) {}
4103 virtual void run(const MatchFinder::MatchResult &Result) {
4104 EXPECT_FALSE(Called);
4105 }
4106 virtual void onEndOfTranslationUnit() {
4107 Called = true;
4108 }
4109 bool Called;
4110};
4111
4112TEST(MatchFinder, InterceptsEndOfTranslationUnit) {
4113 MatchFinder Finder;
4114 VerifyEndOfTranslationUnit VerifyCallback;
4115 Finder.addMatcher(decl(), &VerifyCallback);
4116 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4117 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4118 EXPECT_TRUE(VerifyCallback.Called);
4119}
4120
Manuel Klimekcf52ca62013-06-20 14:06:32 +00004121TEST(EqualsBoundNodeMatcher, QualType) {
4122 EXPECT_TRUE(matches(
4123 "int i = 1;", varDecl(hasType(qualType().bind("type")),
4124 hasInitializer(ignoringParenImpCasts(
4125 hasType(qualType(equalsBoundNode("type"))))))));
4126 EXPECT_TRUE(notMatches("int i = 1.f;",
4127 varDecl(hasType(qualType().bind("type")),
4128 hasInitializer(ignoringParenImpCasts(hasType(
4129 qualType(equalsBoundNode("type"))))))));
4130}
4131
4132TEST(EqualsBoundNodeMatcher, NonMatchingTypes) {
4133 EXPECT_TRUE(notMatches(
4134 "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"),
4135 hasInitializer(ignoringParenImpCasts(
4136 hasType(qualType(equalsBoundNode("type"))))))));
4137}
4138
4139TEST(EqualsBoundNodeMatcher, Stmt) {
4140 EXPECT_TRUE(
4141 matches("void f() { if(true) {} }",
4142 stmt(allOf(ifStmt().bind("if"),
4143 hasParent(stmt(has(stmt(equalsBoundNode("if")))))))));
4144
4145 EXPECT_TRUE(notMatches(
4146 "void f() { if(true) { if (true) {} } }",
4147 stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if")))))));
4148}
4149
4150TEST(EqualsBoundNodeMatcher, Decl) {
4151 EXPECT_TRUE(matches(
4152 "class X { class Y {}; };",
4153 decl(allOf(recordDecl(hasName("::X::Y")).bind("record"),
4154 hasParent(decl(has(decl(equalsBoundNode("record")))))))));
4155
4156 EXPECT_TRUE(notMatches("class X { class Y {}; };",
4157 decl(allOf(recordDecl(hasName("::X")).bind("record"),
4158 has(decl(equalsBoundNode("record")))))));
4159}
4160
4161TEST(EqualsBoundNodeMatcher, Type) {
4162 EXPECT_TRUE(matches(
4163 "class X { int a; int b; };",
4164 recordDecl(
4165 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4166 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4167
4168 EXPECT_TRUE(notMatches(
4169 "class X { int a; double b; };",
4170 recordDecl(
4171 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4172 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4173}
4174
4175TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) {
4176
4177 EXPECT_TRUE(matchAndVerifyResultTrue(
4178 "int f() {"
4179 " if (1) {"
4180 " int i = 9;"
4181 " }"
4182 " int j = 10;"
4183 " {"
4184 " float k = 9.0;"
4185 " }"
4186 " return 0;"
4187 "}",
4188 // Look for variable declarations within functions whose type is the same
4189 // as the function return type.
4190 functionDecl(returns(qualType().bind("type")),
4191 forEachDescendant(varDecl(hasType(
4192 qualType(equalsBoundNode("type")))).bind("decl"))),
4193 // Only i and j should match, not k.
4194 new VerifyIdIsBoundTo<VarDecl>("decl", 2)));
4195}
4196
4197TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) {
4198 EXPECT_TRUE(matchAndVerifyResultTrue(
4199 "void f() {"
4200 " int x;"
4201 " double d;"
4202 " x = d + x - d + x;"
4203 "}",
4204 functionDecl(
4205 hasName("f"), forEachDescendant(varDecl().bind("d")),
4206 forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))),
4207 new VerifyIdIsBoundTo<VarDecl>("d", 5)));
4208}
4209
Manuel Klimek4da21662012-07-06 05:48:52 +00004210} // end namespace ast_matchers
4211} // end namespace clang