blob: cc13c01fec39eedbbe8691f5fa4c90f7921c948a [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
Peter Collingbourned2bd5892013-11-07 22:30:32 +000043TEST(Finder, DynamicOnlyAcceptsSomeMatchers) {
44 MatchFinder Finder;
45 EXPECT_TRUE(Finder.addDynamicMatcher(decl(), NULL));
46 EXPECT_TRUE(Finder.addDynamicMatcher(callExpr(), NULL));
47 EXPECT_TRUE(Finder.addDynamicMatcher(constantArrayType(hasSize(42)), NULL));
48
49 // Do not accept non-toplevel matchers.
50 EXPECT_FALSE(Finder.addDynamicMatcher(isArrow(), NULL));
51 EXPECT_FALSE(Finder.addDynamicMatcher(hasSize(2), NULL));
52 EXPECT_FALSE(Finder.addDynamicMatcher(hasName("x"), NULL));
53}
54
Manuel Klimek715c9562012-07-25 10:02:02 +000055TEST(Decl, MatchesDeclarations) {
56 EXPECT_TRUE(notMatches("", decl(usingDecl())));
57 EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
58 decl(usingDecl())));
59}
60
Manuel Klimek4da21662012-07-06 05:48:52 +000061TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000062 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +000063 EXPECT_TRUE(matches("typedef int X;", NamedX));
64 EXPECT_TRUE(matches("int X;", NamedX));
65 EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
66 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
67 EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
68 EXPECT_TRUE(matches("namespace X { }", NamedX));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000069 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek4da21662012-07-06 05:48:52 +000070
71 EXPECT_TRUE(notMatches("#define X 1", NamedX));
72}
73
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000074TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000075 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000076 EXPECT_TRUE(matches("typedef int Xa;", NamedX));
77 EXPECT_TRUE(matches("int Xb;", NamedX));
78 EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
79 EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
80 EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
81 EXPECT_TRUE(matches("namespace Xij { }", NamedX));
82 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
83
84 EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
85
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000086 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000087 EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
88 EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
89
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000090 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000091 EXPECT_TRUE(matches("int abc;", Abc));
92 EXPECT_TRUE(matches("int aFOObBARc;", Abc));
93 EXPECT_TRUE(notMatches("int cab;", Abc));
94 EXPECT_TRUE(matches("int cabc;", Abc));
Manuel Klimekec33b6f2012-12-10 07:08:53 +000095
96 DeclarationMatcher StartsWithK = namedDecl(matchesName(":k[^:]*$"));
97 EXPECT_TRUE(matches("int k;", StartsWithK));
98 EXPECT_TRUE(matches("int kAbc;", StartsWithK));
99 EXPECT_TRUE(matches("namespace x { int kTest; }", StartsWithK));
100 EXPECT_TRUE(matches("class C { int k; };", StartsWithK));
101 EXPECT_TRUE(notMatches("class C { int ckc; };", StartsWithK));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000102}
103
Manuel Klimek4da21662012-07-06 05:48:52 +0000104TEST(DeclarationMatcher, MatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000105 DeclarationMatcher ClassMatcher(recordDecl());
Manuel Klimeke265c872012-07-10 14:21:30 +0000106#if !defined(_MSC_VER)
Manuel Klimek4da21662012-07-06 05:48:52 +0000107 EXPECT_FALSE(matches("", ClassMatcher));
Manuel Klimeke265c872012-07-10 14:21:30 +0000108#else
109 // Matches class type_info.
110 EXPECT_TRUE(matches("", ClassMatcher));
111#endif
Manuel Klimek4da21662012-07-06 05:48:52 +0000112
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000113 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000114 EXPECT_TRUE(matches("class X;", ClassX));
115 EXPECT_TRUE(matches("class X {};", ClassX));
116 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
117 EXPECT_TRUE(notMatches("", ClassX));
118}
119
120TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000121 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000122
123 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000124 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
125 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek4da21662012-07-06 05:48:52 +0000126 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
127 EXPECT_TRUE(notMatches("", IsDerivedFromX));
128
Daniel Jasper63d88722012-09-12 21:14:15 +0000129 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000130
131 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
132 EXPECT_TRUE(matches("class X {};", IsAX));
133 EXPECT_TRUE(matches("class X;", IsAX));
134 EXPECT_TRUE(notMatches("class Y;", IsAX));
135 EXPECT_TRUE(notMatches("", IsAX));
136
Manuel Klimek4da21662012-07-06 05:48:52 +0000137 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000138 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000139 EXPECT_TRUE(
140 matches("class X {}; class Y : public X {}; class Z : public Y {};",
141 ZIsDerivedFromX));
142 EXPECT_TRUE(
143 matches("class X {};"
144 "template<class T> class Y : public X {};"
145 "class Z : public Y<int> {};", ZIsDerivedFromX));
146 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
147 ZIsDerivedFromX));
148 EXPECT_TRUE(
149 matches("template<class T> class X {}; "
150 "template<class T> class Z : public X<T> {};",
151 ZIsDerivedFromX));
152 EXPECT_TRUE(
153 matches("template<class T, class U=T> class X {}; "
154 "template<class T> class Z : public X<T> {};",
155 ZIsDerivedFromX));
156 EXPECT_TRUE(
157 notMatches("template<class X> class A { class Z : public X {}; };",
158 ZIsDerivedFromX));
159 EXPECT_TRUE(
160 matches("template<class X> class A { public: class Z : public X {}; }; "
161 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
162 EXPECT_TRUE(
163 matches("template <class T> class X {}; "
164 "template<class Y> class A { class Z : public X<Y> {}; };",
165 ZIsDerivedFromX));
166 EXPECT_TRUE(
167 notMatches("template<template<class T> class X> class A { "
168 " class Z : public X<int> {}; };", ZIsDerivedFromX));
169 EXPECT_TRUE(
170 matches("template<template<class T> class X> class A { "
171 " public: class Z : public X<int> {}; }; "
172 "template<class T> class X {}; void y() { A<X>::Z z; }",
173 ZIsDerivedFromX));
174 EXPECT_TRUE(
175 notMatches("template<class X> class A { class Z : public X::D {}; };",
176 ZIsDerivedFromX));
177 EXPECT_TRUE(
178 matches("template<class X> class A { public: "
179 " class Z : public X::D {}; }; "
180 "class Y { public: class X {}; typedef X D; }; "
181 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
182 EXPECT_TRUE(
183 matches("class X {}; typedef X Y; class Z : public Y {};",
184 ZIsDerivedFromX));
185 EXPECT_TRUE(
186 matches("template<class T> class Y { typedef typename T::U X; "
187 " class Z : public X {}; };", ZIsDerivedFromX));
188 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
189 ZIsDerivedFromX));
190 EXPECT_TRUE(
191 notMatches("template<class T> class X {}; "
192 "template<class T> class A { class Z : public X<T>::D {}; };",
193 ZIsDerivedFromX));
194 EXPECT_TRUE(
195 matches("template<class T> class X { public: typedef X<T> D; }; "
196 "template<class T> class A { public: "
197 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
198 ZIsDerivedFromX));
199 EXPECT_TRUE(
200 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
201 ZIsDerivedFromX));
202 EXPECT_TRUE(
203 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
204 ZIsDerivedFromX));
205 EXPECT_TRUE(
206 matches("class X {}; class Y : public X {}; "
207 "typedef Y V; typedef V W; class Z : public W {};",
208 ZIsDerivedFromX));
209 EXPECT_TRUE(
210 matches("template<class T, class U> class X {}; "
211 "template<class T> class A { class Z : public X<T, int> {}; };",
212 ZIsDerivedFromX));
213 EXPECT_TRUE(
214 notMatches("template<class X> class D { typedef X A; typedef A B; "
215 " typedef B C; class Z : public C {}; };",
216 ZIsDerivedFromX));
217 EXPECT_TRUE(
218 matches("class X {}; typedef X A; typedef A B; "
219 "class Z : public B {};", ZIsDerivedFromX));
220 EXPECT_TRUE(
221 matches("class X {}; typedef X A; typedef A B; typedef B C; "
222 "class Z : public C {};", ZIsDerivedFromX));
223 EXPECT_TRUE(
224 matches("class U {}; typedef U X; typedef X V; "
225 "class Z : public V {};", ZIsDerivedFromX));
226 EXPECT_TRUE(
227 matches("class Base {}; typedef Base X; "
228 "class Z : public Base {};", ZIsDerivedFromX));
229 EXPECT_TRUE(
230 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
231 "class Z : public Base {};", ZIsDerivedFromX));
232 EXPECT_TRUE(
233 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
234 "class Z : public Base {};", ZIsDerivedFromX));
235 EXPECT_TRUE(
236 matches("class A {}; typedef A X; typedef A Y; "
237 "class Z : public Y {};", ZIsDerivedFromX));
238 EXPECT_TRUE(
239 notMatches("template <typename T> class Z;"
240 "template <> class Z<void> {};"
241 "template <typename T> class Z : public Z<void> {};",
242 IsDerivedFromX));
243 EXPECT_TRUE(
244 matches("template <typename T> class X;"
245 "template <> class X<void> {};"
246 "template <typename T> class X : public X<void> {};",
247 IsDerivedFromX));
248 EXPECT_TRUE(matches(
249 "class X {};"
250 "template <typename T> class Z;"
251 "template <> class Z<void> {};"
252 "template <typename T> class Z : public Z<void>, public X {};",
253 ZIsDerivedFromX));
Manuel Klimek987c2f52012-12-04 13:40:29 +0000254 EXPECT_TRUE(
255 notMatches("template<int> struct X;"
256 "template<int i> struct X : public X<i-1> {};",
257 recordDecl(isDerivedFrom(recordDecl(hasName("Some"))))));
258 EXPECT_TRUE(matches(
259 "struct A {};"
260 "template<int> struct X;"
261 "template<int i> struct X : public X<i-1> {};"
262 "template<> struct X<0> : public A {};"
263 "struct B : public X<42> {};",
264 recordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000265
266 // FIXME: Once we have better matchers for template type matching,
267 // get rid of the Variable(...) matching and match the right template
268 // declarations directly.
269 const char *RecursiveTemplateOneParameter =
270 "class Base1 {}; class Base2 {};"
271 "template <typename T> class Z;"
272 "template <> class Z<void> : public Base1 {};"
273 "template <> class Z<int> : public Base2 {};"
274 "template <> class Z<float> : public Z<void> {};"
275 "template <> class Z<double> : public Z<int> {};"
276 "template <typename T> class Z : public Z<float>, public Z<double> {};"
277 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
278 EXPECT_TRUE(matches(
279 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000280 varDecl(hasName("z_float"),
281 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000282 EXPECT_TRUE(notMatches(
283 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000284 varDecl(hasName("z_float"),
285 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000286 EXPECT_TRUE(matches(
287 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000288 varDecl(hasName("z_char"),
289 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
290 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000291
292 const char *RecursiveTemplateTwoParameters =
293 "class Base1 {}; class Base2 {};"
294 "template <typename T1, typename T2> class Z;"
295 "template <typename T> class Z<void, T> : public Base1 {};"
296 "template <typename T> class Z<int, T> : public Base2 {};"
297 "template <typename T> class Z<float, T> : public Z<void, T> {};"
298 "template <typename T> class Z<double, T> : public Z<int, T> {};"
299 "template <typename T1, typename T2> class Z : "
300 " public Z<float, T2>, public Z<double, T2> {};"
301 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
302 " Z<char, void> z_char; }";
303 EXPECT_TRUE(matches(
304 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000305 varDecl(hasName("z_float"),
306 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000307 EXPECT_TRUE(notMatches(
308 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000309 varDecl(hasName("z_float"),
310 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000311 EXPECT_TRUE(matches(
312 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000313 varDecl(hasName("z_char"),
314 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
315 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000316 EXPECT_TRUE(matches(
317 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000318 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000319 EXPECT_TRUE(notMatches(
320 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000321 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000322
323 EXPECT_TRUE(matches(
324 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000325 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimekb56da8c2013-08-02 21:24:09 +0000326
327 EXPECT_TRUE(matches(
328 "template<typename T> class X {};"
329 "template<typename T> using Z = X<T>;"
330 "template <typename T> class Y : Z<T> {};",
331 recordDecl(isDerivedFrom(namedDecl(hasName("X"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000332}
333
Edwin Vane6a19a972013-03-06 17:02:57 +0000334TEST(DeclarationMatcher, hasMethod) {
335 EXPECT_TRUE(matches("class A { void func(); };",
336 recordDecl(hasMethod(hasName("func")))));
337 EXPECT_TRUE(notMatches("class A { void func(); };",
338 recordDecl(hasMethod(isPublic()))));
339}
340
Daniel Jasper08f0c532012-09-18 14:17:42 +0000341TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
342 EXPECT_TRUE(matches(
343 "template <typename T> struct A {"
344 " template <typename T2> struct F {};"
345 "};"
346 "template <typename T> struct B : A<T>::template F<T> {};"
347 "B<int> b;",
348 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
349}
350
Edwin Vane742d9e72013-02-25 20:43:32 +0000351TEST(DeclarationMatcher, hasDeclContext) {
352 EXPECT_TRUE(matches(
353 "namespace N {"
354 " namespace M {"
355 " class D {};"
356 " }"
357 "}",
Daniel Jasperabe92232013-04-08 16:44:05 +0000358 recordDecl(hasDeclContext(namespaceDecl(hasName("M"))))));
Edwin Vane742d9e72013-02-25 20:43:32 +0000359 EXPECT_TRUE(notMatches(
360 "namespace N {"
361 " namespace M {"
362 " class D {};"
363 " }"
364 "}",
Daniel Jasperabe92232013-04-08 16:44:05 +0000365 recordDecl(hasDeclContext(namespaceDecl(hasName("N"))))));
366
367 EXPECT_TRUE(matches("namespace {"
368 " namespace M {"
369 " class D {};"
370 " }"
371 "}",
372 recordDecl(hasDeclContext(namespaceDecl(
373 hasName("M"), hasDeclContext(namespaceDecl()))))));
Edwin Vane742d9e72013-02-25 20:43:32 +0000374}
375
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000376TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000377 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000378 EXPECT_TRUE(notMatches("class X;", ClassX));
379 EXPECT_TRUE(notMatches("class X {};", ClassX));
380}
381
382TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000383 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000384 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
385 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
386}
387
388TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
389 EXPECT_TRUE(notMatches("template<typename T> class X { };"
390 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000391 classTemplateDecl(hasName("X"),
392 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000393}
394
395TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
396 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
397 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000398 classTemplateDecl(hasName("X"),
399 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000400}
401
Daniel Jasper6a124492012-07-12 08:50:38 +0000402TEST(AllOf, AllOverloadsWork) {
403 const char Program[] =
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000404 "struct T { };"
405 "int f(int, T*, int, int);"
406 "void g(int x) { T t; f(x, &t, 3, 4); }";
Daniel Jasper6a124492012-07-12 08:50:38 +0000407 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000408 callExpr(allOf(callee(functionDecl(hasName("f"))),
409 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000410 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000411 callExpr(allOf(callee(functionDecl(hasName("f"))),
412 hasArgument(0, declRefExpr(to(varDecl()))),
413 hasArgument(1, hasType(pointsTo(
414 recordDecl(hasName("T")))))))));
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000415 EXPECT_TRUE(matches(Program,
416 callExpr(allOf(callee(functionDecl(hasName("f"))),
417 hasArgument(0, declRefExpr(to(varDecl()))),
418 hasArgument(1, hasType(pointsTo(
419 recordDecl(hasName("T"))))),
420 hasArgument(2, integerLiteral(equals(3)))))));
421 EXPECT_TRUE(matches(Program,
422 callExpr(allOf(callee(functionDecl(hasName("f"))),
423 hasArgument(0, declRefExpr(to(varDecl()))),
424 hasArgument(1, hasType(pointsTo(
425 recordDecl(hasName("T"))))),
426 hasArgument(2, integerLiteral(equals(3))),
427 hasArgument(3, integerLiteral(equals(4)))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000428}
429
Manuel Klimek4da21662012-07-06 05:48:52 +0000430TEST(DeclarationMatcher, MatchAnyOf) {
431 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000432 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000433 EXPECT_TRUE(
434 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
435 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
436 EXPECT_TRUE(
437 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
438 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
439
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000440 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000441 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000442 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
443 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
444
Manuel Klimek4da21662012-07-06 05:48:52 +0000445 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000446 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
447 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000448 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
449 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
450 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
451 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
452 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
453 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
454}
455
456TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000457 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000458 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
459 EXPECT_TRUE(matches("class X {};", HasClassX));
460
461 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000462 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000463 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
464 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
465 EXPECT_TRUE(
466 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
467}
468
469TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
470 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000471 recordDecl(
472 has(recordDecl(
473 has(recordDecl(hasName("X"))),
474 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000475 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000476 has(recordDecl(
477 has(recordDecl(hasName("A"))),
478 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000479 hasName("C"))),
480 hasName("F"));
481
482 EXPECT_TRUE(matches(
483 "class F {"
484 " class Z {"
485 " class X {};"
486 " class Y {};"
487 " };"
488 " class C {"
489 " class A {};"
490 " class B {};"
491 " };"
492 "};", Recursive));
493
494 EXPECT_TRUE(matches(
495 "class F {"
496 " class Z {"
497 " class A {};"
498 " class X {};"
499 " class Y {};"
500 " };"
501 " class C {"
502 " class X {};"
503 " class A {};"
504 " class B {};"
505 " };"
506 "};", Recursive));
507
508 EXPECT_TRUE(matches(
509 "class O1 {"
510 " class O2 {"
511 " class F {"
512 " class Z {"
513 " class A {};"
514 " class X {};"
515 " class Y {};"
516 " };"
517 " class C {"
518 " class X {};"
519 " class A {};"
520 " class B {};"
521 " };"
522 " };"
523 " };"
524 "};", Recursive));
525}
526
527TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
528 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000529 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000530 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000531 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000532 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000533 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000534 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000535 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000536 hasName("Y"))),
537 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000538 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000539 anyOf(
540 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000541 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000542 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000543 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000544 hasName("B")))))),
545 hasName("F")));
546
547 EXPECT_TRUE(matches("class F {};", Recursive));
548 EXPECT_TRUE(matches("class Z {};", Recursive));
549 EXPECT_TRUE(matches("class C {};", Recursive));
550 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
551 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
552 EXPECT_TRUE(
553 matches("class O1 { class O2 {"
554 " class M { class N { class B {}; }; }; "
555 "}; };", Recursive));
556}
557
558TEST(DeclarationMatcher, MatchNot) {
559 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000560 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000561 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000562 unless(hasName("X")));
563 EXPECT_TRUE(notMatches("", NotClassX));
564 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
565 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
566 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
567 EXPECT_TRUE(
568 notMatches("class Y {}; class Z {}; class X : public Y {};",
569 NotClassX));
570
571 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000572 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000573 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000574 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000575 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000576 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000577 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
578 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
579 ClassXHasNotClassY));
580}
581
582TEST(DeclarationMatcher, HasDescendant) {
583 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000584 recordDecl(
585 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000586 hasName("Z"));
587 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
588 EXPECT_TRUE(
589 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
590 EXPECT_TRUE(
591 matches("class Z { class A { class Y { class X {}; }; }; };",
592 ZDescendantClassX));
593 EXPECT_TRUE(
594 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
595 ZDescendantClassX));
596 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
597
598 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000599 recordDecl(
600 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000601 hasName("X"))),
602 hasName("Z"));
603 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
604 ZDescendantClassXHasClassY));
605 EXPECT_TRUE(
606 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
607 ZDescendantClassXHasClassY));
608 EXPECT_TRUE(notMatches(
609 "class Z {"
610 " class A {"
611 " class B {"
612 " class X {"
613 " class C {"
614 " class Y {};"
615 " };"
616 " };"
617 " }; "
618 " };"
619 "};", ZDescendantClassXHasClassY));
620
621 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000622 recordDecl(
623 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
624 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000625 hasName("Z"));
626 EXPECT_TRUE(
627 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
628 ZDescendantClassXDescendantClassY));
629 EXPECT_TRUE(matches(
630 "class Z {"
631 " class A {"
632 " class X {"
633 " class B {"
634 " class Y {};"
635 " };"
636 " class Y {};"
637 " };"
638 " };"
639 "};", ZDescendantClassXDescendantClassY));
640}
641
Daniel Jaspera267cf62012-10-29 10:14:44 +0000642// Implements a run method that returns whether BoundNodes contains a
643// Decl bound to Id that can be dynamically cast to T.
644// Optionally checks that the check succeeded a specific number of times.
645template <typename T>
646class VerifyIdIsBoundTo : public BoundNodesCallback {
647public:
648 // Create an object that checks that a node of type \c T was bound to \c Id.
649 // Does not check for a certain number of matches.
650 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
651 : Id(Id), ExpectedCount(-1), Count(0) {}
652
653 // Create an object that checks that a node of type \c T was bound to \c Id.
654 // Checks that there were exactly \c ExpectedCount matches.
655 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
656 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
657
658 // Create an object that checks that a node of type \c T was bound to \c Id.
659 // Checks that there was exactly one match with the name \c ExpectedName.
660 // Note that \c T must be a NamedDecl for this to work.
Manuel Klimek374516c2013-03-14 16:33:21 +0000661 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName,
662 int ExpectedCount = 1)
663 : Id(Id), ExpectedCount(ExpectedCount), Count(0),
664 ExpectedName(ExpectedName) {}
Daniel Jaspera267cf62012-10-29 10:14:44 +0000665
Peter Collingbourned2bd5892013-11-07 22:30:32 +0000666 void onEndOfTranslationUnit() LLVM_OVERRIDE {
Daniel Jaspera267cf62012-10-29 10:14:44 +0000667 if (ExpectedCount != -1)
668 EXPECT_EQ(ExpectedCount, Count);
669 if (!ExpectedName.empty())
670 EXPECT_EQ(ExpectedName, Name);
Peter Collingbourned2bd5892013-11-07 22:30:32 +0000671 Count = 0;
672 Name.clear();
673 }
674
675 ~VerifyIdIsBoundTo() {
676 EXPECT_EQ(0, Count);
677 EXPECT_EQ("", Name);
Daniel Jaspera267cf62012-10-29 10:14:44 +0000678 }
679
680 virtual bool run(const BoundNodes *Nodes) {
Peter Collingbourne3f0e0402013-11-06 00:27:07 +0000681 const BoundNodes::IDToNodeMap &M = Nodes->getMap();
Daniel Jaspera267cf62012-10-29 10:14:44 +0000682 if (Nodes->getNodeAs<T>(Id)) {
683 ++Count;
684 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
685 Name = Named->getNameAsString();
686 } else if (const NestedNameSpecifier *NNS =
687 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
688 llvm::raw_string_ostream OS(Name);
689 NNS->print(OS, PrintingPolicy(LangOptions()));
690 }
Peter Collingbourne3f0e0402013-11-06 00:27:07 +0000691 BoundNodes::IDToNodeMap::const_iterator I = M.find(Id);
692 EXPECT_NE(M.end(), I);
693 if (I != M.end())
694 EXPECT_EQ(Nodes->getNodeAs<T>(Id), I->second.get<T>());
Daniel Jaspera267cf62012-10-29 10:14:44 +0000695 return true;
696 }
Peter Collingbourne3f0e0402013-11-06 00:27:07 +0000697 EXPECT_TRUE(M.count(Id) == 0 || M.find(Id)->second.template get<T>() == 0);
Daniel Jaspera267cf62012-10-29 10:14:44 +0000698 return false;
699 }
700
Daniel Jasper452abbc2012-10-29 10:48:25 +0000701 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
702 return run(Nodes);
703 }
704
Daniel Jaspera267cf62012-10-29 10:14:44 +0000705private:
706 const std::string Id;
707 const int ExpectedCount;
708 int Count;
709 const std::string ExpectedName;
710 std::string Name;
711};
712
713TEST(HasDescendant, MatchesDescendantTypes) {
714 EXPECT_TRUE(matches("void f() { int i = 3; }",
715 decl(hasDescendant(loc(builtinType())))));
716 EXPECT_TRUE(matches("void f() { int i = 3; }",
717 stmt(hasDescendant(builtinType()))));
718
719 EXPECT_TRUE(matches("void f() { int i = 3; }",
720 stmt(hasDescendant(loc(builtinType())))));
721 EXPECT_TRUE(matches("void f() { int i = 3; }",
722 stmt(hasDescendant(qualType(builtinType())))));
723
724 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
725 stmt(hasDescendant(isInteger()))));
726
727 EXPECT_TRUE(matchAndVerifyResultTrue(
728 "void f() { int a; float c; int d; int e; }",
729 functionDecl(forEachDescendant(
730 varDecl(hasDescendant(isInteger())).bind("x"))),
731 new VerifyIdIsBoundTo<Decl>("x", 3)));
732}
733
734TEST(HasDescendant, MatchesDescendantsOfTypes) {
735 EXPECT_TRUE(matches("void f() { int*** i; }",
736 qualType(hasDescendant(builtinType()))));
737 EXPECT_TRUE(matches("void f() { int*** i; }",
738 qualType(hasDescendant(
739 pointerType(pointee(builtinType()))))));
740 EXPECT_TRUE(matches("void f() { int*** i; }",
David Blaikie5be093c2013-02-18 19:04:16 +0000741 typeLoc(hasDescendant(loc(builtinType())))));
Daniel Jaspera267cf62012-10-29 10:14:44 +0000742
743 EXPECT_TRUE(matchAndVerifyResultTrue(
744 "void f() { int*** i; }",
745 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
746 new VerifyIdIsBoundTo<Type>("x", 2)));
747}
748
749TEST(Has, MatchesChildrenOfTypes) {
750 EXPECT_TRUE(matches("int i;",
751 varDecl(hasName("i"), has(isInteger()))));
752 EXPECT_TRUE(notMatches("int** i;",
753 varDecl(hasName("i"), has(isInteger()))));
754 EXPECT_TRUE(matchAndVerifyResultTrue(
755 "int (*f)(float, int);",
756 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
757 new VerifyIdIsBoundTo<QualType>("x", 2)));
758}
759
760TEST(Has, MatchesChildTypes) {
761 EXPECT_TRUE(matches(
762 "int* i;",
763 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
764 EXPECT_TRUE(notMatches(
765 "int* i;",
766 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
767}
768
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000769TEST(Enum, DoesNotMatchClasses) {
770 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
771}
772
773TEST(Enum, MatchesEnums) {
774 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
775}
776
777TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000778 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000779 EXPECT_TRUE(matches("enum X{ A };", Matcher));
780 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
781 EXPECT_TRUE(notMatches("enum X {};", Matcher));
782}
783
Manuel Klimek4da21662012-07-06 05:48:52 +0000784TEST(StatementMatcher, Has) {
785 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000786 expr(hasType(pointsTo(recordDecl(hasName("X")))),
787 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000788
789 EXPECT_TRUE(matches(
790 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
791 EXPECT_TRUE(notMatches(
792 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
793}
794
795TEST(StatementMatcher, HasDescendant) {
796 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000797 expr(hasType(pointsTo(recordDecl(hasName("X")))),
798 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000799
800 EXPECT_TRUE(matches(
801 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
802 HasDescendantVariableI));
803 EXPECT_TRUE(notMatches(
804 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
805 HasDescendantVariableI));
806}
807
808TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000809 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000810
811 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
812 EXPECT_TRUE(notMatches("class A {};", TypeA));
813
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000814 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000815
816 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
817 TypeDerivedFromA));
818 EXPECT_TRUE(notMatches("class A {};", TypeA));
819
820 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000821 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000822
823 EXPECT_TRUE(
824 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
825}
826
Manuel Klimek4da21662012-07-06 05:48:52 +0000827TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000828 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000829
830 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000831 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000832
833 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000834 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000835
836 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000837 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000838
839 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
840 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000841 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000842
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000843 StatementMatcher MethodX =
844 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000845
846 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
847 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000848 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000849}
850
851TEST(Matcher, BindTheSameNameInAlternatives) {
852 StatementMatcher matcher = anyOf(
853 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000854 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000855 hasRHS(integerLiteral(equals(0)))),
856 binaryOperator(hasOperatorName("+"),
857 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000858 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000859
860 EXPECT_TRUE(matchAndVerifyResultTrue(
861 // The first branch of the matcher binds x to 0 but then fails.
862 // The second branch binds x to f() and succeeds.
863 "int f() { return 0 + f(); }",
864 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000865 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000866}
867
Manuel Klimek66341c52012-08-30 19:41:06 +0000868TEST(Matcher, BindsIDForMemoizedResults) {
869 // Using the same matcher in two match expressions will make memoization
870 // kick in.
871 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
872 EXPECT_TRUE(matchAndVerifyResultTrue(
873 "class A { class B { class X {}; }; };",
874 DeclarationMatcher(anyOf(
875 recordDecl(hasName("A"), hasDescendant(ClassX)),
876 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000877 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000878}
879
Daniel Jasper189f2e42012-12-03 15:43:25 +0000880TEST(HasDeclaration, HasDeclarationOfEnumType) {
881 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
882 expr(hasType(pointsTo(
883 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
884}
885
Edwin Vaneb45083d2013-02-25 14:32:42 +0000886TEST(HasDeclaration, HasGetDeclTraitTest) {
887 EXPECT_TRUE(internal::has_getDecl<TypedefType>::value);
888 EXPECT_TRUE(internal::has_getDecl<RecordType>::value);
889 EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value);
890}
891
Edwin Vane52380602013-02-19 17:14:34 +0000892TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) {
893 EXPECT_TRUE(matches("typedef int X; X a;",
894 varDecl(hasName("a"),
895 hasType(typedefType(hasDeclaration(decl()))))));
896
897 // FIXME: Add tests for other types with getDecl() (e.g. RecordType)
898}
899
Edwin Vane3abf7782013-02-25 14:49:29 +0000900TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) {
901 EXPECT_TRUE(matches("template <typename T> class A {}; A<int> a;",
902 varDecl(hasType(templateSpecializationType(
903 hasDeclaration(namedDecl(hasName("A"))))))));
904}
905
Manuel Klimek4da21662012-07-06 05:48:52 +0000906TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000907 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000908 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000909 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000910 EXPECT_TRUE(
911 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000912 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000913 EXPECT_TRUE(
914 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000915 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000916}
917
918TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000919 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000920 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000921 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000922 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000923 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000924 EXPECT_TRUE(
925 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000926 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000927}
928
929TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000930 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000931 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000932 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000933 EXPECT_TRUE(
934 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000935 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000936}
937
938TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000939 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000940 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000941 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000942 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000943 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000944}
945
Manuel Klimek1a68afd2013-06-20 13:08:29 +0000946TEST(HasTypeLoc, MatchesDeclaratorDecls) {
947 EXPECT_TRUE(matches("int x;",
948 varDecl(hasName("x"), hasTypeLoc(loc(asString("int"))))));
949
950 // Make sure we don't crash on implicit constructors.
951 EXPECT_TRUE(notMatches("class X {}; X x;",
952 declaratorDecl(hasTypeLoc(loc(asString("int"))))));
953}
954
Manuel Klimek4da21662012-07-06 05:48:52 +0000955TEST(Matcher, Call) {
956 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000957 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000958 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000959
960 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
961 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
962
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000963 StatementMatcher MethodOnY =
964 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000965
966 EXPECT_TRUE(
967 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
968 MethodOnY));
969 EXPECT_TRUE(
970 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
971 MethodOnY));
972 EXPECT_TRUE(
973 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
974 MethodOnY));
975 EXPECT_TRUE(
976 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
977 MethodOnY));
978 EXPECT_TRUE(
979 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
980 MethodOnY));
981
982 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000983 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000984
985 EXPECT_TRUE(
986 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
987 MethodOnYPointer));
988 EXPECT_TRUE(
989 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
990 MethodOnYPointer));
991 EXPECT_TRUE(
992 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
993 MethodOnYPointer));
994 EXPECT_TRUE(
995 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
996 MethodOnYPointer));
997 EXPECT_TRUE(
998 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
999 MethodOnYPointer));
1000}
1001
Daniel Jasper31f7c082012-10-01 13:40:41 +00001002TEST(Matcher, Lambda) {
1003 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
1004 lambdaExpr()));
1005}
1006
1007TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +00001008 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
1009 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00001010 forRangeStmt()));
1011 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
1012 forRangeStmt()));
1013}
1014
1015TEST(Matcher, UserDefinedLiteral) {
1016 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
1017 " return i + 1;"
1018 "}"
1019 "char c = 'a'_inc;",
1020 userDefinedLiteral()));
1021}
1022
Daniel Jasperb54b7642012-09-20 14:12:57 +00001023TEST(Matcher, FlowControl) {
1024 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
1025 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
1026 continueStmt()));
1027 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
1028 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
1029 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
1030}
1031
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001032TEST(HasType, MatchesAsString) {
1033 EXPECT_TRUE(
1034 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001035 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001036 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001037 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001038 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001039 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001040 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001041 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001042}
1043
Manuel Klimek4da21662012-07-06 05:48:52 +00001044TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001045 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001046 // Unary operator
1047 EXPECT_TRUE(matches("class Y { }; "
1048 "bool operator!(Y x) { return false; }; "
1049 "Y y; bool c = !y;", OpCall));
1050 // No match -- special operators like "new", "delete"
1051 // FIXME: operator new takes size_t, for which we need stddef.h, for which
1052 // we need to figure out include paths in the test.
1053 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
1054 // "class Y { }; "
1055 // "void *operator new(size_t size) { return 0; } "
1056 // "Y *y = new Y;", OpCall));
1057 EXPECT_TRUE(notMatches("class Y { }; "
1058 "void operator delete(void *p) { } "
1059 "void a() {Y *y = new Y; delete y;}", OpCall));
1060 // Binary operator
1061 EXPECT_TRUE(matches("class Y { }; "
1062 "bool operator&&(Y x, Y y) { return true; }; "
1063 "Y a; Y b; bool c = a && b;",
1064 OpCall));
1065 // No match -- normal operator, not an overloaded one.
1066 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
1067 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
1068}
1069
1070TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
1071 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001072 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001073 EXPECT_TRUE(matches("class Y { }; "
1074 "bool operator&&(Y x, Y y) { return true; }; "
1075 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
1076 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001077 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001078 EXPECT_TRUE(notMatches("class Y { }; "
1079 "bool operator&&(Y x, Y y) { return true; }; "
1080 "Y a; Y b; bool c = a && b;",
1081 OpCallLessLess));
Edwin Vane6a19a972013-03-06 17:02:57 +00001082 DeclarationMatcher ClassWithOpStar =
1083 recordDecl(hasMethod(hasOverloadedOperatorName("*")));
1084 EXPECT_TRUE(matches("class Y { int operator*(); };",
1085 ClassWithOpStar));
1086 EXPECT_TRUE(notMatches("class Y { void myOperator(); };",
1087 ClassWithOpStar)) ;
Manuel Klimek4da21662012-07-06 05:48:52 +00001088}
1089
Daniel Jasper278057f2012-11-15 03:29:05 +00001090TEST(Matcher, NestedOverloadedOperatorCalls) {
1091 EXPECT_TRUE(matchAndVerifyResultTrue(
1092 "class Y { }; "
1093 "Y& operator&&(Y& x, Y& y) { return x; }; "
1094 "Y a; Y b; Y c; Y d = a && b && c;",
1095 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
1096 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
1097 EXPECT_TRUE(matches(
1098 "class Y { }; "
1099 "Y& operator&&(Y& x, Y& y) { return x; }; "
1100 "Y a; Y b; Y c; Y d = a && b && c;",
1101 operatorCallExpr(hasParent(operatorCallExpr()))));
1102 EXPECT_TRUE(matches(
1103 "class Y { }; "
1104 "Y& operator&&(Y& x, Y& y) { return x; }; "
1105 "Y a; Y b; Y c; Y d = a && b && c;",
1106 operatorCallExpr(hasDescendant(operatorCallExpr()))));
1107}
1108
Manuel Klimek4da21662012-07-06 05:48:52 +00001109TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +00001110 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001111 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001112
1113 EXPECT_TRUE(
1114 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1115 MethodOnY));
1116 EXPECT_TRUE(
1117 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1118 MethodOnY));
1119 EXPECT_TRUE(
1120 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1121 MethodOnY));
1122 EXPECT_TRUE(
1123 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1124 MethodOnY));
1125 EXPECT_TRUE(
1126 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1127 MethodOnY));
1128
1129 EXPECT_TRUE(matches(
1130 "class Y {"
1131 " public: virtual void x();"
1132 "};"
1133 "class X : public Y {"
1134 " public: virtual void x();"
1135 "};"
1136 "void z() { X *x; x->Y::x(); }", MethodOnY));
1137}
1138
1139TEST(Matcher, VariableUsage) {
1140 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001141 declRefExpr(to(
1142 varDecl(hasInitializer(
1143 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001144
1145 EXPECT_TRUE(matches(
1146 "class Y {"
1147 " public:"
1148 " bool x() const;"
1149 "};"
1150 "void z(const Y &y) {"
1151 " bool b = y.x();"
1152 " if (b) {}"
1153 "}", Reference));
1154
1155 EXPECT_TRUE(notMatches(
1156 "class Y {"
1157 " public:"
1158 " bool x() const;"
1159 "};"
1160 "void z(const Y &y) {"
1161 " bool b = y.x();"
1162 "}", Reference));
1163}
1164
Manuel Klimek8cb9bf52012-12-04 14:42:08 +00001165TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper9bd28092012-07-30 05:03:25 +00001166 EXPECT_TRUE(matches(
1167 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001168 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001169}
1170
Manuel Klimek4da21662012-07-06 05:48:52 +00001171TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001172 StatementMatcher CallOnVariableY =
1173 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001174
1175 EXPECT_TRUE(matches(
1176 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1177 EXPECT_TRUE(matches(
1178 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1179 EXPECT_TRUE(matches(
1180 "class Y { public: void x(); };"
1181 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1182 EXPECT_TRUE(matches(
1183 "class Y { public: void x(); };"
1184 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1185 EXPECT_TRUE(notMatches(
1186 "class Y { public: void x(); };"
1187 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1188 CallOnVariableY));
1189}
1190
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001191TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1192 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1193 unaryExprOrTypeTraitExpr()));
1194 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1195 alignOfExpr(anything())));
1196 // FIXME: Uncomment once alignof is enabled.
1197 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1198 // unaryExprOrTypeTraitExpr()));
1199 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1200 // sizeOfExpr()));
1201}
1202
1203TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1204 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1205 hasArgumentOfType(asString("int")))));
1206 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1207 hasArgumentOfType(asString("float")))));
1208 EXPECT_TRUE(matches(
1209 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001210 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001211 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001212 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001213}
1214
Manuel Klimek4da21662012-07-06 05:48:52 +00001215TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001216 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001217}
1218
1219TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001220 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001221}
1222
1223TEST(MemberExpression, MatchesVariable) {
1224 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001225 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001226 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001227 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001228 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001229 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001230}
1231
1232TEST(MemberExpression, MatchesStaticVariable) {
1233 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001234 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001235 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001236 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001237 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001238 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001239}
1240
Daniel Jasper6a124492012-07-12 08:50:38 +00001241TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001242 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1243 EXPECT_TRUE(matches(
1244 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1245 callExpr(hasArgument(0, declRefExpr(
1246 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001247}
1248
1249TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001250 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001251 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001252 callExpr(hasArgument(0, declRefExpr(
1253 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001254}
1255
Manuel Klimek4da21662012-07-06 05:48:52 +00001256TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1257 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001258 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001259 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001260 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001261 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001262 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001263}
1264
1265TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1266 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001267 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001268 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001269 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001270 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001271 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001272}
1273
1274TEST(IsArrow, MatchesMemberCallsViaArrow) {
1275 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001276 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001277 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001278 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001279 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001280 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001281}
1282
1283TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001284 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001285
1286 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1287 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1288}
1289
1290TEST(Callee, MatchesMemberExpressions) {
1291 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001292 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001293 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001294 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001295}
1296
1297TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001298 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001299
1300 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1301 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1302
Manuel Klimeke265c872012-07-10 14:21:30 +00001303#if !defined(_MSC_VER)
1304 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001305 // Dependent contexts, but a non-dependent call.
1306 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1307 CallFunctionF));
1308 EXPECT_TRUE(
1309 matches("void f(); template <int N> struct S { void g() { f(); } };",
1310 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001311#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001312
1313 // Depedent calls don't match.
1314 EXPECT_TRUE(
1315 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1316 CallFunctionF));
1317 EXPECT_TRUE(
1318 notMatches("void f(int);"
1319 "template <typename T> struct S { void g(T t) { f(t); } };",
1320 CallFunctionF));
1321}
1322
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001323TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1324 EXPECT_TRUE(
1325 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001326 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001327}
1328
1329TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1330 EXPECT_TRUE(
1331 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001332 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001333}
1334
1335TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1336 EXPECT_TRUE(
1337 notMatches("void g(); template <typename T> void f(T t) {}"
1338 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001339 functionTemplateDecl(hasName("f"),
1340 hasDescendant(declRefExpr(to(
1341 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001342}
1343
Manuel Klimek4da21662012-07-06 05:48:52 +00001344TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001345 StatementMatcher CallArgumentY = callExpr(
1346 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001347
1348 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1349 EXPECT_TRUE(
1350 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1351 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1352
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001353 StatementMatcher WrongIndex = callExpr(
1354 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001355 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1356}
1357
1358TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001359 StatementMatcher CallArgumentY = callExpr(
1360 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001361 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1362 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1363 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1364}
1365
1366TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001367 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001368
1369 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1370 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1371 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1372}
1373
Daniel Jasper36e29d62012-12-04 11:54:27 +00001374TEST(Matcher, ParameterCount) {
1375 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1376 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1377 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1378 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1379 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1380}
1381
Manuel Klimek4da21662012-07-06 05:48:52 +00001382TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001383 DeclarationMatcher ReferenceClassX = varDecl(
1384 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001385 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1386 ReferenceClassX));
1387 EXPECT_TRUE(
1388 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
Michael Han4b6730d2013-09-11 15:53:29 +00001389 // The match here is on the implicit copy constructor code for
1390 // class X, not on code 'X x = y'.
Manuel Klimek4da21662012-07-06 05:48:52 +00001391 EXPECT_TRUE(
Michael Han4b6730d2013-09-11 15:53:29 +00001392 matches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1393 EXPECT_TRUE(
1394 notMatches("class X {}; extern X x;", ReferenceClassX));
Manuel Klimek4da21662012-07-06 05:48:52 +00001395 EXPECT_TRUE(
1396 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1397}
1398
Edwin Vane6a19a972013-03-06 17:02:57 +00001399TEST(QualType, hasCanonicalType) {
1400 EXPECT_TRUE(notMatches("typedef int &int_ref;"
1401 "int a;"
1402 "int_ref b = a;",
1403 varDecl(hasType(qualType(referenceType())))));
1404 EXPECT_TRUE(
1405 matches("typedef int &int_ref;"
1406 "int a;"
1407 "int_ref b = a;",
1408 varDecl(hasType(qualType(hasCanonicalType(referenceType()))))));
1409}
1410
Edwin Vane7b69cd02013-04-02 18:15:55 +00001411TEST(QualType, hasLocalQualifiers) {
1412 EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;",
1413 varDecl(hasType(hasLocalQualifiers()))));
1414 EXPECT_TRUE(matches("int *const j = nullptr;",
1415 varDecl(hasType(hasLocalQualifiers()))));
1416 EXPECT_TRUE(matches("int *volatile k;",
1417 varDecl(hasType(hasLocalQualifiers()))));
1418 EXPECT_TRUE(notMatches("int m;",
1419 varDecl(hasType(hasLocalQualifiers()))));
1420}
1421
Manuel Klimek4da21662012-07-06 05:48:52 +00001422TEST(HasParameter, CallsInnerMatcher) {
1423 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001424 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001425 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001426 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001427}
1428
1429TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1430 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001431 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001432}
1433
1434TEST(HasType, MatchesParameterVariableTypesStrictly) {
1435 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001436 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001437 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001438 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001439 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001440 methodDecl(hasParameter(0,
1441 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001442 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001443 methodDecl(hasParameter(0,
1444 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001445}
1446
1447TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1448 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001449 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001450 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001451 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001452}
1453
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001454TEST(Returns, MatchesReturnTypes) {
1455 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001456 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001457 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001458 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001459 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001460 functionDecl(returns(hasDeclaration(
1461 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001462}
1463
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001464TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001465 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1466 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1467 functionDecl(isExternC())));
1468 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001469}
1470
Manuel Klimek4da21662012-07-06 05:48:52 +00001471TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1472 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001473 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001474}
1475
1476TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1477 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001478 methodDecl(hasAnyParameter(hasType(pointsTo(
1479 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001480}
1481
1482TEST(HasName, MatchesParameterVariableDeclartions) {
1483 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001484 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001485 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001486 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001487}
1488
Daniel Jasper371f9392012-08-01 08:40:24 +00001489TEST(Matcher, MatchesClassTemplateSpecialization) {
1490 EXPECT_TRUE(matches("template<typename T> struct A {};"
1491 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001492 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001493 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001494 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001495 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001496 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001497}
1498
Manuel Klimek1a68afd2013-06-20 13:08:29 +00001499TEST(DeclaratorDecl, MatchesDeclaratorDecls) {
1500 EXPECT_TRUE(matches("int x;", declaratorDecl()));
1501 EXPECT_TRUE(notMatches("class A {};", declaratorDecl()));
1502}
1503
1504TEST(ParmVarDecl, MatchesParmVars) {
1505 EXPECT_TRUE(matches("void f(int x);", parmVarDecl()));
1506 EXPECT_TRUE(notMatches("void f();", parmVarDecl()));
1507}
1508
Daniel Jasper371f9392012-08-01 08:40:24 +00001509TEST(Matcher, MatchesTypeTemplateArgument) {
1510 EXPECT_TRUE(matches(
1511 "template<typename T> struct B {};"
1512 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001513 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001514 asString("int"))))));
1515}
1516
1517TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1518 EXPECT_TRUE(matches(
1519 "struct B { int next; };"
1520 "template<int(B::*next_ptr)> struct A {};"
1521 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001522 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1523 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001524
1525 EXPECT_TRUE(notMatches(
1526 "template <typename T> struct A {};"
1527 "A<int> a;",
1528 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1529 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001530}
1531
1532TEST(Matcher, MatchesSpecificArgument) {
1533 EXPECT_TRUE(matches(
1534 "template<typename T, typename U> class A {};"
1535 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001536 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001537 1, refersToType(asString("int"))))));
1538 EXPECT_TRUE(notMatches(
1539 "template<typename T, typename U> class A {};"
1540 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001541 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001542 1, refersToType(asString("int"))))));
1543}
1544
Daniel Jasperf3197e92013-02-25 12:02:08 +00001545TEST(Matcher, MatchesAccessSpecDecls) {
1546 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1547 EXPECT_TRUE(
1548 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1549 EXPECT_TRUE(
1550 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1551 EXPECT_TRUE(
1552 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1553
1554 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1555}
1556
Edwin Vane5771a2f2013-04-09 20:46:36 +00001557TEST(Matcher, MatchesVirtualMethod) {
1558 EXPECT_TRUE(matches("class X { virtual int f(); };",
1559 methodDecl(isVirtual(), hasName("::X::f"))));
1560 EXPECT_TRUE(notMatches("class X { int f(); };",
1561 methodDecl(isVirtual())));
1562}
1563
Edwin Vane32a6ebc2013-05-09 17:00:17 +00001564TEST(Matcher, MatchesConstMethod) {
1565 EXPECT_TRUE(matches("struct A { void foo() const; };",
1566 methodDecl(isConst())));
1567 EXPECT_TRUE(notMatches("struct A { void foo(); };",
1568 methodDecl(isConst())));
1569}
1570
Edwin Vane5771a2f2013-04-09 20:46:36 +00001571TEST(Matcher, MatchesOverridingMethod) {
1572 EXPECT_TRUE(matches("class X { virtual int f(); }; "
1573 "class Y : public X { int f(); };",
1574 methodDecl(isOverride(), hasName("::Y::f"))));
1575 EXPECT_TRUE(notMatches("class X { virtual int f(); }; "
1576 "class Y : public X { int f(); };",
1577 methodDecl(isOverride(), hasName("::X::f"))));
1578 EXPECT_TRUE(notMatches("class X { int f(); }; "
1579 "class Y : public X { int f(); };",
1580 methodDecl(isOverride())));
1581 EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ",
1582 methodDecl(isOverride())));
1583}
1584
Manuel Klimek4da21662012-07-06 05:48:52 +00001585TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001586 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001587
1588 EXPECT_TRUE(
1589 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1590 EXPECT_TRUE(
1591 matches("class X { public: X(); }; void x() { X x = X(); }",
1592 Constructor));
1593 EXPECT_TRUE(
1594 matches("class X { public: X(int); }; void x() { X x = 0; }",
1595 Constructor));
1596 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1597}
1598
1599TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001600 StatementMatcher Constructor = constructExpr(
1601 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001602
1603 EXPECT_TRUE(
1604 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1605 Constructor));
1606 EXPECT_TRUE(
1607 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1608 Constructor));
1609 EXPECT_TRUE(
1610 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1611 Constructor));
1612 EXPECT_TRUE(
1613 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1614 Constructor));
1615
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001616 StatementMatcher WrongIndex = constructExpr(
1617 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001618 EXPECT_TRUE(
1619 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1620 WrongIndex));
1621}
1622
1623TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001624 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001625
1626 EXPECT_TRUE(
1627 matches("class X { public: X(int); }; void x() { X x(0); }",
1628 Constructor1Arg));
1629 EXPECT_TRUE(
1630 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1631 Constructor1Arg));
1632 EXPECT_TRUE(
1633 matches("class X { public: X(int); }; void x() { X x = 0; }",
1634 Constructor1Arg));
1635 EXPECT_TRUE(
1636 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1637 Constructor1Arg));
1638}
1639
Manuel Klimek70b9db92012-10-23 10:40:50 +00001640TEST(Matcher,ThisExpr) {
1641 EXPECT_TRUE(
1642 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1643 EXPECT_TRUE(
1644 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1645}
1646
Manuel Klimek4da21662012-07-06 05:48:52 +00001647TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001648 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001649
1650 std::string ClassString = "class string { public: string(); ~string(); }; ";
1651
1652 EXPECT_TRUE(
1653 matches(ClassString +
1654 "string GetStringByValue();"
1655 "void FunctionTakesString(string s);"
1656 "void run() { FunctionTakesString(GetStringByValue()); }",
1657 TempExpression));
1658
1659 EXPECT_TRUE(
1660 notMatches(ClassString +
1661 "string* GetStringPointer(); "
1662 "void FunctionTakesStringPtr(string* s);"
1663 "void run() {"
1664 " string* s = GetStringPointer();"
1665 " FunctionTakesStringPtr(GetStringPointer());"
1666 " FunctionTakesStringPtr(s);"
1667 "}",
1668 TempExpression));
1669
1670 EXPECT_TRUE(
1671 notMatches("class no_dtor {};"
1672 "no_dtor GetObjByValue();"
1673 "void ConsumeObj(no_dtor param);"
1674 "void run() { ConsumeObj(GetObjByValue()); }",
1675 TempExpression));
1676}
1677
Sam Panzere16acd32012-08-24 22:04:44 +00001678TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1679 std::string ClassString =
1680 "class string { public: string(); int length(); }; ";
1681
1682 EXPECT_TRUE(
1683 matches(ClassString +
1684 "string GetStringByValue();"
1685 "void FunctionTakesString(string s);"
1686 "void run() { FunctionTakesString(GetStringByValue()); }",
1687 materializeTemporaryExpr()));
1688
1689 EXPECT_TRUE(
1690 notMatches(ClassString +
1691 "string* GetStringPointer(); "
1692 "void FunctionTakesStringPtr(string* s);"
1693 "void run() {"
1694 " string* s = GetStringPointer();"
1695 " FunctionTakesStringPtr(GetStringPointer());"
1696 " FunctionTakesStringPtr(s);"
1697 "}",
1698 materializeTemporaryExpr()));
1699
1700 EXPECT_TRUE(
1701 notMatches(ClassString +
1702 "string GetStringByValue();"
1703 "void run() { int k = GetStringByValue().length(); }",
1704 materializeTemporaryExpr()));
1705
1706 EXPECT_TRUE(
1707 notMatches(ClassString +
1708 "string GetStringByValue();"
1709 "void run() { GetStringByValue(); }",
1710 materializeTemporaryExpr()));
1711}
1712
Manuel Klimek4da21662012-07-06 05:48:52 +00001713TEST(ConstructorDeclaration, SimpleCase) {
1714 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001715 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001716 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001717 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001718}
1719
1720TEST(ConstructorDeclaration, IsImplicit) {
1721 // This one doesn't match because the constructor is not added by the
1722 // compiler (it is not needed).
1723 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001724 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001725 // The compiler added the implicit default constructor.
1726 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001727 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001728 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001729 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001730}
1731
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001732TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1733 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001734 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001735}
1736
1737TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001738 EXPECT_TRUE(notMatches("class Foo {};",
1739 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001740}
1741
Manuel Klimek4da21662012-07-06 05:48:52 +00001742TEST(HasAnyConstructorInitializer, SimpleCase) {
1743 EXPECT_TRUE(notMatches(
1744 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001745 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001746 EXPECT_TRUE(matches(
1747 "class Foo {"
1748 " Foo() : foo_() { }"
1749 " int foo_;"
1750 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001751 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001752}
1753
1754TEST(HasAnyConstructorInitializer, ForField) {
1755 static const char Code[] =
1756 "class Baz { };"
1757 "class Foo {"
1758 " Foo() : foo_() { }"
1759 " Baz foo_;"
1760 " Baz bar_;"
1761 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001762 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1763 forField(hasType(recordDecl(hasName("Baz"))))))));
1764 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001765 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001766 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1767 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001768}
1769
1770TEST(HasAnyConstructorInitializer, WithInitializer) {
1771 static const char Code[] =
1772 "class Foo {"
1773 " Foo() : foo_(0) { }"
1774 " int foo_;"
1775 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001776 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001777 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001778 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001779 withInitializer(integerLiteral(equals(1)))))));
1780}
1781
1782TEST(HasAnyConstructorInitializer, IsWritten) {
1783 static const char Code[] =
1784 "struct Bar { Bar(){} };"
1785 "class Foo {"
1786 " Foo() : foo_() { }"
1787 " Bar foo_;"
1788 " Bar bar_;"
1789 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001790 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001791 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001792 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001793 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001794 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001795 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1796}
1797
1798TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001799 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001800
1801 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1802 EXPECT_TRUE(
1803 matches("class X { public: X(); }; void x() { new X(); }", New));
1804 EXPECT_TRUE(
1805 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1806 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1807}
1808
1809TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001810 StatementMatcher New = constructExpr(
1811 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001812
1813 EXPECT_TRUE(
1814 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1815 New));
1816 EXPECT_TRUE(
1817 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1818 New));
1819 EXPECT_TRUE(
1820 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1821 New));
1822
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001823 StatementMatcher WrongIndex = constructExpr(
1824 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001825 EXPECT_TRUE(
1826 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1827 WrongIndex));
1828}
1829
1830TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001831 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001832
1833 EXPECT_TRUE(
1834 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1835 EXPECT_TRUE(
1836 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1837 New));
1838}
1839
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001840TEST(Matcher, DeleteExpression) {
1841 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001842 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001843}
1844
Manuel Klimek4da21662012-07-06 05:48:52 +00001845TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001846 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001847
1848 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1849 EXPECT_TRUE(
1850 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1851 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1852}
1853
1854TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001855 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001856 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1857 // wide string
1858 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1859 // with escaped characters
1860 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1861 // no matching -- though the data type is the same, there is no string literal
1862 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1863}
1864
1865TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001866 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001867 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1868 // wide character
1869 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1870 // wide character, Hex encoded, NOT MATCHED!
1871 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1872 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1873}
1874
1875TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001876 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001877 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1878 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1879 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1880 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1881
1882 // Non-matching cases (character literals, float and double)
1883 EXPECT_TRUE(notMatches("int i = L'a';",
1884 HasIntLiteral)); // this is actually a character
1885 // literal cast to int
1886 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1887 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1888 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1889}
1890
Daniel Jasperc5ae7172013-07-26 18:52:58 +00001891TEST(Matcher, FloatLiterals) {
1892 StatementMatcher HasFloatLiteral = floatLiteral();
1893 EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral));
1894 EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral));
1895 EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral));
1896 EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral));
1897 EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral));
1898
1899 EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral));
1900}
1901
Daniel Jasper31f7c082012-10-01 13:40:41 +00001902TEST(Matcher, NullPtrLiteral) {
1903 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1904}
1905
Daniel Jasperb54b7642012-09-20 14:12:57 +00001906TEST(Matcher, AsmStatement) {
1907 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1908}
1909
Manuel Klimek4da21662012-07-06 05:48:52 +00001910TEST(Matcher, Conditions) {
1911 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1912
1913 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1914 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1915 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1916 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1917 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1918}
1919
1920TEST(MatchBinaryOperator, HasOperatorName) {
1921 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1922
1923 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1924 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1925}
1926
1927TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1928 StatementMatcher OperatorTrueFalse =
1929 binaryOperator(hasLHS(boolLiteral(equals(true))),
1930 hasRHS(boolLiteral(equals(false))));
1931
1932 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1933 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1934 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1935}
1936
1937TEST(MatchBinaryOperator, HasEitherOperand) {
1938 StatementMatcher HasOperand =
1939 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1940
1941 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1942 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1943 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1944}
1945
1946TEST(Matcher, BinaryOperatorTypes) {
1947 // Integration test that verifies the AST provides all binary operators in
1948 // a way we expect.
1949 // FIXME: Operator ','
1950 EXPECT_TRUE(
1951 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1952 EXPECT_TRUE(
1953 matches("bool b; bool c = (b = true);",
1954 binaryOperator(hasOperatorName("="))));
1955 EXPECT_TRUE(
1956 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1957 EXPECT_TRUE(
1958 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1959 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1960 EXPECT_TRUE(
1961 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1962 EXPECT_TRUE(
1963 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1964 EXPECT_TRUE(
1965 matches("int i = 1; int j = (i <<= 2);",
1966 binaryOperator(hasOperatorName("<<="))));
1967 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1968 EXPECT_TRUE(
1969 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1970 EXPECT_TRUE(
1971 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1972 EXPECT_TRUE(
1973 matches("int i = 1; int j = (i >>= 2);",
1974 binaryOperator(hasOperatorName(">>="))));
1975 EXPECT_TRUE(
1976 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1977 EXPECT_TRUE(
1978 matches("int i = 42; int j = (i ^= 42);",
1979 binaryOperator(hasOperatorName("^="))));
1980 EXPECT_TRUE(
1981 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1982 EXPECT_TRUE(
1983 matches("int i = 42; int j = (i %= 42);",
1984 binaryOperator(hasOperatorName("%="))));
1985 EXPECT_TRUE(
1986 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1987 EXPECT_TRUE(
1988 matches("bool b = true && false;",
1989 binaryOperator(hasOperatorName("&&"))));
1990 EXPECT_TRUE(
1991 matches("bool b = true; bool c = (b &= false);",
1992 binaryOperator(hasOperatorName("&="))));
1993 EXPECT_TRUE(
1994 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1995 EXPECT_TRUE(
1996 matches("bool b = true || false;",
1997 binaryOperator(hasOperatorName("||"))));
1998 EXPECT_TRUE(
1999 matches("bool b = true; bool c = (b |= false);",
2000 binaryOperator(hasOperatorName("|="))));
2001 EXPECT_TRUE(
2002 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
2003 EXPECT_TRUE(
2004 matches("int i = 42; int j = (i *= 23);",
2005 binaryOperator(hasOperatorName("*="))));
2006 EXPECT_TRUE(
2007 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
2008 EXPECT_TRUE(
2009 matches("int i = 42; int j = (i /= 23);",
2010 binaryOperator(hasOperatorName("/="))));
2011 EXPECT_TRUE(
2012 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
2013 EXPECT_TRUE(
2014 matches("int i = 42; int j = (i += 23);",
2015 binaryOperator(hasOperatorName("+="))));
2016 EXPECT_TRUE(
2017 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
2018 EXPECT_TRUE(
2019 matches("int i = 42; int j = (i -= 23);",
2020 binaryOperator(hasOperatorName("-="))));
2021 EXPECT_TRUE(
2022 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
2023 binaryOperator(hasOperatorName("->*"))));
2024 EXPECT_TRUE(
2025 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
2026 binaryOperator(hasOperatorName(".*"))));
2027
2028 // Member expressions as operators are not supported in matches.
2029 EXPECT_TRUE(
2030 notMatches("struct A { void x(A *a) { a->x(this); } };",
2031 binaryOperator(hasOperatorName("->"))));
2032
2033 // Initializer assignments are not represented as operator equals.
2034 EXPECT_TRUE(
2035 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
2036
2037 // Array indexing is not represented as operator.
2038 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
2039
2040 // Overloaded operators do not match at all.
2041 EXPECT_TRUE(notMatches(
2042 "struct A { bool operator&&(const A &a) const { return false; } };"
2043 "void x() { A a, b; a && b; }",
2044 binaryOperator()));
2045}
2046
2047TEST(MatchUnaryOperator, HasOperatorName) {
2048 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
2049
2050 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
2051 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
2052}
2053
2054TEST(MatchUnaryOperator, HasUnaryOperand) {
2055 StatementMatcher OperatorOnFalse =
2056 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
2057
2058 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
2059 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
2060}
2061
2062TEST(Matcher, UnaryOperatorTypes) {
2063 // Integration test that verifies the AST provides all unary operators in
2064 // a way we expect.
2065 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
2066 EXPECT_TRUE(
2067 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
2068 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
2069 EXPECT_TRUE(
2070 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
2071 EXPECT_TRUE(
2072 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
2073 EXPECT_TRUE(
2074 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
2075 EXPECT_TRUE(
2076 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
2077 EXPECT_TRUE(
2078 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
2079 EXPECT_TRUE(
2080 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
2081 EXPECT_TRUE(
2082 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
2083
2084 // We don't match conversion operators.
2085 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
2086
2087 // Function calls are not represented as operator.
2088 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
2089
2090 // Overloaded operators do not match at all.
2091 // FIXME: We probably want to add that.
2092 EXPECT_TRUE(notMatches(
2093 "struct A { bool operator!() const { return false; } };"
2094 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
2095}
2096
2097TEST(Matcher, ConditionalOperator) {
2098 StatementMatcher Conditional = conditionalOperator(
2099 hasCondition(boolLiteral(equals(true))),
2100 hasTrueExpression(boolLiteral(equals(false))));
2101
2102 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
2103 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
2104 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
2105
2106 StatementMatcher ConditionalFalse = conditionalOperator(
2107 hasFalseExpression(boolLiteral(equals(false))));
2108
2109 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
2110 EXPECT_TRUE(
2111 notMatches("void x() { true ? false : true; }", ConditionalFalse));
2112}
2113
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002114TEST(ArraySubscriptMatchers, ArraySubscripts) {
2115 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2116 arraySubscriptExpr()));
2117 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2118 arraySubscriptExpr()));
2119}
2120
2121TEST(ArraySubscriptMatchers, ArrayIndex) {
2122 EXPECT_TRUE(matches(
2123 "int i[2]; void f() { i[1] = 1; }",
2124 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2125 EXPECT_TRUE(matches(
2126 "int i[2]; void f() { 1[i] = 1; }",
2127 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2128 EXPECT_TRUE(notMatches(
2129 "int i[2]; void f() { i[1] = 1; }",
2130 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2131}
2132
2133TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2134 EXPECT_TRUE(matches(
2135 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002136 arraySubscriptExpr(hasBase(implicitCastExpr(
2137 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002138}
2139
Manuel Klimek4da21662012-07-06 05:48:52 +00002140TEST(Matcher, HasNameSupportsNamespaces) {
2141 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002142 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002143 EXPECT_TRUE(matches("namespace a { namespace 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(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002146 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002147 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002148 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002149 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002150 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002151 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002152 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002153 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002154 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002155 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002156 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002157 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002158 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002159 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002160 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002161 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002162 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002163 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002164 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002165}
2166
2167TEST(Matcher, HasNameSupportsOuterClasses) {
2168 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002169 matches("class A { class B { class C; }; };",
2170 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002171 EXPECT_TRUE(
2172 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002173 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002174 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002175 matches("class A { class B { class C; }; };",
2176 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002177 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002178 matches("class A { class B { class C; }; };",
2179 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002180 EXPECT_TRUE(
2181 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002182 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002183 EXPECT_TRUE(
2184 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002185 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002186 EXPECT_TRUE(
2187 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002188 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002189 EXPECT_TRUE(
2190 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002191 recordDecl(hasName("::C"))));
2192 EXPECT_TRUE(
2193 notMatches("class A { class B { class C; }; };",
2194 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002195 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002196 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002197 EXPECT_TRUE(
2198 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002199 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002200}
2201
2202TEST(Matcher, IsDefinition) {
2203 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002204 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002205 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2206 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2207
2208 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002209 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002210 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2211 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2212
2213 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002214 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002215 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2216 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2217}
2218
2219TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002220 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00002221 ofClass(hasName("X")))));
2222
2223 EXPECT_TRUE(
2224 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2225 EXPECT_TRUE(
2226 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2227 Constructor));
2228 EXPECT_TRUE(
2229 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2230 Constructor));
2231}
2232
2233TEST(Matcher, VisitsTemplateInstantiations) {
2234 EXPECT_TRUE(matches(
2235 "class A { public: void x(); };"
2236 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002237 "void f() { B<A> b; b.y(); }",
2238 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002239
2240 EXPECT_TRUE(matches(
2241 "class A { public: void x(); };"
2242 "class C {"
2243 " public:"
2244 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2245 "};"
2246 "void f() {"
2247 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002248 "}",
2249 recordDecl(hasName("C"),
2250 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002251}
2252
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002253TEST(Matcher, HandlesNullQualTypes) {
2254 // FIXME: Add a Type matcher so we can replace uses of this
2255 // variable with Type(True())
2256 const TypeMatcher AnyType = anything();
2257
2258 // We don't really care whether this matcher succeeds; we're testing that
2259 // it completes without crashing.
2260 EXPECT_TRUE(matches(
2261 "struct A { };"
2262 "template <typename T>"
2263 "void f(T t) {"
2264 " T local_t(t /* this becomes a null QualType in the AST */);"
2265 "}"
2266 "void g() {"
2267 " f(0);"
2268 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002269 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002270 anyOf(
2271 TypeMatcher(hasDeclaration(anything())),
2272 pointsTo(AnyType),
2273 references(AnyType)
2274 // Other QualType matchers should go here.
2275 ))))));
2276}
2277
Manuel Klimek4da21662012-07-06 05:48:52 +00002278// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002279AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002280 // Make sure all special variables are used: node, match_finder,
2281 // bound_nodes_builder, and the parameter named 'AMatcher'.
2282 return AMatcher.matches(Node, Finder, Builder);
2283}
2284
2285TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002286 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002287
2288 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002289 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002290
2291 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002292 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002293
2294 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002295 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002296}
2297
2298AST_POLYMORPHIC_MATCHER_P(
Samuel Benzaquenef7eb022013-06-21 15:51:31 +00002299 polymorphicHas,
2300 AST_POLYMORPHIC_SUPPORTED_TYPES_2(Decl, Stmt),
2301 internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002302 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002303 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002304 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2305 ASTMatchFinder::BK_First);
2306}
2307
2308TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002309 DeclarationMatcher HasClassB =
2310 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002311
2312 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002313 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002314
2315 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002316 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002317
2318 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002319 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002320
2321 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002322 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002323
2324 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2325}
2326
2327TEST(For, FindsForLoops) {
2328 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2329 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002330 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2331 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002332 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002333}
2334
Daniel Jasper6a124492012-07-12 08:50:38 +00002335TEST(For, ForLoopInternals) {
2336 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2337 forStmt(hasCondition(anything()))));
2338 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2339 forStmt(hasLoopInit(anything()))));
2340}
2341
2342TEST(For, NegativeForLoopInternals) {
2343 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002344 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002345 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2346 forStmt(hasLoopInit(anything()))));
2347}
2348
Manuel Klimek4da21662012-07-06 05:48:52 +00002349TEST(For, ReportsNoFalsePositives) {
2350 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2351 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2352}
2353
2354TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002355 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2356 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2357 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002358}
2359
2360TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2361 // It's not a compound statement just because there's "{}" in the source
2362 // text. This is an AST search, not grep.
2363 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002364 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002365 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002366 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002367}
2368
Daniel Jasper6a124492012-07-12 08:50:38 +00002369TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002370 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002371 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002372 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002373 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002374 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002375 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002376 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002377 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002378}
2379
2380TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2381 // The simplest case: every compound statement is in a function
2382 // definition, and the function body itself must be a compound
2383 // statement.
2384 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002385 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002386}
2387
2388TEST(HasAnySubstatement, IsNotRecursive) {
2389 // It's really "has any immediate substatement".
2390 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002391 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002392}
2393
2394TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2395 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002396 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002397}
2398
2399TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2400 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002401 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002402}
2403
2404TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2405 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002406 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002407 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002408 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002409}
2410
2411TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2412 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002413 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002414 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002415 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002416 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002417 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002418}
2419
2420TEST(StatementCountIs, WorksWithMultipleStatements) {
2421 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002422 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002423}
2424
2425TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2426 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002427 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002428 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002429 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002430 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002431 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002432 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002433 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002434}
2435
2436TEST(Member, WorksInSimplestCase) {
2437 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002438 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002439}
2440
2441TEST(Member, DoesNotMatchTheBaseExpression) {
2442 // Don't pick out the wrong part of the member expression, this should
2443 // be checking the member (name) only.
2444 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002445 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002446}
2447
2448TEST(Member, MatchesInMemberFunctionCall) {
2449 EXPECT_TRUE(matches("void f() {"
2450 " struct { void first() {}; } s;"
2451 " s.first();"
2452 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002453 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002454}
2455
Daniel Jasperc711af22012-10-23 15:46:39 +00002456TEST(Member, MatchesMember) {
2457 EXPECT_TRUE(matches(
2458 "struct A { int i; }; void f() { A a; a.i = 2; }",
2459 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2460 EXPECT_TRUE(notMatches(
2461 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2462 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2463}
2464
Daniel Jasperf3197e92013-02-25 12:02:08 +00002465TEST(Member, UnderstandsAccess) {
2466 EXPECT_TRUE(matches(
2467 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2468 EXPECT_TRUE(notMatches(
2469 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2470 EXPECT_TRUE(notMatches(
2471 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2472
2473 EXPECT_TRUE(notMatches(
2474 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2475 EXPECT_TRUE(notMatches(
2476 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2477 EXPECT_TRUE(matches(
2478 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2479
2480 EXPECT_TRUE(notMatches(
2481 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2482 EXPECT_TRUE(matches("class A { protected: int i; };",
2483 fieldDecl(isProtected(), hasName("i"))));
2484 EXPECT_TRUE(notMatches(
2485 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2486
2487 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2488 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2489 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2490 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2491}
2492
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002493TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002494 // Fails in C++11 mode
2495 EXPECT_TRUE(matchesConditionally(
2496 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2497 "class X { void *operator new(std::size_t); };",
2498 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002499
2500 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002501 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002502
Daniel Jasper31f7c082012-10-01 13:40:41 +00002503 // Fails in C++11 mode
2504 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002505 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2506 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002507 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002508}
2509
Manuel Klimek4da21662012-07-06 05:48:52 +00002510TEST(HasObjectExpression, DoesNotMatchMember) {
2511 EXPECT_TRUE(notMatches(
2512 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002513 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002514}
2515
2516TEST(HasObjectExpression, MatchesBaseOfVariable) {
2517 EXPECT_TRUE(matches(
2518 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002519 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002520 EXPECT_TRUE(matches(
2521 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002522 memberExpr(hasObjectExpression(
2523 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002524}
2525
2526TEST(HasObjectExpression,
2527 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2528 EXPECT_TRUE(matches(
2529 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002530 memberExpr(hasObjectExpression(
2531 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002532 EXPECT_TRUE(matches(
2533 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002534 memberExpr(hasObjectExpression(
2535 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002536}
2537
2538TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002539 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2540 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2541 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2542 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002543}
2544
2545TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002546 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002547}
2548
2549TEST(IsConstQualified, MatchesConstInt) {
2550 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002551 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002552}
2553
2554TEST(IsConstQualified, MatchesConstPointer) {
2555 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002556 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002557}
2558
2559TEST(IsConstQualified, MatchesThroughTypedef) {
2560 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002561 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002562 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002563 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002564}
2565
2566TEST(IsConstQualified, DoesNotMatchInappropriately) {
2567 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002568 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002569 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002570 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002571}
2572
Sam Panzer089e5b32012-08-16 16:58:10 +00002573TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002574 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2575 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2576 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2577 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002578}
2579TEST(CastExpression, MatchesImplicitCasts) {
2580 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002581 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002582 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002583 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002584}
2585
2586TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002587 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2588 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2589 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2590 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002591}
2592
Manuel Klimek4da21662012-07-06 05:48:52 +00002593TEST(ReinterpretCast, MatchesSimpleCase) {
2594 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002595 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002596}
2597
2598TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002599 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002600 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002601 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002602 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002603 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002604 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2605 "B b;"
2606 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002607 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002608}
2609
2610TEST(FunctionalCast, MatchesSimpleCase) {
2611 std::string foo_class = "class Foo { public: Foo(char*); };";
2612 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002613 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002614}
2615
2616TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2617 std::string FooClass = "class Foo { public: Foo(char*); };";
2618 EXPECT_TRUE(
2619 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002620 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002621 EXPECT_TRUE(
2622 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002623 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002624}
2625
2626TEST(DynamicCast, MatchesSimpleCase) {
2627 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2628 "B b;"
2629 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002630 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002631}
2632
2633TEST(StaticCast, MatchesSimpleCase) {
2634 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002635 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002636}
2637
2638TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002639 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002640 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002641 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002642 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002643 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002644 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2645 "B b;"
2646 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002647 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002648}
2649
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002650TEST(CStyleCast, MatchesSimpleCase) {
2651 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2652}
2653
2654TEST(CStyleCast, DoesNotMatchOtherCasts) {
2655 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2656 "char q, *r = const_cast<char*>(&q);"
2657 "void* s = reinterpret_cast<char*>(&s);"
2658 "struct B { virtual ~B() {} }; struct D : B {};"
2659 "B b;"
2660 "D* t = dynamic_cast<D*>(&b);",
2661 cStyleCastExpr()));
2662}
2663
Manuel Klimek4da21662012-07-06 05:48:52 +00002664TEST(HasDestinationType, MatchesSimpleCase) {
2665 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002666 staticCastExpr(hasDestinationType(
2667 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002668}
2669
Sam Panzer089e5b32012-08-16 16:58:10 +00002670TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2671 // This test creates an implicit const cast.
2672 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002673 implicitCastExpr(
2674 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002675 // This test creates an implicit array-to-pointer cast.
2676 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002677 implicitCastExpr(hasImplicitDestinationType(
2678 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002679}
2680
2681TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2682 // This test creates an implicit cast from int to char.
2683 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002684 implicitCastExpr(hasImplicitDestinationType(
2685 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002686 // This test creates an implicit array-to-pointer cast.
2687 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002688 implicitCastExpr(hasImplicitDestinationType(
2689 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002690}
2691
2692TEST(ImplicitCast, MatchesSimpleCase) {
2693 // This test creates an implicit const cast.
2694 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002695 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002696 // This test creates an implicit cast from int to char.
2697 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002698 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002699 // This test creates an implicit array-to-pointer cast.
2700 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002701 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002702}
2703
2704TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002705 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002706 // are present, and that it ignores explicit and paren casts.
2707
2708 // These two test cases have no casts.
2709 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002710 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002711 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002712 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002713
2714 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002715 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002716 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002717 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002718
2719 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002720 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002721}
2722
2723TEST(IgnoringImpCasts, MatchesImpCasts) {
2724 // This test checks that ignoringImpCasts matches when implicit casts are
2725 // present and its inner matcher alone does not match.
2726 // Note that this test creates an implicit const cast.
2727 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002728 varDecl(hasInitializer(ignoringImpCasts(
2729 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002730 // This test creates an implict cast from int to char.
2731 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002732 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002733 integerLiteral(equals(0)))))));
2734}
2735
2736TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2737 // These tests verify that ignoringImpCasts does not match if the inner
2738 // matcher does not match.
2739 // Note that the first test creates an implicit const cast.
2740 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002741 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002742 unless(anything()))))));
2743 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002744 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002745 unless(anything()))))));
2746
2747 // These tests verify that ignoringImplictCasts does not look through explicit
2748 // casts or parentheses.
2749 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002750 varDecl(hasInitializer(ignoringImpCasts(
2751 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002752 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002753 varDecl(hasInitializer(ignoringImpCasts(
2754 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002755 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002756 varDecl(hasInitializer(ignoringImpCasts(
2757 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002758 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002759 varDecl(hasInitializer(ignoringImpCasts(
2760 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002761}
2762
2763TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2764 // This test verifies that expressions that do not have implicit casts
2765 // still match the inner matcher.
2766 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002767 varDecl(hasInitializer(ignoringImpCasts(
2768 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002769}
2770
2771TEST(IgnoringParenCasts, MatchesParenCasts) {
2772 // This test checks that ignoringParenCasts matches when parentheses and/or
2773 // casts are present and its inner matcher alone does not match.
2774 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002775 varDecl(hasInitializer(ignoringParenCasts(
2776 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002777 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002778 varDecl(hasInitializer(ignoringParenCasts(
2779 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002780
2781 // This test creates an implict cast from int to char in addition to the
2782 // parentheses.
2783 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002784 varDecl(hasInitializer(ignoringParenCasts(
2785 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002786
2787 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002788 varDecl(hasInitializer(ignoringParenCasts(
2789 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002790 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002791 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002792 integerLiteral(equals(0)))))));
2793}
2794
2795TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2796 // This test verifies that expressions that do not have any casts still match.
2797 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002798 varDecl(hasInitializer(ignoringParenCasts(
2799 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002800}
2801
2802TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2803 // These tests verify that ignoringImpCasts does not match if the inner
2804 // matcher does not match.
2805 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002806 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002807 unless(anything()))))));
2808
2809 // This test creates an implicit cast from int to char in addition to the
2810 // parentheses.
2811 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002812 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002813 unless(anything()))))));
2814
2815 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002816 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002817 unless(anything()))))));
2818}
2819
2820TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2821 // This test checks that ignoringParenAndImpCasts matches when
2822 // parentheses and/or implicit casts are present and its inner matcher alone
2823 // does not match.
2824 // Note that this test creates an implicit const cast.
2825 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002826 varDecl(hasInitializer(ignoringParenImpCasts(
2827 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002828 // This test creates an implicit cast from int to char.
2829 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002830 varDecl(hasInitializer(ignoringParenImpCasts(
2831 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002832}
2833
2834TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2835 // This test verifies that expressions that do not have parentheses or
2836 // implicit casts still match.
2837 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002838 varDecl(hasInitializer(ignoringParenImpCasts(
2839 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002840 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002841 varDecl(hasInitializer(ignoringParenImpCasts(
2842 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002843}
2844
2845TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2846 // These tests verify that ignoringParenImpCasts does not match if
2847 // the inner matcher does not match.
2848 // This test creates an implicit cast.
2849 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002850 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002851 unless(anything()))))));
2852 // These tests verify that ignoringParenAndImplictCasts does not look
2853 // through explicit casts.
2854 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002855 varDecl(hasInitializer(ignoringParenImpCasts(
2856 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002857 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002858 varDecl(hasInitializer(ignoringParenImpCasts(
2859 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002860 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002861 varDecl(hasInitializer(ignoringParenImpCasts(
2862 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002863}
2864
Manuel Klimek715c9562012-07-25 10:02:02 +00002865TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002866 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2867 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002868 implicitCastExpr(
2869 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002870}
2871
Manuel Klimek715c9562012-07-25 10:02:02 +00002872TEST(HasSourceExpression, MatchesExplicitCasts) {
2873 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002874 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002875 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002876 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002877}
2878
Manuel Klimek4da21662012-07-06 05:48:52 +00002879TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002880 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002881}
2882
2883TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002884 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002885}
2886
2887TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002888 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002889}
2890
2891TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002892 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002893}
2894
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002895TEST(InitListExpression, MatchesInitListExpression) {
2896 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2897 initListExpr(hasType(asString("int [2]")))));
2898 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002899 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002900}
2901
2902TEST(UsingDeclaration, MatchesUsingDeclarations) {
2903 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2904 usingDecl()));
2905}
2906
2907TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2908 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2909 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2910}
2911
2912TEST(UsingDeclaration, MatchesSpecificTarget) {
2913 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2914 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002915 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002916 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2917 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002918 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002919}
2920
2921TEST(UsingDeclaration, ThroughUsingDeclaration) {
2922 EXPECT_TRUE(matches(
2923 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002924 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002925 EXPECT_TRUE(notMatches(
2926 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002927 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002928}
2929
Sam Panzer425f41b2012-08-16 17:20:59 +00002930TEST(SingleDecl, IsSingleDecl) {
2931 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002932 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002933 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2934 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2935 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2936 SingleDeclStmt));
2937}
2938
2939TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002940 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002941
2942 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002943 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002944 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002945 declStmt(containsDeclaration(0, MatchesInit),
2946 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002947 unsigned WrongIndex = 42;
2948 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002949 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002950 MatchesInit))));
2951}
2952
2953TEST(DeclCount, DeclCountIsCorrect) {
2954 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002955 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002956 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002957 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002958 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002959 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002960}
2961
Manuel Klimek4da21662012-07-06 05:48:52 +00002962TEST(While, MatchesWhileLoops) {
2963 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2964 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2965 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2966}
2967
2968TEST(Do, MatchesDoLoops) {
2969 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2970 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2971}
2972
2973TEST(Do, DoesNotMatchWhileLoops) {
2974 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2975}
2976
2977TEST(SwitchCase, MatchesCase) {
2978 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2979 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2980 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2981 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2982}
2983
Daniel Jasperb54b7642012-09-20 14:12:57 +00002984TEST(SwitchCase, MatchesSwitch) {
2985 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2986 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2987 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2988 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2989}
2990
Peter Collingbourneacf02712013-05-10 11:52:02 +00002991TEST(SwitchCase, MatchesEachCase) {
2992 EXPECT_TRUE(notMatches("void x() { switch(42); }",
2993 switchStmt(forEachSwitchCase(caseStmt()))));
2994 EXPECT_TRUE(matches("void x() { switch(42) case 42:; }",
2995 switchStmt(forEachSwitchCase(caseStmt()))));
2996 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }",
2997 switchStmt(forEachSwitchCase(caseStmt()))));
2998 EXPECT_TRUE(notMatches(
2999 "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }",
3000 ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt()))))));
3001 EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }",
3002 switchStmt(forEachSwitchCase(
3003 caseStmt(hasCaseConstant(integerLiteral()))))));
3004 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }",
3005 switchStmt(forEachSwitchCase(
3006 caseStmt(hasCaseConstant(integerLiteral()))))));
3007 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }",
3008 switchStmt(forEachSwitchCase(
3009 caseStmt(hasCaseConstant(integerLiteral()))))));
3010 EXPECT_TRUE(matchAndVerifyResultTrue(
3011 "void x() { switch (42) { case 1: case 2: case 3: default:; } }",
3012 switchStmt(forEachSwitchCase(caseStmt().bind("x"))),
3013 new VerifyIdIsBoundTo<CaseStmt>("x", 3)));
3014}
3015
Manuel Klimek06963012013-07-19 11:50:54 +00003016TEST(ForEachConstructorInitializer, MatchesInitializers) {
3017 EXPECT_TRUE(matches(
3018 "struct X { X() : i(42), j(42) {} int i, j; };",
3019 constructorDecl(forEachConstructorInitializer(ctorInitializer()))));
3020}
3021
Daniel Jasperb54b7642012-09-20 14:12:57 +00003022TEST(ExceptionHandling, SimpleCases) {
3023 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
3024 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
3025 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
3026 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
3027 throwExpr()));
3028 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
3029 throwExpr()));
3030}
3031
Manuel Klimek4da21662012-07-06 05:48:52 +00003032TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
3033 EXPECT_TRUE(notMatches(
3034 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003035 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003036 EXPECT_TRUE(notMatches(
3037 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003038 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003039}
3040
3041TEST(HasConditionVariableStatement, MatchesConditionVariables) {
3042 EXPECT_TRUE(matches(
3043 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003044 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003045}
3046
3047TEST(ForEach, BindsOneNode) {
3048 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003049 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003050 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003051}
3052
3053TEST(ForEach, BindsMultipleNodes) {
3054 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003055 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003056 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003057}
3058
3059TEST(ForEach, BindsRecursiveCombinations) {
3060 EXPECT_TRUE(matchAndVerifyResultTrue(
3061 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003062 recordDecl(hasName("C"),
3063 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003064 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003065}
3066
3067TEST(ForEachDescendant, BindsOneNode) {
3068 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003069 recordDecl(hasName("C"),
3070 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003071 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003072}
3073
Daniel Jasper5f684e92012-11-16 18:39:22 +00003074TEST(ForEachDescendant, NestedForEachDescendant) {
3075 DeclarationMatcher m = recordDecl(
3076 isDefinition(), decl().bind("x"), hasName("C"));
3077 EXPECT_TRUE(matchAndVerifyResultTrue(
3078 "class A { class B { class C {}; }; };",
3079 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
3080 new VerifyIdIsBoundTo<Decl>("x", "C")));
3081
Manuel Klimek054d0492013-06-19 15:42:45 +00003082 // Check that a partial match of 'm' that binds 'x' in the
3083 // first part of anyOf(m, anything()) will not overwrite the
3084 // binding created by the earlier binding in the hasDescendant.
3085 EXPECT_TRUE(matchAndVerifyResultTrue(
3086 "class A { class B { class C {}; }; };",
3087 recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
3088 new VerifyIdIsBoundTo<Decl>("x", "C")));
Daniel Jasper5f684e92012-11-16 18:39:22 +00003089}
3090
Manuel Klimek4da21662012-07-06 05:48:52 +00003091TEST(ForEachDescendant, BindsMultipleNodes) {
3092 EXPECT_TRUE(matchAndVerifyResultTrue(
3093 "class C { class D { int x; int y; }; "
3094 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003095 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003096 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003097}
3098
3099TEST(ForEachDescendant, BindsRecursiveCombinations) {
3100 EXPECT_TRUE(matchAndVerifyResultTrue(
3101 "class C { class D { "
3102 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003103 recordDecl(hasName("C"), forEachDescendant(recordDecl(
3104 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003105 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003106}
3107
Manuel Klimek054d0492013-06-19 15:42:45 +00003108TEST(ForEachDescendant, BindsCombinations) {
3109 EXPECT_TRUE(matchAndVerifyResultTrue(
3110 "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while "
3111 "(true) {} }",
3112 compoundStmt(forEachDescendant(ifStmt().bind("if")),
3113 forEachDescendant(whileStmt().bind("while"))),
3114 new VerifyIdIsBoundTo<IfStmt>("if", 6)));
3115}
3116
3117TEST(Has, DoesNotDeleteBindings) {
3118 EXPECT_TRUE(matchAndVerifyResultTrue(
3119 "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())),
3120 new VerifyIdIsBoundTo<Decl>("x", 1)));
3121}
3122
3123TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) {
3124 // Those matchers cover all the cases where an inner matcher is called
3125 // and there is not a 1:1 relationship between the match of the outer
3126 // matcher and the match of the inner matcher.
3127 // The pattern to look for is:
3128 // ... return InnerMatcher.matches(...); ...
3129 // In which case no special handling is needed.
3130 //
3131 // On the other hand, if there are multiple alternative matches
3132 // (for example forEach*) or matches might be discarded (for example has*)
3133 // the implementation must make sure that the discarded matches do not
3134 // affect the bindings.
3135 // When new such matchers are added, add a test here that:
3136 // - matches a simple node, and binds it as the first thing in the matcher:
3137 // recordDecl(decl().bind("x"), hasName("X")))
3138 // - uses the matcher under test afterwards in a way that not the first
3139 // alternative is matched; for anyOf, that means the first branch
3140 // would need to return false; for hasAncestor, it means that not
3141 // the direct parent matches the inner matcher.
3142
3143 EXPECT_TRUE(matchAndVerifyResultTrue(
3144 "class X { int y; };",
3145 recordDecl(
3146 recordDecl().bind("x"), hasName("::X"),
3147 anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())),
3148 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3149 EXPECT_TRUE(matchAndVerifyResultTrue(
3150 "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"),
3151 anyOf(unless(anything()), anything())),
3152 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3153 EXPECT_TRUE(matchAndVerifyResultTrue(
3154 "template<typename T1, typename T2> class X {}; X<float, int> x;",
3155 classTemplateSpecializationDecl(
3156 decl().bind("x"),
3157 hasAnyTemplateArgument(refersToType(asString("int")))),
3158 new VerifyIdIsBoundTo<Decl>("x", 1)));
3159 EXPECT_TRUE(matchAndVerifyResultTrue(
3160 "class X { void f(); void g(); };",
3161 recordDecl(decl().bind("x"), hasMethod(hasName("g"))),
3162 new VerifyIdIsBoundTo<Decl>("x", 1)));
3163 EXPECT_TRUE(matchAndVerifyResultTrue(
3164 "class X { X() : a(1), b(2) {} double a; int b; };",
3165 recordDecl(decl().bind("x"),
3166 has(constructorDecl(
3167 hasAnyConstructorInitializer(forField(hasName("b")))))),
3168 new VerifyIdIsBoundTo<Decl>("x", 1)));
3169 EXPECT_TRUE(matchAndVerifyResultTrue(
3170 "void x(int, int) { x(0, 42); }",
3171 callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))),
3172 new VerifyIdIsBoundTo<Expr>("x", 1)));
3173 EXPECT_TRUE(matchAndVerifyResultTrue(
3174 "void x(int, int y) {}",
3175 functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))),
3176 new VerifyIdIsBoundTo<Decl>("x", 1)));
3177 EXPECT_TRUE(matchAndVerifyResultTrue(
3178 "void x() { return; if (true) {} }",
3179 functionDecl(decl().bind("x"),
3180 has(compoundStmt(hasAnySubstatement(ifStmt())))),
3181 new VerifyIdIsBoundTo<Decl>("x", 1)));
3182 EXPECT_TRUE(matchAndVerifyResultTrue(
3183 "namespace X { void b(int); void b(); }"
3184 "using X::b;",
3185 usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl(
3186 functionDecl(parameterCountIs(1))))),
3187 new VerifyIdIsBoundTo<Decl>("x", 1)));
3188 EXPECT_TRUE(matchAndVerifyResultTrue(
3189 "class A{}; class B{}; class C : B, A {};",
3190 recordDecl(decl().bind("x"), isDerivedFrom("::A")),
3191 new VerifyIdIsBoundTo<Decl>("x", 1)));
3192 EXPECT_TRUE(matchAndVerifyResultTrue(
3193 "class A{}; typedef A B; typedef A C; typedef A D;"
3194 "class E : A {};",
3195 recordDecl(decl().bind("x"), isDerivedFrom("C")),
3196 new VerifyIdIsBoundTo<Decl>("x", 1)));
3197 EXPECT_TRUE(matchAndVerifyResultTrue(
3198 "class A { class B { void f() {} }; };",
3199 functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3200 new VerifyIdIsBoundTo<Decl>("x", 1)));
3201 EXPECT_TRUE(matchAndVerifyResultTrue(
3202 "template <typename T> struct A { struct B {"
3203 " void f() { if(true) {} }"
3204 "}; };"
3205 "void t() { A<int>::B b; b.f(); }",
3206 ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3207 new VerifyIdIsBoundTo<Stmt>("x", 2)));
3208 EXPECT_TRUE(matchAndVerifyResultTrue(
3209 "class A {};",
3210 recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))),
3211 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimek06963012013-07-19 11:50:54 +00003212 EXPECT_TRUE(matchAndVerifyResultTrue(
3213 "class A { A() : s(), i(42) {} const char *s; int i; };",
3214 constructorDecl(hasName("::A::A"), decl().bind("x"),
3215 forEachConstructorInitializer(forField(hasName("i")))),
3216 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimek054d0492013-06-19 15:42:45 +00003217}
3218
Daniel Jasper11c98772012-11-11 22:14:55 +00003219TEST(ForEachDescendant, BindsCorrectNodes) {
3220 EXPECT_TRUE(matchAndVerifyResultTrue(
3221 "class C { void f(); int i; };",
3222 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3223 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
3224 EXPECT_TRUE(matchAndVerifyResultTrue(
3225 "class C { void f() {} int i; };",
3226 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3227 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
3228}
3229
Manuel Klimek152ea0e2013-02-04 10:59:20 +00003230TEST(FindAll, BindsNodeOnMatch) {
3231 EXPECT_TRUE(matchAndVerifyResultTrue(
3232 "class A {};",
3233 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
3234 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
3235}
3236
3237TEST(FindAll, BindsDescendantNodeOnMatch) {
3238 EXPECT_TRUE(matchAndVerifyResultTrue(
3239 "class A { int a; int b; };",
3240 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
3241 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3242}
3243
3244TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
3245 EXPECT_TRUE(matchAndVerifyResultTrue(
3246 "class A { int a; int b; };",
3247 recordDecl(hasName("::A"),
3248 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
3249 fieldDecl().bind("v"))))),
3250 new VerifyIdIsBoundTo<Decl>("v", 3)));
3251
3252 EXPECT_TRUE(matchAndVerifyResultTrue(
3253 "class A { class B {}; class C {}; };",
3254 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
3255 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3256}
3257
Manuel Klimek73876732013-02-04 09:42:38 +00003258TEST(EachOf, TriggersForEachMatch) {
3259 EXPECT_TRUE(matchAndVerifyResultTrue(
3260 "class A { int a; int b; };",
3261 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3262 has(fieldDecl(hasName("b")).bind("v")))),
3263 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3264}
3265
3266TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3267 EXPECT_TRUE(matchAndVerifyResultTrue(
3268 "class A { int a; int c; };",
3269 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3270 has(fieldDecl(hasName("b")).bind("v")))),
3271 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3272 EXPECT_TRUE(matchAndVerifyResultTrue(
3273 "class A { int c; int b; };",
3274 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3275 has(fieldDecl(hasName("b")).bind("v")))),
3276 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3277 EXPECT_TRUE(notMatches(
3278 "class A { int c; int d; };",
3279 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3280 has(fieldDecl(hasName("b")).bind("v"))))));
3281}
Manuel Klimek4da21662012-07-06 05:48:52 +00003282
3283TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3284 // Make sure that we can both match the class by name (::X) and by the type
3285 // the template was instantiated with (via a field).
3286
3287 EXPECT_TRUE(matches(
3288 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003289 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003290
3291 EXPECT_TRUE(matches(
3292 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003293 recordDecl(isTemplateInstantiation(), hasDescendant(
3294 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003295}
3296
3297TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3298 EXPECT_TRUE(matches(
3299 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003300 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00003301 isTemplateInstantiation())));
3302}
3303
3304TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3305 EXPECT_TRUE(matches(
3306 "template <typename T> class X { T t; }; class A {};"
3307 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003308 recordDecl(isTemplateInstantiation(), hasDescendant(
3309 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003310}
3311
3312TEST(IsTemplateInstantiation,
3313 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3314 EXPECT_TRUE(matches(
3315 "template <typename T> class X {};"
3316 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003317 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003318}
3319
3320TEST(IsTemplateInstantiation,
3321 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3322 EXPECT_TRUE(matches(
3323 "class A {};"
3324 "class X {"
3325 " template <typename U> class Y { U u; };"
3326 " Y<A> y;"
3327 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003328 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003329}
3330
3331TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3332 // FIXME: Figure out whether this makes sense. It doesn't affect the
3333 // normal use case as long as the uppermost instantiation always is marked
3334 // as template instantiation, but it might be confusing as a predicate.
3335 EXPECT_TRUE(matches(
3336 "class A {};"
3337 "template <typename T> class X {"
3338 " template <typename U> class Y { U u; };"
3339 " Y<T> y;"
3340 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003341 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003342}
3343
3344TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3345 EXPECT_TRUE(notMatches(
3346 "template <typename T> class X {}; class A {};"
3347 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003348 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003349}
3350
3351TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3352 EXPECT_TRUE(notMatches(
3353 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003354 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003355}
3356
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003357TEST(IsExplicitTemplateSpecialization,
3358 DoesNotMatchPrimaryTemplate) {
3359 EXPECT_TRUE(notMatches(
3360 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003361 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003362 EXPECT_TRUE(notMatches(
3363 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003364 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003365}
3366
3367TEST(IsExplicitTemplateSpecialization,
3368 DoesNotMatchExplicitTemplateInstantiations) {
3369 EXPECT_TRUE(notMatches(
3370 "template <typename T> class X {};"
3371 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003372 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003373 EXPECT_TRUE(notMatches(
3374 "template <typename T> void f(T t) {}"
3375 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003376 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003377}
3378
3379TEST(IsExplicitTemplateSpecialization,
3380 DoesNotMatchImplicitTemplateInstantiations) {
3381 EXPECT_TRUE(notMatches(
3382 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003383 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003384 EXPECT_TRUE(notMatches(
3385 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003386 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003387}
3388
3389TEST(IsExplicitTemplateSpecialization,
3390 MatchesExplicitTemplateSpecializations) {
3391 EXPECT_TRUE(matches(
3392 "template <typename T> class X {};"
3393 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003394 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003395 EXPECT_TRUE(matches(
3396 "template <typename T> void f(T t) {}"
3397 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003398 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003399}
3400
Manuel Klimek579b1202012-09-07 09:26:10 +00003401TEST(HasAncenstor, MatchesDeclarationAncestors) {
3402 EXPECT_TRUE(matches(
3403 "class A { class B { class C {}; }; };",
3404 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3405}
3406
3407TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3408 EXPECT_TRUE(notMatches(
3409 "class A { class B { class C {}; }; };",
3410 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3411}
3412
3413TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3414 EXPECT_TRUE(matches(
3415 "class A { class B { void f() { C c; } class C {}; }; };",
3416 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3417 hasAncestor(recordDecl(hasName("A"))))))));
3418}
3419
3420TEST(HasAncenstor, MatchesStatementAncestors) {
3421 EXPECT_TRUE(matches(
3422 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003423 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003424}
3425
3426TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3427 EXPECT_TRUE(matches(
3428 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003429 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003430}
3431
3432TEST(HasAncestor, BindsRecursiveCombinations) {
3433 EXPECT_TRUE(matchAndVerifyResultTrue(
3434 "class C { class D { class E { class F { int y; }; }; }; };",
3435 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003436 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00003437}
3438
3439TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3440 EXPECT_TRUE(matchAndVerifyResultTrue(
3441 "class C { class D { class E { class F { int y; }; }; }; };",
3442 fieldDecl(hasAncestor(
3443 decl(
3444 hasDescendant(recordDecl(isDefinition(),
3445 hasAncestor(recordDecl())))
3446 ).bind("d")
3447 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00003448 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00003449}
3450
Manuel Klimek374516c2013-03-14 16:33:21 +00003451TEST(HasAncestor, MatchesClosestAncestor) {
3452 EXPECT_TRUE(matchAndVerifyResultTrue(
3453 "template <typename T> struct C {"
3454 " void f(int) {"
3455 " struct I { void g(T) { int x; } } i; i.g(42);"
3456 " }"
3457 "};"
3458 "template struct C<int>;",
3459 varDecl(hasName("x"),
3460 hasAncestor(functionDecl(hasParameter(
3461 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"),
3462 new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2)));
3463}
3464
Manuel Klimek579b1202012-09-07 09:26:10 +00003465TEST(HasAncestor, MatchesInTemplateInstantiations) {
3466 EXPECT_TRUE(matches(
3467 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3468 "A<int>::B::C a;",
3469 fieldDecl(hasType(asString("int")),
3470 hasAncestor(recordDecl(hasName("A"))))));
3471}
3472
3473TEST(HasAncestor, MatchesInImplicitCode) {
3474 EXPECT_TRUE(matches(
3475 "struct X {}; struct A { A() {} X x; };",
3476 constructorDecl(
3477 hasAnyConstructorInitializer(withInitializer(expr(
3478 hasAncestor(recordDecl(hasName("A")))))))));
3479}
3480
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003481TEST(HasParent, MatchesOnlyParent) {
3482 EXPECT_TRUE(matches(
3483 "void f() { if (true) { int x = 42; } }",
3484 compoundStmt(hasParent(ifStmt()))));
3485 EXPECT_TRUE(notMatches(
3486 "void f() { for (;;) { int x = 42; } }",
3487 compoundStmt(hasParent(ifStmt()))));
3488 EXPECT_TRUE(notMatches(
3489 "void f() { if (true) for (;;) { int x = 42; } }",
3490 compoundStmt(hasParent(ifStmt()))));
3491}
3492
Manuel Klimek30ace372012-12-06 14:42:48 +00003493TEST(HasAncestor, MatchesAllAncestors) {
3494 EXPECT_TRUE(matches(
3495 "template <typename T> struct C { static void f() { 42; } };"
3496 "void t() { C<int>::f(); }",
3497 integerLiteral(
3498 equals(42),
3499 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3500 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3501}
3502
3503TEST(HasParent, MatchesAllParents) {
3504 EXPECT_TRUE(matches(
3505 "template <typename T> struct C { static void f() { 42; } };"
3506 "void t() { C<int>::f(); }",
3507 integerLiteral(
3508 equals(42),
3509 hasParent(compoundStmt(hasParent(functionDecl(
3510 hasParent(recordDecl(isTemplateInstantiation())))))))));
3511 EXPECT_TRUE(matches(
3512 "template <typename T> struct C { static void f() { 42; } };"
3513 "void t() { C<int>::f(); }",
3514 integerLiteral(
3515 equals(42),
3516 hasParent(compoundStmt(hasParent(functionDecl(
3517 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3518 EXPECT_TRUE(matches(
3519 "template <typename T> struct C { static void f() { 42; } };"
3520 "void t() { C<int>::f(); }",
3521 integerLiteral(equals(42),
3522 hasParent(compoundStmt(allOf(
3523 hasParent(functionDecl(
3524 hasParent(recordDecl(isTemplateInstantiation())))),
3525 hasParent(functionDecl(hasParent(recordDecl(
3526 unless(isTemplateInstantiation())))))))))));
Manuel Klimek374516c2013-03-14 16:33:21 +00003527 EXPECT_TRUE(
3528 notMatches("template <typename T> struct C { static void f() {} };"
3529 "void t() { C<int>::f(); }",
3530 compoundStmt(hasParent(recordDecl()))));
Manuel Klimek30ace372012-12-06 14:42:48 +00003531}
3532
Daniel Jasperce620072012-10-17 08:52:59 +00003533TEST(TypeMatching, MatchesTypes) {
3534 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3535}
3536
3537TEST(TypeMatching, MatchesArrayTypes) {
3538 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3539 EXPECT_TRUE(matches("int a[42];", arrayType()));
3540 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3541
3542 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3543 arrayType(hasElementType(builtinType()))));
3544
3545 EXPECT_TRUE(matches(
3546 "int const a[] = { 2, 3 };",
3547 qualType(arrayType(hasElementType(builtinType())))));
3548 EXPECT_TRUE(matches(
3549 "int const a[] = { 2, 3 };",
3550 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3551 EXPECT_TRUE(matches(
3552 "typedef const int T; T x[] = { 1, 2 };",
3553 qualType(isConstQualified(), arrayType())));
3554
3555 EXPECT_TRUE(notMatches(
3556 "int a[] = { 2, 3 };",
3557 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3558 EXPECT_TRUE(notMatches(
3559 "int a[] = { 2, 3 };",
3560 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3561 EXPECT_TRUE(notMatches(
3562 "int const a[] = { 2, 3 };",
3563 qualType(arrayType(hasElementType(builtinType())),
3564 unless(isConstQualified()))));
3565
3566 EXPECT_TRUE(matches("int a[2];",
3567 constantArrayType(hasElementType(builtinType()))));
3568 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3569}
3570
3571TEST(TypeMatching, MatchesComplexTypes) {
3572 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3573 EXPECT_TRUE(matches(
3574 "_Complex float f;",
3575 complexType(hasElementType(builtinType()))));
3576 EXPECT_TRUE(notMatches(
3577 "_Complex float f;",
3578 complexType(hasElementType(isInteger()))));
3579}
3580
3581TEST(TypeMatching, MatchesConstantArrayTypes) {
3582 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3583 EXPECT_TRUE(notMatches(
3584 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3585 constantArrayType(hasElementType(builtinType()))));
3586
3587 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3588 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3589 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3590}
3591
3592TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3593 EXPECT_TRUE(matches(
3594 "template <typename T, int Size> class array { T data[Size]; };",
3595 dependentSizedArrayType()));
3596 EXPECT_TRUE(notMatches(
3597 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3598 dependentSizedArrayType()));
3599}
3600
3601TEST(TypeMatching, MatchesIncompleteArrayType) {
3602 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3603 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3604
3605 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3606 incompleteArrayType()));
3607}
3608
3609TEST(TypeMatching, MatchesVariableArrayType) {
3610 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3611 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3612
3613 EXPECT_TRUE(matches(
3614 "void f(int b) { int a[b]; }",
3615 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3616 varDecl(hasName("b")))))))));
3617}
3618
3619TEST(TypeMatching, MatchesAtomicTypes) {
3620 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3621
3622 EXPECT_TRUE(matches("_Atomic(int) i;",
3623 atomicType(hasValueType(isInteger()))));
3624 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3625 atomicType(hasValueType(isInteger()))));
3626}
3627
3628TEST(TypeMatching, MatchesAutoTypes) {
3629 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3630 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3631 autoType()));
3632
Richard Smith9b131752013-04-30 21:23:01 +00003633 // FIXME: Matching against the type-as-written can't work here, because the
3634 // type as written was not deduced.
3635 //EXPECT_TRUE(matches("auto a = 1;",
3636 // autoType(hasDeducedType(isInteger()))));
3637 //EXPECT_TRUE(notMatches("auto b = 2.0;",
3638 // autoType(hasDeducedType(isInteger()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003639}
3640
Daniel Jaspera267cf62012-10-29 10:14:44 +00003641TEST(TypeMatching, MatchesFunctionTypes) {
3642 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3643 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3644}
3645
Edwin Vane88be2fd2013-04-01 18:33:34 +00003646TEST(TypeMatching, MatchesParenType) {
3647 EXPECT_TRUE(
3648 matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
3649 EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
3650
3651 EXPECT_TRUE(matches(
3652 "int (*ptr_to_func)(int);",
3653 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3654 EXPECT_TRUE(notMatches(
3655 "int (*ptr_to_array)[4];",
3656 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3657}
3658
Daniel Jasperce620072012-10-17 08:52:59 +00003659TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003660 // FIXME: Reactive when these tests can be more specific (not matching
3661 // implicit code on certain platforms), likely when we have hasDescendant for
3662 // Types/TypeLocs.
3663 //EXPECT_TRUE(matchAndVerifyResultTrue(
3664 // "int* a;",
3665 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3666 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3667 //EXPECT_TRUE(matchAndVerifyResultTrue(
3668 // "int* a;",
3669 // pointerTypeLoc().bind("loc"),
3670 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003671 EXPECT_TRUE(matches(
3672 "int** a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003673 loc(pointerType(pointee(qualType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003674 EXPECT_TRUE(matches(
3675 "int** a;",
3676 loc(pointerType(pointee(pointerType())))));
3677 EXPECT_TRUE(matches(
3678 "int* b; int* * const a = &b;",
3679 loc(qualType(isConstQualified(), pointerType()))));
3680
3681 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003682 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3683 hasType(blockPointerType()))));
3684 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3685 hasType(memberPointerType()))));
3686 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3687 hasType(pointerType()))));
3688 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3689 hasType(referenceType()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003690 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3691 hasType(lValueReferenceType()))));
3692 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3693 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003694
Daniel Jasper1802daf2012-10-17 13:35:36 +00003695 Fragment = "int *ptr;";
3696 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3697 hasType(blockPointerType()))));
3698 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3699 hasType(memberPointerType()))));
3700 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3701 hasType(pointerType()))));
3702 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3703 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003704
Daniel Jasper1802daf2012-10-17 13:35:36 +00003705 Fragment = "int a; int &ref = a;";
3706 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3707 hasType(blockPointerType()))));
3708 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3709 hasType(memberPointerType()))));
3710 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3711 hasType(pointerType()))));
3712 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3713 hasType(referenceType()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003714 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3715 hasType(lValueReferenceType()))));
3716 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3717 hasType(rValueReferenceType()))));
3718
3719 Fragment = "int &&ref = 2;";
3720 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3721 hasType(blockPointerType()))));
3722 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3723 hasType(memberPointerType()))));
3724 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3725 hasType(pointerType()))));
3726 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3727 hasType(referenceType()))));
3728 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3729 hasType(lValueReferenceType()))));
3730 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3731 hasType(rValueReferenceType()))));
3732}
3733
3734TEST(TypeMatching, AutoRefTypes) {
3735 std::string Fragment = "auto a = 1;"
3736 "auto b = a;"
3737 "auto &c = a;"
3738 "auto &&d = c;"
3739 "auto &&e = 2;";
3740 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
3741 hasType(referenceType()))));
3742 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
3743 hasType(referenceType()))));
3744 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3745 hasType(referenceType()))));
3746 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3747 hasType(lValueReferenceType()))));
3748 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
3749 hasType(rValueReferenceType()))));
3750 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3751 hasType(referenceType()))));
3752 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3753 hasType(lValueReferenceType()))));
3754 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
3755 hasType(rValueReferenceType()))));
3756 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3757 hasType(referenceType()))));
3758 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
3759 hasType(lValueReferenceType()))));
3760 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3761 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003762}
3763
3764TEST(TypeMatching, PointeeTypes) {
3765 EXPECT_TRUE(matches("int b; int &a = b;",
3766 referenceType(pointee(builtinType()))));
3767 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3768
3769 EXPECT_TRUE(matches("int *a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003770 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003771
3772 EXPECT_TRUE(matches(
3773 "int const *A;",
3774 pointerType(pointee(isConstQualified(), builtinType()))));
3775 EXPECT_TRUE(notMatches(
3776 "int *A;",
3777 pointerType(pointee(isConstQualified(), builtinType()))));
3778}
3779
3780TEST(TypeMatching, MatchesPointersToConstTypes) {
3781 EXPECT_TRUE(matches("int b; int * const a = &b;",
3782 loc(pointerType())));
3783 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003784 loc(pointerType())));
Daniel Jasperce620072012-10-17 08:52:59 +00003785 EXPECT_TRUE(matches(
3786 "int b; const int * a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003787 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003788 EXPECT_TRUE(matches(
3789 "int b; const int * a = &b;",
3790 pointerType(pointee(builtinType()))));
3791}
3792
3793TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003794 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3795 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003796}
3797
Edwin Vane3abf7782013-02-25 14:49:29 +00003798TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vane742d9e72013-02-25 20:43:32 +00003799 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vane3abf7782013-02-25 14:49:29 +00003800 templateSpecializationType()));
3801}
3802
Edwin Vane742d9e72013-02-25 20:43:32 +00003803TEST(TypeMatching, MatchesRecordType) {
3804 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek0cc798f2013-02-27 11:56:58 +00003805 EXPECT_TRUE(matches("struct S{}; S s;",
3806 recordType(hasDeclaration(recordDecl(hasName("S"))))));
3807 EXPECT_TRUE(notMatches("int i;",
3808 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003809}
3810
3811TEST(TypeMatching, MatchesElaboratedType) {
3812 EXPECT_TRUE(matches(
3813 "namespace N {"
3814 " namespace M {"
3815 " class D {};"
3816 " }"
3817 "}"
3818 "N::M::D d;", elaboratedType()));
3819 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
3820 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
3821}
3822
3823TEST(ElaboratedTypeNarrowing, hasQualifier) {
3824 EXPECT_TRUE(matches(
3825 "namespace N {"
3826 " namespace M {"
3827 " class D {};"
3828 " }"
3829 "}"
3830 "N::M::D d;",
3831 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3832 EXPECT_TRUE(notMatches(
3833 "namespace M {"
3834 " class D {};"
3835 "}"
3836 "M::D d;",
3837 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vaneaec89ac2013-03-04 17:51:00 +00003838 EXPECT_TRUE(notMatches(
3839 "struct D {"
3840 "} d;",
3841 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003842}
3843
3844TEST(ElaboratedTypeNarrowing, namesType) {
3845 EXPECT_TRUE(matches(
3846 "namespace N {"
3847 " namespace M {"
3848 " class D {};"
3849 " }"
3850 "}"
3851 "N::M::D d;",
3852 elaboratedType(elaboratedType(namesType(recordType(
3853 hasDeclaration(namedDecl(hasName("D")))))))));
3854 EXPECT_TRUE(notMatches(
3855 "namespace M {"
3856 " class D {};"
3857 "}"
3858 "M::D d;",
3859 elaboratedType(elaboratedType(namesType(typedefType())))));
3860}
3861
Daniel Jaspera7564432012-09-13 13:11:25 +00003862TEST(NNS, MatchesNestedNameSpecifiers) {
3863 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3864 nestedNameSpecifier()));
3865 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3866 nestedNameSpecifier()));
3867 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3868 nestedNameSpecifier()));
3869
3870 EXPECT_TRUE(matches(
3871 "struct A { static void f() {} }; void g() { A::f(); }",
3872 nestedNameSpecifier()));
3873 EXPECT_TRUE(notMatches(
3874 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3875 nestedNameSpecifier()));
3876}
3877
Daniel Jasperb54b7642012-09-20 14:12:57 +00003878TEST(NullStatement, SimpleCases) {
3879 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3880 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3881}
3882
Daniel Jaspera7564432012-09-13 13:11:25 +00003883TEST(NNS, MatchesTypes) {
3884 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3885 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3886 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3887 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3888 Matcher));
3889 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3890}
3891
3892TEST(NNS, MatchesNamespaceDecls) {
3893 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3894 specifiesNamespace(hasName("ns")));
3895 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3896 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3897 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3898}
3899
3900TEST(NNS, BindsNestedNameSpecifiers) {
3901 EXPECT_TRUE(matchAndVerifyResultTrue(
3902 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3903 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3904 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3905}
3906
3907TEST(NNS, BindsNestedNameSpecifierLocs) {
3908 EXPECT_TRUE(matchAndVerifyResultTrue(
3909 "namespace ns { struct B {}; } ns::B b;",
3910 loc(nestedNameSpecifier()).bind("loc"),
3911 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3912}
3913
3914TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3915 EXPECT_TRUE(matches(
3916 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3917 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3918 EXPECT_TRUE(matches(
3919 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003920 nestedNameSpecifierLoc(hasPrefix(
3921 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003922}
3923
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003924TEST(NNS, DescendantsOfNestedNameSpecifiers) {
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 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3931 hasDescendant(nestedNameSpecifier(
3932 specifiesNamespace(hasName("a")))))));
3933 EXPECT_TRUE(notMatches(
3934 Fragment,
3935 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3936 has(nestedNameSpecifier(
3937 specifiesNamespace(hasName("a")))))));
3938 EXPECT_TRUE(matches(
3939 Fragment,
3940 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3941 has(nestedNameSpecifier(
3942 specifiesNamespace(hasName("a")))))));
3943
3944 // Not really useful because a NestedNameSpecifier can af at most one child,
3945 // but to complete the interface.
3946 EXPECT_TRUE(matchAndVerifyResultTrue(
3947 Fragment,
3948 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3949 forEach(nestedNameSpecifier().bind("x"))),
3950 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3951}
3952
3953TEST(NNS, NestedNameSpecifiersAsDescendants) {
3954 std::string Fragment =
3955 "namespace a { struct A { struct B { struct C {}; }; }; };"
3956 "void f() { a::A::B::C c; }";
3957 EXPECT_TRUE(matches(
3958 Fragment,
3959 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3960 asString("struct a::A")))))));
3961 EXPECT_TRUE(matchAndVerifyResultTrue(
3962 Fragment,
3963 functionDecl(hasName("f"),
3964 forEachDescendant(nestedNameSpecifier().bind("x"))),
3965 // Nested names: a, a::A and a::A::B.
3966 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3967}
3968
3969TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3970 std::string Fragment =
3971 "namespace a { struct A { struct B { struct C {}; }; }; };"
3972 "void f() { a::A::B::C c; }";
3973 EXPECT_TRUE(matches(
3974 Fragment,
3975 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3976 hasDescendant(loc(nestedNameSpecifier(
3977 specifiesNamespace(hasName("a"))))))));
3978 EXPECT_TRUE(notMatches(
3979 Fragment,
3980 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3981 has(loc(nestedNameSpecifier(
3982 specifiesNamespace(hasName("a"))))))));
3983 EXPECT_TRUE(matches(
3984 Fragment,
3985 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3986 has(loc(nestedNameSpecifier(
3987 specifiesNamespace(hasName("a"))))))));
3988
3989 EXPECT_TRUE(matchAndVerifyResultTrue(
3990 Fragment,
3991 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3992 forEach(nestedNameSpecifierLoc().bind("x"))),
3993 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3994}
3995
3996TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3997 std::string Fragment =
3998 "namespace a { struct A { struct B { struct C {}; }; }; };"
3999 "void f() { a::A::B::C c; }";
4000 EXPECT_TRUE(matches(
4001 Fragment,
4002 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
4003 asString("struct a::A"))))))));
4004 EXPECT_TRUE(matchAndVerifyResultTrue(
4005 Fragment,
4006 functionDecl(hasName("f"),
4007 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
4008 // Nested names: a, a::A and a::A::B.
4009 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
4010}
4011
Manuel Klimek60969f52013-02-01 13:41:35 +00004012template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004013public:
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004014 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
4015 StringRef InnerId)
4016 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jasper452abbc2012-10-29 10:48:25 +00004017 }
4018
Manuel Klimek60969f52013-02-01 13:41:35 +00004019 virtual bool run(const BoundNodes *Nodes) { return false; }
4020
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004021 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4022 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004023 return selectFirst<const T>(InnerId,
4024 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004025 }
4026private:
4027 std::string Id;
4028 internal::Matcher<T> InnerMatcher;
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004029 std::string InnerId;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004030};
4031
4032TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00004033 EXPECT_TRUE(matchAndVerifyResultTrue(
4034 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4035 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004036 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
4037 "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004038 EXPECT_TRUE(matchAndVerifyResultFalse(
4039 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4040 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004041 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
4042 "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004043}
4044
4045TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00004046 EXPECT_TRUE(matchAndVerifyResultTrue(
4047 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004048 new VerifyMatchOnNode<clang::Stmt>(
4049 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004050 EXPECT_TRUE(matchAndVerifyResultFalse(
4051 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004052 new VerifyMatchOnNode<clang::Stmt>(
4053 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004054}
4055
4056TEST(MatchFinder, CanMatchSingleNodesRecursively) {
4057 EXPECT_TRUE(matchAndVerifyResultTrue(
4058 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4059 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004060 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004061 EXPECT_TRUE(matchAndVerifyResultFalse(
4062 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4063 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004064 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004065}
4066
Manuel Klimekfa37c5c2013-02-07 12:42:10 +00004067template <typename T>
4068class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
4069public:
4070 virtual bool run(const BoundNodes *Nodes) { return false; }
4071
4072 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4073 const T *Node = Nodes->getNodeAs<T>("");
4074 return verify(*Nodes, *Context, Node);
4075 }
4076
4077 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
4078 return selectFirst<const T>(
4079 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
4080 *Node, Context)) != NULL;
4081 }
4082 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
4083 return selectFirst<const T>(
4084 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
4085 *Node, Context)) != NULL;
4086 }
4087};
4088
4089TEST(IsEqualTo, MatchesNodesByIdentity) {
4090 EXPECT_TRUE(matchAndVerifyResultTrue(
4091 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
4092 new VerifyAncestorHasChildIsEqual<Decl>()));
4093 EXPECT_TRUE(
4094 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
4095 new VerifyAncestorHasChildIsEqual<Stmt>()));
4096}
4097
Manuel Klimeke5793282012-11-02 01:31:03 +00004098class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
4099public:
4100 VerifyStartOfTranslationUnit() : Called(false) {}
4101 virtual void run(const MatchFinder::MatchResult &Result) {
4102 EXPECT_TRUE(Called);
4103 }
4104 virtual void onStartOfTranslationUnit() {
4105 Called = true;
4106 }
4107 bool Called;
4108};
4109
4110TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
4111 MatchFinder Finder;
4112 VerifyStartOfTranslationUnit VerifyCallback;
4113 Finder.addMatcher(decl(), &VerifyCallback);
4114 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4115 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4116 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbourne51fcdf82013-11-07 22:30:36 +00004117
4118 VerifyCallback.Called = false;
4119 OwningPtr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
4120 ASSERT_TRUE(AST.get());
4121 Finder.matchAST(AST->getASTContext());
4122 EXPECT_TRUE(VerifyCallback.Called);
Manuel Klimeke5793282012-11-02 01:31:03 +00004123}
4124
Peter Collingbourne8f9e5902013-05-28 19:21:51 +00004125class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback {
4126public:
4127 VerifyEndOfTranslationUnit() : Called(false) {}
4128 virtual void run(const MatchFinder::MatchResult &Result) {
4129 EXPECT_FALSE(Called);
4130 }
4131 virtual void onEndOfTranslationUnit() {
4132 Called = true;
4133 }
4134 bool Called;
4135};
4136
4137TEST(MatchFinder, InterceptsEndOfTranslationUnit) {
4138 MatchFinder Finder;
4139 VerifyEndOfTranslationUnit VerifyCallback;
4140 Finder.addMatcher(decl(), &VerifyCallback);
4141 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4142 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4143 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbourne51fcdf82013-11-07 22:30:36 +00004144
4145 VerifyCallback.Called = false;
4146 OwningPtr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
4147 ASSERT_TRUE(AST.get());
4148 Finder.matchAST(AST->getASTContext());
4149 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbourne8f9e5902013-05-28 19:21:51 +00004150}
4151
Manuel Klimekcf52ca62013-06-20 14:06:32 +00004152TEST(EqualsBoundNodeMatcher, QualType) {
4153 EXPECT_TRUE(matches(
4154 "int i = 1;", varDecl(hasType(qualType().bind("type")),
4155 hasInitializer(ignoringParenImpCasts(
4156 hasType(qualType(equalsBoundNode("type"))))))));
4157 EXPECT_TRUE(notMatches("int i = 1.f;",
4158 varDecl(hasType(qualType().bind("type")),
4159 hasInitializer(ignoringParenImpCasts(hasType(
4160 qualType(equalsBoundNode("type"))))))));
4161}
4162
4163TEST(EqualsBoundNodeMatcher, NonMatchingTypes) {
4164 EXPECT_TRUE(notMatches(
4165 "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"),
4166 hasInitializer(ignoringParenImpCasts(
4167 hasType(qualType(equalsBoundNode("type"))))))));
4168}
4169
4170TEST(EqualsBoundNodeMatcher, Stmt) {
4171 EXPECT_TRUE(
4172 matches("void f() { if(true) {} }",
4173 stmt(allOf(ifStmt().bind("if"),
4174 hasParent(stmt(has(stmt(equalsBoundNode("if")))))))));
4175
4176 EXPECT_TRUE(notMatches(
4177 "void f() { if(true) { if (true) {} } }",
4178 stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if")))))));
4179}
4180
4181TEST(EqualsBoundNodeMatcher, Decl) {
4182 EXPECT_TRUE(matches(
4183 "class X { class Y {}; };",
4184 decl(allOf(recordDecl(hasName("::X::Y")).bind("record"),
4185 hasParent(decl(has(decl(equalsBoundNode("record")))))))));
4186
4187 EXPECT_TRUE(notMatches("class X { class Y {}; };",
4188 decl(allOf(recordDecl(hasName("::X")).bind("record"),
4189 has(decl(equalsBoundNode("record")))))));
4190}
4191
4192TEST(EqualsBoundNodeMatcher, Type) {
4193 EXPECT_TRUE(matches(
4194 "class X { int a; int b; };",
4195 recordDecl(
4196 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4197 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4198
4199 EXPECT_TRUE(notMatches(
4200 "class X { int a; double b; };",
4201 recordDecl(
4202 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4203 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4204}
4205
4206TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) {
4207
4208 EXPECT_TRUE(matchAndVerifyResultTrue(
4209 "int f() {"
4210 " if (1) {"
4211 " int i = 9;"
4212 " }"
4213 " int j = 10;"
4214 " {"
4215 " float k = 9.0;"
4216 " }"
4217 " return 0;"
4218 "}",
4219 // Look for variable declarations within functions whose type is the same
4220 // as the function return type.
4221 functionDecl(returns(qualType().bind("type")),
4222 forEachDescendant(varDecl(hasType(
4223 qualType(equalsBoundNode("type")))).bind("decl"))),
4224 // Only i and j should match, not k.
4225 new VerifyIdIsBoundTo<VarDecl>("decl", 2)));
4226}
4227
4228TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) {
4229 EXPECT_TRUE(matchAndVerifyResultTrue(
4230 "void f() {"
4231 " int x;"
4232 " double d;"
4233 " x = d + x - d + x;"
4234 "}",
4235 functionDecl(
4236 hasName("f"), forEachDescendant(varDecl().bind("d")),
4237 forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))),
4238 new VerifyIdIsBoundTo<VarDecl>("d", 5)));
4239}
4240
Manuel Klimek4da21662012-07-06 05:48:52 +00004241} // end namespace ast_matchers
4242} // end namespace clang