blob: 5c9753f9336b869ff4c6fb5d83a1b71ec2153f62 [file] [log] [blame]
Manuel Klimek04616e42012-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 Kramere5015e52012-12-01 17:22:05 +000011#include "clang/AST/PrettyPrinter.h"
Manuel Klimek04616e42012-07-06 05:48:52 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000013#include "clang/ASTMatchers/ASTMatchers.h"
Manuel Klimek04616e42012-07-06 05:48:52 +000014#include "clang/Tooling/Tooling.h"
15#include "gtest/gtest.h"
16
17namespace clang {
18namespace ast_matchers {
19
Benjamin Kramer60d7f5a2012-07-10 17:30:44 +000020#if GTEST_HAS_DEATH_TEST
Manuel Klimek04616e42012-07-06 05:48:52 +000021TEST(HasNameDeathTest, DiesOnEmptyName) {
22 ASSERT_DEBUG_DEATH({
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000023 DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
Manuel Klimek04616e42012-07-06 05:48:52 +000024 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
25 }, "");
26}
27
Daniel Jasper1dad1832012-07-10 20:20:19 +000028TEST(HasNameDeathTest, DiesOnEmptyPattern) {
29 ASSERT_DEBUG_DEATH({
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000030 DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
Daniel Jasper1dad1832012-07-10 20:20:19 +000031 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
32 }, "");
33}
34
Manuel Klimek04616e42012-07-06 05:48:52 +000035TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
36 ASSERT_DEBUG_DEATH({
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000037 DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom(""));
Manuel Klimek04616e42012-07-06 05:48:52 +000038 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
39 }, "");
40}
Benjamin Kramer60d7f5a2012-07-10 17:30:44 +000041#endif
Manuel Klimek04616e42012-07-06 05:48:52 +000042
Peter Collingbourne2b9471302013-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 Klimeke9235692012-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 Klimek04616e42012-07-06 05:48:52 +000061TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000062 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek04616e42012-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 Jasper1dad1832012-07-10 20:20:19 +000069 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek04616e42012-07-06 05:48:52 +000070
71 EXPECT_TRUE(notMatches("#define X 1", NamedX));
72}
73
Daniel Jasper1dad1832012-07-10 20:20:19 +000074TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000075 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jasper1dad1832012-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 Jasperbd3d76d2012-08-24 05:12:34 +000086 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jasper1dad1832012-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 Jasperbd3d76d2012-08-24 05:12:34 +000090 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jasper1dad1832012-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 Klimeke792efd2012-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 Jasper1dad1832012-07-10 20:20:19 +0000102}
103
Manuel Klimek04616e42012-07-06 05:48:52 +0000104TEST(DeclarationMatcher, MatchClass) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000105 DeclarationMatcher ClassMatcher(recordDecl());
Manuel Klimeka9c86c92012-07-10 14:21:30 +0000106#if !defined(_MSC_VER)
Manuel Klimek04616e42012-07-06 05:48:52 +0000107 EXPECT_FALSE(matches("", ClassMatcher));
Manuel Klimeka9c86c92012-07-10 14:21:30 +0000108#else
109 // Matches class type_info.
110 EXPECT_TRUE(matches("", ClassMatcher));
111#endif
Manuel Klimek04616e42012-07-06 05:48:52 +0000112
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000113 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000121 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000122
123 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasperf49d1e02012-09-07 12:48:17 +0000124 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
125 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek04616e42012-07-06 05:48:52 +0000126 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
127 EXPECT_TRUE(notMatches("", IsDerivedFromX));
128
Daniel Jasperd6b82cb2012-09-12 21:14:15 +0000129 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasperf49d1e02012-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 Klimek04616e42012-07-06 05:48:52 +0000137 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000138 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek04616e42012-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 Klimek5472a522012-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 Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000280 varDecl(hasName("z_float"),
281 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000282 EXPECT_TRUE(notMatches(
283 RecursiveTemplateOneParameter,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000284 varDecl(hasName("z_float"),
285 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000286 EXPECT_TRUE(matches(
287 RecursiveTemplateOneParameter,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000288 varDecl(hasName("z_char"),
289 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
290 isDerivedFrom("Base2")))))));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000305 varDecl(hasName("z_float"),
306 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000307 EXPECT_TRUE(notMatches(
308 RecursiveTemplateTwoParameters,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000309 varDecl(hasName("z_float"),
310 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000311 EXPECT_TRUE(matches(
312 RecursiveTemplateTwoParameters,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000313 varDecl(hasName("z_char"),
314 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
315 isDerivedFrom("Base2")))))));
Daniel Jasper2b3c7d42012-07-17 07:39:27 +0000316 EXPECT_TRUE(matches(
317 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000318 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper2b3c7d42012-07-17 07:39:27 +0000319 EXPECT_TRUE(notMatches(
320 "class X {}; class Y : public X {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000321 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper2b3c7d42012-07-17 07:39:27 +0000322
323 EXPECT_TRUE(matches(
324 "class X {}; class Y : public X {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000325 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek1863e502013-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 Klimek04616e42012-07-06 05:48:52 +0000332}
333
Edwin Vane0a4836e2013-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 Jasper83dafaf2012-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 Vaneb6eae142013-02-25 20:43:32 +0000351TEST(DeclarationMatcher, hasDeclContext) {
352 EXPECT_TRUE(matches(
353 "namespace N {"
354 " namespace M {"
355 " class D {};"
356 " }"
357 "}",
Daniel Jasper9fcdc462013-04-08 16:44:05 +0000358 recordDecl(hasDeclContext(namespaceDecl(hasName("M"))))));
Edwin Vaneb6eae142013-02-25 20:43:32 +0000359 EXPECT_TRUE(notMatches(
360 "namespace N {"
361 " namespace M {"
362 " class D {};"
363 " }"
364 "}",
Daniel Jasper9fcdc462013-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 Vaneb6eae142013-02-25 20:43:32 +0000374}
375
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000376TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000377 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenkod394c8a2012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000383 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenkod394c8a2012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000391 classTemplateDecl(hasName("X"),
392 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenkod394c8a2012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000398 classTemplateDecl(hasName("X"),
399 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000400}
401
Daniel Jasper4e566c42012-07-12 08:50:38 +0000402TEST(AllOf, AllOverloadsWork) {
403 const char Program[] =
Edwin Vanee9dd3602013-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 Jasper4e566c42012-07-12 08:50:38 +0000407 EXPECT_TRUE(matches(Program,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000408 callExpr(allOf(callee(functionDecl(hasName("f"))),
409 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +0000410 EXPECT_TRUE(matches(Program,
Daniel Jasperbd3d76d2012-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 Vanee9dd3602013-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 Jasper4e566c42012-07-12 08:50:38 +0000428}
429
Manuel Klimek04616e42012-07-06 05:48:52 +0000430TEST(DeclarationMatcher, MatchAnyOf) {
431 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000432 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek04616e42012-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 Jasper84c763e2012-07-15 19:57:12 +0000440 DeclarationMatcher XOrYOrZOrU =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000441 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasper84c763e2012-07-15 19:57:12 +0000442 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
443 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
444
Manuel Klimek04616e42012-07-06 05:48:52 +0000445 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000446 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
447 hasName("V")));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000457 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000462 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000471 recordDecl(
472 has(recordDecl(
473 has(recordDecl(hasName("X"))),
474 has(recordDecl(hasName("Y"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000475 hasName("Z"))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000476 has(recordDecl(
477 has(recordDecl(hasName("A"))),
478 has(recordDecl(hasName("B"))),
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000529 recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000530 anyOf(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000531 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000532 anyOf(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000533 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000534 hasName("X"))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000535 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000536 hasName("Y"))),
537 hasName("Z")))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000538 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000539 anyOf(
540 hasName("C"),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000541 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000542 hasName("A"))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000543 has(recordDecl(
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000560 recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000561 isDerivedFrom("Y"),
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000572 recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000573 hasName("X"),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000574 has(recordDecl(hasName("Z"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000575 unless(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000576 has(recordDecl(hasName("Y")))));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000584 recordDecl(
585 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000599 recordDecl(
600 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000622 recordDecl(
623 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
624 hasName("X"))),
Manuel Klimek04616e42012-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 Jasperd29d5fa2012-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 Klimekb64d6b72013-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 Jasperd29d5fa2012-10-29 10:14:44 +0000665
Peter Collingbourne2b9471302013-11-07 22:30:32 +0000666 void onEndOfTranslationUnit() LLVM_OVERRIDE {
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000667 if (ExpectedCount != -1)
668 EXPECT_EQ(ExpectedCount, Count);
669 if (!ExpectedName.empty())
670 EXPECT_EQ(ExpectedName, Name);
Peter Collingbourne2b9471302013-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 Jasperd29d5fa2012-10-29 10:14:44 +0000678 }
679
680 virtual bool run(const BoundNodes *Nodes) {
Peter Collingbourne093a7292013-11-06 00:27:07 +0000681 const BoundNodes::IDToNodeMap &M = Nodes->getMap();
Daniel Jasperd29d5fa2012-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 Collingbourne093a7292013-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 Jasperd29d5fa2012-10-29 10:14:44 +0000695 return true;
696 }
Peter Collingbourne093a7292013-11-06 00:27:07 +0000697 EXPECT_TRUE(M.count(Id) == 0 || M.find(Id)->second.template get<T>() == 0);
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000698 return false;
699 }
700
Daniel Jaspere9aa6872012-10-29 10:48:25 +0000701 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
702 return run(Nodes);
703 }
704
Daniel Jasperd29d5fa2012-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 Blaikieb61d0872013-02-18 19:04:16 +0000741 typeLoc(hasDescendant(loc(builtinType())))));
Daniel Jasperd29d5fa2012-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 Jasper1dad1832012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000778 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jasper1dad1832012-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 Klimek04616e42012-07-06 05:48:52 +0000784TEST(StatementMatcher, Has) {
785 StatementMatcher HasVariableI =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000786 expr(hasType(pointsTo(recordDecl(hasName("X")))),
787 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000797 expr(hasType(pointsTo(recordDecl(hasName("X")))),
798 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000809 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000814 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000821 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000822
823 EXPECT_TRUE(
824 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
825}
826
Manuel Klimek04616e42012-07-06 05:48:52 +0000827TEST(Matcher, BindMatchedNodes) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000828 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000829
830 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000831 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000832
833 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000834 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000835
836 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000837 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000838
839 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
840 TypeAHasClassB,
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000841 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000842
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000843 StatementMatcher MethodX =
844 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek04616e42012-07-06 05:48:52 +0000845
846 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
847 MethodX,
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000848 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000849}
850
851TEST(Matcher, BindTheSameNameInAlternatives) {
852 StatementMatcher matcher = anyOf(
853 binaryOperator(hasOperatorName("+"),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000854 hasLHS(expr().bind("x")),
Daniel Jasper1dad1832012-07-10 20:20:19 +0000855 hasRHS(integerLiteral(equals(0)))),
856 binaryOperator(hasOperatorName("+"),
857 hasLHS(integerLiteral(equals(0))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000858 hasRHS(expr().bind("x"))));
Daniel Jasper1dad1832012-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 Jaspera6bc1f62012-09-13 13:11:25 +0000865 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000866}
867
Manuel Klimekfdf98762012-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 Jaspera6bc1f62012-09-13 13:11:25 +0000877 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimekfdf98762012-08-30 19:41:06 +0000878}
879
Daniel Jasper856194d02012-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 Vaneed936452013-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 Vane2c197e02013-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 Vanef901b712013-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 Klimek04616e42012-07-06 05:48:52 +0000906TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000907 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000908 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000909 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000910 EXPECT_TRUE(
911 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000912 expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000913 EXPECT_TRUE(
914 matches("class X {}; void y(X *x) { x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000915 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000916}
917
918TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000919 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000920 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000921 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000922 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000923 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000924 EXPECT_TRUE(
925 matches("class X {}; void y() { X *x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000926 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000927}
928
929TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000930 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000931 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000932 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000933 EXPECT_TRUE(
934 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000935 expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000936}
937
938TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000939 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000940 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000941 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000942 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000943 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000944}
945
Manuel Klimekc16c6522013-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 Klimek04616e42012-07-06 05:48:52 +0000955TEST(Matcher, Call) {
956 // FIXME: Do we want to overload Call() to directly take
Daniel Jasper1dad1832012-07-10 20:20:19 +0000957 // Matcher<Decl>, too?
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000958 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000963 StatementMatcher MethodOnY =
964 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +0000983 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek04616e42012-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 Jasper5901e472012-10-01 13:40:41 +00001002TEST(Matcher, Lambda) {
Richard Smith3d584b02014-02-06 21:49:08 +00001003 EXPECT_TRUE(matches("auto f = [] (int i) { return i; };",
Daniel Jasper5901e472012-10-01 13:40:41 +00001004 lambdaExpr()));
1005}
1006
1007TEST(Matcher, ForRange) {
Daniel Jasper6f595392012-10-01 15:05:34 +00001008 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
1009 "void f() { for (auto &a : as); }",
Daniel Jasper5901e472012-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 Jasper87c3d362012-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 Jasper1dad1832012-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 Jasperbd3d76d2012-08-24 05:12:34 +00001035 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001036 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001037 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001038 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001039 fieldDecl(hasType(asString("ns::A")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001040 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001041 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001042}
1043
Manuel Klimek04616e42012-07-06 05:48:52 +00001044TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001045 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +00001072 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +00001077 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek04616e42012-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 Vane0a4836e2013-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 Klimek04616e42012-07-06 05:48:52 +00001088}
1089
Daniel Jasper0f9f0192012-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 Klimek04616e42012-07-06 05:48:52 +00001109TEST(Matcher, ThisPointerType) {
Manuel Klimek86f8bbc2012-07-24 13:37:29 +00001110 StatementMatcher MethodOnY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001111 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +00001141 declRefExpr(to(
1142 varDecl(hasInitializer(
1143 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek04616e42012-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 Klimek61379422012-12-04 14:42:08 +00001165TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper3cb72b42012-07-30 05:03:25 +00001166 EXPECT_TRUE(matches(
1167 "void f(int i) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001168 varDecl(hasName("i"))));
Daniel Jasper3cb72b42012-07-30 05:03:25 +00001169}
1170
Manuel Klimek04616e42012-07-06 05:48:52 +00001171TEST(Matcher, CalledVariable) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001172 StatementMatcher CallOnVariableY =
1173 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-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 Jasper1dad1832012-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 Jasperbd3d76d2012-08-24 05:12:34 +00001210 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001211 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001212 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001213}
1214
Manuel Klimek04616e42012-07-06 05:48:52 +00001215TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001216 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001217}
1218
1219TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001220 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001221}
1222
1223TEST(MemberExpression, MatchesVariable) {
1224 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001225 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001226 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001227 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001228 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001229 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001230}
1231
1232TEST(MemberExpression, MatchesStaticVariable) {
1233 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001234 memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001235 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001236 memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001237 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001238 memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001239}
1240
Daniel Jasper4e566c42012-07-12 08:50:38 +00001241TEST(IsInteger, MatchesIntegers) {
Daniel Jasperbd3d76d2012-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 Jasper4e566c42012-07-12 08:50:38 +00001247}
1248
1249TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001250 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00001251 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001252 callExpr(hasArgument(0, declRefExpr(
1253 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00001254}
1255
Manuel Klimek04616e42012-07-06 05:48:52 +00001256TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1257 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001258 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001259 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001260 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001261 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001262 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001263}
1264
1265TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1266 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001267 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001268 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001269 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001270 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001271 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001272}
1273
1274TEST(IsArrow, MatchesMemberCallsViaArrow) {
1275 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001276 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001277 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001278 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001279 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001280 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001281}
1282
1283TEST(Callee, MatchesDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001284 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +00001292 callExpr(callee(memberExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001293 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001294 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001295}
1296
1297TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001298 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001299
1300 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1301 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1302
Manuel Klimeka9c86c92012-07-10 14:21:30 +00001303#if !defined(_MSC_VER)
1304 // FIXME: Make this work for MSVC.
Manuel Klimek04616e42012-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 Klimeka9c86c92012-07-10 14:21:30 +00001311#endif
Manuel Klimek04616e42012-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 Gribenkod394c8a2012-08-17 18:42:47 +00001323TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1324 EXPECT_TRUE(
1325 matches("template <typename T> void f(T t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001326 functionTemplateDecl(hasName("f"))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001327}
1328
1329TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1330 EXPECT_TRUE(
1331 notMatches("void f(double d); void f(int t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001332 functionTemplateDecl(hasName("f"))));
Dmitri Gribenkod394c8a2012-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 Jasperbd3d76d2012-08-24 05:12:34 +00001339 functionTemplateDecl(hasName("f"),
1340 hasDescendant(declRefExpr(to(
1341 functionDecl(hasName("g"))))))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001342}
1343
Manuel Klimek04616e42012-07-06 05:48:52 +00001344TEST(Matcher, Argument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001345 StatementMatcher CallArgumentY = callExpr(
1346 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-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 Jasper848cbe12012-09-18 13:09:13 +00001353 StatementMatcher WrongIndex = callExpr(
1354 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001355 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1356}
1357
1358TEST(Matcher, AnyArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001359 StatementMatcher CallArgumentY = callExpr(
1360 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-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 Jasper848cbe12012-09-18 13:09:13 +00001367 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek04616e42012-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 Jasper9f501292012-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 Klimek04616e42012-07-06 05:48:52 +00001382TEST(Matcher, References) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001383 DeclarationMatcher ReferenceClassX = varDecl(
1384 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek04616e42012-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 Hanc90d12d2013-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 Klimek04616e42012-07-06 05:48:52 +00001391 EXPECT_TRUE(
Michael Hanc90d12d2013-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 Klimek04616e42012-07-06 05:48:52 +00001395 EXPECT_TRUE(
1396 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1397}
1398
Edwin Vane0a4836e2013-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 Vane119d3df2013-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 Klimek04616e42012-07-06 05:48:52 +00001422TEST(HasParameter, CallsInnerMatcher) {
1423 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001424 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001425 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001426 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001427}
1428
1429TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1430 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001431 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001432}
1433
1434TEST(HasType, MatchesParameterVariableTypesStrictly) {
1435 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001436 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001437 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001438 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001439 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001440 methodDecl(hasParameter(0,
1441 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001442 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001443 methodDecl(hasParameter(0,
1444 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-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 Jasperbd3d76d2012-08-24 05:12:34 +00001449 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001450 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001451 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001452}
1453
Daniel Jasper1dad1832012-07-10 20:20:19 +00001454TEST(Returns, MatchesReturnTypes) {
1455 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001456 functionDecl(returns(asString("int")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001457 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001458 functionDecl(returns(asString("float")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001459 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001460 functionDecl(returns(hasDeclaration(
1461 recordDecl(hasName("Y")))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001462}
1463
Daniel Jasperfaaffe32012-08-15 18:52:19 +00001464TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasperbd3d76d2012-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 Jasperfaaffe32012-08-15 18:52:19 +00001469}
1470
Manuel Klimek04616e42012-07-06 05:48:52 +00001471TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1472 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001473 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001474}
1475
1476TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1477 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001478 methodDecl(hasAnyParameter(hasType(pointsTo(
1479 recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001480}
1481
Alp Toker8db6e7a2014-01-05 06:38:57 +00001482TEST(HasName, MatchesParameterVariableDeclarations) {
Manuel Klimek04616e42012-07-06 05:48:52 +00001483 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001484 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001485 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001486 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001487}
1488
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001489TEST(Matcher, MatchesClassTemplateSpecialization) {
1490 EXPECT_TRUE(matches("template<typename T> struct A {};"
1491 "template<> struct A<int> {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001492 classTemplateSpecializationDecl()));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001493 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001494 classTemplateSpecializationDecl()));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001495 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001496 classTemplateSpecializationDecl()));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001497}
1498
Manuel Klimekc16c6522013-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 Jasper8bd14aa2012-08-01 08:40:24 +00001509TEST(Matcher, MatchesTypeTemplateArgument) {
1510 EXPECT_TRUE(matches(
1511 "template<typename T> struct B {};"
1512 "B<int> b;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001513 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper8bd14aa2012-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 Jasperbd3d76d2012-08-24 05:12:34 +00001522 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1523 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasper0c303372012-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 Jasper8bd14aa2012-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 Jasperbd3d76d2012-08-24 05:12:34 +00001536 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper8bd14aa2012-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 Jasperbd3d76d2012-08-24 05:12:34 +00001541 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001542 1, refersToType(asString("int"))))));
1543}
1544
Daniel Jasper639522c2013-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 Vane37ee1d72013-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 Vanefc4f7dc2013-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 Vane37ee1d72013-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 Klimek04616e42012-07-06 05:48:52 +00001585TEST(Matcher, ConstructorCall) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001586 StatementMatcher Constructor = constructExpr();
Manuel Klimek04616e42012-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 Jasper848cbe12012-09-18 13:09:13 +00001600 StatementMatcher Constructor = constructExpr(
1601 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-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 Jasper848cbe12012-09-18 13:09:13 +00001616 StatementMatcher WrongIndex = constructExpr(
1617 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-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 Jasper848cbe12012-09-18 13:09:13 +00001624 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek04616e42012-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
Peter Collingbourne1fec3df2014-02-06 21:52:24 +00001640TEST(Matcher, ConstructorListInitialization) {
1641 StatementMatcher ConstructorListInit = constructExpr(isListInitialization());
1642
1643 EXPECT_TRUE(
1644 matches("class X { public: X(int); }; void x() { X x{0}; }",
1645 ConstructorListInit));
1646 EXPECT_FALSE(
1647 matches("class X { public: X(int); }; void x() { X x(0); }",
1648 ConstructorListInit));
1649}
1650
Manuel Klimek7fca93b2012-10-23 10:40:50 +00001651TEST(Matcher,ThisExpr) {
1652 EXPECT_TRUE(
1653 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1654 EXPECT_TRUE(
1655 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1656}
1657
Manuel Klimek04616e42012-07-06 05:48:52 +00001658TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001659 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001660
1661 std::string ClassString = "class string { public: string(); ~string(); }; ";
1662
1663 EXPECT_TRUE(
1664 matches(ClassString +
1665 "string GetStringByValue();"
1666 "void FunctionTakesString(string s);"
1667 "void run() { FunctionTakesString(GetStringByValue()); }",
1668 TempExpression));
1669
1670 EXPECT_TRUE(
1671 notMatches(ClassString +
1672 "string* GetStringPointer(); "
1673 "void FunctionTakesStringPtr(string* s);"
1674 "void run() {"
1675 " string* s = GetStringPointer();"
1676 " FunctionTakesStringPtr(GetStringPointer());"
1677 " FunctionTakesStringPtr(s);"
1678 "}",
1679 TempExpression));
1680
1681 EXPECT_TRUE(
1682 notMatches("class no_dtor {};"
1683 "no_dtor GetObjByValue();"
1684 "void ConsumeObj(no_dtor param);"
1685 "void run() { ConsumeObj(GetObjByValue()); }",
1686 TempExpression));
1687}
1688
Sam Panzer68a35af2012-08-24 22:04:44 +00001689TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1690 std::string ClassString =
1691 "class string { public: string(); int length(); }; ";
1692
1693 EXPECT_TRUE(
1694 matches(ClassString +
1695 "string GetStringByValue();"
1696 "void FunctionTakesString(string s);"
1697 "void run() { FunctionTakesString(GetStringByValue()); }",
1698 materializeTemporaryExpr()));
1699
1700 EXPECT_TRUE(
1701 notMatches(ClassString +
1702 "string* GetStringPointer(); "
1703 "void FunctionTakesStringPtr(string* s);"
1704 "void run() {"
1705 " string* s = GetStringPointer();"
1706 " FunctionTakesStringPtr(GetStringPointer());"
1707 " FunctionTakesStringPtr(s);"
1708 "}",
1709 materializeTemporaryExpr()));
1710
1711 EXPECT_TRUE(
1712 notMatches(ClassString +
1713 "string GetStringByValue();"
1714 "void run() { int k = GetStringByValue().length(); }",
1715 materializeTemporaryExpr()));
1716
1717 EXPECT_TRUE(
1718 notMatches(ClassString +
1719 "string GetStringByValue();"
1720 "void run() { GetStringByValue(); }",
1721 materializeTemporaryExpr()));
1722}
1723
Manuel Klimek04616e42012-07-06 05:48:52 +00001724TEST(ConstructorDeclaration, SimpleCase) {
1725 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001726 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001727 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001728 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001729}
1730
1731TEST(ConstructorDeclaration, IsImplicit) {
1732 // This one doesn't match because the constructor is not added by the
1733 // compiler (it is not needed).
1734 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001735 constructorDecl(isImplicit())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001736 // The compiler added the implicit default constructor.
1737 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001738 constructorDecl(isImplicit())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001739 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001740 constructorDecl(unless(isImplicit()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001741}
1742
Daniel Jasper1dad1832012-07-10 20:20:19 +00001743TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1744 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001745 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001746}
1747
1748TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001749 EXPECT_TRUE(notMatches("class Foo {};",
1750 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001751}
1752
Manuel Klimek04616e42012-07-06 05:48:52 +00001753TEST(HasAnyConstructorInitializer, SimpleCase) {
1754 EXPECT_TRUE(notMatches(
1755 "class Foo { Foo() { } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001756 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001757 EXPECT_TRUE(matches(
1758 "class Foo {"
1759 " Foo() : foo_() { }"
1760 " int foo_;"
1761 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001762 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001763}
1764
1765TEST(HasAnyConstructorInitializer, ForField) {
1766 static const char Code[] =
1767 "class Baz { };"
1768 "class Foo {"
1769 " Foo() : foo_() { }"
1770 " Baz foo_;"
1771 " Baz bar_;"
1772 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001773 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1774 forField(hasType(recordDecl(hasName("Baz"))))))));
1775 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001776 forField(hasName("foo_"))))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001777 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1778 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001779}
1780
1781TEST(HasAnyConstructorInitializer, WithInitializer) {
1782 static const char Code[] =
1783 "class Foo {"
1784 " Foo() : foo_(0) { }"
1785 " int foo_;"
1786 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001787 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001788 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001789 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001790 withInitializer(integerLiteral(equals(1)))))));
1791}
1792
1793TEST(HasAnyConstructorInitializer, IsWritten) {
1794 static const char Code[] =
1795 "struct Bar { Bar(){} };"
1796 "class Foo {"
1797 " Foo() : foo_() { }"
1798 " Bar foo_;"
1799 " Bar bar_;"
1800 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001801 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001802 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001803 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001804 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001805 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001806 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1807}
1808
1809TEST(Matcher, NewExpression) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001810 StatementMatcher New = newExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001811
1812 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1813 EXPECT_TRUE(
1814 matches("class X { public: X(); }; void x() { new X(); }", New));
1815 EXPECT_TRUE(
1816 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1817 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1818}
1819
1820TEST(Matcher, NewExpressionArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001821 StatementMatcher New = constructExpr(
1822 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001823
1824 EXPECT_TRUE(
1825 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1826 New));
1827 EXPECT_TRUE(
1828 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1829 New));
1830 EXPECT_TRUE(
1831 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1832 New));
1833
Daniel Jasper848cbe12012-09-18 13:09:13 +00001834 StatementMatcher WrongIndex = constructExpr(
1835 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001836 EXPECT_TRUE(
1837 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1838 WrongIndex));
1839}
1840
1841TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001842 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00001843
1844 EXPECT_TRUE(
1845 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1846 EXPECT_TRUE(
1847 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1848 New));
1849}
1850
Daniel Jasper1dad1832012-07-10 20:20:19 +00001851TEST(Matcher, DeleteExpression) {
1852 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001853 deleteExpr()));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001854}
1855
Manuel Klimek04616e42012-07-06 05:48:52 +00001856TEST(Matcher, DefaultArgument) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001857 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001858
1859 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1860 EXPECT_TRUE(
1861 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1862 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1863}
1864
1865TEST(Matcher, StringLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001866 StatementMatcher Literal = stringLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00001867 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1868 // wide string
1869 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1870 // with escaped characters
1871 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1872 // no matching -- though the data type is the same, there is no string literal
1873 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1874}
1875
1876TEST(Matcher, CharacterLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001877 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00001878 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1879 // wide character
1880 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1881 // wide character, Hex encoded, NOT MATCHED!
1882 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1883 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1884}
1885
1886TEST(Matcher, IntegerLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001887 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00001888 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1889 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1890 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1891 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1892
1893 // Non-matching cases (character literals, float and double)
1894 EXPECT_TRUE(notMatches("int i = L'a';",
1895 HasIntLiteral)); // this is actually a character
1896 // literal cast to int
1897 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1898 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1899 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1900}
1901
Daniel Jasper91f1c8c2013-07-26 18:52:58 +00001902TEST(Matcher, FloatLiterals) {
1903 StatementMatcher HasFloatLiteral = floatLiteral();
1904 EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral));
1905 EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral));
1906 EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral));
1907 EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral));
1908 EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral));
1909
1910 EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral));
1911}
1912
Daniel Jasper5901e472012-10-01 13:40:41 +00001913TEST(Matcher, NullPtrLiteral) {
1914 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1915}
1916
Daniel Jasper87c3d362012-09-20 14:12:57 +00001917TEST(Matcher, AsmStatement) {
1918 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1919}
1920
Manuel Klimek04616e42012-07-06 05:48:52 +00001921TEST(Matcher, Conditions) {
1922 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1923
1924 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1925 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1926 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1927 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1928 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1929}
1930
1931TEST(MatchBinaryOperator, HasOperatorName) {
1932 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1933
1934 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1935 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1936}
1937
1938TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1939 StatementMatcher OperatorTrueFalse =
1940 binaryOperator(hasLHS(boolLiteral(equals(true))),
1941 hasRHS(boolLiteral(equals(false))));
1942
1943 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1944 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1945 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1946}
1947
1948TEST(MatchBinaryOperator, HasEitherOperand) {
1949 StatementMatcher HasOperand =
1950 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1951
1952 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1953 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1954 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1955}
1956
1957TEST(Matcher, BinaryOperatorTypes) {
1958 // Integration test that verifies the AST provides all binary operators in
1959 // a way we expect.
1960 // FIXME: Operator ','
1961 EXPECT_TRUE(
1962 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1963 EXPECT_TRUE(
1964 matches("bool b; bool c = (b = true);",
1965 binaryOperator(hasOperatorName("="))));
1966 EXPECT_TRUE(
1967 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1968 EXPECT_TRUE(
1969 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1970 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1971 EXPECT_TRUE(
1972 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1973 EXPECT_TRUE(
1974 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1975 EXPECT_TRUE(
1976 matches("int i = 1; int j = (i <<= 2);",
1977 binaryOperator(hasOperatorName("<<="))));
1978 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1979 EXPECT_TRUE(
1980 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1981 EXPECT_TRUE(
1982 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1983 EXPECT_TRUE(
1984 matches("int i = 1; int j = (i >>= 2);",
1985 binaryOperator(hasOperatorName(">>="))));
1986 EXPECT_TRUE(
1987 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1988 EXPECT_TRUE(
1989 matches("int i = 42; int j = (i ^= 42);",
1990 binaryOperator(hasOperatorName("^="))));
1991 EXPECT_TRUE(
1992 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1993 EXPECT_TRUE(
1994 matches("int i = 42; int j = (i %= 42);",
1995 binaryOperator(hasOperatorName("%="))));
1996 EXPECT_TRUE(
1997 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1998 EXPECT_TRUE(
1999 matches("bool b = true && false;",
2000 binaryOperator(hasOperatorName("&&"))));
2001 EXPECT_TRUE(
2002 matches("bool b = true; bool c = (b &= false);",
2003 binaryOperator(hasOperatorName("&="))));
2004 EXPECT_TRUE(
2005 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
2006 EXPECT_TRUE(
2007 matches("bool b = true || false;",
2008 binaryOperator(hasOperatorName("||"))));
2009 EXPECT_TRUE(
2010 matches("bool b = true; bool c = (b |= false);",
2011 binaryOperator(hasOperatorName("|="))));
2012 EXPECT_TRUE(
2013 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
2014 EXPECT_TRUE(
2015 matches("int i = 42; int j = (i *= 23);",
2016 binaryOperator(hasOperatorName("*="))));
2017 EXPECT_TRUE(
2018 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
2019 EXPECT_TRUE(
2020 matches("int i = 42; int j = (i /= 23);",
2021 binaryOperator(hasOperatorName("/="))));
2022 EXPECT_TRUE(
2023 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
2024 EXPECT_TRUE(
2025 matches("int i = 42; int j = (i += 23);",
2026 binaryOperator(hasOperatorName("+="))));
2027 EXPECT_TRUE(
2028 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
2029 EXPECT_TRUE(
2030 matches("int i = 42; int j = (i -= 23);",
2031 binaryOperator(hasOperatorName("-="))));
2032 EXPECT_TRUE(
2033 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
2034 binaryOperator(hasOperatorName("->*"))));
2035 EXPECT_TRUE(
2036 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
2037 binaryOperator(hasOperatorName(".*"))));
2038
2039 // Member expressions as operators are not supported in matches.
2040 EXPECT_TRUE(
2041 notMatches("struct A { void x(A *a) { a->x(this); } };",
2042 binaryOperator(hasOperatorName("->"))));
2043
2044 // Initializer assignments are not represented as operator equals.
2045 EXPECT_TRUE(
2046 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
2047
2048 // Array indexing is not represented as operator.
2049 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
2050
2051 // Overloaded operators do not match at all.
2052 EXPECT_TRUE(notMatches(
2053 "struct A { bool operator&&(const A &a) const { return false; } };"
2054 "void x() { A a, b; a && b; }",
2055 binaryOperator()));
2056}
2057
2058TEST(MatchUnaryOperator, HasOperatorName) {
2059 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
2060
2061 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
2062 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
2063}
2064
2065TEST(MatchUnaryOperator, HasUnaryOperand) {
2066 StatementMatcher OperatorOnFalse =
2067 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
2068
2069 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
2070 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
2071}
2072
2073TEST(Matcher, UnaryOperatorTypes) {
2074 // Integration test that verifies the AST provides all unary operators in
2075 // a way we expect.
2076 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
2077 EXPECT_TRUE(
2078 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
2079 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
2080 EXPECT_TRUE(
2081 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
2082 EXPECT_TRUE(
2083 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
2084 EXPECT_TRUE(
2085 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
2086 EXPECT_TRUE(
2087 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
2088 EXPECT_TRUE(
2089 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
2090 EXPECT_TRUE(
2091 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
2092 EXPECT_TRUE(
2093 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
2094
2095 // We don't match conversion operators.
2096 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
2097
2098 // Function calls are not represented as operator.
2099 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
2100
2101 // Overloaded operators do not match at all.
2102 // FIXME: We probably want to add that.
2103 EXPECT_TRUE(notMatches(
2104 "struct A { bool operator!() const { return false; } };"
2105 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
2106}
2107
2108TEST(Matcher, ConditionalOperator) {
2109 StatementMatcher Conditional = conditionalOperator(
2110 hasCondition(boolLiteral(equals(true))),
2111 hasTrueExpression(boolLiteral(equals(false))));
2112
2113 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
2114 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
2115 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
2116
2117 StatementMatcher ConditionalFalse = conditionalOperator(
2118 hasFalseExpression(boolLiteral(equals(false))));
2119
2120 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
2121 EXPECT_TRUE(
2122 notMatches("void x() { true ? false : true; }", ConditionalFalse));
2123}
2124
Daniel Jasper1dad1832012-07-10 20:20:19 +00002125TEST(ArraySubscriptMatchers, ArraySubscripts) {
2126 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2127 arraySubscriptExpr()));
2128 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2129 arraySubscriptExpr()));
2130}
2131
2132TEST(ArraySubscriptMatchers, ArrayIndex) {
2133 EXPECT_TRUE(matches(
2134 "int i[2]; void f() { i[1] = 1; }",
2135 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2136 EXPECT_TRUE(matches(
2137 "int i[2]; void f() { 1[i] = 1; }",
2138 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2139 EXPECT_TRUE(notMatches(
2140 "int i[2]; void f() { i[1] = 1; }",
2141 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2142}
2143
2144TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2145 EXPECT_TRUE(matches(
2146 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002147 arraySubscriptExpr(hasBase(implicitCastExpr(
2148 hasSourceExpression(declRefExpr()))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002149}
2150
Manuel Klimek04616e42012-07-06 05:48:52 +00002151TEST(Matcher, HasNameSupportsNamespaces) {
2152 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002153 recordDecl(hasName("a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002154 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002155 recordDecl(hasName("::a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002156 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002157 recordDecl(hasName("b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002158 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002159 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002160 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002161 recordDecl(hasName("c::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002162 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002163 recordDecl(hasName("a::c::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002164 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002165 recordDecl(hasName("a::b::A"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002166 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002167 recordDecl(hasName("::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002168 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002169 recordDecl(hasName("::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002170 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002171 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002172 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002173 recordDecl(hasName("a+b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002174 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002175 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002176}
2177
2178TEST(Matcher, HasNameSupportsOuterClasses) {
2179 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002180 matches("class A { class B { class C; }; };",
2181 recordDecl(hasName("A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002182 EXPECT_TRUE(
2183 matches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002184 recordDecl(hasName("::A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002185 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002186 matches("class A { class B { class C; }; };",
2187 recordDecl(hasName("B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002188 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002189 matches("class A { class B { class C; }; };",
2190 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002191 EXPECT_TRUE(
2192 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002193 recordDecl(hasName("c::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002194 EXPECT_TRUE(
2195 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002196 recordDecl(hasName("A::c::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002197 EXPECT_TRUE(
2198 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002199 recordDecl(hasName("A::B::A"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002200 EXPECT_TRUE(
2201 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002202 recordDecl(hasName("::C"))));
2203 EXPECT_TRUE(
2204 notMatches("class A { class B { class C; }; };",
2205 recordDecl(hasName("::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002206 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002207 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002208 EXPECT_TRUE(
2209 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002210 recordDecl(hasName("A+B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002211}
2212
2213TEST(Matcher, IsDefinition) {
2214 DeclarationMatcher DefinitionOfClassA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002215 recordDecl(hasName("A"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002216 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2217 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2218
2219 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002220 varDecl(hasName("a"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002221 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2222 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2223
2224 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002225 methodDecl(hasName("a"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002226 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2227 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2228}
2229
2230TEST(Matcher, OfClass) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002231 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +00002232 ofClass(hasName("X")))));
2233
2234 EXPECT_TRUE(
2235 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2236 EXPECT_TRUE(
2237 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2238 Constructor));
2239 EXPECT_TRUE(
2240 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2241 Constructor));
2242}
2243
2244TEST(Matcher, VisitsTemplateInstantiations) {
2245 EXPECT_TRUE(matches(
2246 "class A { public: void x(); };"
2247 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002248 "void f() { B<A> b; b.y(); }",
2249 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002250
2251 EXPECT_TRUE(matches(
2252 "class A { public: void x(); };"
2253 "class C {"
2254 " public:"
2255 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2256 "};"
2257 "void f() {"
2258 " C::B<A> b; b.y();"
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002259 "}",
2260 recordDecl(hasName("C"),
2261 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002262}
2263
Daniel Jasper1dad1832012-07-10 20:20:19 +00002264TEST(Matcher, HandlesNullQualTypes) {
2265 // FIXME: Add a Type matcher so we can replace uses of this
2266 // variable with Type(True())
2267 const TypeMatcher AnyType = anything();
2268
2269 // We don't really care whether this matcher succeeds; we're testing that
2270 // it completes without crashing.
2271 EXPECT_TRUE(matches(
2272 "struct A { };"
2273 "template <typename T>"
2274 "void f(T t) {"
2275 " T local_t(t /* this becomes a null QualType in the AST */);"
2276 "}"
2277 "void g() {"
2278 " f(0);"
2279 "}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002280 expr(hasType(TypeMatcher(
Daniel Jasper1dad1832012-07-10 20:20:19 +00002281 anyOf(
2282 TypeMatcher(hasDeclaration(anything())),
2283 pointsTo(AnyType),
2284 references(AnyType)
2285 // Other QualType matchers should go here.
2286 ))))));
2287}
2288
Manuel Klimek04616e42012-07-06 05:48:52 +00002289// For testing AST_MATCHER_P().
Daniel Jasper1dad1832012-07-10 20:20:19 +00002290AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002291 // Make sure all special variables are used: node, match_finder,
2292 // bound_nodes_builder, and the parameter named 'AMatcher'.
2293 return AMatcher.matches(Node, Finder, Builder);
2294}
2295
2296TEST(AstMatcherPMacro, Works) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002297 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002298
2299 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002300 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002301
2302 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002303 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002304
2305 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002306 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002307}
2308
2309AST_POLYMORPHIC_MATCHER_P(
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +00002310 polymorphicHas,
2311 AST_POLYMORPHIC_SUPPORTED_TYPES_2(Decl, Stmt),
2312 internal::Matcher<Decl>, AMatcher) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002313 return Finder->matchesChildOf(
Manuel Klimekeb958de2012-09-05 12:12:07 +00002314 Node, AMatcher, Builder,
Manuel Klimek04616e42012-07-06 05:48:52 +00002315 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2316 ASTMatchFinder::BK_First);
2317}
2318
2319TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002320 DeclarationMatcher HasClassB =
2321 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek04616e42012-07-06 05:48:52 +00002322
2323 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002324 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002325
2326 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002327 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002328
2329 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002330 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002331
2332 StatementMatcher StatementHasClassB =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002333 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002334
2335 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2336}
2337
2338TEST(For, FindsForLoops) {
2339 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2340 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper6f595392012-10-01 15:05:34 +00002341 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2342 "void f() { for (auto &a : as); }",
Daniel Jasper5901e472012-10-01 13:40:41 +00002343 forStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002344}
2345
Daniel Jasper4e566c42012-07-12 08:50:38 +00002346TEST(For, ForLoopInternals) {
2347 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2348 forStmt(hasCondition(anything()))));
2349 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2350 forStmt(hasLoopInit(anything()))));
2351}
2352
Alexander Kornienko9b539e12014-02-05 16:35:08 +00002353TEST(For, ForRangeLoopInternals) {
2354 EXPECT_TRUE(matches("void f(){ int a[] {1, 2}; for (int i : a); }",
2355 forRangeStmt(hasLoopVariable(anything()))));
2356}
2357
Daniel Jasper4e566c42012-07-12 08:50:38 +00002358TEST(For, NegativeForLoopInternals) {
2359 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002360 forStmt(hasCondition(expr()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002361 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2362 forStmt(hasLoopInit(anything()))));
2363}
2364
Manuel Klimek04616e42012-07-06 05:48:52 +00002365TEST(For, ReportsNoFalsePositives) {
2366 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2367 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2368}
2369
2370TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002371 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2372 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2373 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002374}
2375
2376TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2377 // It's not a compound statement just because there's "{}" in the source
2378 // text. This is an AST search, not grep.
2379 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002380 compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002381 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002382 compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002383}
2384
Daniel Jasper4e566c42012-07-12 08:50:38 +00002385TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002386 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002387 forStmt(hasBody(compoundStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002388 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002389 forStmt(hasBody(compoundStmt()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002390 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002391 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002392 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002393 doStmt(hasBody(compoundStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002394}
2395
2396TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2397 // The simplest case: every compound statement is in a function
2398 // definition, and the function body itself must be a compound
2399 // statement.
2400 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002401 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002402}
2403
2404TEST(HasAnySubstatement, IsNotRecursive) {
2405 // It's really "has any immediate substatement".
2406 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002407 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002408}
2409
2410TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2411 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002412 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002413}
2414
2415TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2416 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002417 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002418}
2419
2420TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2421 EXPECT_TRUE(matches("void f() { }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002422 compoundStmt(statementCountIs(0))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002423 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002424 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002425}
2426
2427TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2428 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002429 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002430 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002431 compoundStmt(statementCountIs(0))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002432 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002433 compoundStmt(statementCountIs(2))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002434}
2435
2436TEST(StatementCountIs, WorksWithMultipleStatements) {
2437 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002438 compoundStmt(statementCountIs(3))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002439}
2440
2441TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2442 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002443 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002444 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002445 compoundStmt(statementCountIs(2))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002446 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002447 compoundStmt(statementCountIs(3))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002448 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002449 compoundStmt(statementCountIs(4))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002450}
2451
2452TEST(Member, WorksInSimplestCase) {
2453 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002454 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002455}
2456
2457TEST(Member, DoesNotMatchTheBaseExpression) {
2458 // Don't pick out the wrong part of the member expression, this should
2459 // be checking the member (name) only.
2460 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002461 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002462}
2463
2464TEST(Member, MatchesInMemberFunctionCall) {
2465 EXPECT_TRUE(matches("void f() {"
2466 " struct { void first() {}; } s;"
2467 " s.first();"
2468 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002469 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002470}
2471
Daniel Jasperb0c7b612012-10-23 15:46:39 +00002472TEST(Member, MatchesMember) {
2473 EXPECT_TRUE(matches(
2474 "struct A { int i; }; void f() { A a; a.i = 2; }",
2475 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2476 EXPECT_TRUE(notMatches(
2477 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2478 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2479}
2480
Daniel Jasper639522c2013-02-25 12:02:08 +00002481TEST(Member, UnderstandsAccess) {
2482 EXPECT_TRUE(matches(
2483 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2484 EXPECT_TRUE(notMatches(
2485 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2486 EXPECT_TRUE(notMatches(
2487 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2488
2489 EXPECT_TRUE(notMatches(
2490 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2491 EXPECT_TRUE(notMatches(
2492 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2493 EXPECT_TRUE(matches(
2494 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2495
2496 EXPECT_TRUE(notMatches(
2497 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2498 EXPECT_TRUE(matches("class A { protected: int i; };",
2499 fieldDecl(isProtected(), hasName("i"))));
2500 EXPECT_TRUE(notMatches(
2501 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2502
2503 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2504 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2505 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2506 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2507}
2508
Dmitri Gribenko06963042012-08-18 00:29:27 +00002509TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper5901e472012-10-01 13:40:41 +00002510 // Fails in C++11 mode
2511 EXPECT_TRUE(matchesConditionally(
2512 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2513 "class X { void *operator new(std::size_t); };",
2514 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002515
2516 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002517 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002518
Daniel Jasper5901e472012-10-01 13:40:41 +00002519 // Fails in C++11 mode
2520 EXPECT_TRUE(matchesConditionally(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002521 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2522 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper5901e472012-10-01 13:40:41 +00002523 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002524}
2525
Manuel Klimek04616e42012-07-06 05:48:52 +00002526TEST(HasObjectExpression, DoesNotMatchMember) {
2527 EXPECT_TRUE(notMatches(
2528 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002529 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002530}
2531
2532TEST(HasObjectExpression, MatchesBaseOfVariable) {
2533 EXPECT_TRUE(matches(
2534 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002535 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002536 EXPECT_TRUE(matches(
2537 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002538 memberExpr(hasObjectExpression(
2539 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002540}
2541
2542TEST(HasObjectExpression,
2543 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2544 EXPECT_TRUE(matches(
2545 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002546 memberExpr(hasObjectExpression(
2547 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002548 EXPECT_TRUE(matches(
2549 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002550 memberExpr(hasObjectExpression(
2551 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002552}
2553
2554TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002555 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2556 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2557 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2558 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002559}
2560
2561TEST(Field, MatchesField) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002562 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002563}
2564
2565TEST(IsConstQualified, MatchesConstInt) {
2566 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002567 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002568}
2569
2570TEST(IsConstQualified, MatchesConstPointer) {
2571 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002572 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002573}
2574
2575TEST(IsConstQualified, MatchesThroughTypedef) {
2576 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002577 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002578 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002579 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002580}
2581
2582TEST(IsConstQualified, DoesNotMatchInappropriately) {
2583 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002584 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002585 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002586 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002587}
2588
Sam Panzer80c13772012-08-16 16:58:10 +00002589TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002590 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2591 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2592 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2593 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002594}
2595TEST(CastExpression, MatchesImplicitCasts) {
2596 // This test creates an implicit cast from int to char.
Daniel Jasper848cbe12012-09-18 13:09:13 +00002597 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002598 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper848cbe12012-09-18 13:09:13 +00002599 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002600}
2601
2602TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002603 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2604 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2605 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2606 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002607}
2608
Manuel Klimek04616e42012-07-06 05:48:52 +00002609TEST(ReinterpretCast, MatchesSimpleCase) {
2610 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002611 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002612}
2613
2614TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002615 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002616 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002617 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002618 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002619 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002620 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2621 "B b;"
2622 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002623 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002624}
2625
2626TEST(FunctionalCast, MatchesSimpleCase) {
Ismail Pazarbasi1121de32014-01-17 21:08:52 +00002627 std::string foo_class = "class Foo { public: Foo(const char*); };";
Manuel Klimek04616e42012-07-06 05:48:52 +00002628 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002629 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002630}
2631
2632TEST(FunctionalCast, DoesNotMatchOtherCasts) {
Ismail Pazarbasi1121de32014-01-17 21:08:52 +00002633 std::string FooClass = "class Foo { public: Foo(const char*); };";
Manuel Klimek04616e42012-07-06 05:48:52 +00002634 EXPECT_TRUE(
2635 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002636 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002637 EXPECT_TRUE(
2638 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002639 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002640}
2641
2642TEST(DynamicCast, MatchesSimpleCase) {
2643 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2644 "B b;"
2645 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002646 dynamicCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002647}
2648
2649TEST(StaticCast, MatchesSimpleCase) {
2650 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002651 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002652}
2653
2654TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002655 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002656 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002657 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002658 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002659 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002660 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2661 "B b;"
2662 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002663 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002664}
2665
Daniel Jasper417f7762012-09-18 13:36:17 +00002666TEST(CStyleCast, MatchesSimpleCase) {
2667 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2668}
2669
2670TEST(CStyleCast, DoesNotMatchOtherCasts) {
2671 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2672 "char q, *r = const_cast<char*>(&q);"
2673 "void* s = reinterpret_cast<char*>(&s);"
2674 "struct B { virtual ~B() {} }; struct D : B {};"
2675 "B b;"
2676 "D* t = dynamic_cast<D*>(&b);",
2677 cStyleCastExpr()));
2678}
2679
Manuel Klimek04616e42012-07-06 05:48:52 +00002680TEST(HasDestinationType, MatchesSimpleCase) {
2681 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002682 staticCastExpr(hasDestinationType(
2683 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002684}
2685
Sam Panzer80c13772012-08-16 16:58:10 +00002686TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2687 // This test creates an implicit const cast.
2688 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002689 implicitCastExpr(
2690 hasImplicitDestinationType(isInteger()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002691 // This test creates an implicit array-to-pointer cast.
2692 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002693 implicitCastExpr(hasImplicitDestinationType(
2694 pointsTo(TypeMatcher(anything()))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002695}
2696
2697TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2698 // This test creates an implicit cast from int to char.
2699 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002700 implicitCastExpr(hasImplicitDestinationType(
2701 unless(anything())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002702 // This test creates an implicit array-to-pointer cast.
2703 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002704 implicitCastExpr(hasImplicitDestinationType(
2705 unless(anything())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002706}
2707
2708TEST(ImplicitCast, MatchesSimpleCase) {
2709 // This test creates an implicit const cast.
2710 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002711 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002712 // This test creates an implicit cast from int to char.
2713 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002714 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002715 // This test creates an implicit array-to-pointer cast.
2716 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002717 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002718}
2719
2720TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002721 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer80c13772012-08-16 16:58:10 +00002722 // are present, and that it ignores explicit and paren casts.
2723
2724 // These two test cases have no casts.
2725 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002726 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002727 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002728 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002729
2730 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002731 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002732 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002733 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002734
2735 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002736 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002737}
2738
2739TEST(IgnoringImpCasts, MatchesImpCasts) {
2740 // This test checks that ignoringImpCasts matches when implicit casts are
2741 // present and its inner matcher alone does not match.
2742 // Note that this test creates an implicit const cast.
2743 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002744 varDecl(hasInitializer(ignoringImpCasts(
2745 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002746 // This test creates an implict cast from int to char.
2747 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002748 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002749 integerLiteral(equals(0)))))));
2750}
2751
2752TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2753 // These tests verify that ignoringImpCasts does not match if the inner
2754 // matcher does not match.
2755 // Note that the first test creates an implicit const cast.
2756 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002757 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002758 unless(anything()))))));
2759 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002760 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002761 unless(anything()))))));
2762
2763 // These tests verify that ignoringImplictCasts does not look through explicit
2764 // casts or parentheses.
2765 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002766 varDecl(hasInitializer(ignoringImpCasts(
2767 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002768 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002769 varDecl(hasInitializer(ignoringImpCasts(
2770 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002771 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002772 varDecl(hasInitializer(ignoringImpCasts(
2773 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002774 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002775 varDecl(hasInitializer(ignoringImpCasts(
2776 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002777}
2778
2779TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2780 // This test verifies that expressions that do not have implicit casts
2781 // still match the inner matcher.
2782 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002783 varDecl(hasInitializer(ignoringImpCasts(
2784 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002785}
2786
2787TEST(IgnoringParenCasts, MatchesParenCasts) {
2788 // This test checks that ignoringParenCasts matches when parentheses and/or
2789 // casts are present and its inner matcher alone does not match.
2790 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002791 varDecl(hasInitializer(ignoringParenCasts(
2792 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002793 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002794 varDecl(hasInitializer(ignoringParenCasts(
2795 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002796
2797 // This test creates an implict cast from int to char in addition to the
2798 // parentheses.
2799 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002800 varDecl(hasInitializer(ignoringParenCasts(
2801 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002802
2803 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002804 varDecl(hasInitializer(ignoringParenCasts(
2805 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002806 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002807 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002808 integerLiteral(equals(0)))))));
2809}
2810
2811TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2812 // This test verifies that expressions that do not have any casts still match.
2813 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002814 varDecl(hasInitializer(ignoringParenCasts(
2815 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002816}
2817
2818TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2819 // These tests verify that ignoringImpCasts does not match if the inner
2820 // matcher does not match.
2821 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002822 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002823 unless(anything()))))));
2824
2825 // This test creates an implicit cast from int to char in addition to the
2826 // parentheses.
2827 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002828 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002829 unless(anything()))))));
2830
2831 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002832 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002833 unless(anything()))))));
2834}
2835
2836TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2837 // This test checks that ignoringParenAndImpCasts matches when
2838 // parentheses and/or implicit casts are present and its inner matcher alone
2839 // does not match.
2840 // Note that this test creates an implicit const cast.
2841 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002842 varDecl(hasInitializer(ignoringParenImpCasts(
2843 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002844 // This test creates an implicit cast from int to char.
2845 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002846 varDecl(hasInitializer(ignoringParenImpCasts(
2847 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002848}
2849
2850TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2851 // This test verifies that expressions that do not have parentheses or
2852 // implicit casts still match.
2853 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002854 varDecl(hasInitializer(ignoringParenImpCasts(
2855 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002856 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002857 varDecl(hasInitializer(ignoringParenImpCasts(
2858 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002859}
2860
2861TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2862 // These tests verify that ignoringParenImpCasts does not match if
2863 // the inner matcher does not match.
2864 // This test creates an implicit cast.
2865 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002866 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002867 unless(anything()))))));
2868 // These tests verify that ignoringParenAndImplictCasts does not look
2869 // through explicit casts.
2870 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002871 varDecl(hasInitializer(ignoringParenImpCasts(
2872 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002873 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002874 varDecl(hasInitializer(ignoringParenImpCasts(
2875 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002876 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002877 varDecl(hasInitializer(ignoringParenImpCasts(
2878 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002879}
2880
Manuel Klimeke9235692012-07-25 10:02:02 +00002881TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002882 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2883 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002884 implicitCastExpr(
2885 hasSourceExpression(constructExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002886}
2887
Manuel Klimeke9235692012-07-25 10:02:02 +00002888TEST(HasSourceExpression, MatchesExplicitCasts) {
2889 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002890 explicitCastExpr(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002891 hasSourceExpression(hasDescendant(
Daniel Jasper848cbe12012-09-18 13:09:13 +00002892 expr(integerLiteral()))))));
Manuel Klimeke9235692012-07-25 10:02:02 +00002893}
2894
Manuel Klimek04616e42012-07-06 05:48:52 +00002895TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002896 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002897}
2898
2899TEST(Statement, MatchesCompoundStatments) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002900 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002901}
2902
2903TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002904 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002905}
2906
2907TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002908 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002909}
2910
Daniel Jasper1dad1832012-07-10 20:20:19 +00002911TEST(InitListExpression, MatchesInitListExpression) {
2912 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2913 initListExpr(hasType(asString("int [2]")))));
2914 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002915 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002916}
2917
2918TEST(UsingDeclaration, MatchesUsingDeclarations) {
2919 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2920 usingDecl()));
2921}
2922
2923TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2924 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2925 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2926}
2927
2928TEST(UsingDeclaration, MatchesSpecificTarget) {
2929 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2930 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002931 hasTargetDecl(functionDecl())))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002932 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2933 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002934 hasTargetDecl(functionDecl())))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002935}
2936
2937TEST(UsingDeclaration, ThroughUsingDeclaration) {
2938 EXPECT_TRUE(matches(
2939 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002940 declRefExpr(throughUsingDecl(anything()))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002941 EXPECT_TRUE(notMatches(
2942 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002943 declRefExpr(throughUsingDecl(anything()))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002944}
2945
Sam Panzerd624bfb2012-08-16 17:20:59 +00002946TEST(SingleDecl, IsSingleDecl) {
2947 StatementMatcher SingleDeclStmt =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002948 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002949 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2950 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2951 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2952 SingleDeclStmt));
2953}
2954
2955TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002956 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002957
2958 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002959 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002960 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002961 declStmt(containsDeclaration(0, MatchesInit),
2962 containsDeclaration(1, MatchesInit))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002963 unsigned WrongIndex = 42;
2964 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002965 declStmt(containsDeclaration(WrongIndex,
Sam Panzerd624bfb2012-08-16 17:20:59 +00002966 MatchesInit))));
2967}
2968
2969TEST(DeclCount, DeclCountIsCorrect) {
2970 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002971 declStmt(declCountIs(2))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002972 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002973 declStmt(declCountIs(3))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002974 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002975 declStmt(declCountIs(3))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002976}
2977
Manuel Klimek04616e42012-07-06 05:48:52 +00002978TEST(While, MatchesWhileLoops) {
2979 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2980 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2981 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2982}
2983
2984TEST(Do, MatchesDoLoops) {
2985 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2986 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2987}
2988
2989TEST(Do, DoesNotMatchWhileLoops) {
2990 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2991}
2992
2993TEST(SwitchCase, MatchesCase) {
2994 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2995 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2996 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2997 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2998}
2999
Daniel Jasper87c3d362012-09-20 14:12:57 +00003000TEST(SwitchCase, MatchesSwitch) {
3001 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
3002 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
3003 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
3004 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
3005}
3006
Peter Collingbourne3154a102013-05-10 11:52:02 +00003007TEST(SwitchCase, MatchesEachCase) {
3008 EXPECT_TRUE(notMatches("void x() { switch(42); }",
3009 switchStmt(forEachSwitchCase(caseStmt()))));
3010 EXPECT_TRUE(matches("void x() { switch(42) case 42:; }",
3011 switchStmt(forEachSwitchCase(caseStmt()))));
3012 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }",
3013 switchStmt(forEachSwitchCase(caseStmt()))));
3014 EXPECT_TRUE(notMatches(
3015 "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }",
3016 ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt()))))));
3017 EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }",
3018 switchStmt(forEachSwitchCase(
3019 caseStmt(hasCaseConstant(integerLiteral()))))));
3020 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }",
3021 switchStmt(forEachSwitchCase(
3022 caseStmt(hasCaseConstant(integerLiteral()))))));
3023 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }",
3024 switchStmt(forEachSwitchCase(
3025 caseStmt(hasCaseConstant(integerLiteral()))))));
3026 EXPECT_TRUE(matchAndVerifyResultTrue(
3027 "void x() { switch (42) { case 1: case 2: case 3: default:; } }",
3028 switchStmt(forEachSwitchCase(caseStmt().bind("x"))),
3029 new VerifyIdIsBoundTo<CaseStmt>("x", 3)));
3030}
3031
Manuel Klimekba46fc02013-07-19 11:50:54 +00003032TEST(ForEachConstructorInitializer, MatchesInitializers) {
3033 EXPECT_TRUE(matches(
3034 "struct X { X() : i(42), j(42) {} int i, j; };",
3035 constructorDecl(forEachConstructorInitializer(ctorInitializer()))));
3036}
3037
Daniel Jasper87c3d362012-09-20 14:12:57 +00003038TEST(ExceptionHandling, SimpleCases) {
3039 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
3040 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
3041 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
3042 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
3043 throwExpr()));
3044 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
3045 throwExpr()));
3046}
3047
Manuel Klimek04616e42012-07-06 05:48:52 +00003048TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
3049 EXPECT_TRUE(notMatches(
3050 "void x() { if(true) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003051 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003052 EXPECT_TRUE(notMatches(
3053 "void x() { int x; if((x = 42)) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003054 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003055}
3056
3057TEST(HasConditionVariableStatement, MatchesConditionVariables) {
3058 EXPECT_TRUE(matches(
3059 "void x() { if(int* a = 0) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003060 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003061}
3062
3063TEST(ForEach, BindsOneNode) {
3064 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003065 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003066 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003067}
3068
3069TEST(ForEach, BindsMultipleNodes) {
3070 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003071 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003072 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003073}
3074
3075TEST(ForEach, BindsRecursiveCombinations) {
3076 EXPECT_TRUE(matchAndVerifyResultTrue(
3077 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003078 recordDecl(hasName("C"),
3079 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003080 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003081}
3082
3083TEST(ForEachDescendant, BindsOneNode) {
3084 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003085 recordDecl(hasName("C"),
3086 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003087 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003088}
3089
Daniel Jasper94a56852012-11-16 18:39:22 +00003090TEST(ForEachDescendant, NestedForEachDescendant) {
3091 DeclarationMatcher m = recordDecl(
3092 isDefinition(), decl().bind("x"), hasName("C"));
3093 EXPECT_TRUE(matchAndVerifyResultTrue(
3094 "class A { class B { class C {}; }; };",
3095 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
3096 new VerifyIdIsBoundTo<Decl>("x", "C")));
3097
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003098 // Check that a partial match of 'm' that binds 'x' in the
3099 // first part of anyOf(m, anything()) will not overwrite the
3100 // binding created by the earlier binding in the hasDescendant.
3101 EXPECT_TRUE(matchAndVerifyResultTrue(
3102 "class A { class B { class C {}; }; };",
3103 recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
3104 new VerifyIdIsBoundTo<Decl>("x", "C")));
Daniel Jasper94a56852012-11-16 18:39:22 +00003105}
3106
Manuel Klimek04616e42012-07-06 05:48:52 +00003107TEST(ForEachDescendant, BindsMultipleNodes) {
3108 EXPECT_TRUE(matchAndVerifyResultTrue(
3109 "class C { class D { int x; int y; }; "
3110 " class E { class F { int y; int z; }; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003111 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003112 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003113}
3114
3115TEST(ForEachDescendant, BindsRecursiveCombinations) {
3116 EXPECT_TRUE(matchAndVerifyResultTrue(
3117 "class C { class D { "
3118 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003119 recordDecl(hasName("C"), forEachDescendant(recordDecl(
3120 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003121 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003122}
3123
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003124TEST(ForEachDescendant, BindsCombinations) {
3125 EXPECT_TRUE(matchAndVerifyResultTrue(
3126 "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while "
3127 "(true) {} }",
3128 compoundStmt(forEachDescendant(ifStmt().bind("if")),
3129 forEachDescendant(whileStmt().bind("while"))),
3130 new VerifyIdIsBoundTo<IfStmt>("if", 6)));
3131}
3132
3133TEST(Has, DoesNotDeleteBindings) {
3134 EXPECT_TRUE(matchAndVerifyResultTrue(
3135 "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())),
3136 new VerifyIdIsBoundTo<Decl>("x", 1)));
3137}
3138
3139TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) {
3140 // Those matchers cover all the cases where an inner matcher is called
3141 // and there is not a 1:1 relationship between the match of the outer
3142 // matcher and the match of the inner matcher.
3143 // The pattern to look for is:
3144 // ... return InnerMatcher.matches(...); ...
3145 // In which case no special handling is needed.
3146 //
3147 // On the other hand, if there are multiple alternative matches
3148 // (for example forEach*) or matches might be discarded (for example has*)
3149 // the implementation must make sure that the discarded matches do not
3150 // affect the bindings.
3151 // When new such matchers are added, add a test here that:
3152 // - matches a simple node, and binds it as the first thing in the matcher:
3153 // recordDecl(decl().bind("x"), hasName("X")))
3154 // - uses the matcher under test afterwards in a way that not the first
3155 // alternative is matched; for anyOf, that means the first branch
3156 // would need to return false; for hasAncestor, it means that not
3157 // the direct parent matches the inner matcher.
3158
3159 EXPECT_TRUE(matchAndVerifyResultTrue(
3160 "class X { int y; };",
3161 recordDecl(
3162 recordDecl().bind("x"), hasName("::X"),
3163 anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())),
3164 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3165 EXPECT_TRUE(matchAndVerifyResultTrue(
3166 "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"),
3167 anyOf(unless(anything()), anything())),
3168 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3169 EXPECT_TRUE(matchAndVerifyResultTrue(
3170 "template<typename T1, typename T2> class X {}; X<float, int> x;",
3171 classTemplateSpecializationDecl(
3172 decl().bind("x"),
3173 hasAnyTemplateArgument(refersToType(asString("int")))),
3174 new VerifyIdIsBoundTo<Decl>("x", 1)));
3175 EXPECT_TRUE(matchAndVerifyResultTrue(
3176 "class X { void f(); void g(); };",
3177 recordDecl(decl().bind("x"), hasMethod(hasName("g"))),
3178 new VerifyIdIsBoundTo<Decl>("x", 1)));
3179 EXPECT_TRUE(matchAndVerifyResultTrue(
3180 "class X { X() : a(1), b(2) {} double a; int b; };",
3181 recordDecl(decl().bind("x"),
3182 has(constructorDecl(
3183 hasAnyConstructorInitializer(forField(hasName("b")))))),
3184 new VerifyIdIsBoundTo<Decl>("x", 1)));
3185 EXPECT_TRUE(matchAndVerifyResultTrue(
3186 "void x(int, int) { x(0, 42); }",
3187 callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))),
3188 new VerifyIdIsBoundTo<Expr>("x", 1)));
3189 EXPECT_TRUE(matchAndVerifyResultTrue(
3190 "void x(int, int y) {}",
3191 functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))),
3192 new VerifyIdIsBoundTo<Decl>("x", 1)));
3193 EXPECT_TRUE(matchAndVerifyResultTrue(
3194 "void x() { return; if (true) {} }",
3195 functionDecl(decl().bind("x"),
3196 has(compoundStmt(hasAnySubstatement(ifStmt())))),
3197 new VerifyIdIsBoundTo<Decl>("x", 1)));
3198 EXPECT_TRUE(matchAndVerifyResultTrue(
3199 "namespace X { void b(int); void b(); }"
3200 "using X::b;",
3201 usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl(
3202 functionDecl(parameterCountIs(1))))),
3203 new VerifyIdIsBoundTo<Decl>("x", 1)));
3204 EXPECT_TRUE(matchAndVerifyResultTrue(
3205 "class A{}; class B{}; class C : B, A {};",
3206 recordDecl(decl().bind("x"), isDerivedFrom("::A")),
3207 new VerifyIdIsBoundTo<Decl>("x", 1)));
3208 EXPECT_TRUE(matchAndVerifyResultTrue(
3209 "class A{}; typedef A B; typedef A C; typedef A D;"
3210 "class E : A {};",
3211 recordDecl(decl().bind("x"), isDerivedFrom("C")),
3212 new VerifyIdIsBoundTo<Decl>("x", 1)));
3213 EXPECT_TRUE(matchAndVerifyResultTrue(
3214 "class A { class B { void f() {} }; };",
3215 functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3216 new VerifyIdIsBoundTo<Decl>("x", 1)));
3217 EXPECT_TRUE(matchAndVerifyResultTrue(
3218 "template <typename T> struct A { struct B {"
3219 " void f() { if(true) {} }"
3220 "}; };"
3221 "void t() { A<int>::B b; b.f(); }",
3222 ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3223 new VerifyIdIsBoundTo<Stmt>("x", 2)));
3224 EXPECT_TRUE(matchAndVerifyResultTrue(
3225 "class A {};",
3226 recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))),
3227 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimekba46fc02013-07-19 11:50:54 +00003228 EXPECT_TRUE(matchAndVerifyResultTrue(
3229 "class A { A() : s(), i(42) {} const char *s; int i; };",
3230 constructorDecl(hasName("::A::A"), decl().bind("x"),
3231 forEachConstructorInitializer(forField(hasName("i")))),
3232 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003233}
3234
Daniel Jasper33806cd2012-11-11 22:14:55 +00003235TEST(ForEachDescendant, BindsCorrectNodes) {
3236 EXPECT_TRUE(matchAndVerifyResultTrue(
3237 "class C { void f(); int i; };",
3238 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3239 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
3240 EXPECT_TRUE(matchAndVerifyResultTrue(
3241 "class C { void f() {} int i; };",
3242 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3243 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
3244}
3245
Manuel Klimekabf43712013-02-04 10:59:20 +00003246TEST(FindAll, BindsNodeOnMatch) {
3247 EXPECT_TRUE(matchAndVerifyResultTrue(
3248 "class A {};",
3249 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
3250 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
3251}
3252
3253TEST(FindAll, BindsDescendantNodeOnMatch) {
3254 EXPECT_TRUE(matchAndVerifyResultTrue(
3255 "class A { int a; int b; };",
3256 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
3257 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3258}
3259
3260TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
3261 EXPECT_TRUE(matchAndVerifyResultTrue(
3262 "class A { int a; int b; };",
3263 recordDecl(hasName("::A"),
3264 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
3265 fieldDecl().bind("v"))))),
3266 new VerifyIdIsBoundTo<Decl>("v", 3)));
3267
3268 EXPECT_TRUE(matchAndVerifyResultTrue(
3269 "class A { class B {}; class C {}; };",
3270 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
3271 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3272}
3273
Manuel Klimek88b95872013-02-04 09:42:38 +00003274TEST(EachOf, TriggersForEachMatch) {
3275 EXPECT_TRUE(matchAndVerifyResultTrue(
3276 "class A { int a; int b; };",
3277 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3278 has(fieldDecl(hasName("b")).bind("v")))),
3279 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3280}
3281
3282TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3283 EXPECT_TRUE(matchAndVerifyResultTrue(
3284 "class A { int a; int c; };",
3285 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3286 has(fieldDecl(hasName("b")).bind("v")))),
3287 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3288 EXPECT_TRUE(matchAndVerifyResultTrue(
3289 "class A { int c; int b; };",
3290 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3291 has(fieldDecl(hasName("b")).bind("v")))),
3292 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3293 EXPECT_TRUE(notMatches(
3294 "class A { int c; int d; };",
3295 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3296 has(fieldDecl(hasName("b")).bind("v"))))));
3297}
Manuel Klimek04616e42012-07-06 05:48:52 +00003298
3299TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3300 // Make sure that we can both match the class by name (::X) and by the type
3301 // the template was instantiated with (via a field).
3302
3303 EXPECT_TRUE(matches(
3304 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003305 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003306
3307 EXPECT_TRUE(matches(
3308 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003309 recordDecl(isTemplateInstantiation(), hasDescendant(
3310 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003311}
3312
3313TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3314 EXPECT_TRUE(matches(
3315 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003316 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek04616e42012-07-06 05:48:52 +00003317 isTemplateInstantiation())));
3318}
3319
3320TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3321 EXPECT_TRUE(matches(
3322 "template <typename T> class X { T t; }; class A {};"
3323 "template class X<A>;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003324 recordDecl(isTemplateInstantiation(), hasDescendant(
3325 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003326}
3327
3328TEST(IsTemplateInstantiation,
3329 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3330 EXPECT_TRUE(matches(
3331 "template <typename T> class X {};"
3332 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003333 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003334}
3335
3336TEST(IsTemplateInstantiation,
3337 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3338 EXPECT_TRUE(matches(
3339 "class A {};"
3340 "class X {"
3341 " template <typename U> class Y { U u; };"
3342 " Y<A> y;"
3343 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003344 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003345}
3346
3347TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3348 // FIXME: Figure out whether this makes sense. It doesn't affect the
3349 // normal use case as long as the uppermost instantiation always is marked
3350 // as template instantiation, but it might be confusing as a predicate.
3351 EXPECT_TRUE(matches(
3352 "class A {};"
3353 "template <typename T> class X {"
3354 " template <typename U> class Y { U u; };"
3355 " Y<T> y;"
3356 "}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003357 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003358}
3359
3360TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3361 EXPECT_TRUE(notMatches(
3362 "template <typename T> class X {}; class A {};"
3363 "template <> class X<A> {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003364 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003365}
3366
3367TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3368 EXPECT_TRUE(notMatches(
3369 "class A {}; class Y { A a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003370 recordDecl(isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003371}
3372
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003373TEST(IsExplicitTemplateSpecialization,
3374 DoesNotMatchPrimaryTemplate) {
3375 EXPECT_TRUE(notMatches(
3376 "template <typename T> class X {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003377 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003378 EXPECT_TRUE(notMatches(
3379 "template <typename T> void f(T t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003380 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003381}
3382
3383TEST(IsExplicitTemplateSpecialization,
3384 DoesNotMatchExplicitTemplateInstantiations) {
3385 EXPECT_TRUE(notMatches(
3386 "template <typename T> class X {};"
3387 "template class X<int>; extern template class X<long>;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003388 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003389 EXPECT_TRUE(notMatches(
3390 "template <typename T> void f(T t) {}"
3391 "template void f(int t); extern template void f(long t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003392 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003393}
3394
3395TEST(IsExplicitTemplateSpecialization,
3396 DoesNotMatchImplicitTemplateInstantiations) {
3397 EXPECT_TRUE(notMatches(
3398 "template <typename T> class X {}; X<int> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003399 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003400 EXPECT_TRUE(notMatches(
3401 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003402 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003403}
3404
3405TEST(IsExplicitTemplateSpecialization,
3406 MatchesExplicitTemplateSpecializations) {
3407 EXPECT_TRUE(matches(
3408 "template <typename T> class X {};"
3409 "template<> class X<int> {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003410 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003411 EXPECT_TRUE(matches(
3412 "template <typename T> void f(T t) {}"
3413 "template<> void f(int t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003414 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003415}
3416
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003417TEST(HasAncenstor, MatchesDeclarationAncestors) {
3418 EXPECT_TRUE(matches(
3419 "class A { class B { class C {}; }; };",
3420 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3421}
3422
3423TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3424 EXPECT_TRUE(notMatches(
3425 "class A { class B { class C {}; }; };",
3426 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3427}
3428
3429TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3430 EXPECT_TRUE(matches(
3431 "class A { class B { void f() { C c; } class C {}; }; };",
3432 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3433 hasAncestor(recordDecl(hasName("A"))))))));
3434}
3435
3436TEST(HasAncenstor, MatchesStatementAncestors) {
3437 EXPECT_TRUE(matches(
3438 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003439 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003440}
3441
3442TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3443 EXPECT_TRUE(matches(
3444 "void f() { if (true) { int x = 42; } }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003445 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003446}
3447
3448TEST(HasAncestor, BindsRecursiveCombinations) {
3449 EXPECT_TRUE(matchAndVerifyResultTrue(
3450 "class C { class D { class E { class F { int y; }; }; }; };",
3451 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003452 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003453}
3454
3455TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3456 EXPECT_TRUE(matchAndVerifyResultTrue(
3457 "class C { class D { class E { class F { int y; }; }; }; };",
3458 fieldDecl(hasAncestor(
3459 decl(
3460 hasDescendant(recordDecl(isDefinition(),
3461 hasAncestor(recordDecl())))
3462 ).bind("d")
3463 )),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003464 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003465}
3466
Manuel Klimekb64d6b72013-03-14 16:33:21 +00003467TEST(HasAncestor, MatchesClosestAncestor) {
3468 EXPECT_TRUE(matchAndVerifyResultTrue(
3469 "template <typename T> struct C {"
3470 " void f(int) {"
3471 " struct I { void g(T) { int x; } } i; i.g(42);"
3472 " }"
3473 "};"
3474 "template struct C<int>;",
3475 varDecl(hasName("x"),
3476 hasAncestor(functionDecl(hasParameter(
3477 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"),
3478 new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2)));
3479}
3480
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003481TEST(HasAncestor, MatchesInTemplateInstantiations) {
3482 EXPECT_TRUE(matches(
3483 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3484 "A<int>::B::C a;",
3485 fieldDecl(hasType(asString("int")),
3486 hasAncestor(recordDecl(hasName("A"))))));
3487}
3488
3489TEST(HasAncestor, MatchesInImplicitCode) {
3490 EXPECT_TRUE(matches(
3491 "struct X {}; struct A { A() {} X x; };",
3492 constructorDecl(
3493 hasAnyConstructorInitializer(withInitializer(expr(
3494 hasAncestor(recordDecl(hasName("A")))))))));
3495}
3496
Daniel Jasper632aea92012-10-22 16:26:51 +00003497TEST(HasParent, MatchesOnlyParent) {
3498 EXPECT_TRUE(matches(
3499 "void f() { if (true) { int x = 42; } }",
3500 compoundStmt(hasParent(ifStmt()))));
3501 EXPECT_TRUE(notMatches(
3502 "void f() { for (;;) { int x = 42; } }",
3503 compoundStmt(hasParent(ifStmt()))));
3504 EXPECT_TRUE(notMatches(
3505 "void f() { if (true) for (;;) { int x = 42; } }",
3506 compoundStmt(hasParent(ifStmt()))));
3507}
3508
Manuel Klimekc844a462012-12-06 14:42:48 +00003509TEST(HasAncestor, MatchesAllAncestors) {
3510 EXPECT_TRUE(matches(
3511 "template <typename T> struct C { static void f() { 42; } };"
3512 "void t() { C<int>::f(); }",
3513 integerLiteral(
3514 equals(42),
3515 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3516 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3517}
3518
3519TEST(HasParent, MatchesAllParents) {
3520 EXPECT_TRUE(matches(
3521 "template <typename T> struct C { static void f() { 42; } };"
3522 "void t() { C<int>::f(); }",
3523 integerLiteral(
3524 equals(42),
3525 hasParent(compoundStmt(hasParent(functionDecl(
3526 hasParent(recordDecl(isTemplateInstantiation())))))))));
3527 EXPECT_TRUE(matches(
3528 "template <typename T> struct C { static void f() { 42; } };"
3529 "void t() { C<int>::f(); }",
3530 integerLiteral(
3531 equals(42),
3532 hasParent(compoundStmt(hasParent(functionDecl(
3533 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3534 EXPECT_TRUE(matches(
3535 "template <typename T> struct C { static void f() { 42; } };"
3536 "void t() { C<int>::f(); }",
3537 integerLiteral(equals(42),
3538 hasParent(compoundStmt(allOf(
3539 hasParent(functionDecl(
3540 hasParent(recordDecl(isTemplateInstantiation())))),
3541 hasParent(functionDecl(hasParent(recordDecl(
3542 unless(isTemplateInstantiation())))))))))));
Manuel Klimekb64d6b72013-03-14 16:33:21 +00003543 EXPECT_TRUE(
3544 notMatches("template <typename T> struct C { static void f() {} };"
3545 "void t() { C<int>::f(); }",
3546 compoundStmt(hasParent(recordDecl()))));
Manuel Klimekc844a462012-12-06 14:42:48 +00003547}
3548
Daniel Jasper516b02e2012-10-17 08:52:59 +00003549TEST(TypeMatching, MatchesTypes) {
3550 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3551}
3552
3553TEST(TypeMatching, MatchesArrayTypes) {
3554 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3555 EXPECT_TRUE(matches("int a[42];", arrayType()));
3556 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3557
3558 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3559 arrayType(hasElementType(builtinType()))));
3560
3561 EXPECT_TRUE(matches(
3562 "int const a[] = { 2, 3 };",
3563 qualType(arrayType(hasElementType(builtinType())))));
3564 EXPECT_TRUE(matches(
3565 "int const a[] = { 2, 3 };",
3566 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3567 EXPECT_TRUE(matches(
3568 "typedef const int T; T x[] = { 1, 2 };",
3569 qualType(isConstQualified(), arrayType())));
3570
3571 EXPECT_TRUE(notMatches(
3572 "int a[] = { 2, 3 };",
3573 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3574 EXPECT_TRUE(notMatches(
3575 "int a[] = { 2, 3 };",
3576 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3577 EXPECT_TRUE(notMatches(
3578 "int const a[] = { 2, 3 };",
3579 qualType(arrayType(hasElementType(builtinType())),
3580 unless(isConstQualified()))));
3581
3582 EXPECT_TRUE(matches("int a[2];",
3583 constantArrayType(hasElementType(builtinType()))));
3584 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3585}
3586
3587TEST(TypeMatching, MatchesComplexTypes) {
3588 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3589 EXPECT_TRUE(matches(
3590 "_Complex float f;",
3591 complexType(hasElementType(builtinType()))));
3592 EXPECT_TRUE(notMatches(
3593 "_Complex float f;",
3594 complexType(hasElementType(isInteger()))));
3595}
3596
3597TEST(TypeMatching, MatchesConstantArrayTypes) {
3598 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3599 EXPECT_TRUE(notMatches(
3600 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3601 constantArrayType(hasElementType(builtinType()))));
3602
3603 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3604 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3605 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3606}
3607
3608TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3609 EXPECT_TRUE(matches(
3610 "template <typename T, int Size> class array { T data[Size]; };",
3611 dependentSizedArrayType()));
3612 EXPECT_TRUE(notMatches(
3613 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3614 dependentSizedArrayType()));
3615}
3616
3617TEST(TypeMatching, MatchesIncompleteArrayType) {
3618 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3619 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3620
3621 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3622 incompleteArrayType()));
3623}
3624
3625TEST(TypeMatching, MatchesVariableArrayType) {
3626 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3627 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3628
3629 EXPECT_TRUE(matches(
3630 "void f(int b) { int a[b]; }",
3631 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3632 varDecl(hasName("b")))))))));
3633}
3634
3635TEST(TypeMatching, MatchesAtomicTypes) {
3636 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3637
3638 EXPECT_TRUE(matches("_Atomic(int) i;",
3639 atomicType(hasValueType(isInteger()))));
3640 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3641 atomicType(hasValueType(isInteger()))));
3642}
3643
3644TEST(TypeMatching, MatchesAutoTypes) {
3645 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3646 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3647 autoType()));
3648
Richard Smith061f1e22013-04-30 21:23:01 +00003649 // FIXME: Matching against the type-as-written can't work here, because the
3650 // type as written was not deduced.
3651 //EXPECT_TRUE(matches("auto a = 1;",
3652 // autoType(hasDeducedType(isInteger()))));
3653 //EXPECT_TRUE(notMatches("auto b = 2.0;",
3654 // autoType(hasDeducedType(isInteger()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003655}
3656
Daniel Jasperd29d5fa2012-10-29 10:14:44 +00003657TEST(TypeMatching, MatchesFunctionTypes) {
3658 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3659 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3660}
3661
Edwin Vaneec074802013-04-01 18:33:34 +00003662TEST(TypeMatching, MatchesParenType) {
3663 EXPECT_TRUE(
3664 matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
3665 EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
3666
3667 EXPECT_TRUE(matches(
3668 "int (*ptr_to_func)(int);",
3669 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3670 EXPECT_TRUE(notMatches(
3671 "int (*ptr_to_array)[4];",
3672 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3673}
3674
Daniel Jasper516b02e2012-10-17 08:52:59 +00003675TEST(TypeMatching, PointerTypes) {
Daniel Jasper7943eb52012-10-17 13:35:36 +00003676 // FIXME: Reactive when these tests can be more specific (not matching
3677 // implicit code on certain platforms), likely when we have hasDescendant for
3678 // Types/TypeLocs.
3679 //EXPECT_TRUE(matchAndVerifyResultTrue(
3680 // "int* a;",
3681 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3682 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3683 //EXPECT_TRUE(matchAndVerifyResultTrue(
3684 // "int* a;",
3685 // pointerTypeLoc().bind("loc"),
3686 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003687 EXPECT_TRUE(matches(
3688 "int** a;",
David Blaikieb61d0872013-02-18 19:04:16 +00003689 loc(pointerType(pointee(qualType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003690 EXPECT_TRUE(matches(
3691 "int** a;",
3692 loc(pointerType(pointee(pointerType())))));
3693 EXPECT_TRUE(matches(
3694 "int* b; int* * const a = &b;",
3695 loc(qualType(isConstQualified(), pointerType()))));
3696
3697 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper7943eb52012-10-17 13:35:36 +00003698 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3699 hasType(blockPointerType()))));
3700 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3701 hasType(memberPointerType()))));
3702 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3703 hasType(pointerType()))));
3704 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3705 hasType(referenceType()))));
Edwin Vane2a760d02013-03-07 15:44:40 +00003706 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3707 hasType(lValueReferenceType()))));
3708 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3709 hasType(rValueReferenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003710
Daniel Jasper7943eb52012-10-17 13:35:36 +00003711 Fragment = "int *ptr;";
3712 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3713 hasType(blockPointerType()))));
3714 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3715 hasType(memberPointerType()))));
3716 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3717 hasType(pointerType()))));
3718 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3719 hasType(referenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003720
Daniel Jasper7943eb52012-10-17 13:35:36 +00003721 Fragment = "int a; int &ref = a;";
3722 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3723 hasType(blockPointerType()))));
3724 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3725 hasType(memberPointerType()))));
3726 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3727 hasType(pointerType()))));
3728 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3729 hasType(referenceType()))));
Edwin Vane2a760d02013-03-07 15:44:40 +00003730 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3731 hasType(lValueReferenceType()))));
3732 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3733 hasType(rValueReferenceType()))));
3734
3735 Fragment = "int &&ref = 2;";
3736 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3737 hasType(blockPointerType()))));
3738 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3739 hasType(memberPointerType()))));
3740 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3741 hasType(pointerType()))));
3742 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3743 hasType(referenceType()))));
3744 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3745 hasType(lValueReferenceType()))));
3746 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3747 hasType(rValueReferenceType()))));
3748}
3749
3750TEST(TypeMatching, AutoRefTypes) {
3751 std::string Fragment = "auto a = 1;"
3752 "auto b = a;"
3753 "auto &c = a;"
3754 "auto &&d = c;"
3755 "auto &&e = 2;";
3756 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
3757 hasType(referenceType()))));
3758 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
3759 hasType(referenceType()))));
3760 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3761 hasType(referenceType()))));
3762 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3763 hasType(lValueReferenceType()))));
3764 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
3765 hasType(rValueReferenceType()))));
3766 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3767 hasType(referenceType()))));
3768 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3769 hasType(lValueReferenceType()))));
3770 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
3771 hasType(rValueReferenceType()))));
3772 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3773 hasType(referenceType()))));
3774 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
3775 hasType(lValueReferenceType()))));
3776 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3777 hasType(rValueReferenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003778}
3779
3780TEST(TypeMatching, PointeeTypes) {
3781 EXPECT_TRUE(matches("int b; int &a = b;",
3782 referenceType(pointee(builtinType()))));
3783 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3784
3785 EXPECT_TRUE(matches("int *a;",
David Blaikieb61d0872013-02-18 19:04:16 +00003786 loc(pointerType(pointee(builtinType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003787
3788 EXPECT_TRUE(matches(
3789 "int const *A;",
3790 pointerType(pointee(isConstQualified(), builtinType()))));
3791 EXPECT_TRUE(notMatches(
3792 "int *A;",
3793 pointerType(pointee(isConstQualified(), builtinType()))));
3794}
3795
3796TEST(TypeMatching, MatchesPointersToConstTypes) {
3797 EXPECT_TRUE(matches("int b; int * const a = &b;",
3798 loc(pointerType())));
3799 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikieb61d0872013-02-18 19:04:16 +00003800 loc(pointerType())));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003801 EXPECT_TRUE(matches(
3802 "int b; const int * a = &b;",
David Blaikieb61d0872013-02-18 19:04:16 +00003803 loc(pointerType(pointee(builtinType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003804 EXPECT_TRUE(matches(
3805 "int b; const int * a = &b;",
3806 pointerType(pointee(builtinType()))));
3807}
3808
3809TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper7943eb52012-10-17 13:35:36 +00003810 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3811 hasType(typedefType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003812}
3813
Edwin Vanef901b712013-02-25 14:49:29 +00003814TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vaneb6eae142013-02-25 20:43:32 +00003815 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vanef901b712013-02-25 14:49:29 +00003816 templateSpecializationType()));
3817}
3818
Edwin Vaneb6eae142013-02-25 20:43:32 +00003819TEST(TypeMatching, MatchesRecordType) {
3820 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek59b0af62013-02-27 11:56:58 +00003821 EXPECT_TRUE(matches("struct S{}; S s;",
3822 recordType(hasDeclaration(recordDecl(hasName("S"))))));
3823 EXPECT_TRUE(notMatches("int i;",
3824 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vaneb6eae142013-02-25 20:43:32 +00003825}
3826
3827TEST(TypeMatching, MatchesElaboratedType) {
3828 EXPECT_TRUE(matches(
3829 "namespace N {"
3830 " namespace M {"
3831 " class D {};"
3832 " }"
3833 "}"
3834 "N::M::D d;", elaboratedType()));
3835 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
3836 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
3837}
3838
3839TEST(ElaboratedTypeNarrowing, hasQualifier) {
3840 EXPECT_TRUE(matches(
3841 "namespace N {"
3842 " namespace M {"
3843 " class D {};"
3844 " }"
3845 "}"
3846 "N::M::D d;",
3847 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3848 EXPECT_TRUE(notMatches(
3849 "namespace M {"
3850 " class D {};"
3851 "}"
3852 "M::D d;",
3853 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vane6972f6d2013-03-04 17:51:00 +00003854 EXPECT_TRUE(notMatches(
3855 "struct D {"
3856 "} d;",
3857 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vaneb6eae142013-02-25 20:43:32 +00003858}
3859
3860TEST(ElaboratedTypeNarrowing, namesType) {
3861 EXPECT_TRUE(matches(
3862 "namespace N {"
3863 " namespace M {"
3864 " class D {};"
3865 " }"
3866 "}"
3867 "N::M::D d;",
3868 elaboratedType(elaboratedType(namesType(recordType(
3869 hasDeclaration(namedDecl(hasName("D")))))))));
3870 EXPECT_TRUE(notMatches(
3871 "namespace M {"
3872 " class D {};"
3873 "}"
3874 "M::D d;",
3875 elaboratedType(elaboratedType(namesType(typedefType())))));
3876}
3877
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003878TEST(NNS, MatchesNestedNameSpecifiers) {
3879 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3880 nestedNameSpecifier()));
3881 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3882 nestedNameSpecifier()));
3883 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3884 nestedNameSpecifier()));
3885
3886 EXPECT_TRUE(matches(
3887 "struct A { static void f() {} }; void g() { A::f(); }",
3888 nestedNameSpecifier()));
3889 EXPECT_TRUE(notMatches(
3890 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3891 nestedNameSpecifier()));
3892}
3893
Daniel Jasper87c3d362012-09-20 14:12:57 +00003894TEST(NullStatement, SimpleCases) {
3895 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3896 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3897}
3898
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003899TEST(NNS, MatchesTypes) {
3900 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3901 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3902 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3903 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3904 Matcher));
3905 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3906}
3907
3908TEST(NNS, MatchesNamespaceDecls) {
3909 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3910 specifiesNamespace(hasName("ns")));
3911 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3912 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3913 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3914}
3915
3916TEST(NNS, BindsNestedNameSpecifiers) {
3917 EXPECT_TRUE(matchAndVerifyResultTrue(
3918 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3919 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3920 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3921}
3922
3923TEST(NNS, BindsNestedNameSpecifierLocs) {
3924 EXPECT_TRUE(matchAndVerifyResultTrue(
3925 "namespace ns { struct B {}; } ns::B b;",
3926 loc(nestedNameSpecifier()).bind("loc"),
3927 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3928}
3929
3930TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3931 EXPECT_TRUE(matches(
3932 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3933 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3934 EXPECT_TRUE(matches(
3935 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasper516b02e2012-10-17 08:52:59 +00003936 nestedNameSpecifierLoc(hasPrefix(
3937 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003938}
3939
Daniel Jasper6fc34332012-10-30 15:42:00 +00003940TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3941 std::string Fragment =
3942 "namespace a { struct A { struct B { struct C {}; }; }; };"
3943 "void f() { a::A::B::C c; }";
3944 EXPECT_TRUE(matches(
3945 Fragment,
3946 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3947 hasDescendant(nestedNameSpecifier(
3948 specifiesNamespace(hasName("a")))))));
3949 EXPECT_TRUE(notMatches(
3950 Fragment,
3951 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3952 has(nestedNameSpecifier(
3953 specifiesNamespace(hasName("a")))))));
3954 EXPECT_TRUE(matches(
3955 Fragment,
3956 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3957 has(nestedNameSpecifier(
3958 specifiesNamespace(hasName("a")))))));
3959
3960 // Not really useful because a NestedNameSpecifier can af at most one child,
3961 // but to complete the interface.
3962 EXPECT_TRUE(matchAndVerifyResultTrue(
3963 Fragment,
3964 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3965 forEach(nestedNameSpecifier().bind("x"))),
3966 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3967}
3968
3969TEST(NNS, NestedNameSpecifiersAsDescendants) {
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 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3976 asString("struct a::A")))))));
3977 EXPECT_TRUE(matchAndVerifyResultTrue(
3978 Fragment,
3979 functionDecl(hasName("f"),
3980 forEachDescendant(nestedNameSpecifier().bind("x"))),
3981 // Nested names: a, a::A and a::A::B.
3982 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3983}
3984
3985TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3986 std::string Fragment =
3987 "namespace a { struct A { struct B { struct C {}; }; }; };"
3988 "void f() { a::A::B::C c; }";
3989 EXPECT_TRUE(matches(
3990 Fragment,
3991 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3992 hasDescendant(loc(nestedNameSpecifier(
3993 specifiesNamespace(hasName("a"))))))));
3994 EXPECT_TRUE(notMatches(
3995 Fragment,
3996 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3997 has(loc(nestedNameSpecifier(
3998 specifiesNamespace(hasName("a"))))))));
3999 EXPECT_TRUE(matches(
4000 Fragment,
4001 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
4002 has(loc(nestedNameSpecifier(
4003 specifiesNamespace(hasName("a"))))))));
4004
4005 EXPECT_TRUE(matchAndVerifyResultTrue(
4006 Fragment,
4007 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4008 forEach(nestedNameSpecifierLoc().bind("x"))),
4009 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
4010}
4011
4012TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
4013 std::string Fragment =
4014 "namespace a { struct A { struct B { struct C {}; }; }; };"
4015 "void f() { a::A::B::C c; }";
4016 EXPECT_TRUE(matches(
4017 Fragment,
4018 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
4019 asString("struct a::A"))))))));
4020 EXPECT_TRUE(matchAndVerifyResultTrue(
4021 Fragment,
4022 functionDecl(hasName("f"),
4023 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
4024 // Nested names: a, a::A and a::A::B.
4025 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
4026}
4027
Manuel Klimek191c0932013-02-01 13:41:35 +00004028template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimekc2687452012-10-24 14:47:44 +00004029public:
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004030 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
4031 StringRef InnerId)
4032 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jaspere9aa6872012-10-29 10:48:25 +00004033 }
4034
Manuel Klimek191c0932013-02-01 13:41:35 +00004035 virtual bool run(const BoundNodes *Nodes) { return false; }
4036
Manuel Klimekc2687452012-10-24 14:47:44 +00004037 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4038 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004039 return selectFirst<const T>(InnerId,
4040 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimekc2687452012-10-24 14:47:44 +00004041 }
4042private:
4043 std::string Id;
4044 internal::Matcher<T> InnerMatcher;
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004045 std::string InnerId;
Manuel Klimekc2687452012-10-24 14:47:44 +00004046};
4047
4048TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek191c0932013-02-01 13:41:35 +00004049 EXPECT_TRUE(matchAndVerifyResultTrue(
4050 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4051 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004052 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
4053 "Y")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004054 EXPECT_TRUE(matchAndVerifyResultFalse(
4055 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4056 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004057 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
4058 "Z")));
Manuel Klimekc2687452012-10-24 14:47:44 +00004059}
4060
4061TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek191c0932013-02-01 13:41:35 +00004062 EXPECT_TRUE(matchAndVerifyResultTrue(
4063 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004064 new VerifyMatchOnNode<clang::Stmt>(
4065 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004066 EXPECT_TRUE(matchAndVerifyResultFalse(
4067 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004068 new VerifyMatchOnNode<clang::Stmt>(
4069 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004070}
4071
4072TEST(MatchFinder, CanMatchSingleNodesRecursively) {
4073 EXPECT_TRUE(matchAndVerifyResultTrue(
4074 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4075 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004076 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004077 EXPECT_TRUE(matchAndVerifyResultFalse(
4078 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4079 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004080 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimekc2687452012-10-24 14:47:44 +00004081}
4082
Manuel Klimekbee08572013-02-07 12:42:10 +00004083template <typename T>
4084class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
4085public:
4086 virtual bool run(const BoundNodes *Nodes) { return false; }
4087
4088 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4089 const T *Node = Nodes->getNodeAs<T>("");
4090 return verify(*Nodes, *Context, Node);
4091 }
4092
4093 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
4094 return selectFirst<const T>(
4095 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
4096 *Node, Context)) != NULL;
4097 }
4098 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
4099 return selectFirst<const T>(
4100 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
4101 *Node, Context)) != NULL;
4102 }
4103};
4104
4105TEST(IsEqualTo, MatchesNodesByIdentity) {
4106 EXPECT_TRUE(matchAndVerifyResultTrue(
4107 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
4108 new VerifyAncestorHasChildIsEqual<Decl>()));
4109 EXPECT_TRUE(
4110 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
4111 new VerifyAncestorHasChildIsEqual<Stmt>()));
4112}
4113
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004114class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
4115public:
4116 VerifyStartOfTranslationUnit() : Called(false) {}
4117 virtual void run(const MatchFinder::MatchResult &Result) {
4118 EXPECT_TRUE(Called);
4119 }
4120 virtual void onStartOfTranslationUnit() {
4121 Called = true;
4122 }
4123 bool Called;
4124};
4125
4126TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
4127 MatchFinder Finder;
4128 VerifyStartOfTranslationUnit VerifyCallback;
4129 Finder.addMatcher(decl(), &VerifyCallback);
4130 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4131 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4132 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbournea2334162013-11-07 22:30:36 +00004133
4134 VerifyCallback.Called = false;
4135 OwningPtr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
4136 ASSERT_TRUE(AST.get());
4137 Finder.matchAST(AST->getASTContext());
4138 EXPECT_TRUE(VerifyCallback.Called);
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004139}
4140
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004141class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback {
4142public:
4143 VerifyEndOfTranslationUnit() : Called(false) {}
4144 virtual void run(const MatchFinder::MatchResult &Result) {
4145 EXPECT_FALSE(Called);
4146 }
4147 virtual void onEndOfTranslationUnit() {
4148 Called = true;
4149 }
4150 bool Called;
4151};
4152
4153TEST(MatchFinder, InterceptsEndOfTranslationUnit) {
4154 MatchFinder Finder;
4155 VerifyEndOfTranslationUnit VerifyCallback;
4156 Finder.addMatcher(decl(), &VerifyCallback);
4157 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4158 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4159 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbournea2334162013-11-07 22:30:36 +00004160
4161 VerifyCallback.Called = false;
4162 OwningPtr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
4163 ASSERT_TRUE(AST.get());
4164 Finder.matchAST(AST->getASTContext());
4165 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004166}
4167
Manuel Klimekbbb75852013-06-20 14:06:32 +00004168TEST(EqualsBoundNodeMatcher, QualType) {
4169 EXPECT_TRUE(matches(
4170 "int i = 1;", varDecl(hasType(qualType().bind("type")),
4171 hasInitializer(ignoringParenImpCasts(
4172 hasType(qualType(equalsBoundNode("type"))))))));
4173 EXPECT_TRUE(notMatches("int i = 1.f;",
4174 varDecl(hasType(qualType().bind("type")),
4175 hasInitializer(ignoringParenImpCasts(hasType(
4176 qualType(equalsBoundNode("type"))))))));
4177}
4178
4179TEST(EqualsBoundNodeMatcher, NonMatchingTypes) {
4180 EXPECT_TRUE(notMatches(
4181 "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"),
4182 hasInitializer(ignoringParenImpCasts(
4183 hasType(qualType(equalsBoundNode("type"))))))));
4184}
4185
4186TEST(EqualsBoundNodeMatcher, Stmt) {
4187 EXPECT_TRUE(
4188 matches("void f() { if(true) {} }",
4189 stmt(allOf(ifStmt().bind("if"),
4190 hasParent(stmt(has(stmt(equalsBoundNode("if")))))))));
4191
4192 EXPECT_TRUE(notMatches(
4193 "void f() { if(true) { if (true) {} } }",
4194 stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if")))))));
4195}
4196
4197TEST(EqualsBoundNodeMatcher, Decl) {
4198 EXPECT_TRUE(matches(
4199 "class X { class Y {}; };",
4200 decl(allOf(recordDecl(hasName("::X::Y")).bind("record"),
4201 hasParent(decl(has(decl(equalsBoundNode("record")))))))));
4202
4203 EXPECT_TRUE(notMatches("class X { class Y {}; };",
4204 decl(allOf(recordDecl(hasName("::X")).bind("record"),
4205 has(decl(equalsBoundNode("record")))))));
4206}
4207
4208TEST(EqualsBoundNodeMatcher, Type) {
4209 EXPECT_TRUE(matches(
4210 "class X { int a; int b; };",
4211 recordDecl(
4212 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4213 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4214
4215 EXPECT_TRUE(notMatches(
4216 "class X { int a; double b; };",
4217 recordDecl(
4218 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4219 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4220}
4221
4222TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) {
4223
4224 EXPECT_TRUE(matchAndVerifyResultTrue(
4225 "int f() {"
4226 " if (1) {"
4227 " int i = 9;"
4228 " }"
4229 " int j = 10;"
4230 " {"
4231 " float k = 9.0;"
4232 " }"
4233 " return 0;"
4234 "}",
4235 // Look for variable declarations within functions whose type is the same
4236 // as the function return type.
4237 functionDecl(returns(qualType().bind("type")),
4238 forEachDescendant(varDecl(hasType(
4239 qualType(equalsBoundNode("type")))).bind("decl"))),
4240 // Only i and j should match, not k.
4241 new VerifyIdIsBoundTo<VarDecl>("decl", 2)));
4242}
4243
4244TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) {
4245 EXPECT_TRUE(matchAndVerifyResultTrue(
4246 "void f() {"
4247 " int x;"
4248 " double d;"
4249 " x = d + x - d + x;"
4250 "}",
4251 functionDecl(
4252 hasName("f"), forEachDescendant(varDecl().bind("d")),
4253 forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))),
4254 new VerifyIdIsBoundTo<VarDecl>("d", 5)));
4255}
4256
Manuel Klimek04616e42012-07-06 05:48:52 +00004257} // end namespace ast_matchers
4258} // end namespace clang