blob: 556b0b896e639e9bec75cd4c530e0be609c7e3f8 [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) {
662 if (Nodes->getNodeAs<T>(Id)) {
663 ++Count;
664 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
665 Name = Named->getNameAsString();
666 } else if (const NestedNameSpecifier *NNS =
667 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
668 llvm::raw_string_ostream OS(Name);
669 NNS->print(OS, PrintingPolicy(LangOptions()));
670 }
671 return true;
672 }
673 return false;
674 }
675
Daniel Jasper452abbc2012-10-29 10:48:25 +0000676 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
677 return run(Nodes);
678 }
679
Daniel Jaspera267cf62012-10-29 10:14:44 +0000680private:
681 const std::string Id;
682 const int ExpectedCount;
683 int Count;
684 const std::string ExpectedName;
685 std::string Name;
686};
687
688TEST(HasDescendant, MatchesDescendantTypes) {
689 EXPECT_TRUE(matches("void f() { int i = 3; }",
690 decl(hasDescendant(loc(builtinType())))));
691 EXPECT_TRUE(matches("void f() { int i = 3; }",
692 stmt(hasDescendant(builtinType()))));
693
694 EXPECT_TRUE(matches("void f() { int i = 3; }",
695 stmt(hasDescendant(loc(builtinType())))));
696 EXPECT_TRUE(matches("void f() { int i = 3; }",
697 stmt(hasDescendant(qualType(builtinType())))));
698
699 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
700 stmt(hasDescendant(isInteger()))));
701
702 EXPECT_TRUE(matchAndVerifyResultTrue(
703 "void f() { int a; float c; int d; int e; }",
704 functionDecl(forEachDescendant(
705 varDecl(hasDescendant(isInteger())).bind("x"))),
706 new VerifyIdIsBoundTo<Decl>("x", 3)));
707}
708
709TEST(HasDescendant, MatchesDescendantsOfTypes) {
710 EXPECT_TRUE(matches("void f() { int*** i; }",
711 qualType(hasDescendant(builtinType()))));
712 EXPECT_TRUE(matches("void f() { int*** i; }",
713 qualType(hasDescendant(
714 pointerType(pointee(builtinType()))))));
715 EXPECT_TRUE(matches("void f() { int*** i; }",
David Blaikie5be093c2013-02-18 19:04:16 +0000716 typeLoc(hasDescendant(loc(builtinType())))));
Daniel Jaspera267cf62012-10-29 10:14:44 +0000717
718 EXPECT_TRUE(matchAndVerifyResultTrue(
719 "void f() { int*** i; }",
720 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
721 new VerifyIdIsBoundTo<Type>("x", 2)));
722}
723
724TEST(Has, MatchesChildrenOfTypes) {
725 EXPECT_TRUE(matches("int i;",
726 varDecl(hasName("i"), has(isInteger()))));
727 EXPECT_TRUE(notMatches("int** i;",
728 varDecl(hasName("i"), has(isInteger()))));
729 EXPECT_TRUE(matchAndVerifyResultTrue(
730 "int (*f)(float, int);",
731 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
732 new VerifyIdIsBoundTo<QualType>("x", 2)));
733}
734
735TEST(Has, MatchesChildTypes) {
736 EXPECT_TRUE(matches(
737 "int* i;",
738 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
739 EXPECT_TRUE(notMatches(
740 "int* i;",
741 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
742}
743
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000744TEST(Enum, DoesNotMatchClasses) {
745 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
746}
747
748TEST(Enum, MatchesEnums) {
749 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
750}
751
752TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000753 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000754 EXPECT_TRUE(matches("enum X{ A };", Matcher));
755 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
756 EXPECT_TRUE(notMatches("enum X {};", Matcher));
757}
758
Manuel Klimek4da21662012-07-06 05:48:52 +0000759TEST(StatementMatcher, Has) {
760 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000761 expr(hasType(pointsTo(recordDecl(hasName("X")))),
762 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000763
764 EXPECT_TRUE(matches(
765 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
766 EXPECT_TRUE(notMatches(
767 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
768}
769
770TEST(StatementMatcher, HasDescendant) {
771 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000772 expr(hasType(pointsTo(recordDecl(hasName("X")))),
773 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000774
775 EXPECT_TRUE(matches(
776 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
777 HasDescendantVariableI));
778 EXPECT_TRUE(notMatches(
779 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
780 HasDescendantVariableI));
781}
782
783TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000784 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000785
786 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
787 EXPECT_TRUE(notMatches("class A {};", TypeA));
788
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000789 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000790
791 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
792 TypeDerivedFromA));
793 EXPECT_TRUE(notMatches("class A {};", TypeA));
794
795 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000796 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000797
798 EXPECT_TRUE(
799 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
800}
801
Manuel Klimek4da21662012-07-06 05:48:52 +0000802TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000803 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000804
805 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000806 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000807
808 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000809 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000810
811 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000812 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000813
814 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
815 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000816 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000817
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000818 StatementMatcher MethodX =
819 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000820
821 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
822 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000823 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000824}
825
826TEST(Matcher, BindTheSameNameInAlternatives) {
827 StatementMatcher matcher = anyOf(
828 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000829 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000830 hasRHS(integerLiteral(equals(0)))),
831 binaryOperator(hasOperatorName("+"),
832 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000833 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000834
835 EXPECT_TRUE(matchAndVerifyResultTrue(
836 // The first branch of the matcher binds x to 0 but then fails.
837 // The second branch binds x to f() and succeeds.
838 "int f() { return 0 + f(); }",
839 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000840 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000841}
842
Manuel Klimek66341c52012-08-30 19:41:06 +0000843TEST(Matcher, BindsIDForMemoizedResults) {
844 // Using the same matcher in two match expressions will make memoization
845 // kick in.
846 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
847 EXPECT_TRUE(matchAndVerifyResultTrue(
848 "class A { class B { class X {}; }; };",
849 DeclarationMatcher(anyOf(
850 recordDecl(hasName("A"), hasDescendant(ClassX)),
851 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000852 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000853}
854
Daniel Jasper189f2e42012-12-03 15:43:25 +0000855TEST(HasDeclaration, HasDeclarationOfEnumType) {
856 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
857 expr(hasType(pointsTo(
858 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
859}
860
Edwin Vaneb45083d2013-02-25 14:32:42 +0000861TEST(HasDeclaration, HasGetDeclTraitTest) {
862 EXPECT_TRUE(internal::has_getDecl<TypedefType>::value);
863 EXPECT_TRUE(internal::has_getDecl<RecordType>::value);
864 EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value);
865}
866
Edwin Vane52380602013-02-19 17:14:34 +0000867TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) {
868 EXPECT_TRUE(matches("typedef int X; X a;",
869 varDecl(hasName("a"),
870 hasType(typedefType(hasDeclaration(decl()))))));
871
872 // FIXME: Add tests for other types with getDecl() (e.g. RecordType)
873}
874
Edwin Vane3abf7782013-02-25 14:49:29 +0000875TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) {
876 EXPECT_TRUE(matches("template <typename T> class A {}; A<int> a;",
877 varDecl(hasType(templateSpecializationType(
878 hasDeclaration(namedDecl(hasName("A"))))))));
879}
880
Manuel Klimek4da21662012-07-06 05:48:52 +0000881TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000882 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000883 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000884 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000885 EXPECT_TRUE(
886 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000887 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000888 EXPECT_TRUE(
889 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000890 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000891}
892
893TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000894 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000895 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000896 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000897 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000898 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000899 EXPECT_TRUE(
900 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000901 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000902}
903
904TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000905 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000906 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000907 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000908 EXPECT_TRUE(
909 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000910 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000911}
912
913TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000914 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000915 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000916 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000917 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000918 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000919}
920
Manuel Klimek1a68afd2013-06-20 13:08:29 +0000921TEST(HasTypeLoc, MatchesDeclaratorDecls) {
922 EXPECT_TRUE(matches("int x;",
923 varDecl(hasName("x"), hasTypeLoc(loc(asString("int"))))));
924
925 // Make sure we don't crash on implicit constructors.
926 EXPECT_TRUE(notMatches("class X {}; X x;",
927 declaratorDecl(hasTypeLoc(loc(asString("int"))))));
928}
929
Manuel Klimek4da21662012-07-06 05:48:52 +0000930TEST(Matcher, Call) {
931 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000932 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000933 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000934
935 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
936 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
937
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000938 StatementMatcher MethodOnY =
939 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000940
941 EXPECT_TRUE(
942 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
943 MethodOnY));
944 EXPECT_TRUE(
945 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
946 MethodOnY));
947 EXPECT_TRUE(
948 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
949 MethodOnY));
950 EXPECT_TRUE(
951 notMatches("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
957 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000958 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000959
960 EXPECT_TRUE(
961 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
962 MethodOnYPointer));
963 EXPECT_TRUE(
964 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
965 MethodOnYPointer));
966 EXPECT_TRUE(
967 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
968 MethodOnYPointer));
969 EXPECT_TRUE(
970 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
971 MethodOnYPointer));
972 EXPECT_TRUE(
973 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
974 MethodOnYPointer));
975}
976
Daniel Jasper31f7c082012-10-01 13:40:41 +0000977TEST(Matcher, Lambda) {
978 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
979 lambdaExpr()));
980}
981
982TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000983 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
984 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000985 forRangeStmt()));
986 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
987 forRangeStmt()));
988}
989
990TEST(Matcher, UserDefinedLiteral) {
991 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
992 " return i + 1;"
993 "}"
994 "char c = 'a'_inc;",
995 userDefinedLiteral()));
996}
997
Daniel Jasperb54b7642012-09-20 14:12:57 +0000998TEST(Matcher, FlowControl) {
999 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
1000 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
1001 continueStmt()));
1002 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
1003 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
1004 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
1005}
1006
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001007TEST(HasType, MatchesAsString) {
1008 EXPECT_TRUE(
1009 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001010 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001011 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001012 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001013 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001014 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001015 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001016 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001017}
1018
Manuel Klimek4da21662012-07-06 05:48:52 +00001019TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001020 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001021 // Unary operator
1022 EXPECT_TRUE(matches("class Y { }; "
1023 "bool operator!(Y x) { return false; }; "
1024 "Y y; bool c = !y;", OpCall));
1025 // No match -- special operators like "new", "delete"
1026 // FIXME: operator new takes size_t, for which we need stddef.h, for which
1027 // we need to figure out include paths in the test.
1028 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
1029 // "class Y { }; "
1030 // "void *operator new(size_t size) { return 0; } "
1031 // "Y *y = new Y;", OpCall));
1032 EXPECT_TRUE(notMatches("class Y { }; "
1033 "void operator delete(void *p) { } "
1034 "void a() {Y *y = new Y; delete y;}", OpCall));
1035 // Binary operator
1036 EXPECT_TRUE(matches("class Y { }; "
1037 "bool operator&&(Y x, Y y) { return true; }; "
1038 "Y a; Y b; bool c = a && b;",
1039 OpCall));
1040 // No match -- normal operator, not an overloaded one.
1041 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
1042 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
1043}
1044
1045TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
1046 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001047 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001048 EXPECT_TRUE(matches("class Y { }; "
1049 "bool operator&&(Y x, Y y) { return true; }; "
1050 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
1051 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001052 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001053 EXPECT_TRUE(notMatches("class Y { }; "
1054 "bool operator&&(Y x, Y y) { return true; }; "
1055 "Y a; Y b; bool c = a && b;",
1056 OpCallLessLess));
Edwin Vane6a19a972013-03-06 17:02:57 +00001057 DeclarationMatcher ClassWithOpStar =
1058 recordDecl(hasMethod(hasOverloadedOperatorName("*")));
1059 EXPECT_TRUE(matches("class Y { int operator*(); };",
1060 ClassWithOpStar));
1061 EXPECT_TRUE(notMatches("class Y { void myOperator(); };",
1062 ClassWithOpStar)) ;
Manuel Klimek4da21662012-07-06 05:48:52 +00001063}
1064
Daniel Jasper278057f2012-11-15 03:29:05 +00001065TEST(Matcher, NestedOverloadedOperatorCalls) {
1066 EXPECT_TRUE(matchAndVerifyResultTrue(
1067 "class Y { }; "
1068 "Y& operator&&(Y& x, Y& y) { return x; }; "
1069 "Y a; Y b; Y c; Y d = a && b && c;",
1070 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
1071 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
1072 EXPECT_TRUE(matches(
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(hasParent(operatorCallExpr()))));
1077 EXPECT_TRUE(matches(
1078 "class Y { }; "
1079 "Y& operator&&(Y& x, Y& y) { return x; }; "
1080 "Y a; Y b; Y c; Y d = a && b && c;",
1081 operatorCallExpr(hasDescendant(operatorCallExpr()))));
1082}
1083
Manuel Klimek4da21662012-07-06 05:48:52 +00001084TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +00001085 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001086 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001087
1088 EXPECT_TRUE(
1089 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1090 MethodOnY));
1091 EXPECT_TRUE(
1092 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1093 MethodOnY));
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
1104 EXPECT_TRUE(matches(
1105 "class Y {"
1106 " public: virtual void x();"
1107 "};"
1108 "class X : public Y {"
1109 " public: virtual void x();"
1110 "};"
1111 "void z() { X *x; x->Y::x(); }", MethodOnY));
1112}
1113
1114TEST(Matcher, VariableUsage) {
1115 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001116 declRefExpr(to(
1117 varDecl(hasInitializer(
1118 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001119
1120 EXPECT_TRUE(matches(
1121 "class Y {"
1122 " public:"
1123 " bool x() const;"
1124 "};"
1125 "void z(const Y &y) {"
1126 " bool b = y.x();"
1127 " if (b) {}"
1128 "}", Reference));
1129
1130 EXPECT_TRUE(notMatches(
1131 "class Y {"
1132 " public:"
1133 " bool x() const;"
1134 "};"
1135 "void z(const Y &y) {"
1136 " bool b = y.x();"
1137 "}", Reference));
1138}
1139
Manuel Klimek8cb9bf52012-12-04 14:42:08 +00001140TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper9bd28092012-07-30 05:03:25 +00001141 EXPECT_TRUE(matches(
1142 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001143 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001144}
1145
Manuel Klimek4da21662012-07-06 05:48:52 +00001146TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001147 StatementMatcher CallOnVariableY =
1148 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001149
1150 EXPECT_TRUE(matches(
1151 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1152 EXPECT_TRUE(matches(
1153 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1154 EXPECT_TRUE(matches(
1155 "class Y { public: void x(); };"
1156 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1157 EXPECT_TRUE(matches(
1158 "class Y { public: void x(); };"
1159 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1160 EXPECT_TRUE(notMatches(
1161 "class Y { public: void x(); };"
1162 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1163 CallOnVariableY));
1164}
1165
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001166TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1167 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1168 unaryExprOrTypeTraitExpr()));
1169 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1170 alignOfExpr(anything())));
1171 // FIXME: Uncomment once alignof is enabled.
1172 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1173 // unaryExprOrTypeTraitExpr()));
1174 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1175 // sizeOfExpr()));
1176}
1177
1178TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1179 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1180 hasArgumentOfType(asString("int")))));
1181 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1182 hasArgumentOfType(asString("float")))));
1183 EXPECT_TRUE(matches(
1184 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001185 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001186 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001187 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001188}
1189
Manuel Klimek4da21662012-07-06 05:48:52 +00001190TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001191 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001192}
1193
1194TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001195 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001196}
1197
1198TEST(MemberExpression, MatchesVariable) {
1199 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001200 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001201 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001202 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001203 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001204 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001205}
1206
1207TEST(MemberExpression, MatchesStaticVariable) {
1208 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001209 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001210 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001211 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001212 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001213 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001214}
1215
Daniel Jasper6a124492012-07-12 08:50:38 +00001216TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001217 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1218 EXPECT_TRUE(matches(
1219 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1220 callExpr(hasArgument(0, declRefExpr(
1221 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001222}
1223
1224TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001225 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001226 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001227 callExpr(hasArgument(0, declRefExpr(
1228 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001229}
1230
Manuel Klimek4da21662012-07-06 05:48:52 +00001231TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1232 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001233 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001234 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001235 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001236 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001237 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001238}
1239
1240TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1241 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001242 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001243 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001244 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001245 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001246 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001247}
1248
1249TEST(IsArrow, MatchesMemberCallsViaArrow) {
1250 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001251 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001252 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001253 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001254 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001255 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001256}
1257
1258TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001259 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001260
1261 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1262 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1263}
1264
1265TEST(Callee, MatchesMemberExpressions) {
1266 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001267 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001268 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001269 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001270}
1271
1272TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001273 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001274
1275 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1276 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1277
Manuel Klimeke265c872012-07-10 14:21:30 +00001278#if !defined(_MSC_VER)
1279 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001280 // Dependent contexts, but a non-dependent call.
1281 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1282 CallFunctionF));
1283 EXPECT_TRUE(
1284 matches("void f(); template <int N> struct S { void g() { f(); } };",
1285 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001286#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001287
1288 // Depedent calls don't match.
1289 EXPECT_TRUE(
1290 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1291 CallFunctionF));
1292 EXPECT_TRUE(
1293 notMatches("void f(int);"
1294 "template <typename T> struct S { void g(T t) { f(t); } };",
1295 CallFunctionF));
1296}
1297
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001298TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1299 EXPECT_TRUE(
1300 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001301 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001302}
1303
1304TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1305 EXPECT_TRUE(
1306 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001307 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001308}
1309
1310TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1311 EXPECT_TRUE(
1312 notMatches("void g(); template <typename T> void f(T t) {}"
1313 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001314 functionTemplateDecl(hasName("f"),
1315 hasDescendant(declRefExpr(to(
1316 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001317}
1318
Manuel Klimek4da21662012-07-06 05:48:52 +00001319TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001320 StatementMatcher CallArgumentY = callExpr(
1321 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001322
1323 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1324 EXPECT_TRUE(
1325 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1326 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1327
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001328 StatementMatcher WrongIndex = callExpr(
1329 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001330 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1331}
1332
1333TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001334 StatementMatcher CallArgumentY = callExpr(
1335 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001336 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1337 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1338 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1339}
1340
1341TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001342 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001343
1344 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1345 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1346 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1347}
1348
Daniel Jasper36e29d62012-12-04 11:54:27 +00001349TEST(Matcher, ParameterCount) {
1350 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1351 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1352 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1353 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1354 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1355}
1356
Manuel Klimek4da21662012-07-06 05:48:52 +00001357TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001358 DeclarationMatcher ReferenceClassX = varDecl(
1359 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001360 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1361 ReferenceClassX));
1362 EXPECT_TRUE(
1363 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
Michael Han4b6730d2013-09-11 15:53:29 +00001364 // The match here is on the implicit copy constructor code for
1365 // class X, not on code 'X x = y'.
Manuel Klimek4da21662012-07-06 05:48:52 +00001366 EXPECT_TRUE(
Michael Han4b6730d2013-09-11 15:53:29 +00001367 matches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1368 EXPECT_TRUE(
1369 notMatches("class X {}; extern X x;", ReferenceClassX));
Manuel Klimek4da21662012-07-06 05:48:52 +00001370 EXPECT_TRUE(
1371 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1372}
1373
Edwin Vane6a19a972013-03-06 17:02:57 +00001374TEST(QualType, hasCanonicalType) {
1375 EXPECT_TRUE(notMatches("typedef int &int_ref;"
1376 "int a;"
1377 "int_ref b = a;",
1378 varDecl(hasType(qualType(referenceType())))));
1379 EXPECT_TRUE(
1380 matches("typedef int &int_ref;"
1381 "int a;"
1382 "int_ref b = a;",
1383 varDecl(hasType(qualType(hasCanonicalType(referenceType()))))));
1384}
1385
Edwin Vane7b69cd02013-04-02 18:15:55 +00001386TEST(QualType, hasLocalQualifiers) {
1387 EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;",
1388 varDecl(hasType(hasLocalQualifiers()))));
1389 EXPECT_TRUE(matches("int *const j = nullptr;",
1390 varDecl(hasType(hasLocalQualifiers()))));
1391 EXPECT_TRUE(matches("int *volatile k;",
1392 varDecl(hasType(hasLocalQualifiers()))));
1393 EXPECT_TRUE(notMatches("int m;",
1394 varDecl(hasType(hasLocalQualifiers()))));
1395}
1396
Manuel Klimek4da21662012-07-06 05:48:52 +00001397TEST(HasParameter, CallsInnerMatcher) {
1398 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001399 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001400 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001401 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001402}
1403
1404TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1405 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001406 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001407}
1408
1409TEST(HasType, MatchesParameterVariableTypesStrictly) {
1410 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001411 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001412 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001413 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001414 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001415 methodDecl(hasParameter(0,
1416 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001417 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001418 methodDecl(hasParameter(0,
1419 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001420}
1421
1422TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1423 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001424 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001425 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001426 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001427}
1428
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001429TEST(Returns, MatchesReturnTypes) {
1430 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001431 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001432 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001433 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001434 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001435 functionDecl(returns(hasDeclaration(
1436 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001437}
1438
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001439TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001440 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1441 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1442 functionDecl(isExternC())));
1443 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001444}
1445
Manuel Klimek4da21662012-07-06 05:48:52 +00001446TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1447 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001448 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001449}
1450
1451TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1452 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001453 methodDecl(hasAnyParameter(hasType(pointsTo(
1454 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001455}
1456
1457TEST(HasName, MatchesParameterVariableDeclartions) {
1458 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001459 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001460 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001461 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001462}
1463
Daniel Jasper371f9392012-08-01 08:40:24 +00001464TEST(Matcher, MatchesClassTemplateSpecialization) {
1465 EXPECT_TRUE(matches("template<typename T> struct A {};"
1466 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001467 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001468 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001469 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001470 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001471 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001472}
1473
Manuel Klimek1a68afd2013-06-20 13:08:29 +00001474TEST(DeclaratorDecl, MatchesDeclaratorDecls) {
1475 EXPECT_TRUE(matches("int x;", declaratorDecl()));
1476 EXPECT_TRUE(notMatches("class A {};", declaratorDecl()));
1477}
1478
1479TEST(ParmVarDecl, MatchesParmVars) {
1480 EXPECT_TRUE(matches("void f(int x);", parmVarDecl()));
1481 EXPECT_TRUE(notMatches("void f();", parmVarDecl()));
1482}
1483
Daniel Jasper371f9392012-08-01 08:40:24 +00001484TEST(Matcher, MatchesTypeTemplateArgument) {
1485 EXPECT_TRUE(matches(
1486 "template<typename T> struct B {};"
1487 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001488 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001489 asString("int"))))));
1490}
1491
1492TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1493 EXPECT_TRUE(matches(
1494 "struct B { int next; };"
1495 "template<int(B::*next_ptr)> struct A {};"
1496 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001497 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1498 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001499
1500 EXPECT_TRUE(notMatches(
1501 "template <typename T> struct A {};"
1502 "A<int> a;",
1503 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1504 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001505}
1506
1507TEST(Matcher, MatchesSpecificArgument) {
1508 EXPECT_TRUE(matches(
1509 "template<typename T, typename U> class A {};"
1510 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001511 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001512 1, refersToType(asString("int"))))));
1513 EXPECT_TRUE(notMatches(
1514 "template<typename T, typename U> class A {};"
1515 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001516 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001517 1, refersToType(asString("int"))))));
1518}
1519
Daniel Jasperf3197e92013-02-25 12:02:08 +00001520TEST(Matcher, MatchesAccessSpecDecls) {
1521 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1522 EXPECT_TRUE(
1523 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1524 EXPECT_TRUE(
1525 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1526 EXPECT_TRUE(
1527 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1528
1529 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1530}
1531
Edwin Vane5771a2f2013-04-09 20:46:36 +00001532TEST(Matcher, MatchesVirtualMethod) {
1533 EXPECT_TRUE(matches("class X { virtual int f(); };",
1534 methodDecl(isVirtual(), hasName("::X::f"))));
1535 EXPECT_TRUE(notMatches("class X { int f(); };",
1536 methodDecl(isVirtual())));
1537}
1538
Edwin Vane32a6ebc2013-05-09 17:00:17 +00001539TEST(Matcher, MatchesConstMethod) {
1540 EXPECT_TRUE(matches("struct A { void foo() const; };",
1541 methodDecl(isConst())));
1542 EXPECT_TRUE(notMatches("struct A { void foo(); };",
1543 methodDecl(isConst())));
1544}
1545
Edwin Vane5771a2f2013-04-09 20:46:36 +00001546TEST(Matcher, MatchesOverridingMethod) {
1547 EXPECT_TRUE(matches("class X { virtual int f(); }; "
1548 "class Y : public X { int f(); };",
1549 methodDecl(isOverride(), hasName("::Y::f"))));
1550 EXPECT_TRUE(notMatches("class X { virtual int f(); }; "
1551 "class Y : public X { int f(); };",
1552 methodDecl(isOverride(), hasName("::X::f"))));
1553 EXPECT_TRUE(notMatches("class X { int f(); }; "
1554 "class Y : public X { int f(); };",
1555 methodDecl(isOverride())));
1556 EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ",
1557 methodDecl(isOverride())));
1558}
1559
Manuel Klimek4da21662012-07-06 05:48:52 +00001560TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001561 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001562
1563 EXPECT_TRUE(
1564 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1565 EXPECT_TRUE(
1566 matches("class X { public: X(); }; void x() { X x = X(); }",
1567 Constructor));
1568 EXPECT_TRUE(
1569 matches("class X { public: X(int); }; void x() { X x = 0; }",
1570 Constructor));
1571 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1572}
1573
1574TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001575 StatementMatcher Constructor = constructExpr(
1576 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001577
1578 EXPECT_TRUE(
1579 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1580 Constructor));
1581 EXPECT_TRUE(
1582 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1583 Constructor));
1584 EXPECT_TRUE(
1585 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1586 Constructor));
1587 EXPECT_TRUE(
1588 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1589 Constructor));
1590
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001591 StatementMatcher WrongIndex = constructExpr(
1592 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001593 EXPECT_TRUE(
1594 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1595 WrongIndex));
1596}
1597
1598TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001599 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001600
1601 EXPECT_TRUE(
1602 matches("class X { public: X(int); }; void x() { X x(0); }",
1603 Constructor1Arg));
1604 EXPECT_TRUE(
1605 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1606 Constructor1Arg));
1607 EXPECT_TRUE(
1608 matches("class X { public: X(int); }; void x() { X x = 0; }",
1609 Constructor1Arg));
1610 EXPECT_TRUE(
1611 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1612 Constructor1Arg));
1613}
1614
Manuel Klimek70b9db92012-10-23 10:40:50 +00001615TEST(Matcher,ThisExpr) {
1616 EXPECT_TRUE(
1617 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1618 EXPECT_TRUE(
1619 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1620}
1621
Manuel Klimek4da21662012-07-06 05:48:52 +00001622TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001623 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001624
1625 std::string ClassString = "class string { public: string(); ~string(); }; ";
1626
1627 EXPECT_TRUE(
1628 matches(ClassString +
1629 "string GetStringByValue();"
1630 "void FunctionTakesString(string s);"
1631 "void run() { FunctionTakesString(GetStringByValue()); }",
1632 TempExpression));
1633
1634 EXPECT_TRUE(
1635 notMatches(ClassString +
1636 "string* GetStringPointer(); "
1637 "void FunctionTakesStringPtr(string* s);"
1638 "void run() {"
1639 " string* s = GetStringPointer();"
1640 " FunctionTakesStringPtr(GetStringPointer());"
1641 " FunctionTakesStringPtr(s);"
1642 "}",
1643 TempExpression));
1644
1645 EXPECT_TRUE(
1646 notMatches("class no_dtor {};"
1647 "no_dtor GetObjByValue();"
1648 "void ConsumeObj(no_dtor param);"
1649 "void run() { ConsumeObj(GetObjByValue()); }",
1650 TempExpression));
1651}
1652
Sam Panzere16acd32012-08-24 22:04:44 +00001653TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1654 std::string ClassString =
1655 "class string { public: string(); int length(); }; ";
1656
1657 EXPECT_TRUE(
1658 matches(ClassString +
1659 "string GetStringByValue();"
1660 "void FunctionTakesString(string s);"
1661 "void run() { FunctionTakesString(GetStringByValue()); }",
1662 materializeTemporaryExpr()));
1663
1664 EXPECT_TRUE(
1665 notMatches(ClassString +
1666 "string* GetStringPointer(); "
1667 "void FunctionTakesStringPtr(string* s);"
1668 "void run() {"
1669 " string* s = GetStringPointer();"
1670 " FunctionTakesStringPtr(GetStringPointer());"
1671 " FunctionTakesStringPtr(s);"
1672 "}",
1673 materializeTemporaryExpr()));
1674
1675 EXPECT_TRUE(
1676 notMatches(ClassString +
1677 "string GetStringByValue();"
1678 "void run() { int k = GetStringByValue().length(); }",
1679 materializeTemporaryExpr()));
1680
1681 EXPECT_TRUE(
1682 notMatches(ClassString +
1683 "string GetStringByValue();"
1684 "void run() { GetStringByValue(); }",
1685 materializeTemporaryExpr()));
1686}
1687
Manuel Klimek4da21662012-07-06 05:48:52 +00001688TEST(ConstructorDeclaration, SimpleCase) {
1689 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001690 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001691 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001692 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001693}
1694
1695TEST(ConstructorDeclaration, IsImplicit) {
1696 // This one doesn't match because the constructor is not added by the
1697 // compiler (it is not needed).
1698 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001699 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001700 // The compiler added the implicit default constructor.
1701 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001702 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001703 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001704 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001705}
1706
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001707TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1708 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001709 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001710}
1711
1712TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001713 EXPECT_TRUE(notMatches("class Foo {};",
1714 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001715}
1716
Manuel Klimek4da21662012-07-06 05:48:52 +00001717TEST(HasAnyConstructorInitializer, SimpleCase) {
1718 EXPECT_TRUE(notMatches(
1719 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001720 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001721 EXPECT_TRUE(matches(
1722 "class Foo {"
1723 " Foo() : foo_() { }"
1724 " int foo_;"
1725 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001726 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001727}
1728
1729TEST(HasAnyConstructorInitializer, ForField) {
1730 static const char Code[] =
1731 "class Baz { };"
1732 "class Foo {"
1733 " Foo() : foo_() { }"
1734 " Baz foo_;"
1735 " Baz bar_;"
1736 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001737 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1738 forField(hasType(recordDecl(hasName("Baz"))))))));
1739 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001740 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001741 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1742 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001743}
1744
1745TEST(HasAnyConstructorInitializer, WithInitializer) {
1746 static const char Code[] =
1747 "class Foo {"
1748 " Foo() : foo_(0) { }"
1749 " int foo_;"
1750 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001751 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001752 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001753 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001754 withInitializer(integerLiteral(equals(1)))))));
1755}
1756
1757TEST(HasAnyConstructorInitializer, IsWritten) {
1758 static const char Code[] =
1759 "struct Bar { Bar(){} };"
1760 "class Foo {"
1761 " Foo() : foo_() { }"
1762 " Bar foo_;"
1763 " Bar bar_;"
1764 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001765 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001766 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001767 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001768 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001769 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001770 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1771}
1772
1773TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001774 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001775
1776 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1777 EXPECT_TRUE(
1778 matches("class X { public: X(); }; void x() { new X(); }", New));
1779 EXPECT_TRUE(
1780 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1781 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1782}
1783
1784TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001785 StatementMatcher New = constructExpr(
1786 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001787
1788 EXPECT_TRUE(
1789 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1790 New));
1791 EXPECT_TRUE(
1792 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1793 New));
1794 EXPECT_TRUE(
1795 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1796 New));
1797
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001798 StatementMatcher WrongIndex = constructExpr(
1799 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001800 EXPECT_TRUE(
1801 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1802 WrongIndex));
1803}
1804
1805TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001806 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001807
1808 EXPECT_TRUE(
1809 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1810 EXPECT_TRUE(
1811 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1812 New));
1813}
1814
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001815TEST(Matcher, DeleteExpression) {
1816 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001817 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001818}
1819
Manuel Klimek4da21662012-07-06 05:48:52 +00001820TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001821 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001822
1823 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1824 EXPECT_TRUE(
1825 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1826 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1827}
1828
1829TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001830 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001831 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1832 // wide string
1833 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1834 // with escaped characters
1835 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1836 // no matching -- though the data type is the same, there is no string literal
1837 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1838}
1839
1840TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001841 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001842 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1843 // wide character
1844 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1845 // wide character, Hex encoded, NOT MATCHED!
1846 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1847 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1848}
1849
1850TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001851 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001852 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1853 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1854 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1855 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1856
1857 // Non-matching cases (character literals, float and double)
1858 EXPECT_TRUE(notMatches("int i = L'a';",
1859 HasIntLiteral)); // this is actually a character
1860 // literal cast to int
1861 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1862 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1863 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1864}
1865
Daniel Jasperc5ae7172013-07-26 18:52:58 +00001866TEST(Matcher, FloatLiterals) {
1867 StatementMatcher HasFloatLiteral = floatLiteral();
1868 EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral));
1869 EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral));
1870 EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral));
1871 EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral));
1872 EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral));
1873
1874 EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral));
1875}
1876
Daniel Jasper31f7c082012-10-01 13:40:41 +00001877TEST(Matcher, NullPtrLiteral) {
1878 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1879}
1880
Daniel Jasperb54b7642012-09-20 14:12:57 +00001881TEST(Matcher, AsmStatement) {
1882 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1883}
1884
Manuel Klimek4da21662012-07-06 05:48:52 +00001885TEST(Matcher, Conditions) {
1886 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1887
1888 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1889 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1890 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1891 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1892 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1893}
1894
1895TEST(MatchBinaryOperator, HasOperatorName) {
1896 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1897
1898 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1899 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1900}
1901
1902TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1903 StatementMatcher OperatorTrueFalse =
1904 binaryOperator(hasLHS(boolLiteral(equals(true))),
1905 hasRHS(boolLiteral(equals(false))));
1906
1907 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1908 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1909 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1910}
1911
1912TEST(MatchBinaryOperator, HasEitherOperand) {
1913 StatementMatcher HasOperand =
1914 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1915
1916 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1917 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1918 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1919}
1920
1921TEST(Matcher, BinaryOperatorTypes) {
1922 // Integration test that verifies the AST provides all binary operators in
1923 // a way we expect.
1924 // FIXME: Operator ','
1925 EXPECT_TRUE(
1926 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1927 EXPECT_TRUE(
1928 matches("bool b; bool c = (b = true);",
1929 binaryOperator(hasOperatorName("="))));
1930 EXPECT_TRUE(
1931 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1932 EXPECT_TRUE(
1933 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1934 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1935 EXPECT_TRUE(
1936 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1937 EXPECT_TRUE(
1938 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1939 EXPECT_TRUE(
1940 matches("int i = 1; int j = (i <<= 2);",
1941 binaryOperator(hasOperatorName("<<="))));
1942 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1943 EXPECT_TRUE(
1944 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1945 EXPECT_TRUE(
1946 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1947 EXPECT_TRUE(
1948 matches("int i = 1; int j = (i >>= 2);",
1949 binaryOperator(hasOperatorName(">>="))));
1950 EXPECT_TRUE(
1951 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1952 EXPECT_TRUE(
1953 matches("int i = 42; int j = (i ^= 42);",
1954 binaryOperator(hasOperatorName("^="))));
1955 EXPECT_TRUE(
1956 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1957 EXPECT_TRUE(
1958 matches("int i = 42; int j = (i %= 42);",
1959 binaryOperator(hasOperatorName("%="))));
1960 EXPECT_TRUE(
1961 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1962 EXPECT_TRUE(
1963 matches("bool b = true && false;",
1964 binaryOperator(hasOperatorName("&&"))));
1965 EXPECT_TRUE(
1966 matches("bool b = true; bool c = (b &= false);",
1967 binaryOperator(hasOperatorName("&="))));
1968 EXPECT_TRUE(
1969 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1970 EXPECT_TRUE(
1971 matches("bool b = true || false;",
1972 binaryOperator(hasOperatorName("||"))));
1973 EXPECT_TRUE(
1974 matches("bool b = true; bool c = (b |= false);",
1975 binaryOperator(hasOperatorName("|="))));
1976 EXPECT_TRUE(
1977 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1978 EXPECT_TRUE(
1979 matches("int i = 42; int j = (i *= 23);",
1980 binaryOperator(hasOperatorName("*="))));
1981 EXPECT_TRUE(
1982 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1983 EXPECT_TRUE(
1984 matches("int i = 42; int j = (i /= 23);",
1985 binaryOperator(hasOperatorName("/="))));
1986 EXPECT_TRUE(
1987 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1988 EXPECT_TRUE(
1989 matches("int i = 42; int j = (i += 23);",
1990 binaryOperator(hasOperatorName("+="))));
1991 EXPECT_TRUE(
1992 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1993 EXPECT_TRUE(
1994 matches("int i = 42; int j = (i -= 23);",
1995 binaryOperator(hasOperatorName("-="))));
1996 EXPECT_TRUE(
1997 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1998 binaryOperator(hasOperatorName("->*"))));
1999 EXPECT_TRUE(
2000 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
2001 binaryOperator(hasOperatorName(".*"))));
2002
2003 // Member expressions as operators are not supported in matches.
2004 EXPECT_TRUE(
2005 notMatches("struct A { void x(A *a) { a->x(this); } };",
2006 binaryOperator(hasOperatorName("->"))));
2007
2008 // Initializer assignments are not represented as operator equals.
2009 EXPECT_TRUE(
2010 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
2011
2012 // Array indexing is not represented as operator.
2013 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
2014
2015 // Overloaded operators do not match at all.
2016 EXPECT_TRUE(notMatches(
2017 "struct A { bool operator&&(const A &a) const { return false; } };"
2018 "void x() { A a, b; a && b; }",
2019 binaryOperator()));
2020}
2021
2022TEST(MatchUnaryOperator, HasOperatorName) {
2023 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
2024
2025 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
2026 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
2027}
2028
2029TEST(MatchUnaryOperator, HasUnaryOperand) {
2030 StatementMatcher OperatorOnFalse =
2031 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
2032
2033 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
2034 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
2035}
2036
2037TEST(Matcher, UnaryOperatorTypes) {
2038 // Integration test that verifies the AST provides all unary operators in
2039 // a way we expect.
2040 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
2041 EXPECT_TRUE(
2042 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
2043 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
2044 EXPECT_TRUE(
2045 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
2046 EXPECT_TRUE(
2047 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
2048 EXPECT_TRUE(
2049 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
2050 EXPECT_TRUE(
2051 matches("int i; int j = ++i;", 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
2059 // We don't match conversion operators.
2060 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
2061
2062 // Function calls are not represented as operator.
2063 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
2064
2065 // Overloaded operators do not match at all.
2066 // FIXME: We probably want to add that.
2067 EXPECT_TRUE(notMatches(
2068 "struct A { bool operator!() const { return false; } };"
2069 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
2070}
2071
2072TEST(Matcher, ConditionalOperator) {
2073 StatementMatcher Conditional = conditionalOperator(
2074 hasCondition(boolLiteral(equals(true))),
2075 hasTrueExpression(boolLiteral(equals(false))));
2076
2077 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
2078 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
2079 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
2080
2081 StatementMatcher ConditionalFalse = conditionalOperator(
2082 hasFalseExpression(boolLiteral(equals(false))));
2083
2084 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
2085 EXPECT_TRUE(
2086 notMatches("void x() { true ? false : true; }", ConditionalFalse));
2087}
2088
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002089TEST(ArraySubscriptMatchers, ArraySubscripts) {
2090 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2091 arraySubscriptExpr()));
2092 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2093 arraySubscriptExpr()));
2094}
2095
2096TEST(ArraySubscriptMatchers, ArrayIndex) {
2097 EXPECT_TRUE(matches(
2098 "int i[2]; void f() { i[1] = 1; }",
2099 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2100 EXPECT_TRUE(matches(
2101 "int i[2]; void f() { 1[i] = 1; }",
2102 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2103 EXPECT_TRUE(notMatches(
2104 "int i[2]; void f() { i[1] = 1; }",
2105 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2106}
2107
2108TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2109 EXPECT_TRUE(matches(
2110 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002111 arraySubscriptExpr(hasBase(implicitCastExpr(
2112 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002113}
2114
Manuel Klimek4da21662012-07-06 05:48:52 +00002115TEST(Matcher, HasNameSupportsNamespaces) {
2116 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002117 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002118 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002119 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002120 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002121 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002122 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002123 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002124 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002125 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002126 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002127 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002128 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002129 recordDecl(hasName("a::b::A"))));
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"))));
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("::b::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("z::a::b::C"))));
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("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002138 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002139 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002140}
2141
2142TEST(Matcher, HasNameSupportsOuterClasses) {
2143 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002144 matches("class A { class B { class C; }; };",
2145 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002146 EXPECT_TRUE(
2147 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002148 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002149 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002150 matches("class A { class B { class C; }; };",
2151 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002152 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002153 matches("class A { class B { class C; }; };",
2154 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002155 EXPECT_TRUE(
2156 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002157 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002158 EXPECT_TRUE(
2159 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002160 recordDecl(hasName("A::c::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("A::B::A"))));
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("::C"))));
2167 EXPECT_TRUE(
2168 notMatches("class A { class B { class C; }; };",
2169 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002170 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002171 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002172 EXPECT_TRUE(
2173 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002174 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002175}
2176
2177TEST(Matcher, IsDefinition) {
2178 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002179 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002180 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2181 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2182
2183 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002184 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002185 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2186 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2187
2188 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002189 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002190 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2191 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2192}
2193
2194TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002195 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00002196 ofClass(hasName("X")))));
2197
2198 EXPECT_TRUE(
2199 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2200 EXPECT_TRUE(
2201 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2202 Constructor));
2203 EXPECT_TRUE(
2204 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2205 Constructor));
2206}
2207
2208TEST(Matcher, VisitsTemplateInstantiations) {
2209 EXPECT_TRUE(matches(
2210 "class A { public: void x(); };"
2211 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002212 "void f() { B<A> b; b.y(); }",
2213 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002214
2215 EXPECT_TRUE(matches(
2216 "class A { public: void x(); };"
2217 "class C {"
2218 " public:"
2219 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2220 "};"
2221 "void f() {"
2222 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002223 "}",
2224 recordDecl(hasName("C"),
2225 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002226}
2227
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002228TEST(Matcher, HandlesNullQualTypes) {
2229 // FIXME: Add a Type matcher so we can replace uses of this
2230 // variable with Type(True())
2231 const TypeMatcher AnyType = anything();
2232
2233 // We don't really care whether this matcher succeeds; we're testing that
2234 // it completes without crashing.
2235 EXPECT_TRUE(matches(
2236 "struct A { };"
2237 "template <typename T>"
2238 "void f(T t) {"
2239 " T local_t(t /* this becomes a null QualType in the AST */);"
2240 "}"
2241 "void g() {"
2242 " f(0);"
2243 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002244 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002245 anyOf(
2246 TypeMatcher(hasDeclaration(anything())),
2247 pointsTo(AnyType),
2248 references(AnyType)
2249 // Other QualType matchers should go here.
2250 ))))));
2251}
2252
Manuel Klimek4da21662012-07-06 05:48:52 +00002253// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002254AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002255 // Make sure all special variables are used: node, match_finder,
2256 // bound_nodes_builder, and the parameter named 'AMatcher'.
2257 return AMatcher.matches(Node, Finder, Builder);
2258}
2259
2260TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002261 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002262
2263 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002264 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002265
2266 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002267 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002268
2269 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002270 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002271}
2272
2273AST_POLYMORPHIC_MATCHER_P(
Samuel Benzaquenef7eb022013-06-21 15:51:31 +00002274 polymorphicHas,
2275 AST_POLYMORPHIC_SUPPORTED_TYPES_2(Decl, Stmt),
2276 internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002277 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002278 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002279 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2280 ASTMatchFinder::BK_First);
2281}
2282
2283TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002284 DeclarationMatcher HasClassB =
2285 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002286
2287 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002288 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002289
2290 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002291 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002292
2293 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002294 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002295
2296 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002297 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002298
2299 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2300}
2301
2302TEST(For, FindsForLoops) {
2303 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2304 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002305 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2306 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002307 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002308}
2309
Daniel Jasper6a124492012-07-12 08:50:38 +00002310TEST(For, ForLoopInternals) {
2311 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2312 forStmt(hasCondition(anything()))));
2313 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2314 forStmt(hasLoopInit(anything()))));
2315}
2316
2317TEST(For, NegativeForLoopInternals) {
2318 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002319 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002320 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2321 forStmt(hasLoopInit(anything()))));
2322}
2323
Manuel Klimek4da21662012-07-06 05:48:52 +00002324TEST(For, ReportsNoFalsePositives) {
2325 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2326 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2327}
2328
2329TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002330 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2331 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2332 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002333}
2334
2335TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2336 // It's not a compound statement just because there's "{}" in the source
2337 // text. This is an AST search, not grep.
2338 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002339 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002340 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002341 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002342}
2343
Daniel Jasper6a124492012-07-12 08:50:38 +00002344TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002345 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002346 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002347 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002348 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002349 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002350 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002351 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002352 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002353}
2354
2355TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2356 // The simplest case: every compound statement is in a function
2357 // definition, and the function body itself must be a compound
2358 // statement.
2359 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002360 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002361}
2362
2363TEST(HasAnySubstatement, IsNotRecursive) {
2364 // It's really "has any immediate substatement".
2365 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002366 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002367}
2368
2369TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2370 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002371 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002372}
2373
2374TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2375 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002376 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002377}
2378
2379TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2380 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002381 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002382 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002383 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002384}
2385
2386TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2387 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002388 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002389 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002390 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002391 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002392 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002393}
2394
2395TEST(StatementCountIs, WorksWithMultipleStatements) {
2396 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002397 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002398}
2399
2400TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2401 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002402 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002403 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002404 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002405 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002406 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002407 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002408 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002409}
2410
2411TEST(Member, WorksInSimplestCase) {
2412 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002413 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002414}
2415
2416TEST(Member, DoesNotMatchTheBaseExpression) {
2417 // Don't pick out the wrong part of the member expression, this should
2418 // be checking the member (name) only.
2419 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002420 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002421}
2422
2423TEST(Member, MatchesInMemberFunctionCall) {
2424 EXPECT_TRUE(matches("void f() {"
2425 " struct { void first() {}; } s;"
2426 " s.first();"
2427 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002428 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002429}
2430
Daniel Jasperc711af22012-10-23 15:46:39 +00002431TEST(Member, MatchesMember) {
2432 EXPECT_TRUE(matches(
2433 "struct A { int i; }; void f() { A a; a.i = 2; }",
2434 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2435 EXPECT_TRUE(notMatches(
2436 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2437 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2438}
2439
Daniel Jasperf3197e92013-02-25 12:02:08 +00002440TEST(Member, UnderstandsAccess) {
2441 EXPECT_TRUE(matches(
2442 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2443 EXPECT_TRUE(notMatches(
2444 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2445 EXPECT_TRUE(notMatches(
2446 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2447
2448 EXPECT_TRUE(notMatches(
2449 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2450 EXPECT_TRUE(notMatches(
2451 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2452 EXPECT_TRUE(matches(
2453 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2454
2455 EXPECT_TRUE(notMatches(
2456 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2457 EXPECT_TRUE(matches("class A { protected: int i; };",
2458 fieldDecl(isProtected(), hasName("i"))));
2459 EXPECT_TRUE(notMatches(
2460 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2461
2462 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2463 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2464 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2465 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2466}
2467
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002468TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002469 // Fails in C++11 mode
2470 EXPECT_TRUE(matchesConditionally(
2471 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2472 "class X { void *operator new(std::size_t); };",
2473 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002474
2475 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002476 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002477
Daniel Jasper31f7c082012-10-01 13:40:41 +00002478 // Fails in C++11 mode
2479 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002480 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2481 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002482 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002483}
2484
Manuel Klimek4da21662012-07-06 05:48:52 +00002485TEST(HasObjectExpression, DoesNotMatchMember) {
2486 EXPECT_TRUE(notMatches(
2487 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002488 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002489}
2490
2491TEST(HasObjectExpression, MatchesBaseOfVariable) {
2492 EXPECT_TRUE(matches(
2493 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002494 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002495 EXPECT_TRUE(matches(
2496 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002497 memberExpr(hasObjectExpression(
2498 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002499}
2500
2501TEST(HasObjectExpression,
2502 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2503 EXPECT_TRUE(matches(
2504 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002505 memberExpr(hasObjectExpression(
2506 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002507 EXPECT_TRUE(matches(
2508 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002509 memberExpr(hasObjectExpression(
2510 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002511}
2512
2513TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002514 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2515 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2516 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2517 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002518}
2519
2520TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002521 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002522}
2523
2524TEST(IsConstQualified, MatchesConstInt) {
2525 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002526 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002527}
2528
2529TEST(IsConstQualified, MatchesConstPointer) {
2530 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002531 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002532}
2533
2534TEST(IsConstQualified, MatchesThroughTypedef) {
2535 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002536 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002537 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002538 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002539}
2540
2541TEST(IsConstQualified, DoesNotMatchInappropriately) {
2542 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002543 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002544 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002545 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002546}
2547
Sam Panzer089e5b32012-08-16 16:58:10 +00002548TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002549 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2550 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2551 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2552 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002553}
2554TEST(CastExpression, MatchesImplicitCasts) {
2555 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002556 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002557 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002558 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002559}
2560
2561TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002562 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2563 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2564 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2565 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002566}
2567
Manuel Klimek4da21662012-07-06 05:48:52 +00002568TEST(ReinterpretCast, MatchesSimpleCase) {
2569 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002570 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002571}
2572
2573TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002574 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002575 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002576 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002577 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002578 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002579 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2580 "B b;"
2581 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002582 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002583}
2584
2585TEST(FunctionalCast, MatchesSimpleCase) {
2586 std::string foo_class = "class Foo { public: Foo(char*); };";
2587 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002588 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002589}
2590
2591TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2592 std::string FooClass = "class Foo { public: Foo(char*); };";
2593 EXPECT_TRUE(
2594 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002595 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002596 EXPECT_TRUE(
2597 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002598 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002599}
2600
2601TEST(DynamicCast, MatchesSimpleCase) {
2602 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2603 "B b;"
2604 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002605 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002606}
2607
2608TEST(StaticCast, MatchesSimpleCase) {
2609 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002610 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002611}
2612
2613TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002614 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002615 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002616 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002617 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002618 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002619 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2620 "B b;"
2621 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002622 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002623}
2624
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002625TEST(CStyleCast, MatchesSimpleCase) {
2626 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2627}
2628
2629TEST(CStyleCast, DoesNotMatchOtherCasts) {
2630 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2631 "char q, *r = const_cast<char*>(&q);"
2632 "void* s = reinterpret_cast<char*>(&s);"
2633 "struct B { virtual ~B() {} }; struct D : B {};"
2634 "B b;"
2635 "D* t = dynamic_cast<D*>(&b);",
2636 cStyleCastExpr()));
2637}
2638
Manuel Klimek4da21662012-07-06 05:48:52 +00002639TEST(HasDestinationType, MatchesSimpleCase) {
2640 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002641 staticCastExpr(hasDestinationType(
2642 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002643}
2644
Sam Panzer089e5b32012-08-16 16:58:10 +00002645TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2646 // This test creates an implicit const cast.
2647 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002648 implicitCastExpr(
2649 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002650 // This test creates an implicit array-to-pointer cast.
2651 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002652 implicitCastExpr(hasImplicitDestinationType(
2653 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002654}
2655
2656TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2657 // This test creates an implicit cast from int to char.
2658 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002659 implicitCastExpr(hasImplicitDestinationType(
2660 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002661 // This test creates an implicit array-to-pointer cast.
2662 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002663 implicitCastExpr(hasImplicitDestinationType(
2664 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002665}
2666
2667TEST(ImplicitCast, MatchesSimpleCase) {
2668 // This test creates an implicit const cast.
2669 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002670 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002671 // This test creates an implicit cast from int to char.
2672 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002673 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002674 // This test creates an implicit array-to-pointer cast.
2675 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002676 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002677}
2678
2679TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002680 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002681 // are present, and that it ignores explicit and paren casts.
2682
2683 // These two test cases have no casts.
2684 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002685 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002686 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002687 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002688
2689 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002690 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002691 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002692 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002693
2694 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002695 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002696}
2697
2698TEST(IgnoringImpCasts, MatchesImpCasts) {
2699 // This test checks that ignoringImpCasts matches when implicit casts are
2700 // present and its inner matcher alone does not match.
2701 // Note that this test creates an implicit const cast.
2702 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002703 varDecl(hasInitializer(ignoringImpCasts(
2704 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002705 // This test creates an implict cast from int to char.
2706 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002707 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002708 integerLiteral(equals(0)))))));
2709}
2710
2711TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2712 // These tests verify that ignoringImpCasts does not match if the inner
2713 // matcher does not match.
2714 // Note that the first test creates an implicit const cast.
2715 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002716 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002717 unless(anything()))))));
2718 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002719 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002720 unless(anything()))))));
2721
2722 // These tests verify that ignoringImplictCasts does not look through explicit
2723 // casts or parentheses.
2724 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002725 varDecl(hasInitializer(ignoringImpCasts(
2726 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002727 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002728 varDecl(hasInitializer(ignoringImpCasts(
2729 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002730 EXPECT_TRUE(notMatches("float i = (float)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("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002734 varDecl(hasInitializer(ignoringImpCasts(
2735 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002736}
2737
2738TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2739 // This test verifies that expressions that do not have implicit casts
2740 // still match the inner matcher.
2741 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002742 varDecl(hasInitializer(ignoringImpCasts(
2743 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002744}
2745
2746TEST(IgnoringParenCasts, MatchesParenCasts) {
2747 // This test checks that ignoringParenCasts matches when parentheses and/or
2748 // casts are present and its inner matcher alone does not match.
2749 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002750 varDecl(hasInitializer(ignoringParenCasts(
2751 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002752 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002753 varDecl(hasInitializer(ignoringParenCasts(
2754 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002755
2756 // This test creates an implict cast from int to char in addition to the
2757 // parentheses.
2758 EXPECT_TRUE(matches("char 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 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002763 varDecl(hasInitializer(ignoringParenCasts(
2764 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002765 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002766 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002767 integerLiteral(equals(0)))))));
2768}
2769
2770TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2771 // This test verifies that expressions that do not have any casts still match.
2772 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002773 varDecl(hasInitializer(ignoringParenCasts(
2774 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002775}
2776
2777TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2778 // These tests verify that ignoringImpCasts does not match if the inner
2779 // matcher does not match.
2780 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002781 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002782 unless(anything()))))));
2783
2784 // This test creates an implicit cast from int to char in addition to the
2785 // parentheses.
2786 EXPECT_TRUE(notMatches("char 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 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002791 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002792 unless(anything()))))));
2793}
2794
2795TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2796 // This test checks that ignoringParenAndImpCasts matches when
2797 // parentheses and/or implicit casts are present and its inner matcher alone
2798 // does not match.
2799 // Note that this test creates an implicit const cast.
2800 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002801 varDecl(hasInitializer(ignoringParenImpCasts(
2802 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002803 // This test creates an implicit cast from int to char.
2804 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002805 varDecl(hasInitializer(ignoringParenImpCasts(
2806 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002807}
2808
2809TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2810 // This test verifies that expressions that do not have parentheses or
2811 // implicit casts still match.
2812 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002813 varDecl(hasInitializer(ignoringParenImpCasts(
2814 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002815 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002816 varDecl(hasInitializer(ignoringParenImpCasts(
2817 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002818}
2819
2820TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2821 // These tests verify that ignoringParenImpCasts does not match if
2822 // the inner matcher does not match.
2823 // This test creates an implicit cast.
2824 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002825 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002826 unless(anything()))))));
2827 // These tests verify that ignoringParenAndImplictCasts does not look
2828 // through explicit casts.
2829 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002830 varDecl(hasInitializer(ignoringParenImpCasts(
2831 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002832 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002833 varDecl(hasInitializer(ignoringParenImpCasts(
2834 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002835 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002836 varDecl(hasInitializer(ignoringParenImpCasts(
2837 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002838}
2839
Manuel Klimek715c9562012-07-25 10:02:02 +00002840TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002841 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2842 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002843 implicitCastExpr(
2844 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002845}
2846
Manuel Klimek715c9562012-07-25 10:02:02 +00002847TEST(HasSourceExpression, MatchesExplicitCasts) {
2848 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002849 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002850 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002851 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002852}
2853
Manuel Klimek4da21662012-07-06 05:48:52 +00002854TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002855 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002856}
2857
2858TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002859 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002860}
2861
2862TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002863 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002864}
2865
2866TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002867 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002868}
2869
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002870TEST(InitListExpression, MatchesInitListExpression) {
2871 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2872 initListExpr(hasType(asString("int [2]")))));
2873 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002874 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002875}
2876
2877TEST(UsingDeclaration, MatchesUsingDeclarations) {
2878 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2879 usingDecl()));
2880}
2881
2882TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2883 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2884 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2885}
2886
2887TEST(UsingDeclaration, MatchesSpecificTarget) {
2888 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2889 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002890 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002891 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2892 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002893 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002894}
2895
2896TEST(UsingDeclaration, ThroughUsingDeclaration) {
2897 EXPECT_TRUE(matches(
2898 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002899 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002900 EXPECT_TRUE(notMatches(
2901 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002902 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002903}
2904
Sam Panzer425f41b2012-08-16 17:20:59 +00002905TEST(SingleDecl, IsSingleDecl) {
2906 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002907 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002908 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2909 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2910 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2911 SingleDeclStmt));
2912}
2913
2914TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002915 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002916
2917 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002918 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002919 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002920 declStmt(containsDeclaration(0, MatchesInit),
2921 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002922 unsigned WrongIndex = 42;
2923 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002924 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002925 MatchesInit))));
2926}
2927
2928TEST(DeclCount, DeclCountIsCorrect) {
2929 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002930 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002931 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002932 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002933 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002934 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002935}
2936
Manuel Klimek4da21662012-07-06 05:48:52 +00002937TEST(While, MatchesWhileLoops) {
2938 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2939 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2940 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2941}
2942
2943TEST(Do, MatchesDoLoops) {
2944 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2945 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2946}
2947
2948TEST(Do, DoesNotMatchWhileLoops) {
2949 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2950}
2951
2952TEST(SwitchCase, MatchesCase) {
2953 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2954 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2955 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2956 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2957}
2958
Daniel Jasperb54b7642012-09-20 14:12:57 +00002959TEST(SwitchCase, MatchesSwitch) {
2960 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2961 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2962 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2963 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2964}
2965
Peter Collingbourneacf02712013-05-10 11:52:02 +00002966TEST(SwitchCase, MatchesEachCase) {
2967 EXPECT_TRUE(notMatches("void x() { switch(42); }",
2968 switchStmt(forEachSwitchCase(caseStmt()))));
2969 EXPECT_TRUE(matches("void x() { switch(42) case 42:; }",
2970 switchStmt(forEachSwitchCase(caseStmt()))));
2971 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }",
2972 switchStmt(forEachSwitchCase(caseStmt()))));
2973 EXPECT_TRUE(notMatches(
2974 "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }",
2975 ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt()))))));
2976 EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }",
2977 switchStmt(forEachSwitchCase(
2978 caseStmt(hasCaseConstant(integerLiteral()))))));
2979 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }",
2980 switchStmt(forEachSwitchCase(
2981 caseStmt(hasCaseConstant(integerLiteral()))))));
2982 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }",
2983 switchStmt(forEachSwitchCase(
2984 caseStmt(hasCaseConstant(integerLiteral()))))));
2985 EXPECT_TRUE(matchAndVerifyResultTrue(
2986 "void x() { switch (42) { case 1: case 2: case 3: default:; } }",
2987 switchStmt(forEachSwitchCase(caseStmt().bind("x"))),
2988 new VerifyIdIsBoundTo<CaseStmt>("x", 3)));
2989}
2990
Manuel Klimek06963012013-07-19 11:50:54 +00002991TEST(ForEachConstructorInitializer, MatchesInitializers) {
2992 EXPECT_TRUE(matches(
2993 "struct X { X() : i(42), j(42) {} int i, j; };",
2994 constructorDecl(forEachConstructorInitializer(ctorInitializer()))));
2995}
2996
Daniel Jasperb54b7642012-09-20 14:12:57 +00002997TEST(ExceptionHandling, SimpleCases) {
2998 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2999 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
3000 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
3001 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
3002 throwExpr()));
3003 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
3004 throwExpr()));
3005}
3006
Manuel Klimek4da21662012-07-06 05:48:52 +00003007TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
3008 EXPECT_TRUE(notMatches(
3009 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003010 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003011 EXPECT_TRUE(notMatches(
3012 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003013 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003014}
3015
3016TEST(HasConditionVariableStatement, MatchesConditionVariables) {
3017 EXPECT_TRUE(matches(
3018 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003019 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003020}
3021
3022TEST(ForEach, BindsOneNode) {
3023 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003024 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003025 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003026}
3027
3028TEST(ForEach, BindsMultipleNodes) {
3029 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003030 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003031 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003032}
3033
3034TEST(ForEach, BindsRecursiveCombinations) {
3035 EXPECT_TRUE(matchAndVerifyResultTrue(
3036 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003037 recordDecl(hasName("C"),
3038 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003039 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003040}
3041
3042TEST(ForEachDescendant, BindsOneNode) {
3043 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003044 recordDecl(hasName("C"),
3045 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003046 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003047}
3048
Daniel Jasper5f684e92012-11-16 18:39:22 +00003049TEST(ForEachDescendant, NestedForEachDescendant) {
3050 DeclarationMatcher m = recordDecl(
3051 isDefinition(), decl().bind("x"), hasName("C"));
3052 EXPECT_TRUE(matchAndVerifyResultTrue(
3053 "class A { class B { class C {}; }; };",
3054 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
3055 new VerifyIdIsBoundTo<Decl>("x", "C")));
3056
Manuel Klimek054d0492013-06-19 15:42:45 +00003057 // Check that a partial match of 'm' that binds 'x' in the
3058 // first part of anyOf(m, anything()) will not overwrite the
3059 // binding created by the earlier binding in the hasDescendant.
3060 EXPECT_TRUE(matchAndVerifyResultTrue(
3061 "class A { class B { class C {}; }; };",
3062 recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
3063 new VerifyIdIsBoundTo<Decl>("x", "C")));
Daniel Jasper5f684e92012-11-16 18:39:22 +00003064}
3065
Manuel Klimek4da21662012-07-06 05:48:52 +00003066TEST(ForEachDescendant, BindsMultipleNodes) {
3067 EXPECT_TRUE(matchAndVerifyResultTrue(
3068 "class C { class D { int x; int y; }; "
3069 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003070 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003071 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003072}
3073
3074TEST(ForEachDescendant, BindsRecursiveCombinations) {
3075 EXPECT_TRUE(matchAndVerifyResultTrue(
3076 "class C { class D { "
3077 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003078 recordDecl(hasName("C"), forEachDescendant(recordDecl(
3079 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003080 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003081}
3082
Manuel Klimek054d0492013-06-19 15:42:45 +00003083TEST(ForEachDescendant, BindsCombinations) {
3084 EXPECT_TRUE(matchAndVerifyResultTrue(
3085 "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while "
3086 "(true) {} }",
3087 compoundStmt(forEachDescendant(ifStmt().bind("if")),
3088 forEachDescendant(whileStmt().bind("while"))),
3089 new VerifyIdIsBoundTo<IfStmt>("if", 6)));
3090}
3091
3092TEST(Has, DoesNotDeleteBindings) {
3093 EXPECT_TRUE(matchAndVerifyResultTrue(
3094 "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())),
3095 new VerifyIdIsBoundTo<Decl>("x", 1)));
3096}
3097
3098TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) {
3099 // Those matchers cover all the cases where an inner matcher is called
3100 // and there is not a 1:1 relationship between the match of the outer
3101 // matcher and the match of the inner matcher.
3102 // The pattern to look for is:
3103 // ... return InnerMatcher.matches(...); ...
3104 // In which case no special handling is needed.
3105 //
3106 // On the other hand, if there are multiple alternative matches
3107 // (for example forEach*) or matches might be discarded (for example has*)
3108 // the implementation must make sure that the discarded matches do not
3109 // affect the bindings.
3110 // When new such matchers are added, add a test here that:
3111 // - matches a simple node, and binds it as the first thing in the matcher:
3112 // recordDecl(decl().bind("x"), hasName("X")))
3113 // - uses the matcher under test afterwards in a way that not the first
3114 // alternative is matched; for anyOf, that means the first branch
3115 // would need to return false; for hasAncestor, it means that not
3116 // the direct parent matches the inner matcher.
3117
3118 EXPECT_TRUE(matchAndVerifyResultTrue(
3119 "class X { int y; };",
3120 recordDecl(
3121 recordDecl().bind("x"), hasName("::X"),
3122 anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())),
3123 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3124 EXPECT_TRUE(matchAndVerifyResultTrue(
3125 "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"),
3126 anyOf(unless(anything()), anything())),
3127 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3128 EXPECT_TRUE(matchAndVerifyResultTrue(
3129 "template<typename T1, typename T2> class X {}; X<float, int> x;",
3130 classTemplateSpecializationDecl(
3131 decl().bind("x"),
3132 hasAnyTemplateArgument(refersToType(asString("int")))),
3133 new VerifyIdIsBoundTo<Decl>("x", 1)));
3134 EXPECT_TRUE(matchAndVerifyResultTrue(
3135 "class X { void f(); void g(); };",
3136 recordDecl(decl().bind("x"), hasMethod(hasName("g"))),
3137 new VerifyIdIsBoundTo<Decl>("x", 1)));
3138 EXPECT_TRUE(matchAndVerifyResultTrue(
3139 "class X { X() : a(1), b(2) {} double a; int b; };",
3140 recordDecl(decl().bind("x"),
3141 has(constructorDecl(
3142 hasAnyConstructorInitializer(forField(hasName("b")))))),
3143 new VerifyIdIsBoundTo<Decl>("x", 1)));
3144 EXPECT_TRUE(matchAndVerifyResultTrue(
3145 "void x(int, int) { x(0, 42); }",
3146 callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))),
3147 new VerifyIdIsBoundTo<Expr>("x", 1)));
3148 EXPECT_TRUE(matchAndVerifyResultTrue(
3149 "void x(int, int y) {}",
3150 functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))),
3151 new VerifyIdIsBoundTo<Decl>("x", 1)));
3152 EXPECT_TRUE(matchAndVerifyResultTrue(
3153 "void x() { return; if (true) {} }",
3154 functionDecl(decl().bind("x"),
3155 has(compoundStmt(hasAnySubstatement(ifStmt())))),
3156 new VerifyIdIsBoundTo<Decl>("x", 1)));
3157 EXPECT_TRUE(matchAndVerifyResultTrue(
3158 "namespace X { void b(int); void b(); }"
3159 "using X::b;",
3160 usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl(
3161 functionDecl(parameterCountIs(1))))),
3162 new VerifyIdIsBoundTo<Decl>("x", 1)));
3163 EXPECT_TRUE(matchAndVerifyResultTrue(
3164 "class A{}; class B{}; class C : B, A {};",
3165 recordDecl(decl().bind("x"), isDerivedFrom("::A")),
3166 new VerifyIdIsBoundTo<Decl>("x", 1)));
3167 EXPECT_TRUE(matchAndVerifyResultTrue(
3168 "class A{}; typedef A B; typedef A C; typedef A D;"
3169 "class E : A {};",
3170 recordDecl(decl().bind("x"), isDerivedFrom("C")),
3171 new VerifyIdIsBoundTo<Decl>("x", 1)));
3172 EXPECT_TRUE(matchAndVerifyResultTrue(
3173 "class A { class B { void f() {} }; };",
3174 functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3175 new VerifyIdIsBoundTo<Decl>("x", 1)));
3176 EXPECT_TRUE(matchAndVerifyResultTrue(
3177 "template <typename T> struct A { struct B {"
3178 " void f() { if(true) {} }"
3179 "}; };"
3180 "void t() { A<int>::B b; b.f(); }",
3181 ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3182 new VerifyIdIsBoundTo<Stmt>("x", 2)));
3183 EXPECT_TRUE(matchAndVerifyResultTrue(
3184 "class A {};",
3185 recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))),
3186 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimek06963012013-07-19 11:50:54 +00003187 EXPECT_TRUE(matchAndVerifyResultTrue(
3188 "class A { A() : s(), i(42) {} const char *s; int i; };",
3189 constructorDecl(hasName("::A::A"), decl().bind("x"),
3190 forEachConstructorInitializer(forField(hasName("i")))),
3191 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimek054d0492013-06-19 15:42:45 +00003192}
3193
Daniel Jasper11c98772012-11-11 22:14:55 +00003194TEST(ForEachDescendant, BindsCorrectNodes) {
3195 EXPECT_TRUE(matchAndVerifyResultTrue(
3196 "class C { void f(); int i; };",
3197 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3198 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
3199 EXPECT_TRUE(matchAndVerifyResultTrue(
3200 "class C { void f() {} int i; };",
3201 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3202 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
3203}
3204
Manuel Klimek152ea0e2013-02-04 10:59:20 +00003205TEST(FindAll, BindsNodeOnMatch) {
3206 EXPECT_TRUE(matchAndVerifyResultTrue(
3207 "class A {};",
3208 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
3209 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
3210}
3211
3212TEST(FindAll, BindsDescendantNodeOnMatch) {
3213 EXPECT_TRUE(matchAndVerifyResultTrue(
3214 "class A { int a; int b; };",
3215 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
3216 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3217}
3218
3219TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
3220 EXPECT_TRUE(matchAndVerifyResultTrue(
3221 "class A { int a; int b; };",
3222 recordDecl(hasName("::A"),
3223 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
3224 fieldDecl().bind("v"))))),
3225 new VerifyIdIsBoundTo<Decl>("v", 3)));
3226
3227 EXPECT_TRUE(matchAndVerifyResultTrue(
3228 "class A { class B {}; class C {}; };",
3229 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
3230 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3231}
3232
Manuel Klimek73876732013-02-04 09:42:38 +00003233TEST(EachOf, TriggersForEachMatch) {
3234 EXPECT_TRUE(matchAndVerifyResultTrue(
3235 "class A { int a; int b; };",
3236 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3237 has(fieldDecl(hasName("b")).bind("v")))),
3238 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3239}
3240
3241TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3242 EXPECT_TRUE(matchAndVerifyResultTrue(
3243 "class A { int a; int c; };",
3244 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3245 has(fieldDecl(hasName("b")).bind("v")))),
3246 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3247 EXPECT_TRUE(matchAndVerifyResultTrue(
3248 "class A { int c; int b; };",
3249 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3250 has(fieldDecl(hasName("b")).bind("v")))),
3251 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3252 EXPECT_TRUE(notMatches(
3253 "class A { int c; int d; };",
3254 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3255 has(fieldDecl(hasName("b")).bind("v"))))));
3256}
Manuel Klimek4da21662012-07-06 05:48:52 +00003257
3258TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3259 // Make sure that we can both match the class by name (::X) and by the type
3260 // the template was instantiated with (via a field).
3261
3262 EXPECT_TRUE(matches(
3263 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003264 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003265
3266 EXPECT_TRUE(matches(
3267 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003268 recordDecl(isTemplateInstantiation(), hasDescendant(
3269 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003270}
3271
3272TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3273 EXPECT_TRUE(matches(
3274 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003275 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00003276 isTemplateInstantiation())));
3277}
3278
3279TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3280 EXPECT_TRUE(matches(
3281 "template <typename T> class X { T t; }; class A {};"
3282 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003283 recordDecl(isTemplateInstantiation(), hasDescendant(
3284 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003285}
3286
3287TEST(IsTemplateInstantiation,
3288 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3289 EXPECT_TRUE(matches(
3290 "template <typename T> class X {};"
3291 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003292 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003293}
3294
3295TEST(IsTemplateInstantiation,
3296 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3297 EXPECT_TRUE(matches(
3298 "class A {};"
3299 "class X {"
3300 " template <typename U> class Y { U u; };"
3301 " Y<A> y;"
3302 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003303 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003304}
3305
3306TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3307 // FIXME: Figure out whether this makes sense. It doesn't affect the
3308 // normal use case as long as the uppermost instantiation always is marked
3309 // as template instantiation, but it might be confusing as a predicate.
3310 EXPECT_TRUE(matches(
3311 "class A {};"
3312 "template <typename T> class X {"
3313 " template <typename U> class Y { U u; };"
3314 " Y<T> y;"
3315 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003316 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003317}
3318
3319TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3320 EXPECT_TRUE(notMatches(
3321 "template <typename T> class X {}; class A {};"
3322 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003323 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003324}
3325
3326TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3327 EXPECT_TRUE(notMatches(
3328 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003329 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003330}
3331
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003332TEST(IsExplicitTemplateSpecialization,
3333 DoesNotMatchPrimaryTemplate) {
3334 EXPECT_TRUE(notMatches(
3335 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003336 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003337 EXPECT_TRUE(notMatches(
3338 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003339 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003340}
3341
3342TEST(IsExplicitTemplateSpecialization,
3343 DoesNotMatchExplicitTemplateInstantiations) {
3344 EXPECT_TRUE(notMatches(
3345 "template <typename T> class X {};"
3346 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003347 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003348 EXPECT_TRUE(notMatches(
3349 "template <typename T> void f(T t) {}"
3350 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003351 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003352}
3353
3354TEST(IsExplicitTemplateSpecialization,
3355 DoesNotMatchImplicitTemplateInstantiations) {
3356 EXPECT_TRUE(notMatches(
3357 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003358 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003359 EXPECT_TRUE(notMatches(
3360 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003361 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003362}
3363
3364TEST(IsExplicitTemplateSpecialization,
3365 MatchesExplicitTemplateSpecializations) {
3366 EXPECT_TRUE(matches(
3367 "template <typename T> class X {};"
3368 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003369 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003370 EXPECT_TRUE(matches(
3371 "template <typename T> void f(T t) {}"
3372 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003373 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003374}
3375
Manuel Klimek579b1202012-09-07 09:26:10 +00003376TEST(HasAncenstor, MatchesDeclarationAncestors) {
3377 EXPECT_TRUE(matches(
3378 "class A { class B { class C {}; }; };",
3379 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3380}
3381
3382TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3383 EXPECT_TRUE(notMatches(
3384 "class A { class B { class C {}; }; };",
3385 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3386}
3387
3388TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3389 EXPECT_TRUE(matches(
3390 "class A { class B { void f() { C c; } class C {}; }; };",
3391 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3392 hasAncestor(recordDecl(hasName("A"))))))));
3393}
3394
3395TEST(HasAncenstor, MatchesStatementAncestors) {
3396 EXPECT_TRUE(matches(
3397 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003398 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003399}
3400
3401TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3402 EXPECT_TRUE(matches(
3403 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003404 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003405}
3406
3407TEST(HasAncestor, BindsRecursiveCombinations) {
3408 EXPECT_TRUE(matchAndVerifyResultTrue(
3409 "class C { class D { class E { class F { int y; }; }; }; };",
3410 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003411 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00003412}
3413
3414TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3415 EXPECT_TRUE(matchAndVerifyResultTrue(
3416 "class C { class D { class E { class F { int y; }; }; }; };",
3417 fieldDecl(hasAncestor(
3418 decl(
3419 hasDescendant(recordDecl(isDefinition(),
3420 hasAncestor(recordDecl())))
3421 ).bind("d")
3422 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00003423 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00003424}
3425
Manuel Klimek374516c2013-03-14 16:33:21 +00003426TEST(HasAncestor, MatchesClosestAncestor) {
3427 EXPECT_TRUE(matchAndVerifyResultTrue(
3428 "template <typename T> struct C {"
3429 " void f(int) {"
3430 " struct I { void g(T) { int x; } } i; i.g(42);"
3431 " }"
3432 "};"
3433 "template struct C<int>;",
3434 varDecl(hasName("x"),
3435 hasAncestor(functionDecl(hasParameter(
3436 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"),
3437 new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2)));
3438}
3439
Manuel Klimek579b1202012-09-07 09:26:10 +00003440TEST(HasAncestor, MatchesInTemplateInstantiations) {
3441 EXPECT_TRUE(matches(
3442 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3443 "A<int>::B::C a;",
3444 fieldDecl(hasType(asString("int")),
3445 hasAncestor(recordDecl(hasName("A"))))));
3446}
3447
3448TEST(HasAncestor, MatchesInImplicitCode) {
3449 EXPECT_TRUE(matches(
3450 "struct X {}; struct A { A() {} X x; };",
3451 constructorDecl(
3452 hasAnyConstructorInitializer(withInitializer(expr(
3453 hasAncestor(recordDecl(hasName("A")))))))));
3454}
3455
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003456TEST(HasParent, MatchesOnlyParent) {
3457 EXPECT_TRUE(matches(
3458 "void f() { if (true) { int x = 42; } }",
3459 compoundStmt(hasParent(ifStmt()))));
3460 EXPECT_TRUE(notMatches(
3461 "void f() { for (;;) { int x = 42; } }",
3462 compoundStmt(hasParent(ifStmt()))));
3463 EXPECT_TRUE(notMatches(
3464 "void f() { if (true) for (;;) { int x = 42; } }",
3465 compoundStmt(hasParent(ifStmt()))));
3466}
3467
Manuel Klimek30ace372012-12-06 14:42:48 +00003468TEST(HasAncestor, MatchesAllAncestors) {
3469 EXPECT_TRUE(matches(
3470 "template <typename T> struct C { static void f() { 42; } };"
3471 "void t() { C<int>::f(); }",
3472 integerLiteral(
3473 equals(42),
3474 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3475 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3476}
3477
3478TEST(HasParent, MatchesAllParents) {
3479 EXPECT_TRUE(matches(
3480 "template <typename T> struct C { static void f() { 42; } };"
3481 "void t() { C<int>::f(); }",
3482 integerLiteral(
3483 equals(42),
3484 hasParent(compoundStmt(hasParent(functionDecl(
3485 hasParent(recordDecl(isTemplateInstantiation())))))))));
3486 EXPECT_TRUE(matches(
3487 "template <typename T> struct C { static void f() { 42; } };"
3488 "void t() { C<int>::f(); }",
3489 integerLiteral(
3490 equals(42),
3491 hasParent(compoundStmt(hasParent(functionDecl(
3492 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3493 EXPECT_TRUE(matches(
3494 "template <typename T> struct C { static void f() { 42; } };"
3495 "void t() { C<int>::f(); }",
3496 integerLiteral(equals(42),
3497 hasParent(compoundStmt(allOf(
3498 hasParent(functionDecl(
3499 hasParent(recordDecl(isTemplateInstantiation())))),
3500 hasParent(functionDecl(hasParent(recordDecl(
3501 unless(isTemplateInstantiation())))))))))));
Manuel Klimek374516c2013-03-14 16:33:21 +00003502 EXPECT_TRUE(
3503 notMatches("template <typename T> struct C { static void f() {} };"
3504 "void t() { C<int>::f(); }",
3505 compoundStmt(hasParent(recordDecl()))));
Manuel Klimek30ace372012-12-06 14:42:48 +00003506}
3507
Daniel Jasperce620072012-10-17 08:52:59 +00003508TEST(TypeMatching, MatchesTypes) {
3509 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3510}
3511
3512TEST(TypeMatching, MatchesArrayTypes) {
3513 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3514 EXPECT_TRUE(matches("int a[42];", arrayType()));
3515 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3516
3517 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3518 arrayType(hasElementType(builtinType()))));
3519
3520 EXPECT_TRUE(matches(
3521 "int const a[] = { 2, 3 };",
3522 qualType(arrayType(hasElementType(builtinType())))));
3523 EXPECT_TRUE(matches(
3524 "int const a[] = { 2, 3 };",
3525 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3526 EXPECT_TRUE(matches(
3527 "typedef const int T; T x[] = { 1, 2 };",
3528 qualType(isConstQualified(), arrayType())));
3529
3530 EXPECT_TRUE(notMatches(
3531 "int a[] = { 2, 3 };",
3532 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3533 EXPECT_TRUE(notMatches(
3534 "int a[] = { 2, 3 };",
3535 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3536 EXPECT_TRUE(notMatches(
3537 "int const a[] = { 2, 3 };",
3538 qualType(arrayType(hasElementType(builtinType())),
3539 unless(isConstQualified()))));
3540
3541 EXPECT_TRUE(matches("int a[2];",
3542 constantArrayType(hasElementType(builtinType()))));
3543 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3544}
3545
3546TEST(TypeMatching, MatchesComplexTypes) {
3547 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3548 EXPECT_TRUE(matches(
3549 "_Complex float f;",
3550 complexType(hasElementType(builtinType()))));
3551 EXPECT_TRUE(notMatches(
3552 "_Complex float f;",
3553 complexType(hasElementType(isInteger()))));
3554}
3555
3556TEST(TypeMatching, MatchesConstantArrayTypes) {
3557 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3558 EXPECT_TRUE(notMatches(
3559 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3560 constantArrayType(hasElementType(builtinType()))));
3561
3562 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3563 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3564 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3565}
3566
3567TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3568 EXPECT_TRUE(matches(
3569 "template <typename T, int Size> class array { T data[Size]; };",
3570 dependentSizedArrayType()));
3571 EXPECT_TRUE(notMatches(
3572 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3573 dependentSizedArrayType()));
3574}
3575
3576TEST(TypeMatching, MatchesIncompleteArrayType) {
3577 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3578 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3579
3580 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3581 incompleteArrayType()));
3582}
3583
3584TEST(TypeMatching, MatchesVariableArrayType) {
3585 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3586 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3587
3588 EXPECT_TRUE(matches(
3589 "void f(int b) { int a[b]; }",
3590 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3591 varDecl(hasName("b")))))))));
3592}
3593
3594TEST(TypeMatching, MatchesAtomicTypes) {
3595 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3596
3597 EXPECT_TRUE(matches("_Atomic(int) i;",
3598 atomicType(hasValueType(isInteger()))));
3599 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3600 atomicType(hasValueType(isInteger()))));
3601}
3602
3603TEST(TypeMatching, MatchesAutoTypes) {
3604 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3605 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3606 autoType()));
3607
Richard Smith9b131752013-04-30 21:23:01 +00003608 // FIXME: Matching against the type-as-written can't work here, because the
3609 // type as written was not deduced.
3610 //EXPECT_TRUE(matches("auto a = 1;",
3611 // autoType(hasDeducedType(isInteger()))));
3612 //EXPECT_TRUE(notMatches("auto b = 2.0;",
3613 // autoType(hasDeducedType(isInteger()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003614}
3615
Daniel Jaspera267cf62012-10-29 10:14:44 +00003616TEST(TypeMatching, MatchesFunctionTypes) {
3617 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3618 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3619}
3620
Edwin Vane88be2fd2013-04-01 18:33:34 +00003621TEST(TypeMatching, MatchesParenType) {
3622 EXPECT_TRUE(
3623 matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
3624 EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
3625
3626 EXPECT_TRUE(matches(
3627 "int (*ptr_to_func)(int);",
3628 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3629 EXPECT_TRUE(notMatches(
3630 "int (*ptr_to_array)[4];",
3631 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3632}
3633
Daniel Jasperce620072012-10-17 08:52:59 +00003634TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003635 // FIXME: Reactive when these tests can be more specific (not matching
3636 // implicit code on certain platforms), likely when we have hasDescendant for
3637 // Types/TypeLocs.
3638 //EXPECT_TRUE(matchAndVerifyResultTrue(
3639 // "int* a;",
3640 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3641 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3642 //EXPECT_TRUE(matchAndVerifyResultTrue(
3643 // "int* a;",
3644 // pointerTypeLoc().bind("loc"),
3645 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003646 EXPECT_TRUE(matches(
3647 "int** a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003648 loc(pointerType(pointee(qualType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003649 EXPECT_TRUE(matches(
3650 "int** a;",
3651 loc(pointerType(pointee(pointerType())))));
3652 EXPECT_TRUE(matches(
3653 "int* b; int* * const a = &b;",
3654 loc(qualType(isConstQualified(), pointerType()))));
3655
3656 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003657 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3658 hasType(blockPointerType()))));
3659 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3660 hasType(memberPointerType()))));
3661 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3662 hasType(pointerType()))));
3663 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3664 hasType(referenceType()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003665 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3666 hasType(lValueReferenceType()))));
3667 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3668 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003669
Daniel Jasper1802daf2012-10-17 13:35:36 +00003670 Fragment = "int *ptr;";
3671 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3672 hasType(blockPointerType()))));
3673 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3674 hasType(memberPointerType()))));
3675 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3676 hasType(pointerType()))));
3677 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3678 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003679
Daniel Jasper1802daf2012-10-17 13:35:36 +00003680 Fragment = "int a; int &ref = a;";
3681 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3682 hasType(blockPointerType()))));
3683 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3684 hasType(memberPointerType()))));
3685 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3686 hasType(pointerType()))));
3687 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3688 hasType(referenceType()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003689 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3690 hasType(lValueReferenceType()))));
3691 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3692 hasType(rValueReferenceType()))));
3693
3694 Fragment = "int &&ref = 2;";
3695 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3696 hasType(blockPointerType()))));
3697 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3698 hasType(memberPointerType()))));
3699 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3700 hasType(pointerType()))));
3701 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3702 hasType(referenceType()))));
3703 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3704 hasType(lValueReferenceType()))));
3705 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3706 hasType(rValueReferenceType()))));
3707}
3708
3709TEST(TypeMatching, AutoRefTypes) {
3710 std::string Fragment = "auto a = 1;"
3711 "auto b = a;"
3712 "auto &c = a;"
3713 "auto &&d = c;"
3714 "auto &&e = 2;";
3715 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
3716 hasType(referenceType()))));
3717 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
3718 hasType(referenceType()))));
3719 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3720 hasType(referenceType()))));
3721 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3722 hasType(lValueReferenceType()))));
3723 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
3724 hasType(rValueReferenceType()))));
3725 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3726 hasType(referenceType()))));
3727 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3728 hasType(lValueReferenceType()))));
3729 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
3730 hasType(rValueReferenceType()))));
3731 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3732 hasType(referenceType()))));
3733 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
3734 hasType(lValueReferenceType()))));
3735 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3736 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003737}
3738
3739TEST(TypeMatching, PointeeTypes) {
3740 EXPECT_TRUE(matches("int b; int &a = b;",
3741 referenceType(pointee(builtinType()))));
3742 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3743
3744 EXPECT_TRUE(matches("int *a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003745 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003746
3747 EXPECT_TRUE(matches(
3748 "int const *A;",
3749 pointerType(pointee(isConstQualified(), builtinType()))));
3750 EXPECT_TRUE(notMatches(
3751 "int *A;",
3752 pointerType(pointee(isConstQualified(), builtinType()))));
3753}
3754
3755TEST(TypeMatching, MatchesPointersToConstTypes) {
3756 EXPECT_TRUE(matches("int b; int * const a = &b;",
3757 loc(pointerType())));
3758 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003759 loc(pointerType())));
Daniel Jasperce620072012-10-17 08:52:59 +00003760 EXPECT_TRUE(matches(
3761 "int b; const int * a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003762 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003763 EXPECT_TRUE(matches(
3764 "int b; const int * a = &b;",
3765 pointerType(pointee(builtinType()))));
3766}
3767
3768TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003769 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3770 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003771}
3772
Edwin Vane3abf7782013-02-25 14:49:29 +00003773TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vane742d9e72013-02-25 20:43:32 +00003774 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vane3abf7782013-02-25 14:49:29 +00003775 templateSpecializationType()));
3776}
3777
Edwin Vane742d9e72013-02-25 20:43:32 +00003778TEST(TypeMatching, MatchesRecordType) {
3779 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek0cc798f2013-02-27 11:56:58 +00003780 EXPECT_TRUE(matches("struct S{}; S s;",
3781 recordType(hasDeclaration(recordDecl(hasName("S"))))));
3782 EXPECT_TRUE(notMatches("int i;",
3783 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003784}
3785
3786TEST(TypeMatching, MatchesElaboratedType) {
3787 EXPECT_TRUE(matches(
3788 "namespace N {"
3789 " namespace M {"
3790 " class D {};"
3791 " }"
3792 "}"
3793 "N::M::D d;", elaboratedType()));
3794 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
3795 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
3796}
3797
3798TEST(ElaboratedTypeNarrowing, hasQualifier) {
3799 EXPECT_TRUE(matches(
3800 "namespace N {"
3801 " namespace M {"
3802 " class D {};"
3803 " }"
3804 "}"
3805 "N::M::D d;",
3806 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3807 EXPECT_TRUE(notMatches(
3808 "namespace M {"
3809 " class D {};"
3810 "}"
3811 "M::D d;",
3812 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vaneaec89ac2013-03-04 17:51:00 +00003813 EXPECT_TRUE(notMatches(
3814 "struct D {"
3815 "} d;",
3816 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003817}
3818
3819TEST(ElaboratedTypeNarrowing, namesType) {
3820 EXPECT_TRUE(matches(
3821 "namespace N {"
3822 " namespace M {"
3823 " class D {};"
3824 " }"
3825 "}"
3826 "N::M::D d;",
3827 elaboratedType(elaboratedType(namesType(recordType(
3828 hasDeclaration(namedDecl(hasName("D")))))))));
3829 EXPECT_TRUE(notMatches(
3830 "namespace M {"
3831 " class D {};"
3832 "}"
3833 "M::D d;",
3834 elaboratedType(elaboratedType(namesType(typedefType())))));
3835}
3836
Daniel Jaspera7564432012-09-13 13:11:25 +00003837TEST(NNS, MatchesNestedNameSpecifiers) {
3838 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3839 nestedNameSpecifier()));
3840 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3841 nestedNameSpecifier()));
3842 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3843 nestedNameSpecifier()));
3844
3845 EXPECT_TRUE(matches(
3846 "struct A { static void f() {} }; void g() { A::f(); }",
3847 nestedNameSpecifier()));
3848 EXPECT_TRUE(notMatches(
3849 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3850 nestedNameSpecifier()));
3851}
3852
Daniel Jasperb54b7642012-09-20 14:12:57 +00003853TEST(NullStatement, SimpleCases) {
3854 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3855 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3856}
3857
Daniel Jaspera7564432012-09-13 13:11:25 +00003858TEST(NNS, MatchesTypes) {
3859 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3860 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3861 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3862 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3863 Matcher));
3864 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3865}
3866
3867TEST(NNS, MatchesNamespaceDecls) {
3868 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3869 specifiesNamespace(hasName("ns")));
3870 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3871 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3872 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3873}
3874
3875TEST(NNS, BindsNestedNameSpecifiers) {
3876 EXPECT_TRUE(matchAndVerifyResultTrue(
3877 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3878 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3879 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3880}
3881
3882TEST(NNS, BindsNestedNameSpecifierLocs) {
3883 EXPECT_TRUE(matchAndVerifyResultTrue(
3884 "namespace ns { struct B {}; } ns::B b;",
3885 loc(nestedNameSpecifier()).bind("loc"),
3886 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3887}
3888
3889TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3890 EXPECT_TRUE(matches(
3891 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3892 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3893 EXPECT_TRUE(matches(
3894 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003895 nestedNameSpecifierLoc(hasPrefix(
3896 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003897}
3898
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003899TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3900 std::string Fragment =
3901 "namespace a { struct A { struct B { struct C {}; }; }; };"
3902 "void f() { a::A::B::C c; }";
3903 EXPECT_TRUE(matches(
3904 Fragment,
3905 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3906 hasDescendant(nestedNameSpecifier(
3907 specifiesNamespace(hasName("a")))))));
3908 EXPECT_TRUE(notMatches(
3909 Fragment,
3910 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3911 has(nestedNameSpecifier(
3912 specifiesNamespace(hasName("a")))))));
3913 EXPECT_TRUE(matches(
3914 Fragment,
3915 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3916 has(nestedNameSpecifier(
3917 specifiesNamespace(hasName("a")))))));
3918
3919 // Not really useful because a NestedNameSpecifier can af at most one child,
3920 // but to complete the interface.
3921 EXPECT_TRUE(matchAndVerifyResultTrue(
3922 Fragment,
3923 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3924 forEach(nestedNameSpecifier().bind("x"))),
3925 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3926}
3927
3928TEST(NNS, NestedNameSpecifiersAsDescendants) {
3929 std::string Fragment =
3930 "namespace a { struct A { struct B { struct C {}; }; }; };"
3931 "void f() { a::A::B::C c; }";
3932 EXPECT_TRUE(matches(
3933 Fragment,
3934 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3935 asString("struct a::A")))))));
3936 EXPECT_TRUE(matchAndVerifyResultTrue(
3937 Fragment,
3938 functionDecl(hasName("f"),
3939 forEachDescendant(nestedNameSpecifier().bind("x"))),
3940 // Nested names: a, a::A and a::A::B.
3941 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3942}
3943
3944TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3945 std::string Fragment =
3946 "namespace a { struct A { struct B { struct C {}; }; }; };"
3947 "void f() { a::A::B::C c; }";
3948 EXPECT_TRUE(matches(
3949 Fragment,
3950 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3951 hasDescendant(loc(nestedNameSpecifier(
3952 specifiesNamespace(hasName("a"))))))));
3953 EXPECT_TRUE(notMatches(
3954 Fragment,
3955 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3956 has(loc(nestedNameSpecifier(
3957 specifiesNamespace(hasName("a"))))))));
3958 EXPECT_TRUE(matches(
3959 Fragment,
3960 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3961 has(loc(nestedNameSpecifier(
3962 specifiesNamespace(hasName("a"))))))));
3963
3964 EXPECT_TRUE(matchAndVerifyResultTrue(
3965 Fragment,
3966 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3967 forEach(nestedNameSpecifierLoc().bind("x"))),
3968 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3969}
3970
3971TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3972 std::string Fragment =
3973 "namespace a { struct A { struct B { struct C {}; }; }; };"
3974 "void f() { a::A::B::C c; }";
3975 EXPECT_TRUE(matches(
3976 Fragment,
3977 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3978 asString("struct a::A"))))))));
3979 EXPECT_TRUE(matchAndVerifyResultTrue(
3980 Fragment,
3981 functionDecl(hasName("f"),
3982 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3983 // Nested names: a, a::A and a::A::B.
3984 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3985}
3986
Manuel Klimek60969f52013-02-01 13:41:35 +00003987template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003988public:
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003989 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
3990 StringRef InnerId)
3991 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jasper452abbc2012-10-29 10:48:25 +00003992 }
3993
Manuel Klimek60969f52013-02-01 13:41:35 +00003994 virtual bool run(const BoundNodes *Nodes) { return false; }
3995
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003996 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3997 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003998 return selectFirst<const T>(InnerId,
3999 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004000 }
4001private:
4002 std::string Id;
4003 internal::Matcher<T> InnerMatcher;
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004004 std::string InnerId;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004005};
4006
4007TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00004008 EXPECT_TRUE(matchAndVerifyResultTrue(
4009 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4010 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004011 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
4012 "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004013 EXPECT_TRUE(matchAndVerifyResultFalse(
4014 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4015 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004016 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
4017 "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004018}
4019
4020TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00004021 EXPECT_TRUE(matchAndVerifyResultTrue(
4022 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004023 new VerifyMatchOnNode<clang::Stmt>(
4024 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004025 EXPECT_TRUE(matchAndVerifyResultFalse(
4026 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004027 new VerifyMatchOnNode<clang::Stmt>(
4028 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004029}
4030
4031TEST(MatchFinder, CanMatchSingleNodesRecursively) {
4032 EXPECT_TRUE(matchAndVerifyResultTrue(
4033 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4034 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004035 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004036 EXPECT_TRUE(matchAndVerifyResultFalse(
4037 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4038 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004039 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004040}
4041
Manuel Klimekfa37c5c2013-02-07 12:42:10 +00004042template <typename T>
4043class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
4044public:
4045 virtual bool run(const BoundNodes *Nodes) { return false; }
4046
4047 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4048 const T *Node = Nodes->getNodeAs<T>("");
4049 return verify(*Nodes, *Context, Node);
4050 }
4051
4052 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
4053 return selectFirst<const T>(
4054 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
4055 *Node, Context)) != NULL;
4056 }
4057 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
4058 return selectFirst<const T>(
4059 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
4060 *Node, Context)) != NULL;
4061 }
4062};
4063
4064TEST(IsEqualTo, MatchesNodesByIdentity) {
4065 EXPECT_TRUE(matchAndVerifyResultTrue(
4066 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
4067 new VerifyAncestorHasChildIsEqual<Decl>()));
4068 EXPECT_TRUE(
4069 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
4070 new VerifyAncestorHasChildIsEqual<Stmt>()));
4071}
4072
Manuel Klimeke5793282012-11-02 01:31:03 +00004073class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
4074public:
4075 VerifyStartOfTranslationUnit() : Called(false) {}
4076 virtual void run(const MatchFinder::MatchResult &Result) {
4077 EXPECT_TRUE(Called);
4078 }
4079 virtual void onStartOfTranslationUnit() {
4080 Called = true;
4081 }
4082 bool Called;
4083};
4084
4085TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
4086 MatchFinder Finder;
4087 VerifyStartOfTranslationUnit VerifyCallback;
4088 Finder.addMatcher(decl(), &VerifyCallback);
4089 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4090 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4091 EXPECT_TRUE(VerifyCallback.Called);
4092}
4093
Peter Collingbourne8f9e5902013-05-28 19:21:51 +00004094class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback {
4095public:
4096 VerifyEndOfTranslationUnit() : Called(false) {}
4097 virtual void run(const MatchFinder::MatchResult &Result) {
4098 EXPECT_FALSE(Called);
4099 }
4100 virtual void onEndOfTranslationUnit() {
4101 Called = true;
4102 }
4103 bool Called;
4104};
4105
4106TEST(MatchFinder, InterceptsEndOfTranslationUnit) {
4107 MatchFinder Finder;
4108 VerifyEndOfTranslationUnit VerifyCallback;
4109 Finder.addMatcher(decl(), &VerifyCallback);
4110 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4111 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4112 EXPECT_TRUE(VerifyCallback.Called);
4113}
4114
Manuel Klimekcf52ca62013-06-20 14:06:32 +00004115TEST(EqualsBoundNodeMatcher, QualType) {
4116 EXPECT_TRUE(matches(
4117 "int i = 1;", varDecl(hasType(qualType().bind("type")),
4118 hasInitializer(ignoringParenImpCasts(
4119 hasType(qualType(equalsBoundNode("type"))))))));
4120 EXPECT_TRUE(notMatches("int i = 1.f;",
4121 varDecl(hasType(qualType().bind("type")),
4122 hasInitializer(ignoringParenImpCasts(hasType(
4123 qualType(equalsBoundNode("type"))))))));
4124}
4125
4126TEST(EqualsBoundNodeMatcher, NonMatchingTypes) {
4127 EXPECT_TRUE(notMatches(
4128 "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"),
4129 hasInitializer(ignoringParenImpCasts(
4130 hasType(qualType(equalsBoundNode("type"))))))));
4131}
4132
4133TEST(EqualsBoundNodeMatcher, Stmt) {
4134 EXPECT_TRUE(
4135 matches("void f() { if(true) {} }",
4136 stmt(allOf(ifStmt().bind("if"),
4137 hasParent(stmt(has(stmt(equalsBoundNode("if")))))))));
4138
4139 EXPECT_TRUE(notMatches(
4140 "void f() { if(true) { if (true) {} } }",
4141 stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if")))))));
4142}
4143
4144TEST(EqualsBoundNodeMatcher, Decl) {
4145 EXPECT_TRUE(matches(
4146 "class X { class Y {}; };",
4147 decl(allOf(recordDecl(hasName("::X::Y")).bind("record"),
4148 hasParent(decl(has(decl(equalsBoundNode("record")))))))));
4149
4150 EXPECT_TRUE(notMatches("class X { class Y {}; };",
4151 decl(allOf(recordDecl(hasName("::X")).bind("record"),
4152 has(decl(equalsBoundNode("record")))))));
4153}
4154
4155TEST(EqualsBoundNodeMatcher, Type) {
4156 EXPECT_TRUE(matches(
4157 "class X { int a; int b; };",
4158 recordDecl(
4159 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4160 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4161
4162 EXPECT_TRUE(notMatches(
4163 "class X { int a; double b; };",
4164 recordDecl(
4165 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4166 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4167}
4168
4169TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) {
4170
4171 EXPECT_TRUE(matchAndVerifyResultTrue(
4172 "int f() {"
4173 " if (1) {"
4174 " int i = 9;"
4175 " }"
4176 " int j = 10;"
4177 " {"
4178 " float k = 9.0;"
4179 " }"
4180 " return 0;"
4181 "}",
4182 // Look for variable declarations within functions whose type is the same
4183 // as the function return type.
4184 functionDecl(returns(qualType().bind("type")),
4185 forEachDescendant(varDecl(hasType(
4186 qualType(equalsBoundNode("type")))).bind("decl"))),
4187 // Only i and j should match, not k.
4188 new VerifyIdIsBoundTo<VarDecl>("decl", 2)));
4189}
4190
4191TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) {
4192 EXPECT_TRUE(matchAndVerifyResultTrue(
4193 "void f() {"
4194 " int x;"
4195 " double d;"
4196 " x = d + x - d + x;"
4197 "}",
4198 functionDecl(
4199 hasName("f"), forEachDescendant(varDecl().bind("d")),
4200 forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))),
4201 new VerifyIdIsBoundTo<VarDecl>("d", 5)));
4202}
4203
Manuel Klimek4da21662012-07-06 05:48:52 +00004204} // end namespace ast_matchers
4205} // end namespace clang