blob: 4994192bda334a44e98717369786862449d0790e [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));
1364 EXPECT_TRUE(
1365 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1366 EXPECT_TRUE(
1367 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1368}
1369
Edwin Vane6a19a972013-03-06 17:02:57 +00001370TEST(QualType, hasCanonicalType) {
1371 EXPECT_TRUE(notMatches("typedef int &int_ref;"
1372 "int a;"
1373 "int_ref b = a;",
1374 varDecl(hasType(qualType(referenceType())))));
1375 EXPECT_TRUE(
1376 matches("typedef int &int_ref;"
1377 "int a;"
1378 "int_ref b = a;",
1379 varDecl(hasType(qualType(hasCanonicalType(referenceType()))))));
1380}
1381
Edwin Vane7b69cd02013-04-02 18:15:55 +00001382TEST(QualType, hasLocalQualifiers) {
1383 EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;",
1384 varDecl(hasType(hasLocalQualifiers()))));
1385 EXPECT_TRUE(matches("int *const j = nullptr;",
1386 varDecl(hasType(hasLocalQualifiers()))));
1387 EXPECT_TRUE(matches("int *volatile k;",
1388 varDecl(hasType(hasLocalQualifiers()))));
1389 EXPECT_TRUE(notMatches("int m;",
1390 varDecl(hasType(hasLocalQualifiers()))));
1391}
1392
Manuel Klimek4da21662012-07-06 05:48:52 +00001393TEST(HasParameter, CallsInnerMatcher) {
1394 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001395 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001396 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001397 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001398}
1399
1400TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1401 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001402 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001403}
1404
1405TEST(HasType, MatchesParameterVariableTypesStrictly) {
1406 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001407 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001408 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001409 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001410 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001411 methodDecl(hasParameter(0,
1412 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001413 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001414 methodDecl(hasParameter(0,
1415 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001416}
1417
1418TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1419 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001420 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001421 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001422 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001423}
1424
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001425TEST(Returns, MatchesReturnTypes) {
1426 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001427 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001428 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001429 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001430 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001431 functionDecl(returns(hasDeclaration(
1432 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001433}
1434
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001435TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001436 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1437 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1438 functionDecl(isExternC())));
1439 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001440}
1441
Manuel Klimek4da21662012-07-06 05:48:52 +00001442TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1443 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001444 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001445}
1446
1447TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1448 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001449 methodDecl(hasAnyParameter(hasType(pointsTo(
1450 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001451}
1452
1453TEST(HasName, MatchesParameterVariableDeclartions) {
1454 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001455 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001456 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001457 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001458}
1459
Daniel Jasper371f9392012-08-01 08:40:24 +00001460TEST(Matcher, MatchesClassTemplateSpecialization) {
1461 EXPECT_TRUE(matches("template<typename T> struct A {};"
1462 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001463 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001464 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001465 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001466 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001467 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001468}
1469
Manuel Klimek1a68afd2013-06-20 13:08:29 +00001470TEST(DeclaratorDecl, MatchesDeclaratorDecls) {
1471 EXPECT_TRUE(matches("int x;", declaratorDecl()));
1472 EXPECT_TRUE(notMatches("class A {};", declaratorDecl()));
1473}
1474
1475TEST(ParmVarDecl, MatchesParmVars) {
1476 EXPECT_TRUE(matches("void f(int x);", parmVarDecl()));
1477 EXPECT_TRUE(notMatches("void f();", parmVarDecl()));
1478}
1479
Daniel Jasper371f9392012-08-01 08:40:24 +00001480TEST(Matcher, MatchesTypeTemplateArgument) {
1481 EXPECT_TRUE(matches(
1482 "template<typename T> struct B {};"
1483 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001484 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001485 asString("int"))))));
1486}
1487
1488TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1489 EXPECT_TRUE(matches(
1490 "struct B { int next; };"
1491 "template<int(B::*next_ptr)> struct A {};"
1492 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001493 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1494 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001495
1496 EXPECT_TRUE(notMatches(
1497 "template <typename T> struct A {};"
1498 "A<int> a;",
1499 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1500 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001501}
1502
1503TEST(Matcher, MatchesSpecificArgument) {
1504 EXPECT_TRUE(matches(
1505 "template<typename T, typename U> class A {};"
1506 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001507 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001508 1, refersToType(asString("int"))))));
1509 EXPECT_TRUE(notMatches(
1510 "template<typename T, typename U> class A {};"
1511 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001512 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001513 1, refersToType(asString("int"))))));
1514}
1515
Daniel Jasperf3197e92013-02-25 12:02:08 +00001516TEST(Matcher, MatchesAccessSpecDecls) {
1517 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1518 EXPECT_TRUE(
1519 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1520 EXPECT_TRUE(
1521 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1522 EXPECT_TRUE(
1523 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1524
1525 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1526}
1527
Edwin Vane5771a2f2013-04-09 20:46:36 +00001528TEST(Matcher, MatchesVirtualMethod) {
1529 EXPECT_TRUE(matches("class X { virtual int f(); };",
1530 methodDecl(isVirtual(), hasName("::X::f"))));
1531 EXPECT_TRUE(notMatches("class X { int f(); };",
1532 methodDecl(isVirtual())));
1533}
1534
Edwin Vane32a6ebc2013-05-09 17:00:17 +00001535TEST(Matcher, MatchesConstMethod) {
1536 EXPECT_TRUE(matches("struct A { void foo() const; };",
1537 methodDecl(isConst())));
1538 EXPECT_TRUE(notMatches("struct A { void foo(); };",
1539 methodDecl(isConst())));
1540}
1541
Edwin Vane5771a2f2013-04-09 20:46:36 +00001542TEST(Matcher, MatchesOverridingMethod) {
1543 EXPECT_TRUE(matches("class X { virtual int f(); }; "
1544 "class Y : public X { int f(); };",
1545 methodDecl(isOverride(), hasName("::Y::f"))));
1546 EXPECT_TRUE(notMatches("class X { virtual int f(); }; "
1547 "class Y : public X { int f(); };",
1548 methodDecl(isOverride(), hasName("::X::f"))));
1549 EXPECT_TRUE(notMatches("class X { int f(); }; "
1550 "class Y : public X { int f(); };",
1551 methodDecl(isOverride())));
1552 EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ",
1553 methodDecl(isOverride())));
1554}
1555
Manuel Klimek4da21662012-07-06 05:48:52 +00001556TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001557 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001558
1559 EXPECT_TRUE(
1560 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1561 EXPECT_TRUE(
1562 matches("class X { public: X(); }; void x() { X x = X(); }",
1563 Constructor));
1564 EXPECT_TRUE(
1565 matches("class X { public: X(int); }; void x() { X x = 0; }",
1566 Constructor));
1567 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1568}
1569
1570TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001571 StatementMatcher Constructor = constructExpr(
1572 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001573
1574 EXPECT_TRUE(
1575 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1576 Constructor));
1577 EXPECT_TRUE(
1578 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1579 Constructor));
1580 EXPECT_TRUE(
1581 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1582 Constructor));
1583 EXPECT_TRUE(
1584 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1585 Constructor));
1586
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001587 StatementMatcher WrongIndex = constructExpr(
1588 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001589 EXPECT_TRUE(
1590 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1591 WrongIndex));
1592}
1593
1594TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001595 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001596
1597 EXPECT_TRUE(
1598 matches("class X { public: X(int); }; void x() { X x(0); }",
1599 Constructor1Arg));
1600 EXPECT_TRUE(
1601 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1602 Constructor1Arg));
1603 EXPECT_TRUE(
1604 matches("class X { public: X(int); }; void x() { X x = 0; }",
1605 Constructor1Arg));
1606 EXPECT_TRUE(
1607 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1608 Constructor1Arg));
1609}
1610
Manuel Klimek70b9db92012-10-23 10:40:50 +00001611TEST(Matcher,ThisExpr) {
1612 EXPECT_TRUE(
1613 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1614 EXPECT_TRUE(
1615 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1616}
1617
Manuel Klimek4da21662012-07-06 05:48:52 +00001618TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001619 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001620
1621 std::string ClassString = "class string { public: string(); ~string(); }; ";
1622
1623 EXPECT_TRUE(
1624 matches(ClassString +
1625 "string GetStringByValue();"
1626 "void FunctionTakesString(string s);"
1627 "void run() { FunctionTakesString(GetStringByValue()); }",
1628 TempExpression));
1629
1630 EXPECT_TRUE(
1631 notMatches(ClassString +
1632 "string* GetStringPointer(); "
1633 "void FunctionTakesStringPtr(string* s);"
1634 "void run() {"
1635 " string* s = GetStringPointer();"
1636 " FunctionTakesStringPtr(GetStringPointer());"
1637 " FunctionTakesStringPtr(s);"
1638 "}",
1639 TempExpression));
1640
1641 EXPECT_TRUE(
1642 notMatches("class no_dtor {};"
1643 "no_dtor GetObjByValue();"
1644 "void ConsumeObj(no_dtor param);"
1645 "void run() { ConsumeObj(GetObjByValue()); }",
1646 TempExpression));
1647}
1648
Sam Panzere16acd32012-08-24 22:04:44 +00001649TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1650 std::string ClassString =
1651 "class string { public: string(); int length(); }; ";
1652
1653 EXPECT_TRUE(
1654 matches(ClassString +
1655 "string GetStringByValue();"
1656 "void FunctionTakesString(string s);"
1657 "void run() { FunctionTakesString(GetStringByValue()); }",
1658 materializeTemporaryExpr()));
1659
1660 EXPECT_TRUE(
1661 notMatches(ClassString +
1662 "string* GetStringPointer(); "
1663 "void FunctionTakesStringPtr(string* s);"
1664 "void run() {"
1665 " string* s = GetStringPointer();"
1666 " FunctionTakesStringPtr(GetStringPointer());"
1667 " FunctionTakesStringPtr(s);"
1668 "}",
1669 materializeTemporaryExpr()));
1670
1671 EXPECT_TRUE(
1672 notMatches(ClassString +
1673 "string GetStringByValue();"
1674 "void run() { int k = GetStringByValue().length(); }",
1675 materializeTemporaryExpr()));
1676
1677 EXPECT_TRUE(
1678 notMatches(ClassString +
1679 "string GetStringByValue();"
1680 "void run() { GetStringByValue(); }",
1681 materializeTemporaryExpr()));
1682}
1683
Manuel Klimek4da21662012-07-06 05:48:52 +00001684TEST(ConstructorDeclaration, SimpleCase) {
1685 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001686 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001687 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001688 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001689}
1690
1691TEST(ConstructorDeclaration, IsImplicit) {
1692 // This one doesn't match because the constructor is not added by the
1693 // compiler (it is not needed).
1694 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001695 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001696 // The compiler added the implicit default constructor.
1697 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001698 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001699 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001700 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001701}
1702
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001703TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1704 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001705 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001706}
1707
1708TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001709 EXPECT_TRUE(notMatches("class Foo {};",
1710 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001711}
1712
Manuel Klimek4da21662012-07-06 05:48:52 +00001713TEST(HasAnyConstructorInitializer, SimpleCase) {
1714 EXPECT_TRUE(notMatches(
1715 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001716 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001717 EXPECT_TRUE(matches(
1718 "class Foo {"
1719 " Foo() : foo_() { }"
1720 " int foo_;"
1721 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001722 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001723}
1724
1725TEST(HasAnyConstructorInitializer, ForField) {
1726 static const char Code[] =
1727 "class Baz { };"
1728 "class Foo {"
1729 " Foo() : foo_() { }"
1730 " Baz foo_;"
1731 " Baz bar_;"
1732 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001733 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1734 forField(hasType(recordDecl(hasName("Baz"))))))));
1735 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001736 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001737 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1738 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001739}
1740
1741TEST(HasAnyConstructorInitializer, WithInitializer) {
1742 static const char Code[] =
1743 "class Foo {"
1744 " Foo() : foo_(0) { }"
1745 " int foo_;"
1746 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001747 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001748 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001749 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001750 withInitializer(integerLiteral(equals(1)))))));
1751}
1752
1753TEST(HasAnyConstructorInitializer, IsWritten) {
1754 static const char Code[] =
1755 "struct Bar { Bar(){} };"
1756 "class Foo {"
1757 " Foo() : foo_() { }"
1758 " Bar foo_;"
1759 " Bar bar_;"
1760 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001761 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001762 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001763 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001764 allOf(forField(hasName("bar_")), isWritten())))));
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("bar_")), unless(isWritten()))))));
1767}
1768
1769TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001770 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001771
1772 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1773 EXPECT_TRUE(
1774 matches("class X { public: X(); }; void x() { new X(); }", New));
1775 EXPECT_TRUE(
1776 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1777 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1778}
1779
1780TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001781 StatementMatcher New = constructExpr(
1782 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001783
1784 EXPECT_TRUE(
1785 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1786 New));
1787 EXPECT_TRUE(
1788 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1789 New));
1790 EXPECT_TRUE(
1791 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1792 New));
1793
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001794 StatementMatcher WrongIndex = constructExpr(
1795 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001796 EXPECT_TRUE(
1797 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1798 WrongIndex));
1799}
1800
1801TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001802 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001803
1804 EXPECT_TRUE(
1805 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1806 EXPECT_TRUE(
1807 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1808 New));
1809}
1810
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001811TEST(Matcher, DeleteExpression) {
1812 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001813 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001814}
1815
Manuel Klimek4da21662012-07-06 05:48:52 +00001816TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001817 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001818
1819 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1820 EXPECT_TRUE(
1821 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1822 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1823}
1824
1825TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001826 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001827 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1828 // wide string
1829 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1830 // with escaped characters
1831 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1832 // no matching -- though the data type is the same, there is no string literal
1833 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1834}
1835
1836TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001837 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001838 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1839 // wide character
1840 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1841 // wide character, Hex encoded, NOT MATCHED!
1842 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1843 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1844}
1845
1846TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001847 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001848 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1849 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1850 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1851 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1852
1853 // Non-matching cases (character literals, float and double)
1854 EXPECT_TRUE(notMatches("int i = L'a';",
1855 HasIntLiteral)); // this is actually a character
1856 // literal cast to int
1857 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1858 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1859 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1860}
1861
Daniel Jasperc5ae7172013-07-26 18:52:58 +00001862TEST(Matcher, FloatLiterals) {
1863 StatementMatcher HasFloatLiteral = floatLiteral();
1864 EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral));
1865 EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral));
1866 EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral));
1867 EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral));
1868 EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral));
1869
1870 EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral));
1871}
1872
Daniel Jasper31f7c082012-10-01 13:40:41 +00001873TEST(Matcher, NullPtrLiteral) {
1874 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1875}
1876
Daniel Jasperb54b7642012-09-20 14:12:57 +00001877TEST(Matcher, AsmStatement) {
1878 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1879}
1880
Manuel Klimek4da21662012-07-06 05:48:52 +00001881TEST(Matcher, Conditions) {
1882 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1883
1884 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1885 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1886 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1887 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1888 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1889}
1890
1891TEST(MatchBinaryOperator, HasOperatorName) {
1892 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1893
1894 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1895 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1896}
1897
1898TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1899 StatementMatcher OperatorTrueFalse =
1900 binaryOperator(hasLHS(boolLiteral(equals(true))),
1901 hasRHS(boolLiteral(equals(false))));
1902
1903 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1904 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1905 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1906}
1907
1908TEST(MatchBinaryOperator, HasEitherOperand) {
1909 StatementMatcher HasOperand =
1910 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1911
1912 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1913 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1914 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1915}
1916
1917TEST(Matcher, BinaryOperatorTypes) {
1918 // Integration test that verifies the AST provides all binary operators in
1919 // a way we expect.
1920 // FIXME: Operator ','
1921 EXPECT_TRUE(
1922 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1923 EXPECT_TRUE(
1924 matches("bool b; bool c = (b = true);",
1925 binaryOperator(hasOperatorName("="))));
1926 EXPECT_TRUE(
1927 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1928 EXPECT_TRUE(
1929 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1930 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1931 EXPECT_TRUE(
1932 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1933 EXPECT_TRUE(
1934 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1935 EXPECT_TRUE(
1936 matches("int i = 1; int j = (i <<= 2);",
1937 binaryOperator(hasOperatorName("<<="))));
1938 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1939 EXPECT_TRUE(
1940 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1941 EXPECT_TRUE(
1942 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1943 EXPECT_TRUE(
1944 matches("int i = 1; int j = (i >>= 2);",
1945 binaryOperator(hasOperatorName(">>="))));
1946 EXPECT_TRUE(
1947 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1948 EXPECT_TRUE(
1949 matches("int i = 42; int j = (i ^= 42);",
1950 binaryOperator(hasOperatorName("^="))));
1951 EXPECT_TRUE(
1952 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1953 EXPECT_TRUE(
1954 matches("int i = 42; int j = (i %= 42);",
1955 binaryOperator(hasOperatorName("%="))));
1956 EXPECT_TRUE(
1957 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1958 EXPECT_TRUE(
1959 matches("bool b = true && false;",
1960 binaryOperator(hasOperatorName("&&"))));
1961 EXPECT_TRUE(
1962 matches("bool b = true; bool c = (b &= false);",
1963 binaryOperator(hasOperatorName("&="))));
1964 EXPECT_TRUE(
1965 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1966 EXPECT_TRUE(
1967 matches("bool b = true || false;",
1968 binaryOperator(hasOperatorName("||"))));
1969 EXPECT_TRUE(
1970 matches("bool b = true; bool c = (b |= false);",
1971 binaryOperator(hasOperatorName("|="))));
1972 EXPECT_TRUE(
1973 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1974 EXPECT_TRUE(
1975 matches("int i = 42; int j = (i *= 23);",
1976 binaryOperator(hasOperatorName("*="))));
1977 EXPECT_TRUE(
1978 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1979 EXPECT_TRUE(
1980 matches("int i = 42; int j = (i /= 23);",
1981 binaryOperator(hasOperatorName("/="))));
1982 EXPECT_TRUE(
1983 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1984 EXPECT_TRUE(
1985 matches("int i = 42; int j = (i += 23);",
1986 binaryOperator(hasOperatorName("+="))));
1987 EXPECT_TRUE(
1988 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1989 EXPECT_TRUE(
1990 matches("int i = 42; int j = (i -= 23);",
1991 binaryOperator(hasOperatorName("-="))));
1992 EXPECT_TRUE(
1993 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1994 binaryOperator(hasOperatorName("->*"))));
1995 EXPECT_TRUE(
1996 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1997 binaryOperator(hasOperatorName(".*"))));
1998
1999 // Member expressions as operators are not supported in matches.
2000 EXPECT_TRUE(
2001 notMatches("struct A { void x(A *a) { a->x(this); } };",
2002 binaryOperator(hasOperatorName("->"))));
2003
2004 // Initializer assignments are not represented as operator equals.
2005 EXPECT_TRUE(
2006 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
2007
2008 // Array indexing is not represented as operator.
2009 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
2010
2011 // Overloaded operators do not match at all.
2012 EXPECT_TRUE(notMatches(
2013 "struct A { bool operator&&(const A &a) const { return false; } };"
2014 "void x() { A a, b; a && b; }",
2015 binaryOperator()));
2016}
2017
2018TEST(MatchUnaryOperator, HasOperatorName) {
2019 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
2020
2021 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
2022 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
2023}
2024
2025TEST(MatchUnaryOperator, HasUnaryOperand) {
2026 StatementMatcher OperatorOnFalse =
2027 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
2028
2029 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
2030 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
2031}
2032
2033TEST(Matcher, UnaryOperatorTypes) {
2034 // Integration test that verifies the AST provides all unary operators in
2035 // a way we expect.
2036 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
2037 EXPECT_TRUE(
2038 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
2039 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
2040 EXPECT_TRUE(
2041 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
2042 EXPECT_TRUE(
2043 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
2044 EXPECT_TRUE(
2045 matches("int i; int j = -i;", 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
2055 // We don't match conversion operators.
2056 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
2057
2058 // Function calls are not represented as operator.
2059 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
2060
2061 // Overloaded operators do not match at all.
2062 // FIXME: We probably want to add that.
2063 EXPECT_TRUE(notMatches(
2064 "struct A { bool operator!() const { return false; } };"
2065 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
2066}
2067
2068TEST(Matcher, ConditionalOperator) {
2069 StatementMatcher Conditional = conditionalOperator(
2070 hasCondition(boolLiteral(equals(true))),
2071 hasTrueExpression(boolLiteral(equals(false))));
2072
2073 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
2074 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
2075 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
2076
2077 StatementMatcher ConditionalFalse = conditionalOperator(
2078 hasFalseExpression(boolLiteral(equals(false))));
2079
2080 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
2081 EXPECT_TRUE(
2082 notMatches("void x() { true ? false : true; }", ConditionalFalse));
2083}
2084
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002085TEST(ArraySubscriptMatchers, ArraySubscripts) {
2086 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2087 arraySubscriptExpr()));
2088 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2089 arraySubscriptExpr()));
2090}
2091
2092TEST(ArraySubscriptMatchers, ArrayIndex) {
2093 EXPECT_TRUE(matches(
2094 "int i[2]; void f() { i[1] = 1; }",
2095 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2096 EXPECT_TRUE(matches(
2097 "int i[2]; void f() { 1[i] = 1; }",
2098 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2099 EXPECT_TRUE(notMatches(
2100 "int i[2]; void f() { i[1] = 1; }",
2101 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2102}
2103
2104TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2105 EXPECT_TRUE(matches(
2106 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002107 arraySubscriptExpr(hasBase(implicitCastExpr(
2108 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002109}
2110
Manuel Klimek4da21662012-07-06 05:48:52 +00002111TEST(Matcher, HasNameSupportsNamespaces) {
2112 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002113 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002114 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002115 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002116 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002117 recordDecl(hasName("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("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002120 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002121 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002122 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002123 recordDecl(hasName("a::c::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("a::b::A"))));
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("::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("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002130 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002131 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002132 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002133 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002134 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002135 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002136}
2137
2138TEST(Matcher, HasNameSupportsOuterClasses) {
2139 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002140 matches("class A { class B { class C; }; };",
2141 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002142 EXPECT_TRUE(
2143 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002144 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002145 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002146 matches("class A { class B { class C; }; };",
2147 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002148 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002149 matches("class A { class B { class C; }; };",
2150 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002151 EXPECT_TRUE(
2152 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002153 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002154 EXPECT_TRUE(
2155 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002156 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002157 EXPECT_TRUE(
2158 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002159 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002160 EXPECT_TRUE(
2161 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002162 recordDecl(hasName("::C"))));
2163 EXPECT_TRUE(
2164 notMatches("class A { class B { class C; }; };",
2165 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002166 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002167 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002168 EXPECT_TRUE(
2169 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002170 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002171}
2172
2173TEST(Matcher, IsDefinition) {
2174 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002175 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002176 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2177 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2178
2179 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002180 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002181 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2182 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2183
2184 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002185 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002186 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2187 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2188}
2189
2190TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002191 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00002192 ofClass(hasName("X")))));
2193
2194 EXPECT_TRUE(
2195 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2196 EXPECT_TRUE(
2197 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2198 Constructor));
2199 EXPECT_TRUE(
2200 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2201 Constructor));
2202}
2203
2204TEST(Matcher, VisitsTemplateInstantiations) {
2205 EXPECT_TRUE(matches(
2206 "class A { public: void x(); };"
2207 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002208 "void f() { B<A> b; b.y(); }",
2209 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002210
2211 EXPECT_TRUE(matches(
2212 "class A { public: void x(); };"
2213 "class C {"
2214 " public:"
2215 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2216 "};"
2217 "void f() {"
2218 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002219 "}",
2220 recordDecl(hasName("C"),
2221 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002222}
2223
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002224TEST(Matcher, HandlesNullQualTypes) {
2225 // FIXME: Add a Type matcher so we can replace uses of this
2226 // variable with Type(True())
2227 const TypeMatcher AnyType = anything();
2228
2229 // We don't really care whether this matcher succeeds; we're testing that
2230 // it completes without crashing.
2231 EXPECT_TRUE(matches(
2232 "struct A { };"
2233 "template <typename T>"
2234 "void f(T t) {"
2235 " T local_t(t /* this becomes a null QualType in the AST */);"
2236 "}"
2237 "void g() {"
2238 " f(0);"
2239 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002240 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002241 anyOf(
2242 TypeMatcher(hasDeclaration(anything())),
2243 pointsTo(AnyType),
2244 references(AnyType)
2245 // Other QualType matchers should go here.
2246 ))))));
2247}
2248
Manuel Klimek4da21662012-07-06 05:48:52 +00002249// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002250AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002251 // Make sure all special variables are used: node, match_finder,
2252 // bound_nodes_builder, and the parameter named 'AMatcher'.
2253 return AMatcher.matches(Node, Finder, Builder);
2254}
2255
2256TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002257 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002258
2259 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002260 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002261
2262 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002263 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002264
2265 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002266 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002267}
2268
2269AST_POLYMORPHIC_MATCHER_P(
Samuel Benzaquenef7eb022013-06-21 15:51:31 +00002270 polymorphicHas,
2271 AST_POLYMORPHIC_SUPPORTED_TYPES_2(Decl, Stmt),
2272 internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002273 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002274 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002275 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2276 ASTMatchFinder::BK_First);
2277}
2278
2279TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002280 DeclarationMatcher HasClassB =
2281 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002282
2283 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002284 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002285
2286 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002287 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002288
2289 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002290 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002291
2292 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002293 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002294
2295 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2296}
2297
2298TEST(For, FindsForLoops) {
2299 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2300 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002301 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2302 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002303 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002304}
2305
Daniel Jasper6a124492012-07-12 08:50:38 +00002306TEST(For, ForLoopInternals) {
2307 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2308 forStmt(hasCondition(anything()))));
2309 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2310 forStmt(hasLoopInit(anything()))));
2311}
2312
2313TEST(For, NegativeForLoopInternals) {
2314 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002315 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002316 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2317 forStmt(hasLoopInit(anything()))));
2318}
2319
Manuel Klimek4da21662012-07-06 05:48:52 +00002320TEST(For, ReportsNoFalsePositives) {
2321 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2322 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2323}
2324
2325TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002326 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2327 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2328 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002329}
2330
2331TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2332 // It's not a compound statement just because there's "{}" in the source
2333 // text. This is an AST search, not grep.
2334 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002335 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002336 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002337 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002338}
2339
Daniel Jasper6a124492012-07-12 08:50:38 +00002340TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002341 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002342 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002343 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002344 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002345 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002346 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002347 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002348 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002349}
2350
2351TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2352 // The simplest case: every compound statement is in a function
2353 // definition, and the function body itself must be a compound
2354 // statement.
2355 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002356 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002357}
2358
2359TEST(HasAnySubstatement, IsNotRecursive) {
2360 // It's really "has any immediate substatement".
2361 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002362 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002363}
2364
2365TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2366 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002367 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002368}
2369
2370TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2371 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002372 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002373}
2374
2375TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2376 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002377 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002378 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002379 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002380}
2381
2382TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2383 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002384 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002385 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002386 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002387 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002388 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002389}
2390
2391TEST(StatementCountIs, WorksWithMultipleStatements) {
2392 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002393 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002394}
2395
2396TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2397 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002398 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002399 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002400 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002401 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002402 compoundStmt(statementCountIs(3))));
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(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002405}
2406
2407TEST(Member, WorksInSimplestCase) {
2408 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002409 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002410}
2411
2412TEST(Member, DoesNotMatchTheBaseExpression) {
2413 // Don't pick out the wrong part of the member expression, this should
2414 // be checking the member (name) only.
2415 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002416 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002417}
2418
2419TEST(Member, MatchesInMemberFunctionCall) {
2420 EXPECT_TRUE(matches("void f() {"
2421 " struct { void first() {}; } s;"
2422 " s.first();"
2423 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002424 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002425}
2426
Daniel Jasperc711af22012-10-23 15:46:39 +00002427TEST(Member, MatchesMember) {
2428 EXPECT_TRUE(matches(
2429 "struct A { int i; }; void f() { A a; a.i = 2; }",
2430 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2431 EXPECT_TRUE(notMatches(
2432 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2433 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2434}
2435
Daniel Jasperf3197e92013-02-25 12:02:08 +00002436TEST(Member, UnderstandsAccess) {
2437 EXPECT_TRUE(matches(
2438 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2439 EXPECT_TRUE(notMatches(
2440 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2441 EXPECT_TRUE(notMatches(
2442 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2443
2444 EXPECT_TRUE(notMatches(
2445 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2446 EXPECT_TRUE(notMatches(
2447 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2448 EXPECT_TRUE(matches(
2449 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2450
2451 EXPECT_TRUE(notMatches(
2452 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2453 EXPECT_TRUE(matches("class A { protected: int i; };",
2454 fieldDecl(isProtected(), hasName("i"))));
2455 EXPECT_TRUE(notMatches(
2456 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2457
2458 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2459 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2460 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2461 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2462}
2463
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002464TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002465 // Fails in C++11 mode
2466 EXPECT_TRUE(matchesConditionally(
2467 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2468 "class X { void *operator new(std::size_t); };",
2469 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002470
2471 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002472 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002473
Daniel Jasper31f7c082012-10-01 13:40:41 +00002474 // Fails in C++11 mode
2475 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002476 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2477 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002478 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002479}
2480
Manuel Klimek4da21662012-07-06 05:48:52 +00002481TEST(HasObjectExpression, DoesNotMatchMember) {
2482 EXPECT_TRUE(notMatches(
2483 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002484 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002485}
2486
2487TEST(HasObjectExpression, MatchesBaseOfVariable) {
2488 EXPECT_TRUE(matches(
2489 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002490 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002491 EXPECT_TRUE(matches(
2492 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002493 memberExpr(hasObjectExpression(
2494 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002495}
2496
2497TEST(HasObjectExpression,
2498 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2499 EXPECT_TRUE(matches(
2500 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002501 memberExpr(hasObjectExpression(
2502 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002503 EXPECT_TRUE(matches(
2504 "class X {}; struct S { X m; void f() { 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}
2508
2509TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002510 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2511 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2512 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2513 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002514}
2515
2516TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002517 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002518}
2519
2520TEST(IsConstQualified, MatchesConstInt) {
2521 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002522 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002523}
2524
2525TEST(IsConstQualified, MatchesConstPointer) {
2526 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002527 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002528}
2529
2530TEST(IsConstQualified, MatchesThroughTypedef) {
2531 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002532 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002533 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002534 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002535}
2536
2537TEST(IsConstQualified, DoesNotMatchInappropriately) {
2538 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002539 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002540 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002541 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002542}
2543
Sam Panzer089e5b32012-08-16 16:58:10 +00002544TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002545 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2546 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2547 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2548 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002549}
2550TEST(CastExpression, MatchesImplicitCasts) {
2551 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002552 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002553 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002554 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002555}
2556
2557TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002558 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2559 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2560 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2561 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002562}
2563
Manuel Klimek4da21662012-07-06 05:48:52 +00002564TEST(ReinterpretCast, MatchesSimpleCase) {
2565 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002566 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002567}
2568
2569TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002570 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002571 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002572 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002573 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002574 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002575 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2576 "B b;"
2577 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002578 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002579}
2580
2581TEST(FunctionalCast, MatchesSimpleCase) {
2582 std::string foo_class = "class Foo { public: Foo(char*); };";
2583 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002584 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002585}
2586
2587TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2588 std::string FooClass = "class Foo { public: Foo(char*); };";
2589 EXPECT_TRUE(
2590 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002591 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002592 EXPECT_TRUE(
2593 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002594 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002595}
2596
2597TEST(DynamicCast, MatchesSimpleCase) {
2598 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2599 "B b;"
2600 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002601 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002602}
2603
2604TEST(StaticCast, MatchesSimpleCase) {
2605 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002606 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002607}
2608
2609TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002610 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002611 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002612 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002613 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002614 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002615 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2616 "B b;"
2617 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002618 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002619}
2620
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002621TEST(CStyleCast, MatchesSimpleCase) {
2622 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2623}
2624
2625TEST(CStyleCast, DoesNotMatchOtherCasts) {
2626 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2627 "char q, *r = const_cast<char*>(&q);"
2628 "void* s = reinterpret_cast<char*>(&s);"
2629 "struct B { virtual ~B() {} }; struct D : B {};"
2630 "B b;"
2631 "D* t = dynamic_cast<D*>(&b);",
2632 cStyleCastExpr()));
2633}
2634
Manuel Klimek4da21662012-07-06 05:48:52 +00002635TEST(HasDestinationType, MatchesSimpleCase) {
2636 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002637 staticCastExpr(hasDestinationType(
2638 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002639}
2640
Sam Panzer089e5b32012-08-16 16:58:10 +00002641TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2642 // This test creates an implicit const cast.
2643 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002644 implicitCastExpr(
2645 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002646 // This test creates an implicit array-to-pointer cast.
2647 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002648 implicitCastExpr(hasImplicitDestinationType(
2649 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002650}
2651
2652TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2653 // This test creates an implicit cast from int to char.
2654 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002655 implicitCastExpr(hasImplicitDestinationType(
2656 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002657 // This test creates an implicit array-to-pointer cast.
2658 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002659 implicitCastExpr(hasImplicitDestinationType(
2660 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002661}
2662
2663TEST(ImplicitCast, MatchesSimpleCase) {
2664 // This test creates an implicit const cast.
2665 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002666 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002667 // This test creates an implicit cast from int to char.
2668 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002669 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002670 // This test creates an implicit array-to-pointer cast.
2671 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002672 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002673}
2674
2675TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002676 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002677 // are present, and that it ignores explicit and paren casts.
2678
2679 // These two test cases have no casts.
2680 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002681 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002682 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002683 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002684
2685 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002686 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002687 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002688 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002689
2690 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002691 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002692}
2693
2694TEST(IgnoringImpCasts, MatchesImpCasts) {
2695 // This test checks that ignoringImpCasts matches when implicit casts are
2696 // present and its inner matcher alone does not match.
2697 // Note that this test creates an implicit const cast.
2698 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002699 varDecl(hasInitializer(ignoringImpCasts(
2700 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002701 // This test creates an implict cast from int to char.
2702 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002703 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002704 integerLiteral(equals(0)))))));
2705}
2706
2707TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2708 // These tests verify that ignoringImpCasts does not match if the inner
2709 // matcher does not match.
2710 // Note that the first test creates an implicit const cast.
2711 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002712 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002713 unless(anything()))))));
2714 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002715 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002716 unless(anything()))))));
2717
2718 // These tests verify that ignoringImplictCasts does not look through explicit
2719 // casts or parentheses.
2720 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002721 varDecl(hasInitializer(ignoringImpCasts(
2722 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002723 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002724 varDecl(hasInitializer(ignoringImpCasts(
2725 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002726 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002727 varDecl(hasInitializer(ignoringImpCasts(
2728 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002729 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002730 varDecl(hasInitializer(ignoringImpCasts(
2731 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002732}
2733
2734TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2735 // This test verifies that expressions that do not have implicit casts
2736 // still match the inner matcher.
2737 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002738 varDecl(hasInitializer(ignoringImpCasts(
2739 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002740}
2741
2742TEST(IgnoringParenCasts, MatchesParenCasts) {
2743 // This test checks that ignoringParenCasts matches when parentheses and/or
2744 // casts are present and its inner matcher alone does not match.
2745 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002746 varDecl(hasInitializer(ignoringParenCasts(
2747 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002748 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002749 varDecl(hasInitializer(ignoringParenCasts(
2750 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002751
2752 // This test creates an implict cast from int to char in addition to the
2753 // parentheses.
2754 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002755 varDecl(hasInitializer(ignoringParenCasts(
2756 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002757
2758 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002759 varDecl(hasInitializer(ignoringParenCasts(
2760 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002761 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002762 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002763 integerLiteral(equals(0)))))));
2764}
2765
2766TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2767 // This test verifies that expressions that do not have any casts still match.
2768 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002769 varDecl(hasInitializer(ignoringParenCasts(
2770 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002771}
2772
2773TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2774 // These tests verify that ignoringImpCasts does not match if the inner
2775 // matcher does not match.
2776 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002777 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002778 unless(anything()))))));
2779
2780 // This test creates an implicit cast from int to char in addition to the
2781 // parentheses.
2782 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002783 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002784 unless(anything()))))));
2785
2786 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002787 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002788 unless(anything()))))));
2789}
2790
2791TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2792 // This test checks that ignoringParenAndImpCasts matches when
2793 // parentheses and/or implicit casts are present and its inner matcher alone
2794 // does not match.
2795 // Note that this test creates an implicit const cast.
2796 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002797 varDecl(hasInitializer(ignoringParenImpCasts(
2798 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002799 // This test creates an implicit cast from int to char.
2800 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002801 varDecl(hasInitializer(ignoringParenImpCasts(
2802 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002803}
2804
2805TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2806 // This test verifies that expressions that do not have parentheses or
2807 // implicit casts still match.
2808 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002809 varDecl(hasInitializer(ignoringParenImpCasts(
2810 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002811 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002812 varDecl(hasInitializer(ignoringParenImpCasts(
2813 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002814}
2815
2816TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2817 // These tests verify that ignoringParenImpCasts does not match if
2818 // the inner matcher does not match.
2819 // This test creates an implicit cast.
2820 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002821 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002822 unless(anything()))))));
2823 // These tests verify that ignoringParenAndImplictCasts does not look
2824 // through explicit casts.
2825 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002826 varDecl(hasInitializer(ignoringParenImpCasts(
2827 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002828 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002829 varDecl(hasInitializer(ignoringParenImpCasts(
2830 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002831 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002832 varDecl(hasInitializer(ignoringParenImpCasts(
2833 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002834}
2835
Manuel Klimek715c9562012-07-25 10:02:02 +00002836TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002837 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2838 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002839 implicitCastExpr(
2840 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002841}
2842
Manuel Klimek715c9562012-07-25 10:02:02 +00002843TEST(HasSourceExpression, MatchesExplicitCasts) {
2844 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002845 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002846 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002847 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002848}
2849
Manuel Klimek4da21662012-07-06 05:48:52 +00002850TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002851 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002852}
2853
2854TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002855 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002856}
2857
2858TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002859 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002860}
2861
2862TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002863 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002864}
2865
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002866TEST(InitListExpression, MatchesInitListExpression) {
2867 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2868 initListExpr(hasType(asString("int [2]")))));
2869 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002870 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002871}
2872
2873TEST(UsingDeclaration, MatchesUsingDeclarations) {
2874 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2875 usingDecl()));
2876}
2877
2878TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2879 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2880 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2881}
2882
2883TEST(UsingDeclaration, MatchesSpecificTarget) {
2884 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2885 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002886 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002887 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2888 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002889 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002890}
2891
2892TEST(UsingDeclaration, ThroughUsingDeclaration) {
2893 EXPECT_TRUE(matches(
2894 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002895 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002896 EXPECT_TRUE(notMatches(
2897 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002898 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002899}
2900
Sam Panzer425f41b2012-08-16 17:20:59 +00002901TEST(SingleDecl, IsSingleDecl) {
2902 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002903 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002904 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2905 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2906 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2907 SingleDeclStmt));
2908}
2909
2910TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002911 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002912
2913 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002914 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002915 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002916 declStmt(containsDeclaration(0, MatchesInit),
2917 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002918 unsigned WrongIndex = 42;
2919 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002920 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002921 MatchesInit))));
2922}
2923
2924TEST(DeclCount, DeclCountIsCorrect) {
2925 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002926 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002927 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002928 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002929 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002930 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002931}
2932
Manuel Klimek4da21662012-07-06 05:48:52 +00002933TEST(While, MatchesWhileLoops) {
2934 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2935 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2936 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2937}
2938
2939TEST(Do, MatchesDoLoops) {
2940 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2941 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2942}
2943
2944TEST(Do, DoesNotMatchWhileLoops) {
2945 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2946}
2947
2948TEST(SwitchCase, MatchesCase) {
2949 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2950 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2951 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2952 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2953}
2954
Daniel Jasperb54b7642012-09-20 14:12:57 +00002955TEST(SwitchCase, MatchesSwitch) {
2956 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2957 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2958 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2959 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2960}
2961
Peter Collingbourneacf02712013-05-10 11:52:02 +00002962TEST(SwitchCase, MatchesEachCase) {
2963 EXPECT_TRUE(notMatches("void x() { switch(42); }",
2964 switchStmt(forEachSwitchCase(caseStmt()))));
2965 EXPECT_TRUE(matches("void x() { switch(42) case 42:; }",
2966 switchStmt(forEachSwitchCase(caseStmt()))));
2967 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }",
2968 switchStmt(forEachSwitchCase(caseStmt()))));
2969 EXPECT_TRUE(notMatches(
2970 "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }",
2971 ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt()))))));
2972 EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }",
2973 switchStmt(forEachSwitchCase(
2974 caseStmt(hasCaseConstant(integerLiteral()))))));
2975 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }",
2976 switchStmt(forEachSwitchCase(
2977 caseStmt(hasCaseConstant(integerLiteral()))))));
2978 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }",
2979 switchStmt(forEachSwitchCase(
2980 caseStmt(hasCaseConstant(integerLiteral()))))));
2981 EXPECT_TRUE(matchAndVerifyResultTrue(
2982 "void x() { switch (42) { case 1: case 2: case 3: default:; } }",
2983 switchStmt(forEachSwitchCase(caseStmt().bind("x"))),
2984 new VerifyIdIsBoundTo<CaseStmt>("x", 3)));
2985}
2986
Manuel Klimek06963012013-07-19 11:50:54 +00002987TEST(ForEachConstructorInitializer, MatchesInitializers) {
2988 EXPECT_TRUE(matches(
2989 "struct X { X() : i(42), j(42) {} int i, j; };",
2990 constructorDecl(forEachConstructorInitializer(ctorInitializer()))));
2991}
2992
Daniel Jasperb54b7642012-09-20 14:12:57 +00002993TEST(ExceptionHandling, SimpleCases) {
2994 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2995 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2996 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2997 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2998 throwExpr()));
2999 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
3000 throwExpr()));
3001}
3002
Manuel Klimek4da21662012-07-06 05:48:52 +00003003TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
3004 EXPECT_TRUE(notMatches(
3005 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003006 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003007 EXPECT_TRUE(notMatches(
3008 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003009 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003010}
3011
3012TEST(HasConditionVariableStatement, MatchesConditionVariables) {
3013 EXPECT_TRUE(matches(
3014 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003015 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003016}
3017
3018TEST(ForEach, BindsOneNode) {
3019 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003020 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003021 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003022}
3023
3024TEST(ForEach, BindsMultipleNodes) {
3025 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003026 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003027 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003028}
3029
3030TEST(ForEach, BindsRecursiveCombinations) {
3031 EXPECT_TRUE(matchAndVerifyResultTrue(
3032 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003033 recordDecl(hasName("C"),
3034 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003035 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003036}
3037
3038TEST(ForEachDescendant, BindsOneNode) {
3039 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003040 recordDecl(hasName("C"),
3041 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003042 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003043}
3044
Daniel Jasper5f684e92012-11-16 18:39:22 +00003045TEST(ForEachDescendant, NestedForEachDescendant) {
3046 DeclarationMatcher m = recordDecl(
3047 isDefinition(), decl().bind("x"), hasName("C"));
3048 EXPECT_TRUE(matchAndVerifyResultTrue(
3049 "class A { class B { class C {}; }; };",
3050 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
3051 new VerifyIdIsBoundTo<Decl>("x", "C")));
3052
Manuel Klimek054d0492013-06-19 15:42:45 +00003053 // Check that a partial match of 'm' that binds 'x' in the
3054 // first part of anyOf(m, anything()) will not overwrite the
3055 // binding created by the earlier binding in the hasDescendant.
3056 EXPECT_TRUE(matchAndVerifyResultTrue(
3057 "class A { class B { class C {}; }; };",
3058 recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
3059 new VerifyIdIsBoundTo<Decl>("x", "C")));
Daniel Jasper5f684e92012-11-16 18:39:22 +00003060}
3061
Manuel Klimek4da21662012-07-06 05:48:52 +00003062TEST(ForEachDescendant, BindsMultipleNodes) {
3063 EXPECT_TRUE(matchAndVerifyResultTrue(
3064 "class C { class D { int x; int y; }; "
3065 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003066 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003067 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003068}
3069
3070TEST(ForEachDescendant, BindsRecursiveCombinations) {
3071 EXPECT_TRUE(matchAndVerifyResultTrue(
3072 "class C { class D { "
3073 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003074 recordDecl(hasName("C"), forEachDescendant(recordDecl(
3075 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003076 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003077}
3078
Manuel Klimek054d0492013-06-19 15:42:45 +00003079TEST(ForEachDescendant, BindsCombinations) {
3080 EXPECT_TRUE(matchAndVerifyResultTrue(
3081 "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while "
3082 "(true) {} }",
3083 compoundStmt(forEachDescendant(ifStmt().bind("if")),
3084 forEachDescendant(whileStmt().bind("while"))),
3085 new VerifyIdIsBoundTo<IfStmt>("if", 6)));
3086}
3087
3088TEST(Has, DoesNotDeleteBindings) {
3089 EXPECT_TRUE(matchAndVerifyResultTrue(
3090 "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())),
3091 new VerifyIdIsBoundTo<Decl>("x", 1)));
3092}
3093
3094TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) {
3095 // Those matchers cover all the cases where an inner matcher is called
3096 // and there is not a 1:1 relationship between the match of the outer
3097 // matcher and the match of the inner matcher.
3098 // The pattern to look for is:
3099 // ... return InnerMatcher.matches(...); ...
3100 // In which case no special handling is needed.
3101 //
3102 // On the other hand, if there are multiple alternative matches
3103 // (for example forEach*) or matches might be discarded (for example has*)
3104 // the implementation must make sure that the discarded matches do not
3105 // affect the bindings.
3106 // When new such matchers are added, add a test here that:
3107 // - matches a simple node, and binds it as the first thing in the matcher:
3108 // recordDecl(decl().bind("x"), hasName("X")))
3109 // - uses the matcher under test afterwards in a way that not the first
3110 // alternative is matched; for anyOf, that means the first branch
3111 // would need to return false; for hasAncestor, it means that not
3112 // the direct parent matches the inner matcher.
3113
3114 EXPECT_TRUE(matchAndVerifyResultTrue(
3115 "class X { int y; };",
3116 recordDecl(
3117 recordDecl().bind("x"), hasName("::X"),
3118 anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())),
3119 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3120 EXPECT_TRUE(matchAndVerifyResultTrue(
3121 "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"),
3122 anyOf(unless(anything()), anything())),
3123 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3124 EXPECT_TRUE(matchAndVerifyResultTrue(
3125 "template<typename T1, typename T2> class X {}; X<float, int> x;",
3126 classTemplateSpecializationDecl(
3127 decl().bind("x"),
3128 hasAnyTemplateArgument(refersToType(asString("int")))),
3129 new VerifyIdIsBoundTo<Decl>("x", 1)));
3130 EXPECT_TRUE(matchAndVerifyResultTrue(
3131 "class X { void f(); void g(); };",
3132 recordDecl(decl().bind("x"), hasMethod(hasName("g"))),
3133 new VerifyIdIsBoundTo<Decl>("x", 1)));
3134 EXPECT_TRUE(matchAndVerifyResultTrue(
3135 "class X { X() : a(1), b(2) {} double a; int b; };",
3136 recordDecl(decl().bind("x"),
3137 has(constructorDecl(
3138 hasAnyConstructorInitializer(forField(hasName("b")))))),
3139 new VerifyIdIsBoundTo<Decl>("x", 1)));
3140 EXPECT_TRUE(matchAndVerifyResultTrue(
3141 "void x(int, int) { x(0, 42); }",
3142 callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))),
3143 new VerifyIdIsBoundTo<Expr>("x", 1)));
3144 EXPECT_TRUE(matchAndVerifyResultTrue(
3145 "void x(int, int y) {}",
3146 functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))),
3147 new VerifyIdIsBoundTo<Decl>("x", 1)));
3148 EXPECT_TRUE(matchAndVerifyResultTrue(
3149 "void x() { return; if (true) {} }",
3150 functionDecl(decl().bind("x"),
3151 has(compoundStmt(hasAnySubstatement(ifStmt())))),
3152 new VerifyIdIsBoundTo<Decl>("x", 1)));
3153 EXPECT_TRUE(matchAndVerifyResultTrue(
3154 "namespace X { void b(int); void b(); }"
3155 "using X::b;",
3156 usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl(
3157 functionDecl(parameterCountIs(1))))),
3158 new VerifyIdIsBoundTo<Decl>("x", 1)));
3159 EXPECT_TRUE(matchAndVerifyResultTrue(
3160 "class A{}; class B{}; class C : B, A {};",
3161 recordDecl(decl().bind("x"), isDerivedFrom("::A")),
3162 new VerifyIdIsBoundTo<Decl>("x", 1)));
3163 EXPECT_TRUE(matchAndVerifyResultTrue(
3164 "class A{}; typedef A B; typedef A C; typedef A D;"
3165 "class E : A {};",
3166 recordDecl(decl().bind("x"), isDerivedFrom("C")),
3167 new VerifyIdIsBoundTo<Decl>("x", 1)));
3168 EXPECT_TRUE(matchAndVerifyResultTrue(
3169 "class A { class B { void f() {} }; };",
3170 functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3171 new VerifyIdIsBoundTo<Decl>("x", 1)));
3172 EXPECT_TRUE(matchAndVerifyResultTrue(
3173 "template <typename T> struct A { struct B {"
3174 " void f() { if(true) {} }"
3175 "}; };"
3176 "void t() { A<int>::B b; b.f(); }",
3177 ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3178 new VerifyIdIsBoundTo<Stmt>("x", 2)));
3179 EXPECT_TRUE(matchAndVerifyResultTrue(
3180 "class A {};",
3181 recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))),
3182 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimek06963012013-07-19 11:50:54 +00003183 EXPECT_TRUE(matchAndVerifyResultTrue(
3184 "class A { A() : s(), i(42) {} const char *s; int i; };",
3185 constructorDecl(hasName("::A::A"), decl().bind("x"),
3186 forEachConstructorInitializer(forField(hasName("i")))),
3187 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimek054d0492013-06-19 15:42:45 +00003188}
3189
Daniel Jasper11c98772012-11-11 22:14:55 +00003190TEST(ForEachDescendant, BindsCorrectNodes) {
3191 EXPECT_TRUE(matchAndVerifyResultTrue(
3192 "class C { void f(); int i; };",
3193 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3194 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
3195 EXPECT_TRUE(matchAndVerifyResultTrue(
3196 "class C { void f() {} int i; };",
3197 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3198 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
3199}
3200
Manuel Klimek152ea0e2013-02-04 10:59:20 +00003201TEST(FindAll, BindsNodeOnMatch) {
3202 EXPECT_TRUE(matchAndVerifyResultTrue(
3203 "class A {};",
3204 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
3205 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
3206}
3207
3208TEST(FindAll, BindsDescendantNodeOnMatch) {
3209 EXPECT_TRUE(matchAndVerifyResultTrue(
3210 "class A { int a; int b; };",
3211 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
3212 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3213}
3214
3215TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
3216 EXPECT_TRUE(matchAndVerifyResultTrue(
3217 "class A { int a; int b; };",
3218 recordDecl(hasName("::A"),
3219 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
3220 fieldDecl().bind("v"))))),
3221 new VerifyIdIsBoundTo<Decl>("v", 3)));
3222
3223 EXPECT_TRUE(matchAndVerifyResultTrue(
3224 "class A { class B {}; class C {}; };",
3225 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
3226 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3227}
3228
Manuel Klimek73876732013-02-04 09:42:38 +00003229TEST(EachOf, TriggersForEachMatch) {
3230 EXPECT_TRUE(matchAndVerifyResultTrue(
3231 "class A { int a; int b; };",
3232 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3233 has(fieldDecl(hasName("b")).bind("v")))),
3234 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3235}
3236
3237TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3238 EXPECT_TRUE(matchAndVerifyResultTrue(
3239 "class A { int a; int c; };",
3240 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3241 has(fieldDecl(hasName("b")).bind("v")))),
3242 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3243 EXPECT_TRUE(matchAndVerifyResultTrue(
3244 "class A { int c; int b; };",
3245 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3246 has(fieldDecl(hasName("b")).bind("v")))),
3247 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3248 EXPECT_TRUE(notMatches(
3249 "class A { int c; int d; };",
3250 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3251 has(fieldDecl(hasName("b")).bind("v"))))));
3252}
Manuel Klimek4da21662012-07-06 05:48:52 +00003253
3254TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3255 // Make sure that we can both match the class by name (::X) and by the type
3256 // the template was instantiated with (via a field).
3257
3258 EXPECT_TRUE(matches(
3259 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003260 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003261
3262 EXPECT_TRUE(matches(
3263 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003264 recordDecl(isTemplateInstantiation(), hasDescendant(
3265 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003266}
3267
3268TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3269 EXPECT_TRUE(matches(
3270 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003271 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00003272 isTemplateInstantiation())));
3273}
3274
3275TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3276 EXPECT_TRUE(matches(
3277 "template <typename T> class X { T t; }; class A {};"
3278 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003279 recordDecl(isTemplateInstantiation(), hasDescendant(
3280 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003281}
3282
3283TEST(IsTemplateInstantiation,
3284 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3285 EXPECT_TRUE(matches(
3286 "template <typename T> class X {};"
3287 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003288 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003289}
3290
3291TEST(IsTemplateInstantiation,
3292 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3293 EXPECT_TRUE(matches(
3294 "class A {};"
3295 "class X {"
3296 " template <typename U> class Y { U u; };"
3297 " Y<A> y;"
3298 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003299 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003300}
3301
3302TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3303 // FIXME: Figure out whether this makes sense. It doesn't affect the
3304 // normal use case as long as the uppermost instantiation always is marked
3305 // as template instantiation, but it might be confusing as a predicate.
3306 EXPECT_TRUE(matches(
3307 "class A {};"
3308 "template <typename T> class X {"
3309 " template <typename U> class Y { U u; };"
3310 " Y<T> y;"
3311 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003312 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003313}
3314
3315TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3316 EXPECT_TRUE(notMatches(
3317 "template <typename T> class X {}; class A {};"
3318 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003319 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003320}
3321
3322TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3323 EXPECT_TRUE(notMatches(
3324 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003325 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003326}
3327
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003328TEST(IsExplicitTemplateSpecialization,
3329 DoesNotMatchPrimaryTemplate) {
3330 EXPECT_TRUE(notMatches(
3331 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003332 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003333 EXPECT_TRUE(notMatches(
3334 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003335 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003336}
3337
3338TEST(IsExplicitTemplateSpecialization,
3339 DoesNotMatchExplicitTemplateInstantiations) {
3340 EXPECT_TRUE(notMatches(
3341 "template <typename T> class X {};"
3342 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003343 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003344 EXPECT_TRUE(notMatches(
3345 "template <typename T> void f(T t) {}"
3346 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003347 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003348}
3349
3350TEST(IsExplicitTemplateSpecialization,
3351 DoesNotMatchImplicitTemplateInstantiations) {
3352 EXPECT_TRUE(notMatches(
3353 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003354 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003355 EXPECT_TRUE(notMatches(
3356 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003357 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003358}
3359
3360TEST(IsExplicitTemplateSpecialization,
3361 MatchesExplicitTemplateSpecializations) {
3362 EXPECT_TRUE(matches(
3363 "template <typename T> class X {};"
3364 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003365 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003366 EXPECT_TRUE(matches(
3367 "template <typename T> void f(T t) {}"
3368 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003369 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003370}
3371
Manuel Klimek579b1202012-09-07 09:26:10 +00003372TEST(HasAncenstor, MatchesDeclarationAncestors) {
3373 EXPECT_TRUE(matches(
3374 "class A { class B { class C {}; }; };",
3375 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3376}
3377
3378TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3379 EXPECT_TRUE(notMatches(
3380 "class A { class B { class C {}; }; };",
3381 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3382}
3383
3384TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3385 EXPECT_TRUE(matches(
3386 "class A { class B { void f() { C c; } class C {}; }; };",
3387 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3388 hasAncestor(recordDecl(hasName("A"))))))));
3389}
3390
3391TEST(HasAncenstor, MatchesStatementAncestors) {
3392 EXPECT_TRUE(matches(
3393 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003394 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003395}
3396
3397TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3398 EXPECT_TRUE(matches(
3399 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003400 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003401}
3402
3403TEST(HasAncestor, BindsRecursiveCombinations) {
3404 EXPECT_TRUE(matchAndVerifyResultTrue(
3405 "class C { class D { class E { class F { int y; }; }; }; };",
3406 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003407 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00003408}
3409
3410TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3411 EXPECT_TRUE(matchAndVerifyResultTrue(
3412 "class C { class D { class E { class F { int y; }; }; }; };",
3413 fieldDecl(hasAncestor(
3414 decl(
3415 hasDescendant(recordDecl(isDefinition(),
3416 hasAncestor(recordDecl())))
3417 ).bind("d")
3418 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00003419 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00003420}
3421
Manuel Klimek374516c2013-03-14 16:33:21 +00003422TEST(HasAncestor, MatchesClosestAncestor) {
3423 EXPECT_TRUE(matchAndVerifyResultTrue(
3424 "template <typename T> struct C {"
3425 " void f(int) {"
3426 " struct I { void g(T) { int x; } } i; i.g(42);"
3427 " }"
3428 "};"
3429 "template struct C<int>;",
3430 varDecl(hasName("x"),
3431 hasAncestor(functionDecl(hasParameter(
3432 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"),
3433 new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2)));
3434}
3435
Manuel Klimek579b1202012-09-07 09:26:10 +00003436TEST(HasAncestor, MatchesInTemplateInstantiations) {
3437 EXPECT_TRUE(matches(
3438 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3439 "A<int>::B::C a;",
3440 fieldDecl(hasType(asString("int")),
3441 hasAncestor(recordDecl(hasName("A"))))));
3442}
3443
3444TEST(HasAncestor, MatchesInImplicitCode) {
3445 EXPECT_TRUE(matches(
3446 "struct X {}; struct A { A() {} X x; };",
3447 constructorDecl(
3448 hasAnyConstructorInitializer(withInitializer(expr(
3449 hasAncestor(recordDecl(hasName("A")))))))));
3450}
3451
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003452TEST(HasParent, MatchesOnlyParent) {
3453 EXPECT_TRUE(matches(
3454 "void f() { if (true) { int x = 42; } }",
3455 compoundStmt(hasParent(ifStmt()))));
3456 EXPECT_TRUE(notMatches(
3457 "void f() { for (;;) { int x = 42; } }",
3458 compoundStmt(hasParent(ifStmt()))));
3459 EXPECT_TRUE(notMatches(
3460 "void f() { if (true) for (;;) { int x = 42; } }",
3461 compoundStmt(hasParent(ifStmt()))));
3462}
3463
Manuel Klimek30ace372012-12-06 14:42:48 +00003464TEST(HasAncestor, MatchesAllAncestors) {
3465 EXPECT_TRUE(matches(
3466 "template <typename T> struct C { static void f() { 42; } };"
3467 "void t() { C<int>::f(); }",
3468 integerLiteral(
3469 equals(42),
3470 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3471 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3472}
3473
3474TEST(HasParent, MatchesAllParents) {
3475 EXPECT_TRUE(matches(
3476 "template <typename T> struct C { static void f() { 42; } };"
3477 "void t() { C<int>::f(); }",
3478 integerLiteral(
3479 equals(42),
3480 hasParent(compoundStmt(hasParent(functionDecl(
3481 hasParent(recordDecl(isTemplateInstantiation())))))))));
3482 EXPECT_TRUE(matches(
3483 "template <typename T> struct C { static void f() { 42; } };"
3484 "void t() { C<int>::f(); }",
3485 integerLiteral(
3486 equals(42),
3487 hasParent(compoundStmt(hasParent(functionDecl(
3488 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3489 EXPECT_TRUE(matches(
3490 "template <typename T> struct C { static void f() { 42; } };"
3491 "void t() { C<int>::f(); }",
3492 integerLiteral(equals(42),
3493 hasParent(compoundStmt(allOf(
3494 hasParent(functionDecl(
3495 hasParent(recordDecl(isTemplateInstantiation())))),
3496 hasParent(functionDecl(hasParent(recordDecl(
3497 unless(isTemplateInstantiation())))))))))));
Manuel Klimek374516c2013-03-14 16:33:21 +00003498 EXPECT_TRUE(
3499 notMatches("template <typename T> struct C { static void f() {} };"
3500 "void t() { C<int>::f(); }",
3501 compoundStmt(hasParent(recordDecl()))));
Manuel Klimek30ace372012-12-06 14:42:48 +00003502}
3503
Daniel Jasperce620072012-10-17 08:52:59 +00003504TEST(TypeMatching, MatchesTypes) {
3505 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3506}
3507
3508TEST(TypeMatching, MatchesArrayTypes) {
3509 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3510 EXPECT_TRUE(matches("int a[42];", arrayType()));
3511 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3512
3513 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3514 arrayType(hasElementType(builtinType()))));
3515
3516 EXPECT_TRUE(matches(
3517 "int const a[] = { 2, 3 };",
3518 qualType(arrayType(hasElementType(builtinType())))));
3519 EXPECT_TRUE(matches(
3520 "int const a[] = { 2, 3 };",
3521 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3522 EXPECT_TRUE(matches(
3523 "typedef const int T; T x[] = { 1, 2 };",
3524 qualType(isConstQualified(), arrayType())));
3525
3526 EXPECT_TRUE(notMatches(
3527 "int a[] = { 2, 3 };",
3528 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3529 EXPECT_TRUE(notMatches(
3530 "int a[] = { 2, 3 };",
3531 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3532 EXPECT_TRUE(notMatches(
3533 "int const a[] = { 2, 3 };",
3534 qualType(arrayType(hasElementType(builtinType())),
3535 unless(isConstQualified()))));
3536
3537 EXPECT_TRUE(matches("int a[2];",
3538 constantArrayType(hasElementType(builtinType()))));
3539 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3540}
3541
3542TEST(TypeMatching, MatchesComplexTypes) {
3543 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3544 EXPECT_TRUE(matches(
3545 "_Complex float f;",
3546 complexType(hasElementType(builtinType()))));
3547 EXPECT_TRUE(notMatches(
3548 "_Complex float f;",
3549 complexType(hasElementType(isInteger()))));
3550}
3551
3552TEST(TypeMatching, MatchesConstantArrayTypes) {
3553 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3554 EXPECT_TRUE(notMatches(
3555 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3556 constantArrayType(hasElementType(builtinType()))));
3557
3558 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3559 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3560 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3561}
3562
3563TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3564 EXPECT_TRUE(matches(
3565 "template <typename T, int Size> class array { T data[Size]; };",
3566 dependentSizedArrayType()));
3567 EXPECT_TRUE(notMatches(
3568 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3569 dependentSizedArrayType()));
3570}
3571
3572TEST(TypeMatching, MatchesIncompleteArrayType) {
3573 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3574 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3575
3576 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3577 incompleteArrayType()));
3578}
3579
3580TEST(TypeMatching, MatchesVariableArrayType) {
3581 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3582 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3583
3584 EXPECT_TRUE(matches(
3585 "void f(int b) { int a[b]; }",
3586 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3587 varDecl(hasName("b")))))))));
3588}
3589
3590TEST(TypeMatching, MatchesAtomicTypes) {
3591 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3592
3593 EXPECT_TRUE(matches("_Atomic(int) i;",
3594 atomicType(hasValueType(isInteger()))));
3595 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3596 atomicType(hasValueType(isInteger()))));
3597}
3598
3599TEST(TypeMatching, MatchesAutoTypes) {
3600 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3601 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3602 autoType()));
3603
Richard Smith9b131752013-04-30 21:23:01 +00003604 // FIXME: Matching against the type-as-written can't work here, because the
3605 // type as written was not deduced.
3606 //EXPECT_TRUE(matches("auto a = 1;",
3607 // autoType(hasDeducedType(isInteger()))));
3608 //EXPECT_TRUE(notMatches("auto b = 2.0;",
3609 // autoType(hasDeducedType(isInteger()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003610}
3611
Daniel Jaspera267cf62012-10-29 10:14:44 +00003612TEST(TypeMatching, MatchesFunctionTypes) {
3613 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3614 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3615}
3616
Edwin Vane88be2fd2013-04-01 18:33:34 +00003617TEST(TypeMatching, MatchesParenType) {
3618 EXPECT_TRUE(
3619 matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
3620 EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
3621
3622 EXPECT_TRUE(matches(
3623 "int (*ptr_to_func)(int);",
3624 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3625 EXPECT_TRUE(notMatches(
3626 "int (*ptr_to_array)[4];",
3627 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3628}
3629
Daniel Jasperce620072012-10-17 08:52:59 +00003630TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003631 // FIXME: Reactive when these tests can be more specific (not matching
3632 // implicit code on certain platforms), likely when we have hasDescendant for
3633 // Types/TypeLocs.
3634 //EXPECT_TRUE(matchAndVerifyResultTrue(
3635 // "int* a;",
3636 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3637 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3638 //EXPECT_TRUE(matchAndVerifyResultTrue(
3639 // "int* a;",
3640 // pointerTypeLoc().bind("loc"),
3641 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003642 EXPECT_TRUE(matches(
3643 "int** a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003644 loc(pointerType(pointee(qualType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003645 EXPECT_TRUE(matches(
3646 "int** a;",
3647 loc(pointerType(pointee(pointerType())))));
3648 EXPECT_TRUE(matches(
3649 "int* b; int* * const a = &b;",
3650 loc(qualType(isConstQualified(), pointerType()))));
3651
3652 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003653 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3654 hasType(blockPointerType()))));
3655 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3656 hasType(memberPointerType()))));
3657 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3658 hasType(pointerType()))));
3659 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3660 hasType(referenceType()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003661 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3662 hasType(lValueReferenceType()))));
3663 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3664 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003665
Daniel Jasper1802daf2012-10-17 13:35:36 +00003666 Fragment = "int *ptr;";
3667 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3668 hasType(blockPointerType()))));
3669 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3670 hasType(memberPointerType()))));
3671 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3672 hasType(pointerType()))));
3673 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3674 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003675
Daniel Jasper1802daf2012-10-17 13:35:36 +00003676 Fragment = "int a; int &ref = a;";
3677 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3678 hasType(blockPointerType()))));
3679 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3680 hasType(memberPointerType()))));
3681 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3682 hasType(pointerType()))));
3683 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3684 hasType(referenceType()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003685 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3686 hasType(lValueReferenceType()))));
3687 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3688 hasType(rValueReferenceType()))));
3689
3690 Fragment = "int &&ref = 2;";
3691 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3692 hasType(blockPointerType()))));
3693 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3694 hasType(memberPointerType()))));
3695 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3696 hasType(pointerType()))));
3697 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3698 hasType(referenceType()))));
3699 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3700 hasType(lValueReferenceType()))));
3701 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3702 hasType(rValueReferenceType()))));
3703}
3704
3705TEST(TypeMatching, AutoRefTypes) {
3706 std::string Fragment = "auto a = 1;"
3707 "auto b = a;"
3708 "auto &c = a;"
3709 "auto &&d = c;"
3710 "auto &&e = 2;";
3711 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
3712 hasType(referenceType()))));
3713 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
3714 hasType(referenceType()))));
3715 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3716 hasType(referenceType()))));
3717 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3718 hasType(lValueReferenceType()))));
3719 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
3720 hasType(rValueReferenceType()))));
3721 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3722 hasType(referenceType()))));
3723 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3724 hasType(lValueReferenceType()))));
3725 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
3726 hasType(rValueReferenceType()))));
3727 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3728 hasType(referenceType()))));
3729 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
3730 hasType(lValueReferenceType()))));
3731 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3732 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003733}
3734
3735TEST(TypeMatching, PointeeTypes) {
3736 EXPECT_TRUE(matches("int b; int &a = b;",
3737 referenceType(pointee(builtinType()))));
3738 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3739
3740 EXPECT_TRUE(matches("int *a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003741 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003742
3743 EXPECT_TRUE(matches(
3744 "int const *A;",
3745 pointerType(pointee(isConstQualified(), builtinType()))));
3746 EXPECT_TRUE(notMatches(
3747 "int *A;",
3748 pointerType(pointee(isConstQualified(), builtinType()))));
3749}
3750
3751TEST(TypeMatching, MatchesPointersToConstTypes) {
3752 EXPECT_TRUE(matches("int b; int * const a = &b;",
3753 loc(pointerType())));
3754 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003755 loc(pointerType())));
Daniel Jasperce620072012-10-17 08:52:59 +00003756 EXPECT_TRUE(matches(
3757 "int b; const int * a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003758 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003759 EXPECT_TRUE(matches(
3760 "int b; const int * a = &b;",
3761 pointerType(pointee(builtinType()))));
3762}
3763
3764TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003765 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3766 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003767}
3768
Edwin Vane3abf7782013-02-25 14:49:29 +00003769TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vane742d9e72013-02-25 20:43:32 +00003770 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vane3abf7782013-02-25 14:49:29 +00003771 templateSpecializationType()));
3772}
3773
Edwin Vane742d9e72013-02-25 20:43:32 +00003774TEST(TypeMatching, MatchesRecordType) {
3775 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek0cc798f2013-02-27 11:56:58 +00003776 EXPECT_TRUE(matches("struct S{}; S s;",
3777 recordType(hasDeclaration(recordDecl(hasName("S"))))));
3778 EXPECT_TRUE(notMatches("int i;",
3779 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003780}
3781
3782TEST(TypeMatching, MatchesElaboratedType) {
3783 EXPECT_TRUE(matches(
3784 "namespace N {"
3785 " namespace M {"
3786 " class D {};"
3787 " }"
3788 "}"
3789 "N::M::D d;", elaboratedType()));
3790 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
3791 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
3792}
3793
3794TEST(ElaboratedTypeNarrowing, hasQualifier) {
3795 EXPECT_TRUE(matches(
3796 "namespace N {"
3797 " namespace M {"
3798 " class D {};"
3799 " }"
3800 "}"
3801 "N::M::D d;",
3802 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3803 EXPECT_TRUE(notMatches(
3804 "namespace M {"
3805 " class D {};"
3806 "}"
3807 "M::D d;",
3808 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vaneaec89ac2013-03-04 17:51:00 +00003809 EXPECT_TRUE(notMatches(
3810 "struct D {"
3811 "} d;",
3812 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003813}
3814
3815TEST(ElaboratedTypeNarrowing, namesType) {
3816 EXPECT_TRUE(matches(
3817 "namespace N {"
3818 " namespace M {"
3819 " class D {};"
3820 " }"
3821 "}"
3822 "N::M::D d;",
3823 elaboratedType(elaboratedType(namesType(recordType(
3824 hasDeclaration(namedDecl(hasName("D")))))))));
3825 EXPECT_TRUE(notMatches(
3826 "namespace M {"
3827 " class D {};"
3828 "}"
3829 "M::D d;",
3830 elaboratedType(elaboratedType(namesType(typedefType())))));
3831}
3832
Daniel Jaspera7564432012-09-13 13:11:25 +00003833TEST(NNS, MatchesNestedNameSpecifiers) {
3834 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3835 nestedNameSpecifier()));
3836 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3837 nestedNameSpecifier()));
3838 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3839 nestedNameSpecifier()));
3840
3841 EXPECT_TRUE(matches(
3842 "struct A { static void f() {} }; void g() { A::f(); }",
3843 nestedNameSpecifier()));
3844 EXPECT_TRUE(notMatches(
3845 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3846 nestedNameSpecifier()));
3847}
3848
Daniel Jasperb54b7642012-09-20 14:12:57 +00003849TEST(NullStatement, SimpleCases) {
3850 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3851 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3852}
3853
Daniel Jaspera7564432012-09-13 13:11:25 +00003854TEST(NNS, MatchesTypes) {
3855 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3856 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3857 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3858 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3859 Matcher));
3860 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3861}
3862
3863TEST(NNS, MatchesNamespaceDecls) {
3864 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3865 specifiesNamespace(hasName("ns")));
3866 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3867 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3868 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3869}
3870
3871TEST(NNS, BindsNestedNameSpecifiers) {
3872 EXPECT_TRUE(matchAndVerifyResultTrue(
3873 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3874 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3875 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3876}
3877
3878TEST(NNS, BindsNestedNameSpecifierLocs) {
3879 EXPECT_TRUE(matchAndVerifyResultTrue(
3880 "namespace ns { struct B {}; } ns::B b;",
3881 loc(nestedNameSpecifier()).bind("loc"),
3882 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3883}
3884
3885TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3886 EXPECT_TRUE(matches(
3887 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3888 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3889 EXPECT_TRUE(matches(
3890 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003891 nestedNameSpecifierLoc(hasPrefix(
3892 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003893}
3894
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003895TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3896 std::string Fragment =
3897 "namespace a { struct A { struct B { struct C {}; }; }; };"
3898 "void f() { a::A::B::C c; }";
3899 EXPECT_TRUE(matches(
3900 Fragment,
3901 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3902 hasDescendant(nestedNameSpecifier(
3903 specifiesNamespace(hasName("a")))))));
3904 EXPECT_TRUE(notMatches(
3905 Fragment,
3906 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3907 has(nestedNameSpecifier(
3908 specifiesNamespace(hasName("a")))))));
3909 EXPECT_TRUE(matches(
3910 Fragment,
3911 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3912 has(nestedNameSpecifier(
3913 specifiesNamespace(hasName("a")))))));
3914
3915 // Not really useful because a NestedNameSpecifier can af at most one child,
3916 // but to complete the interface.
3917 EXPECT_TRUE(matchAndVerifyResultTrue(
3918 Fragment,
3919 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3920 forEach(nestedNameSpecifier().bind("x"))),
3921 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3922}
3923
3924TEST(NNS, NestedNameSpecifiersAsDescendants) {
3925 std::string Fragment =
3926 "namespace a { struct A { struct B { struct C {}; }; }; };"
3927 "void f() { a::A::B::C c; }";
3928 EXPECT_TRUE(matches(
3929 Fragment,
3930 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3931 asString("struct a::A")))))));
3932 EXPECT_TRUE(matchAndVerifyResultTrue(
3933 Fragment,
3934 functionDecl(hasName("f"),
3935 forEachDescendant(nestedNameSpecifier().bind("x"))),
3936 // Nested names: a, a::A and a::A::B.
3937 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3938}
3939
3940TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3941 std::string Fragment =
3942 "namespace a { struct A { struct B { struct C {}; }; }; };"
3943 "void f() { a::A::B::C c; }";
3944 EXPECT_TRUE(matches(
3945 Fragment,
3946 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3947 hasDescendant(loc(nestedNameSpecifier(
3948 specifiesNamespace(hasName("a"))))))));
3949 EXPECT_TRUE(notMatches(
3950 Fragment,
3951 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3952 has(loc(nestedNameSpecifier(
3953 specifiesNamespace(hasName("a"))))))));
3954 EXPECT_TRUE(matches(
3955 Fragment,
3956 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3957 has(loc(nestedNameSpecifier(
3958 specifiesNamespace(hasName("a"))))))));
3959
3960 EXPECT_TRUE(matchAndVerifyResultTrue(
3961 Fragment,
3962 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3963 forEach(nestedNameSpecifierLoc().bind("x"))),
3964 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3965}
3966
3967TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3968 std::string Fragment =
3969 "namespace a { struct A { struct B { struct C {}; }; }; };"
3970 "void f() { a::A::B::C c; }";
3971 EXPECT_TRUE(matches(
3972 Fragment,
3973 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3974 asString("struct a::A"))))))));
3975 EXPECT_TRUE(matchAndVerifyResultTrue(
3976 Fragment,
3977 functionDecl(hasName("f"),
3978 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3979 // Nested names: a, a::A and a::A::B.
3980 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3981}
3982
Manuel Klimek60969f52013-02-01 13:41:35 +00003983template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003984public:
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003985 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
3986 StringRef InnerId)
3987 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jasper452abbc2012-10-29 10:48:25 +00003988 }
3989
Manuel Klimek60969f52013-02-01 13:41:35 +00003990 virtual bool run(const BoundNodes *Nodes) { return false; }
3991
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003992 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3993 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003994 return selectFirst<const T>(InnerId,
3995 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003996 }
3997private:
3998 std::string Id;
3999 internal::Matcher<T> InnerMatcher;
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004000 std::string InnerId;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004001};
4002
4003TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00004004 EXPECT_TRUE(matchAndVerifyResultTrue(
4005 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4006 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004007 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
4008 "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004009 EXPECT_TRUE(matchAndVerifyResultFalse(
4010 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4011 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004012 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
4013 "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004014}
4015
4016TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00004017 EXPECT_TRUE(matchAndVerifyResultTrue(
4018 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004019 new VerifyMatchOnNode<clang::Stmt>(
4020 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004021 EXPECT_TRUE(matchAndVerifyResultFalse(
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(declStmt().bind("decl"))), "decl")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004025}
4026
4027TEST(MatchFinder, CanMatchSingleNodesRecursively) {
4028 EXPECT_TRUE(matchAndVerifyResultTrue(
4029 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4030 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004031 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004032 EXPECT_TRUE(matchAndVerifyResultFalse(
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::Z")).bind("Z"))), "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004036}
4037
Manuel Klimekfa37c5c2013-02-07 12:42:10 +00004038template <typename T>
4039class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
4040public:
4041 virtual bool run(const BoundNodes *Nodes) { return false; }
4042
4043 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4044 const T *Node = Nodes->getNodeAs<T>("");
4045 return verify(*Nodes, *Context, Node);
4046 }
4047
4048 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
4049 return selectFirst<const T>(
4050 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
4051 *Node, Context)) != NULL;
4052 }
4053 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
4054 return selectFirst<const T>(
4055 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
4056 *Node, Context)) != NULL;
4057 }
4058};
4059
4060TEST(IsEqualTo, MatchesNodesByIdentity) {
4061 EXPECT_TRUE(matchAndVerifyResultTrue(
4062 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
4063 new VerifyAncestorHasChildIsEqual<Decl>()));
4064 EXPECT_TRUE(
4065 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
4066 new VerifyAncestorHasChildIsEqual<Stmt>()));
4067}
4068
Manuel Klimeke5793282012-11-02 01:31:03 +00004069class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
4070public:
4071 VerifyStartOfTranslationUnit() : Called(false) {}
4072 virtual void run(const MatchFinder::MatchResult &Result) {
4073 EXPECT_TRUE(Called);
4074 }
4075 virtual void onStartOfTranslationUnit() {
4076 Called = true;
4077 }
4078 bool Called;
4079};
4080
4081TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
4082 MatchFinder Finder;
4083 VerifyStartOfTranslationUnit VerifyCallback;
4084 Finder.addMatcher(decl(), &VerifyCallback);
4085 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4086 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4087 EXPECT_TRUE(VerifyCallback.Called);
4088}
4089
Peter Collingbourne8f9e5902013-05-28 19:21:51 +00004090class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback {
4091public:
4092 VerifyEndOfTranslationUnit() : Called(false) {}
4093 virtual void run(const MatchFinder::MatchResult &Result) {
4094 EXPECT_FALSE(Called);
4095 }
4096 virtual void onEndOfTranslationUnit() {
4097 Called = true;
4098 }
4099 bool Called;
4100};
4101
4102TEST(MatchFinder, InterceptsEndOfTranslationUnit) {
4103 MatchFinder Finder;
4104 VerifyEndOfTranslationUnit VerifyCallback;
4105 Finder.addMatcher(decl(), &VerifyCallback);
4106 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4107 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4108 EXPECT_TRUE(VerifyCallback.Called);
4109}
4110
Manuel Klimekcf52ca62013-06-20 14:06:32 +00004111TEST(EqualsBoundNodeMatcher, QualType) {
4112 EXPECT_TRUE(matches(
4113 "int i = 1;", varDecl(hasType(qualType().bind("type")),
4114 hasInitializer(ignoringParenImpCasts(
4115 hasType(qualType(equalsBoundNode("type"))))))));
4116 EXPECT_TRUE(notMatches("int i = 1.f;",
4117 varDecl(hasType(qualType().bind("type")),
4118 hasInitializer(ignoringParenImpCasts(hasType(
4119 qualType(equalsBoundNode("type"))))))));
4120}
4121
4122TEST(EqualsBoundNodeMatcher, NonMatchingTypes) {
4123 EXPECT_TRUE(notMatches(
4124 "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"),
4125 hasInitializer(ignoringParenImpCasts(
4126 hasType(qualType(equalsBoundNode("type"))))))));
4127}
4128
4129TEST(EqualsBoundNodeMatcher, Stmt) {
4130 EXPECT_TRUE(
4131 matches("void f() { if(true) {} }",
4132 stmt(allOf(ifStmt().bind("if"),
4133 hasParent(stmt(has(stmt(equalsBoundNode("if")))))))));
4134
4135 EXPECT_TRUE(notMatches(
4136 "void f() { if(true) { if (true) {} } }",
4137 stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if")))))));
4138}
4139
4140TEST(EqualsBoundNodeMatcher, Decl) {
4141 EXPECT_TRUE(matches(
4142 "class X { class Y {}; };",
4143 decl(allOf(recordDecl(hasName("::X::Y")).bind("record"),
4144 hasParent(decl(has(decl(equalsBoundNode("record")))))))));
4145
4146 EXPECT_TRUE(notMatches("class X { class Y {}; };",
4147 decl(allOf(recordDecl(hasName("::X")).bind("record"),
4148 has(decl(equalsBoundNode("record")))))));
4149}
4150
4151TEST(EqualsBoundNodeMatcher, Type) {
4152 EXPECT_TRUE(matches(
4153 "class X { int a; int b; };",
4154 recordDecl(
4155 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4156 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4157
4158 EXPECT_TRUE(notMatches(
4159 "class X { int a; double b; };",
4160 recordDecl(
4161 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4162 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4163}
4164
4165TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) {
4166
4167 EXPECT_TRUE(matchAndVerifyResultTrue(
4168 "int f() {"
4169 " if (1) {"
4170 " int i = 9;"
4171 " }"
4172 " int j = 10;"
4173 " {"
4174 " float k = 9.0;"
4175 " }"
4176 " return 0;"
4177 "}",
4178 // Look for variable declarations within functions whose type is the same
4179 // as the function return type.
4180 functionDecl(returns(qualType().bind("type")),
4181 forEachDescendant(varDecl(hasType(
4182 qualType(equalsBoundNode("type")))).bind("decl"))),
4183 // Only i and j should match, not k.
4184 new VerifyIdIsBoundTo<VarDecl>("decl", 2)));
4185}
4186
4187TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) {
4188 EXPECT_TRUE(matchAndVerifyResultTrue(
4189 "void f() {"
4190 " int x;"
4191 " double d;"
4192 " x = d + x - d + x;"
4193 "}",
4194 functionDecl(
4195 hasName("f"), forEachDescendant(varDecl().bind("d")),
4196 forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))),
4197 new VerifyIdIsBoundTo<VarDecl>("d", 5)));
4198}
4199
Manuel Klimek4da21662012-07-06 05:48:52 +00004200} // end namespace ast_matchers
4201} // end namespace clang